diff --git a/source/app/assets/stylesheets/urls.css.scss b/source/app/assets/stylesheets/urls.css.scss index a4281ec..e6e53e1 100644 --- a/source/app/assets/stylesheets/urls.css.scss +++ b/source/app/assets/stylesheets/urls.css.scss @@ -1,3 +1,31 @@ // Place all the styles related to the Urls controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ + +$red: #8C2318; +$headline: "Times New Roman", Times, serif; +$primary-font: Verdana, Geneva, sans-serif; +$my_grey: #F5F5F5; + + +.container { + margin: 5% auto; + width: 70%; + padding: 2%; + border: 3px solid black; + background-color: $my_grey; + font-family: $primary-font; +} + +.wrap-center { + margin:5px; + display:block; + text-align: center; +} + +.error { + background-color: pink; + padding:10px; + color:$red; + display:inline-block; +} \ No newline at end of file diff --git a/source/app/controllers/urls_controller.rb b/source/app/controllers/urls_controller.rb index ef26710..58c8f9f 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -1,2 +1,43 @@ class UrlsController < ApplicationController + def index + @urls = Url.all + end + + def show + @url = Url.find(params[:id]) + end + + def new + @url = Url.new + end + + def create + + begin + response = Net::HTTP.get_response(uri) + rescue + flash[:error] = "not a valid url" + end + + @url = Url.new(url_params) + @url.shorten_url + @url.counter = 0 + if @url.save + redirect_to url_path(@url) + else + render 'new' + end + end + + def link + @url = Url.where(short: params[:short]) + @url[0].counter +=1 + @url[0].save + redirect_to 'http://' + @url[0].orig + end + + private + def url_params + params.require(:url).permit(:orig) + end end diff --git a/source/app/models/url.rb b/source/app/models/url.rb new file mode 100644 index 0000000..77c8f2e --- /dev/null +++ b/source/app/models/url.rb @@ -0,0 +1,14 @@ +require 'securerandom' +require "net/http" +require "uri" + + +class Url < ActiveRecord::Base + validates :orig, presence: true, format: { with: /.+\.\w+/, message: "Not a valid web address." } + # before_save do + # self.short = SecureRandom.hex(3) + # end + def shorten_url + self.short = SecureRandom.hex(3) + end +end diff --git a/source/app/views/layouts/application.html.erb b/source/app/views/layouts/application.html.erb index f946432..47e5d8f 100644 --- a/source/app/views/layouts/application.html.erb +++ b/source/app/views/layouts/application.html.erb @@ -8,7 +8,9 @@
-<%= yield %> +