From 0aa6d5a3639b7ccfa7fc627557337c3786aed5dd Mon Sep 17 00:00:00 2001 From: Cara Kane Date: Mon, 14 May 2018 16:21:46 -0400 Subject: [PATCH 1/7] finished release 0 --- source/Gemfile | 2 +- source/Gemfile.lock | 9 +++++++ source/app/controllers/urls_controller.rb | 30 +++++++++++++++++++++++ source/app/models/url.rb | 7 ++++++ source/app/views/urls/index.html.erb | 5 ++++ source/app/views/urls/new.html.erb | 5 ++++ source/app/views/urls/show.html.erb | 2 ++ source/config/application.rb | 1 + source/config/routes.rb | 2 ++ source/db/migrate/20180514183131_urls.rb | 8 ++++++ source/db/schema.rb | 21 ++++++++++++++++ 11 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 source/app/models/url.rb create mode 100644 source/app/views/urls/index.html.erb create mode 100644 source/app/views/urls/new.html.erb create mode 100644 source/app/views/urls/show.html.erb create mode 100644 source/db/migrate/20180514183131_urls.rb create mode 100644 source/db/schema.rb diff --git a/source/Gemfile b/source/Gemfile index 9627b8b..4d97f83 100644 --- a/source/Gemfile +++ b/source/Gemfile @@ -1,6 +1,6 @@ source 'https://rubygems.org' - +gem 'pry' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.1.6' # Use sqlite3 as the database for Active Record diff --git a/source/Gemfile.lock b/source/Gemfile.lock index fcf8b98..ab6209f 100644 --- a/source/Gemfile.lock +++ b/source/Gemfile.lock @@ -29,6 +29,7 @@ GEM tzinfo (~> 1.1) arel (5.0.1.20140414130214) builder (3.2.2) + coderay (1.1.2) coffee-rails (4.0.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) @@ -50,9 +51,13 @@ GEM json (1.8.1) mail (2.6.1) mime-types (>= 1.16, < 3) + method_source (0.9.0) mime-types (2.4.1) minitest (5.4.2) multi_json (1.10.1) + pry (0.11.3) + coderay (~> 1.1.0) + method_source (~> 0.9.0) rack (1.5.2) rack-test (0.6.2) rack (>= 1.0) @@ -128,6 +133,7 @@ DEPENDENCIES coffee-rails (~> 4.0.0) jbuilder (~> 2.0) jquery-rails + pry rails (= 4.1.6) rspec-rails sass-rails (~> 4.0.3) @@ -136,3 +142,6 @@ DEPENDENCIES sqlite3 turbolinks uglifier (>= 1.3.0) + +BUNDLED WITH + 1.16.1 diff --git a/source/app/controllers/urls_controller.rb b/source/app/controllers/urls_controller.rb index ef26710..22cdb61 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -1,2 +1,32 @@ class UrlsController < ApplicationController + + def index + @all = Url.all + end + + def show + if params[:id].start_with?("!") + @url = Url.find_by(:short_url => "localhost:3000/urls/#{params[:id]}") + redirect_to "http://#{@url[:long_url]}" + else + @url = Url.find(params[:id]) + url_path(@url) + end + end + + def new + @url = Url.new + end + + def create + @url = Url.create(url_params) + redirect_to url_path(@url) + end + + + + private + def url_params + params.require(:url).permit(:long_url) + end end diff --git a/source/app/models/url.rb b/source/app/models/url.rb new file mode 100644 index 0000000..c050f12 --- /dev/null +++ b/source/app/models/url.rb @@ -0,0 +1,7 @@ +class Url < ActiveRecord::Base + before_save :shorten_url + + def shorten_url + self.short_url = "localhost:3000/urls/!#{(0..3).map{ rand(36).to_s(36) }.join}" + end +end \ No newline at end of file diff --git a/source/app/views/urls/index.html.erb b/source/app/views/urls/index.html.erb new file mode 100644 index 0000000..b066cb6 --- /dev/null +++ b/source/app/views/urls/index.html.erb @@ -0,0 +1,5 @@ +All the URLs + +<% @urls.each do |url| %> + <%= url.long_url %> +<% end %> \ No newline at end of file diff --git a/source/app/views/urls/new.html.erb b/source/app/views/urls/new.html.erb new file mode 100644 index 0000000..dfe32d7 --- /dev/null +++ b/source/app/views/urls/new.html.erb @@ -0,0 +1,5 @@ +<%= form_for @url do |f| %> + <%= f.label "Enter a URL" %> + <%= f.text_field :long_url %> +<%= f.submit %> +<% end %> \ No newline at end of file diff --git a/source/app/views/urls/show.html.erb b/source/app/views/urls/show.html.erb new file mode 100644 index 0000000..fdfba09 --- /dev/null +++ b/source/app/views/urls/show.html.erb @@ -0,0 +1,2 @@ +The URL you entered: <%= @url.long_url %>
+Its short version: <%= @url.short_url %> \ No newline at end of file diff --git a/source/config/application.rb b/source/config/application.rb index 9a1de00..9b24bf8 100644 --- a/source/config/application.rb +++ b/source/config/application.rb @@ -5,6 +5,7 @@ # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) +require 'pry' module Source class Application < Rails::Application diff --git a/source/config/routes.rb b/source/config/routes.rb index 3f66539..4399077 100644 --- a/source/config/routes.rb +++ b/source/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do + + resources :urls # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/source/db/migrate/20180514183131_urls.rb b/source/db/migrate/20180514183131_urls.rb new file mode 100644 index 0000000..c27d759 --- /dev/null +++ b/source/db/migrate/20180514183131_urls.rb @@ -0,0 +1,8 @@ +class Urls < ActiveRecord::Migration + def change + create_table :urls do |t| + t.string :long_url + t.string :short_url + end + end +end diff --git a/source/db/schema.rb b/source/db/schema.rb new file mode 100644 index 0000000..f9d41de --- /dev/null +++ b/source/db/schema.rb @@ -0,0 +1,21 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20180514183131) do + + create_table "urls", force: true do |t| + t.string "long_url" + t.string "short_url" + end + +end From 03138b36f325aae3a663a7899256331c4cc9de73 Mon Sep 17 00:00:00 2001 From: Cara Kane Date: Tue, 15 May 2018 09:40:40 -0400 Subject: [PATCH 2/7] finished release 1 --- source/app/controllers/urls_controller.rb | 5 +++-- source/app/models/url.rb | 2 +- source/app/views/urls/index.html.erb | 4 ++-- source/app/views/urls/show.html.erb | 2 +- .../db/migrate/20180515131738_add_click_count_to_urls.rb | 5 +++++ source/db/schema.rb | 7 ++++--- 6 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 source/db/migrate/20180515131738_add_click_count_to_urls.rb diff --git a/source/app/controllers/urls_controller.rb b/source/app/controllers/urls_controller.rb index 22cdb61..be483bf 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -1,12 +1,13 @@ class UrlsController < ApplicationController def index - @all = Url.all + @urls = Url.all end def show if params[:id].start_with?("!") - @url = Url.find_by(:short_url => "localhost:3000/urls/#{params[:id]}") + @url = Url.find_by(:short_url => "http://localhost:3000/urls/#{params[:id]}") + @url.click_count += 1 redirect_to "http://#{@url[:long_url]}" else @url = Url.find(params[:id]) diff --git a/source/app/models/url.rb b/source/app/models/url.rb index c050f12..cb805cd 100644 --- a/source/app/models/url.rb +++ b/source/app/models/url.rb @@ -2,6 +2,6 @@ class Url < ActiveRecord::Base before_save :shorten_url def shorten_url - self.short_url = "localhost:3000/urls/!#{(0..3).map{ rand(36).to_s(36) }.join}" + self.short_url = "http://localhost:3000/urls/!#{(0..3).map{ rand(36).to_s(36) }.join}" end end \ No newline at end of file diff --git a/source/app/views/urls/index.html.erb b/source/app/views/urls/index.html.erb index b066cb6..80d7be2 100644 --- a/source/app/views/urls/index.html.erb +++ b/source/app/views/urls/index.html.erb @@ -1,5 +1,5 @@ -All the URLs +

