diff --git a/source/Gemfile b/source/Gemfile index 9627b8b..dd42c85 100644 --- a/source/Gemfile +++ b/source/Gemfile @@ -38,4 +38,3 @@ gem 'spring', group: :development # Use debugger # gem 'debugger', group: [:development, :test] gem 'rspec-rails', group: [:development, :test] - diff --git a/source/Gemfile.lock b/source/Gemfile.lock index fcf8b98..5eea0b9 100644 --- a/source/Gemfile.lock +++ b/source/Gemfile.lock @@ -28,33 +28,35 @@ GEM thread_safe (~> 0.1) tzinfo (~> 1.1) arel (5.0.1.20140414130214) - builder (3.2.2) + builder (3.2.3) coffee-rails (4.0.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) - coffee-script (2.3.0) + coffee-script (2.4.1) coffee-script-source execjs - coffee-script-source (1.8.0) - diff-lcs (1.2.5) + coffee-script-source (1.12.2) + concurrent-ruby (1.0.5) + diff-lcs (1.3) erubis (2.7.0) - execjs (2.2.1) + execjs (2.7.0) hike (1.2.3) - i18n (0.6.11) - jbuilder (2.2.2) - activesupport (>= 3.0.0, < 5) - multi_json (~> 1.2) - jquery-rails (3.1.2) + i18n (0.9.5) + concurrent-ruby (~> 1.0) + jbuilder (2.6.4) + activesupport (>= 3.0.0) + multi_json (>= 1.2) + jquery-rails (3.1.4) railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) - json (1.8.1) - mail (2.6.1) - mime-types (>= 1.16, < 3) - mime-types (2.4.1) - minitest (5.4.2) - multi_json (1.10.1) - rack (1.5.2) - rack-test (0.6.2) + json (1.8.6) + mail (2.7.0) + mini_mime (>= 0.1.1) + mini_mime (1.0.0) + minitest (5.11.3) + multi_json (1.13.1) + rack (1.5.5) + rack-test (0.6.3) rack (>= 1.0) rails (4.1.6) actionmailer (= 4.1.6) @@ -71,55 +73,55 @@ GEM activesupport (= 4.1.6) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (10.3.2) - rdoc (4.1.2) - json (~> 1.4) - rspec-core (3.1.6) - rspec-support (~> 3.1.0) - rspec-expectations (3.1.2) + rake (12.3.0) + rdoc (4.3.0) + rspec-core (3.7.1) + rspec-support (~> 3.7.0) + rspec-expectations (3.7.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.1.0) - rspec-mocks (3.1.3) - rspec-support (~> 3.1.0) - rspec-rails (3.1.0) + rspec-support (~> 3.7.0) + rspec-mocks (3.7.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.7.0) + rspec-rails (3.7.2) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) - rspec-core (~> 3.1.0) - rspec-expectations (~> 3.1.0) - rspec-mocks (~> 3.1.0) - rspec-support (~> 3.1.0) - rspec-support (3.1.2) + rspec-core (~> 3.7.0) + rspec-expectations (~> 3.7.0) + rspec-mocks (~> 3.7.0) + rspec-support (~> 3.7.0) + rspec-support (3.7.1) sass (3.2.19) - sass-rails (4.0.3) + sass-rails (4.0.5) railties (>= 4.0.0, < 5.0) - sass (~> 3.2.0) - sprockets (~> 2.8, <= 2.11.0) + sass (~> 3.2.2) + sprockets (~> 2.8, < 3.0) sprockets-rails (~> 2.0) - sdoc (0.4.1) + sdoc (0.4.2) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) - spring (1.1.3) - sprockets (2.11.0) + spring (1.7.2) + sprockets (2.12.4) hike (~> 1.2) multi_json (~> 1.0) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) - sprockets-rails (2.2.0) + sprockets-rails (2.3.3) actionpack (>= 3.0) activesupport (>= 3.0) sprockets (>= 2.8, < 4.0) - sqlite3 (1.3.9) - thor (0.19.1) - thread_safe (0.3.4) + sqlite3 (1.3.13) + thor (0.20.0) + thread_safe (0.3.6) tilt (1.4.1) - turbolinks (2.4.0) - coffee-rails - tzinfo (1.2.2) + turbolinks (5.1.0) + turbolinks-source (~> 5.1) + turbolinks-source (5.1.0) + tzinfo (1.2.5) thread_safe (~> 0.1) - uglifier (2.5.3) - execjs (>= 0.3.0) - json (>= 1.8.0) + uglifier (4.1.6) + execjs (>= 0.3.0, < 3) PLATFORMS ruby @@ -136,3 +138,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..c282bba 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -1,2 +1,57 @@ class UrlsController < ApplicationController + + #GET /urls + def index + @urls = Url.all + end + + #GET /urls/new + def new + @url = Url.new + end + #GET /urls/:id + def show + @url = Url.find(params[:id]) + end + + # POST /urls + def create + puts "inside create" + @url = Url.new(url_params) + if @url.save + redirect_to url_path(@url) + else + render 'new' + end + end + + #DELETE /urls/:id + def destroy + @url =Url.find(params[:id]) + if @url.destroy + redirect_to urls_path + else + + end + end + + #GET /:short_url + def hit_short_url + puts "inside hit_url" + url = Url.find_by short_url: params[:short_url] + if url + url.update('click_count' => url.click_count + 1) + redirect_to url.original_url + else + redirect_to root_path + end + + end + + private + def url_params + params.require(:url).permit(:original_url) + end + + end diff --git a/source/app/models/url.rb b/source/app/models/url.rb new file mode 100644 index 0000000..12c0f3c --- /dev/null +++ b/source/app/models/url.rb @@ -0,0 +1,31 @@ +require 'uri' +class Url < ActiveRecord:: Base + + CHARSET = (('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a) + validates :original_url, presence: true + validate :url_valid? + #before_save :shorten_url, :on => :create + before_create :shorten_url + + private def url_valid? + begin + error.add(:original_url, " is invalid!") unless + URI.parse(original_url).kind_of?(URI::HTTP) || URI.parse(original_url).kind_of?(URI::HTTPS) + rescue + errors.add(:original_url, " is invalid! (must start with 'http' or 'https')") + end +end + + private def shorten_url + puts "inside shotern method" + self.short_url = create_short_url + end + + private def create_short_url + tiny_url ="" + 6.times do + tiny_url << CHARSET.sample + end + tiny_url + end +end diff --git a/source/app/views/layouts/application.html.erb b/source/app/views/layouts/application.html.erb index f946432..ff18f09 100644 --- a/source/app/views/layouts/application.html.erb +++ b/source/app/views/layouts/application.html.erb @@ -5,10 +5,30 @@ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> <%= csrf_meta_tags %> + + + + +
+