From 24bf7f53cae3fc95a97c239ab5919106e5cec3c8 Mon Sep 17 00:00:00 2001 From: artze Date: Mon, 6 Feb 2017 16:13:47 +0800 Subject: [PATCH 01/11] created url table; created UI for index --- app/views/layouts/application.erb | 4 +++- app/views/static/index.erb | 9 +++++++-- .../20170206151059_create_bitly_url_table.rb | 10 ++++++++++ public/css/application.css | 20 +++++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20170206151059_create_bitly_url_table.rb diff --git a/app/views/layouts/application.erb b/app/views/layouts/application.erb index 4b47fec..72665cf 100644 --- a/app/views/layouts/application.erb +++ b/app/views/layouts/application.erb @@ -1,8 +1,10 @@ - Sinatra Framework + Lnkshortnr + +

Lnkshortnr

<%= yield %> \ No newline at end of file diff --git a/app/views/static/index.erb b/app/views/static/index.erb index 622f8bb..6bf56c9 100644 --- a/app/views/static/index.erb +++ b/app/views/static/index.erb @@ -1,2 +1,7 @@ -

Hello World

-

See Me in views/static/index.html.erb

\ No newline at end of file +
+
+ Enter URL: + + +
+
\ No newline at end of file diff --git a/db/migrate/20170206151059_create_bitly_url_table.rb b/db/migrate/20170206151059_create_bitly_url_table.rb new file mode 100644 index 0000000..96020ff --- /dev/null +++ b/db/migrate/20170206151059_create_bitly_url_table.rb @@ -0,0 +1,10 @@ +class CreateBitlyUrlTable < ActiveRecord::Migration + def change + create_table :urls do |t| + t.string :links + t.string :serial_code + + t.timestamps + end + end +end diff --git a/public/css/application.css b/public/css/application.css index e69de29..f120cba 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -0,0 +1,20 @@ +@import url('https://fonts.googleapis.com/css?family=Maven+Pro'); + +body { + background-color: #f5f5f5; +} + +h1 { + color: #a6a6a6; + font-family: 'Maven Pro', sans-serif; + text-align: center; + font-size: 50px; +} + +#header-caps { + color: #e6e600; +} + +.form { + text-align: center; +} \ No newline at end of file From 26e354bbb5359fc6b71a58b43ff209670b9de85f Mon Sep 17 00:00:00 2001 From: artze Date: Tue, 7 Feb 2017 11:16:50 +0800 Subject: [PATCH 02/11] completed shortener feature --- app/controllers/static.rb | 13 +++++++ app/models/url.rb | 10 ++++++ app/views/layouts/application.erb | 3 +- app/views/static/index.erb | 36 +++++++++++++++---- ...0170206171803_rename_serial_code_column.rb | 5 +++ db/migrate/20170206172530_rename_columns.rb | 6 ++++ public/css/application.css | 30 +++++++++++++++- 7 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 app/models/url.rb create mode 100644 db/migrate/20170206171803_rename_serial_code_column.rb create mode 100644 db/migrate/20170206172530_rename_columns.rb diff --git a/app/controllers/static.rb b/app/controllers/static.rb index 33c1897..8c09a7d 100644 --- a/app/controllers/static.rb +++ b/app/controllers/static.rb @@ -1,3 +1,16 @@ get '/' do erb :"static/index" +end + +post '/urls' do + new_url = Url.new + new_url.link = params[:long_url].to_s + new_url.serial_code = new_url.shorten + new_url.save + redirect '/' +end + +get '/:serial_code' do + long_url = 'http://' + Url.find_by(serial_code: params[:serial_code]).link + redirect long_url end \ No newline at end of file diff --git a/app/models/url.rb b/app/models/url.rb new file mode 100644 index 0000000..29bb865 --- /dev/null +++ b/app/models/url.rb @@ -0,0 +1,10 @@ +class Url < ActiveRecord::Base + # This is Sinatra! Remember to create a migration! + def shorten + # generate random code + characters = [('a'..'z').to_a, ('A'..'Z').to_a, (0..9).to_a].flatten + serial_code = characters.sample(7) + serial_code = serial_code.join('') + serial_code + end +end diff --git a/app/views/layouts/application.erb b/app/views/layouts/application.erb index 72665cf..c87453a 100644 --- a/app/views/layouts/application.erb +++ b/app/views/layouts/application.erb @@ -1,10 +1,11 @@ Lnkshortnr + -

Lnkshortnr

+

Lnkshortnr

<%= yield %> \ No newline at end of file diff --git a/app/views/static/index.erb b/app/views/static/index.erb index 6bf56c9..54a8f8f 100644 --- a/app/views/static/index.erb +++ b/app/views/static/index.erb @@ -1,7 +1,31 @@ -
-
- Enter URL: - - -
+
+
+
+
+
+ +
+ +
+
+
+ +
+ <% unless Url.all %> + + + + + + + + <% Url.all.order(created_at: :desc).each do |result| %> + + + + + <% end %> +
URLShortened URL
<%= result.link %><%= 'http://localhost:9393/' + result.serial_code %>
+ <% end %> +
\ No newline at end of file diff --git a/db/migrate/20170206171803_rename_serial_code_column.rb b/db/migrate/20170206171803_rename_serial_code_column.rb new file mode 100644 index 0000000..6c46f1b --- /dev/null +++ b/db/migrate/20170206171803_rename_serial_code_column.rb @@ -0,0 +1,5 @@ +class RenameSerialCodeColumn < ActiveRecord::Migration + def change + rename_column :urls, :serial_code, :serial_codes + end +end diff --git a/db/migrate/20170206172530_rename_columns.rb b/db/migrate/20170206172530_rename_columns.rb new file mode 100644 index 0000000..47c97db --- /dev/null +++ b/db/migrate/20170206172530_rename_columns.rb @@ -0,0 +1,6 @@ +class RenameColumns < ActiveRecord::Migration + def change + rename_column :urls, :links, :link + rename_column :urls, :serial_codes, :serial_code + end +end diff --git a/public/css/application.css b/public/css/application.css index f120cba..16140b7 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -1,7 +1,7 @@ @import url('https://fonts.googleapis.com/css?family=Maven+Pro'); body { - background-color: #f5f5f5; + background-color: #f5f5f5 ; } h1 { @@ -16,5 +16,33 @@ h1 { } .form { + margin-top: 4em; text-align: center; +} + +.url-list { + margin-top: 8em; + font-family: 'Maven Pro', sans-serif; +} + +td { + height: 30; + color: #ffffff; +} + +th { + color: #ffffff; +} + +.btn-color { + color: #a6a6a6; + font-family: 'Maven Pro', sans-serif; + font-size: 16px; + background-color: #e6e600; + margin-left: -0.5em; + border-radius: 0; +} + +a { + color: #ffffff; } \ No newline at end of file From 162811e4dd21bc2cc117685970e029ac1c4a684a Mon Sep 17 00:00:00 2001 From: artze Date: Tue, 7 Feb 2017 20:15:04 +0800 Subject: [PATCH 03/11] completed validation feature --- .byebug_history | 10 +++++++ Gemfile | 4 ++- Gemfile.lock | 7 ++++- app/controllers/static.rb | 3 ++- app/models/url.rb | 7 +++++ app/views/static/index.erb | 26 ++++++++++++++----- config/environments/init.rb | 2 +- .../20170207201248_add_click_count_column.rb | 5 ++++ public/css/application.css | 24 ++++++++++++----- 9 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 .byebug_history create mode 100644 db/migrate/20170207201248_add_click_count_column.rb diff --git a/.byebug_history b/.byebug_history new file mode 100644 index 0000000..32d17e9 --- /dev/null +++ b/.byebug_history @@ -0,0 +1,10 @@ +exit +@error_messages.messages[:link].each{|e| p e} +@error_messages.messages[:link] +@error_messages.messages +@error_messages +c +new_url.errors.full_messages +new_url.errors.messages +new_url.errors +new_url diff --git a/Gemfile b/Gemfile index 562fecb..79b6d68 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,7 @@ source 'https://rubygems.org' # Adding Sinatra Drivers gem 'sinatra' gem 'sinatra-contrib' - +gem 'byebug' # Adding thin gem as advised gem 'thin' @@ -24,6 +24,8 @@ gem 'rake' # Adding rspec for running unit testing gem 'rspec' +gem 'sinatra-flash' + group :development, :test do # Adding shotgun for local web hosting gem 'shotgun' diff --git a/Gemfile.lock b/Gemfile.lock index ae38c75..48d45b4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -17,6 +17,7 @@ GEM arel (6.0.3) backports (3.6.7) builder (3.2.2) + byebug (9.0.6) daemons (1.2.3) diff-lcs (1.2.5) eventmachine (1.0.8) @@ -66,6 +67,8 @@ GEM rack-test sinatra (~> 1.4.0) tilt (>= 1.3, < 3) + sinatra-flash (0.3.0) + sinatra (>= 1.0.0) thin (1.6.4) daemons (~> 1.0, >= 1.0.9) eventmachine (~> 1.0, >= 1.0.4) @@ -81,6 +84,7 @@ PLATFORMS DEPENDENCIES activerecord activesupport + byebug pg puma rails_12factor @@ -90,7 +94,8 @@ DEPENDENCIES sinatra sinatra-activerecord sinatra-contrib + sinatra-flash thin BUNDLED WITH - 1.10.6 + 1.13.3 diff --git a/app/controllers/static.rb b/app/controllers/static.rb index 8c09a7d..103289d 100644 --- a/app/controllers/static.rb +++ b/app/controllers/static.rb @@ -7,7 +7,8 @@ new_url.link = params[:long_url].to_s new_url.serial_code = new_url.shorten new_url.save - redirect '/' + @error_messages = new_url.errors + erb :'static/index' end get '/:serial_code' do diff --git a/app/models/url.rb b/app/models/url.rb index 29bb865..ec75ba3 100644 --- a/app/models/url.rb +++ b/app/models/url.rb @@ -1,5 +1,12 @@ class Url < ActiveRecord::Base # This is Sinatra! Remember to create a migration! + validates :link, presence:{message: 'URL required'} + validate :check_uri_validity + def check_uri_validity + if URI.parse(self.link).host.nil? + errors.add(:link, 'Invalid URL entered') + end + end def shorten # generate random code characters = [('a'..'z').to_a, ('A'..'Z').to_a, (0..9).to_a].flatten diff --git a/app/views/static/index.erb b/app/views/static/index.erb index 54a8f8f..cd4114a 100644 --- a/app/views/static/index.erb +++ b/app/views/static/index.erb @@ -2,22 +2,36 @@
-
- +
+
-
- <% unless Url.all %> +
+
+ <% if @error_messages %> + <% @error_messages.messages[:link].each do |msg|%> +

+ <%= msg %> +

+ <%end%> + <% end %> +
+
+
+ +
+
+ <% unless Url.all.empty? %> - + - + <% Url.all.order(created_at: :desc).each do |result| %> diff --git a/config/environments/init.rb b/config/environments/init.rb index 44a8a15..0d00996 100644 --- a/config/environments/init.rb +++ b/config/environments/init.rb @@ -7,7 +7,7 @@ require 'rubygems' require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) require 'pathname' - +require 'byebug' # database require 'pg' require 'active_record' diff --git a/db/migrate/20170207201248_add_click_count_column.rb b/db/migrate/20170207201248_add_click_count_column.rb new file mode 100644 index 0000000..459ebeb --- /dev/null +++ b/db/migrate/20170207201248_add_click_count_column.rb @@ -0,0 +1,5 @@ +class AddClickCountColumn < ActiveRecord::Migration + def change + add_column :urls, :click_count, :integer + end +end diff --git a/public/css/application.css b/public/css/application.css index 16140b7..3cbc47f 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -5,6 +5,7 @@ body { } h1 { + margin-top: 3em; color: #a6a6a6; font-family: 'Maven Pro', sans-serif; text-align: center; @@ -16,26 +17,27 @@ h1 { } .form { - margin-top: 4em; + margin-top: 3em; text-align: center; } .url-list { - margin-top: 8em; + margin-top: 4em; font-family: 'Maven Pro', sans-serif; } td { height: 30; - color: #ffffff; + color: #616161; + font-size: 15px; } th { - color: #ffffff; + color: #616161; } .btn-color { - color: #a6a6a6; + color: #616161; font-family: 'Maven Pro', sans-serif; font-size: 16px; background-color: #e6e600; @@ -44,5 +46,15 @@ th { } a { - color: #ffffff; + color: #616161; +} + +a:hover { + color: #d73737; +} + +.error-msg { + font-family: 'Maven Pro', sans-serif; + color: #d73737; + font-style: italic; } \ No newline at end of file From 40c3befdc7505dc6fb36d549637691050c741e8d Mon Sep 17 00:00:00 2001 From: artze Date: Tue, 7 Feb 2017 20:29:27 +0800 Subject: [PATCH 04/11] added clickcount column --- app/controllers/static.rb | 5 ++++- app/models/url.rb | 1 - app/views/static/index.erb | 7 +++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/controllers/static.rb b/app/controllers/static.rb index 103289d..2467ed5 100644 --- a/app/controllers/static.rb +++ b/app/controllers/static.rb @@ -6,12 +6,15 @@ new_url = Url.new new_url.link = params[:long_url].to_s new_url.serial_code = new_url.shorten + new_url.click_count = 0 new_url.save @error_messages = new_url.errors erb :'static/index' end get '/:serial_code' do - long_url = 'http://' + Url.find_by(serial_code: params[:serial_code]).link + url_record = Url.find_by(serial_code: params[:serial_code]) + url_record.increment!(:click_count) + long_url = url_record.link redirect long_url end \ No newline at end of file diff --git a/app/models/url.rb b/app/models/url.rb index ec75ba3..bb88cce 100644 --- a/app/models/url.rb +++ b/app/models/url.rb @@ -8,7 +8,6 @@ def check_uri_validity end end def shorten - # generate random code characters = [('a'..'z').to_a, ('A'..'Z').to_a, (0..9).to_a].flatten serial_code = characters.sample(7) serial_code = serial_code.join('') diff --git a/app/views/static/index.erb b/app/views/static/index.erb index cd4114a..0331d6d 100644 --- a/app/views/static/index.erb +++ b/app/views/static/index.erb @@ -28,15 +28,18 @@ <% unless Url.all.empty? %>
URLShortened URLShortn URL
- + + + - <% Url.all.order(created_at: :desc).each do |result| %> + <% Url.all.order(click_count: :desc).each do |result| %> + <% end %>
URL Shortn URLClick Count
<%= result.link %> <%= 'http://localhost:9393/' + result.serial_code %><%= result.click_count %>
From 2e2aa0fbdbdb4b79ffdffbaf66ef2a50d3868887 Mon Sep 17 00:00:00 2001 From: artze Date: Tue, 7 Feb 2017 22:44:48 +0800 Subject: [PATCH 05/11] /urls redirect; url record ordering --- app/controllers/static.rb | 4 ++++ app/models/url.rb | 1 + app/views/static/index.erb | 6 +++--- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/controllers/static.rb b/app/controllers/static.rb index 2467ed5..a1cef91 100644 --- a/app/controllers/static.rb +++ b/app/controllers/static.rb @@ -2,6 +2,10 @@ erb :"static/index" end +get '/urls' do + redirect '/' +end + post '/urls' do new_url = Url.new new_url.link = params[:long_url].to_s diff --git a/app/models/url.rb b/app/models/url.rb index bb88cce..8a22b10 100644 --- a/app/models/url.rb +++ b/app/models/url.rb @@ -7,6 +7,7 @@ def check_uri_validity errors.add(:link, 'Invalid URL entered') end end + def shorten characters = [('a'..'z').to_a, ('A'..'Z').to_a, (0..9).to_a].flatten serial_code = characters.sample(7) diff --git a/app/views/static/index.erb b/app/views/static/index.erb index 0331d6d..696b5c3 100644 --- a/app/views/static/index.erb +++ b/app/views/static/index.erb @@ -11,7 +11,7 @@
-
+
<% if @error_messages %> <% @error_messages.messages[:link].each do |msg|%>

@@ -32,10 +32,10 @@ URL - Shortn URL + Lnkshortnr URL Click Count - <% Url.all.order(click_count: :desc).each do |result| %> + <% Url.all.order(click_count: :desc, created_at: :desc).each do |result| %> <%= result.link %> <%= 'http://localhost:9393/' + result.serial_code %> From d14b05cd748cfca3dcf2487b1d764f4a9f1199ed Mon Sep 17 00:00:00 2001 From: artze Date: Wed, 8 Feb 2017 12:32:38 +0800 Subject: [PATCH 06/11] added sticky footer --- app/controllers/static.rb | 8 ++++++ app/models/url.rb | 2 +- app/views/layouts/application.erb | 17 ++++++++++++ app/views/static/about.erb | 7 +++++ app/views/static/howto.erb | 13 ++++++++++ public/css/application.css | 43 ++++++++++++++++++++++++++++++- 6 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 app/views/static/about.erb create mode 100644 app/views/static/howto.erb diff --git a/app/controllers/static.rb b/app/controllers/static.rb index a1cef91..1d71d8f 100644 --- a/app/controllers/static.rb +++ b/app/controllers/static.rb @@ -6,6 +6,14 @@ redirect '/' end +get '/howto' do + erb :'static/howto' +end + +get '/about' do + erb :'static/about' +end + post '/urls' do new_url = Url.new new_url.link = params[:long_url].to_s diff --git a/app/models/url.rb b/app/models/url.rb index 8a22b10..ee03521 100644 --- a/app/models/url.rb +++ b/app/models/url.rb @@ -3,7 +3,7 @@ class Url < ActiveRecord::Base validates :link, presence:{message: 'URL required'} validate :check_uri_validity def check_uri_validity - if URI.parse(self.link).host.nil? + if URI.parse(URI.escape(self.link)).host.nil? errors.add(:link, 'Invalid URL entered') end end diff --git a/app/views/layouts/application.erb b/app/views/layouts/application.erb index c87453a..93eeca0 100644 --- a/app/views/layouts/application.erb +++ b/app/views/layouts/application.erb @@ -8,4 +8,21 @@

Lnkshortnr

<%= yield %> + \ No newline at end of file diff --git a/app/views/static/about.erb b/app/views/static/about.erb new file mode 100644 index 0000000..93afedc --- /dev/null +++ b/app/views/static/about.erb @@ -0,0 +1,7 @@ +
+
+
+

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam in tincidunt magna. Morbi congue ante ac porta rutrum. Curabitur ac bibendum erat, vulputate placerat ipsum. Suspendisse a sagittis leo. Nunc nec interdum sem, vel fringilla purus. Donec convallis aliquam est, ut pulvinar enim auctor vitae. Proin at tempus mauris. In mattis nisl a dui euismod, ultricies ullamcorper risus malesuada.

+
+
+
\ No newline at end of file diff --git a/app/views/static/howto.erb b/app/views/static/howto.erb new file mode 100644 index 0000000..028c800 --- /dev/null +++ b/app/views/static/howto.erb @@ -0,0 +1,13 @@ +
+
+
+

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam in tincidunt magna. Morbi congue ante ac porta rutrum. Curabitur ac bibendum erat, vulputate placerat ipsum. Suspendisse a sagittis leo. Nunc nec interdum sem, vel fringilla purus. Donec convallis aliquam est, ut pulvinar enim auctor vitae. Proin at tempus mauris. In mattis nisl a dui euismod, ultricies ullamcorper risus malesuada.

+ +
    +
  • Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  • +
  • Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  • +
  • Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  • +
+
+
+
\ No newline at end of file diff --git a/public/css/application.css b/public/css/application.css index 3cbc47f..f7fa8a1 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -2,6 +2,7 @@ body { background-color: #f5f5f5 ; + margin-bottom: 40px; } h1 { @@ -43,6 +44,16 @@ th { background-color: #e6e600; margin-left: -0.5em; border-radius: 0; + cursor: pointer; +} + +.btn-color:hover { + opacity: 0.7; +} + +.btn-color:active { + opacity: 0.7; + background-color: #b3b300; } a { @@ -50,11 +61,41 @@ a { } a:hover { - color: #d73737; + color: #b3b300; } .error-msg { font-family: 'Maven Pro', sans-serif; color: #d73737; font-style: italic; +} + +.footer12 { + display: inline-block; + opacity: 0.6; + border-top: 1px solid #a6a6a6; + position: fixed; + bottom: 0; + width: 100%; + height: 40px; + line-height: 40px; + font-family: 'Maven Pro', sans-serif; + background-color: #ececec; +} + +span.footer-links { + margin-left: 2em; + font-size: 14px; +} + +span.footer-links a:hover { + color: #b3b300; + text-decoration: none; +} + +.descriptive-content { + margin-top: 3em; + font-family: 'Maven Pro', sans-serif; + color: #616161; + font-size: 15px; } \ No newline at end of file From 0dcd5153d1938988851b15cb82c910681b9e5f1d Mon Sep 17 00:00:00 2001 From: artze Date: Wed, 8 Feb 2017 23:38:45 +0800 Subject: [PATCH 07/11] added AJAX to form submit --- .byebug_history | 20 ++++++++++++++ app/controllers/static.rb | 10 +++++-- app/views/layouts/application.erb | 4 ++- app/views/static/index.erb | 4 +-- public/css/application.css | 46 +++++++++++++++++++++++++------ public/js/application.js | 24 ++++++++++++++++ 6 files changed, 94 insertions(+), 14 deletions(-) diff --git a/.byebug_history b/.byebug_history index 32d17e9..a7512e4 100644 --- a/.byebug_history +++ b/.byebug_history @@ -1,3 +1,23 @@ +c +{a: 233}.to_json +[a: 223].to_json +'test'.to_json +new_url.errors.messages[:link].to_json +new_url.errors.messages[:link] +new_url.errors.messages +new_url.errors +new_url +c +new_url.errors.messages[:links] +c +exit +params +exit +params +exit +params +exit +params exit @error_messages.messages[:link].each{|e| p e} @error_messages.messages[:link] diff --git a/app/controllers/static.rb b/app/controllers/static.rb index 1d71d8f..16a246f 100644 --- a/app/controllers/static.rb +++ b/app/controllers/static.rb @@ -19,9 +19,13 @@ new_url.link = params[:long_url].to_s new_url.serial_code = new_url.shorten new_url.click_count = 0 - new_url.save - @error_messages = new_url.errors - erb :'static/index' + if new_url.save + new_url.to_json + else + {error: 'Invalid URL'}.to_json + end + # @error_messages = new_url.errors + # erb :'static/index' end get '/:serial_code' do diff --git a/app/views/layouts/application.erb b/app/views/layouts/application.erb index 93eeca0..09546de 100644 --- a/app/views/layouts/application.erb +++ b/app/views/layouts/application.erb @@ -3,12 +3,14 @@ Lnkshortnr +

Lnkshortnr

<%= yield %> + -