All the URLs

<% @urls.each do |url| %> - <%= url.long_url %> + <%= url.long_url %>
<% end %> \ No newline at end of file diff --git a/source/app/views/urls/show.html.erb b/source/app/views/urls/show.html.erb index fdfba09..2ef012f 100644 --- a/source/app/views/urls/show.html.erb +++ b/source/app/views/urls/show.html.erb @@ -1,2 +1,2 @@ The URL you entered: <%= @url.long_url %>
-Its short version: <%= @url.short_url %> \ No newline at end of file +Its short version: <%= link_to @url.short_url, @url.short_url %> \ No newline at end of file diff --git a/source/db/migrate/20180515131738_add_click_count_to_urls.rb b/source/db/migrate/20180515131738_add_click_count_to_urls.rb new file mode 100644 index 0000000..93b0e1f --- /dev/null +++ b/source/db/migrate/20180515131738_add_click_count_to_urls.rb @@ -0,0 +1,5 @@ +class AddClickCountToUrls < ActiveRecord::Migration + def change + add_column :urls, :click_count, :integer, default: 0 + end +end diff --git a/source/db/schema.rb b/source/db/schema.rb index f9d41de..c236329 100644 --- a/source/db/schema.rb +++ b/source/db/schema.rb @@ -11,11 +11,12 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180514183131) do +ActiveRecord::Schema.define(version: 20180515131738) do create_table "urls", force: true do |t| - t.string "long_url" - t.string "short_url" + t.string "long_url" + t.string "short_url" + t.integer "click_count", default: 0 end end From 3309367ea1a52e97d5e682d43a7c20ea7ddad0ba Mon Sep 17 00:00:00 2001 From: Cara Kane Date: Tue, 15 May 2018 12:43:58 -0400 Subject: [PATCH 3/7] finished release 2 --- source/app/controllers/urls_controller.rb | 13 ++++++++----- source/app/models/url.rb | 10 ++++++++++ source/app/views/urls/new.html.erb | 10 ++++++++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/source/app/controllers/urls_controller.rb b/source/app/controllers/urls_controller.rb index be483bf..b532281 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -8,7 +8,7 @@ def show if params[:id].start_with?("!") @url = Url.find_by(:short_url => "http://localhost:3000/urls/#{params[:id]}") @url.click_count += 1 - redirect_to "http://#{@url[:long_url]}" + redirect_to @url[:long_url] else @url = Url.find(params[:id]) url_path(@url) @@ -20,12 +20,15 @@ def new end def create - @url = Url.create(url_params) - redirect_to url_path(@url) + @url = Url.new(url_params) + if @url.save + binding.pry + redirect_to url_path(@url) + else + render 'new' + end end - - private def url_params params.require(:url).permit(:long_url) diff --git a/source/app/models/url.rb b/source/app/models/url.rb index cb805cd..13a2e95 100644 --- a/source/app/models/url.rb +++ b/source/app/models/url.rb @@ -1,5 +1,15 @@ +require 'net/http' + class Url < ActiveRecord::Base before_save :shorten_url + validates :long_url, presence: true, allow_blank: false + validate :validate_url + + def validate_url + uri = URI("#{self.long_url}") + res = Net::HTTP.get_response(uri).code + self.errors.add(:base, "This URL is invalid") if res != "200" + end def shorten_url self.short_url = "http://localhost:3000/urls/!#{(0..3).map{ rand(36).to_s(36) }.join}" diff --git a/source/app/views/urls/new.html.erb b/source/app/views/urls/new.html.erb index dfe32d7..5392021 100644 --- a/source/app/views/urls/new.html.erb +++ b/source/app/views/urls/new.html.erb @@ -1,5 +1,11 @@ +<% if @url.errors.any? %> + <% @url.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +<% end %> + <%= form_for @url do |f| %> <%= f.label "Enter a URL" %> - <%= f.text_field :long_url %> + <%= f.url_field :long_url, :required => true %> <%= f.submit %> -<% end %> \ No newline at end of file +<% end %> From f379b73d3f5174aeba71b5e489c81b0fce6a828f Mon Sep 17 00:00:00 2001 From: Cara Kane Date: Tue, 15 May 2018 13:14:58 -0400 Subject: [PATCH 4/7] cleaned up files, added comment about url validation --- source/app/controllers/urls_controller.rb | 1 - source/app/models/url.rb | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/app/controllers/urls_controller.rb b/source/app/controllers/urls_controller.rb index b532281..1e208e7 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -22,7 +22,6 @@ def new def create @url = Url.new(url_params) if @url.save - binding.pry redirect_to url_path(@url) else render 'new' diff --git a/source/app/models/url.rb b/source/app/models/url.rb index 13a2e95..c2eaae4 100644 --- a/source/app/models/url.rb +++ b/source/app/models/url.rb @@ -7,8 +7,9 @@ class Url < ActiveRecord::Base def validate_url uri = URI("#{self.long_url}") - res = Net::HTTP.get_response(uri).code - self.errors.add(:base, "This URL is invalid") if res != "200" + # this doesn't work if the input isn't a url, i.e., it works if an error code is returned + res = Net::HTTP.get_response(uri) + self.errors.add(:base, "This URL is invalid, #{res.message}") if res.code != "200" end def shorten_url From 413ec2a4857d944a8d0d9699eeb4e811f537e63c Mon Sep 17 00:00:00 2001 From: Cara Kane Date: Tue, 15 May 2018 16:12:10 -0400 Subject: [PATCH 5/7] changed comment about error --- source/app/models/url.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/app/models/url.rb b/source/app/models/url.rb index c2eaae4..d089a9b 100644 --- a/source/app/models/url.rb +++ b/source/app/models/url.rb @@ -7,7 +7,7 @@ class Url < ActiveRecord::Base def validate_url uri = URI("#{self.long_url}") - # this doesn't work if the input isn't a url, i.e., it works if an error code is returned + # this doesn't work if the input isn't a url, i.e., it only works if an error code is returned res = Net::HTTP.get_response(uri) self.errors.add(:base, "This URL is invalid, #{res.message}") if res.code != "200" end From ef066252aaa642a1feaada943b2eea5ebaf330b4 Mon Sep 17 00:00:00 2001 From: Cara Kane Date: Thu, 17 May 2018 14:36:14 -0400 Subject: [PATCH 6/7] refactored --- source/app/controllers/urls_controller.rb | 16 ++++++++-------- source/app/models/url.rb | 8 +++++--- source/app/views/urls/show.html.erb | 2 +- source/config/routes.rb | 4 ++++ 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/source/app/controllers/urls_controller.rb b/source/app/controllers/urls_controller.rb index 1e208e7..a288756 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -1,18 +1,18 @@ class UrlsController < ApplicationController + def short + @url = Url.find_by(:short_url => params[:id]) + @url.increment(:click_count) + redirect_to @url[:long_url] + end + def index @urls = Url.all end def show - if params[:id].start_with?("!") - @url = Url.find_by(:short_url => "http://localhost:3000/urls/#{params[:id]}") - @url.click_count += 1 - redirect_to @url[:long_url] - else - @url = Url.find(params[:id]) - url_path(@url) - end + @url = Url.find(params[:id]) + url_path(@url) end def new diff --git a/source/app/models/url.rb b/source/app/models/url.rb index d089a9b..4073874 100644 --- a/source/app/models/url.rb +++ b/source/app/models/url.rb @@ -1,18 +1,20 @@ require 'net/http' class Url < ActiveRecord::Base - before_save :shorten_url + before_create :shorten_url validates :long_url, presence: true, allow_blank: false validate :validate_url def validate_url uri = URI("#{self.long_url}") # this doesn't work if the input isn't a url, i.e., it only works if an error code is returned - res = Net::HTTP.get_response(uri) + if res = Net::HTTP.get_response(uri) + else res.code = "x" + end self.errors.add(:base, "This URL is invalid, #{res.message}") if res.code != "200" end def shorten_url - self.short_url = "http://localhost:3000/urls/!#{(0..3).map{ rand(36).to_s(36) }.join}" + self.short_url = "!#{(0..3).map{ rand(36).to_s(36) }.join}" end end \ No newline at end of file diff --git a/source/app/views/urls/show.html.erb b/source/app/views/urls/show.html.erb index 2ef012f..a9cbb77 100644 --- a/source/app/views/urls/show.html.erb +++ b/source/app/views/urls/show.html.erb @@ -1,2 +1,2 @@ The URL you entered: <%= @url.long_url %>
    -Its short version: <%= link_to @url.short_url, @url.short_url %> \ No newline at end of file +Its short version: <%= link_to "http://localhost:3000/urls/#{@url.short_url}", @url.short_url %> \ No newline at end of file diff --git a/source/config/routes.rb b/source/config/routes.rb index 4399077..1d1092e 100644 --- a/source/config/routes.rb +++ b/source/config/routes.rb @@ -1,6 +1,10 @@ Rails.application.routes.draw do + get 'urls/:id', to: 'urls#short', id: /[:!].{4}/ + resources :urls + + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From cb2861d97b27e311ded3bc635aaf256f306a24bf Mon Sep 17 00:00:00 2001 From: Cara Kane Date: Thu, 17 May 2018 15:46:12 -0400 Subject: [PATCH 7/7] removed extraneous line of code --- source/app/controllers/urls_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/source/app/controllers/urls_controller.rb b/source/app/controllers/urls_controller.rb index a288756..e278eea 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -12,7 +12,6 @@ def index def show @url = Url.find(params[:id]) - url_path(@url) end def new