diff --git a/.byebug_history b/.byebug_history new file mode 100644 index 0000000..a3a475f --- /dev/null +++ b/.byebug_history @@ -0,0 +1,9 @@ +c +@urls.swapcase +@urls +c +Url.all +@urls +Url.last(5) +Url.last +@urls diff --git a/Gemfile b/Gemfile index 562fecb..2f0e1d7 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,7 @@ source 'https://rubygems.org' # Ruby Version # ruby "2.2.1" +ruby "2.4.0" # Adding Sinatra Drivers gem 'sinatra' @@ -24,6 +25,9 @@ gem 'rake' # Adding rspec for running unit testing gem 'rspec' +# Add other gems +gem 'byebug' + group :development, :test do # Adding shotgun for local web hosting gem 'shotgun' @@ -34,4 +38,4 @@ group :production do # ================== gem 'rails_12factor' # Heroku Gem Supports gem 'puma' # Using puma for -end \ No newline at end of file +end diff --git a/Gemfile.lock b/Gemfile.lock index ae38c75..a754d00 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,32 +1,30 @@ GEM remote: https://rubygems.org/ specs: - activemodel (4.2.5) - activesupport (= 4.2.5) - builder (~> 3.1) - activerecord (4.2.5) - activemodel (= 4.2.5) - activesupport (= 4.2.5) - arel (~> 6.0) - activesupport (4.2.5) + activemodel (5.0.2) + activesupport (= 5.0.2) + activerecord (5.0.2) + activemodel (= 5.0.2) + activesupport (= 5.0.2) + arel (~> 7.0) + activesupport (5.0.2) + concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) - json (~> 1.7, >= 1.7.7) minitest (~> 5.1) - thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) - arel (6.0.3) - backports (3.6.7) - builder (3.2.2) - daemons (1.2.3) - diff-lcs (1.2.5) - eventmachine (1.0.8) - i18n (0.7.0) - json (1.8.3) - minitest (5.8.3) - multi_json (1.11.2) - pg (0.18.4) - puma (2.15.3) - rack (1.6.4) + arel (7.1.4) + backports (3.6.8) + byebug (9.0.6) + concurrent-ruby (1.0.5) + daemons (1.2.4) + diff-lcs (1.3) + eventmachine (1.2.3) + i18n (0.8.1) + minitest (5.10.1) + multi_json (1.12.1) + pg (0.19.0) + puma (3.7.1) + rack (1.6.5) rack-protection (1.5.3) rack rack-test (0.6.3) @@ -34,44 +32,44 @@ GEM rails_12factor (0.0.3) rails_serve_static_assets rails_stdout_logging - rails_serve_static_assets (0.0.4) - rails_stdout_logging (0.0.4) - rake (10.4.2) - rspec (3.4.0) - rspec-core (~> 3.4.0) - rspec-expectations (~> 3.4.0) - rspec-mocks (~> 3.4.0) - rspec-core (3.4.1) - rspec-support (~> 3.4.0) - rspec-expectations (3.4.0) + rails_serve_static_assets (0.0.5) + rails_stdout_logging (0.0.5) + rake (12.0.0) + rspec (3.5.0) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) + rspec-core (3.5.4) + rspec-support (~> 3.5.0) + rspec-expectations (3.5.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.4.0) - rspec-mocks (3.4.0) + rspec-support (~> 3.5.0) + rspec-mocks (3.5.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.4.0) - rspec-support (3.4.1) - shotgun (0.9.1) + rspec-support (~> 3.5.0) + rspec-support (3.5.0) + shotgun (0.9.2) rack (>= 1.0) - sinatra (1.4.6) - rack (~> 1.4) + sinatra (1.4.8) + rack (~> 1.5) rack-protection (~> 1.4) tilt (>= 1.3, < 3) - sinatra-activerecord (2.0.9) + sinatra-activerecord (2.0.12) activerecord (>= 3.2) - sinatra (~> 1.0) - sinatra-contrib (1.4.6) + sinatra (>= 1.0) + sinatra-contrib (1.4.7) backports (>= 2.0) multi_json rack-protection rack-test sinatra (~> 1.4.0) tilt (>= 1.3, < 3) - thin (1.6.4) + thin (1.7.0) daemons (~> 1.0, >= 1.0.9) eventmachine (~> 1.0, >= 1.0.4) - rack (~> 1.0) - thread_safe (0.3.5) - tilt (2.0.1) + rack (>= 1, < 3) + thread_safe (0.3.6) + tilt (2.0.6) tzinfo (1.2.2) thread_safe (~> 0.1) @@ -81,6 +79,7 @@ PLATFORMS DEPENDENCIES activerecord activesupport + byebug pg puma rails_12factor @@ -93,4 +92,4 @@ DEPENDENCIES thin BUNDLED WITH - 1.10.6 + 1.14.4 diff --git a/app/controllers/static.rb b/app/controllers/static.rb index 33c1897..cfbcfa8 100644 --- a/app/controllers/static.rb +++ b/app/controllers/static.rb @@ -1,3 +1,31 @@ get '/' do + # let user create new short URL, display a list of shortened URLs + @urls = Url.last(5) + # @test = "hi" + @urls.reverse! erb :"static/index" -end \ No newline at end of file +end + +post '/urls' do + # create a new Url + @url = Url.new(long_url: params[:long_url]) + + if @url.save + redirect "/" + else + @error = "Invalid URL! Please include 'http://' OR 'https://'." + @urls.all + erb :"static/index" + end + +end + +# i.e. /q6bda +get '/:short_url' do + # redirect to appropriate "long" URL + @url = Url.find_by(short_url: params[:short_url]) + redirect @url.long_url + @url.click_count += 1 + @url.save + erb :"static/index" +end diff --git a/app/models/url.rb b/app/models/url.rb new file mode 100644 index 0000000..d277d1b --- /dev/null +++ b/app/models/url.rb @@ -0,0 +1,15 @@ +require 'uri' + +class Url < ActiveRecord::Base + # This is Sinatra! Remember to create a migration! + validates :long_url, :presence => true, :format => URI::regexp(%w(http https)) + before_create :shorten, :counter + + def shorten + self.short_url = SecureRandom.base64(6) + end + + def counter + self.click_count = 0 + end +end diff --git a/app/views/static/index.erb b/app/views/static/index.erb index 622f8bb..6f7acd6 100644 --- a/app/views/static/index.erb +++ b/app/views/static/index.erb @@ -1,2 +1,49 @@ -

Hello World

-

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

\ No newline at end of file + + + + + + + v.l.bitly + + + + +

v.l.bitly

+ +

SIZE DOESN'T MATTER. SO KEEP IT SHORT!

+ +

Paste a link to shorten it

+ +
+ + +
+ +
+ +

These are your shorten URLs!!! =)

+ + + + + + + + + + + + <% @urls.each do |url| %> + + + + + + + <% end %> + + +
Long URLShorten URLVisit Count
<%= url.long_url %> http://v.l.ty/<%= url.short_url%> <%= url.click_count%>
+
+ diff --git a/config/environments/init.rb b/config/environments/init.rb index 44a8a15..0790900 100644 --- a/config/environments/init.rb +++ b/config/environments/init.rb @@ -7,6 +7,7 @@ require 'rubygems' require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) require 'pathname' +require 'byebug' # database require 'pg' @@ -51,4 +52,4 @@ # Setup controller (C) Dir[APP_ROOT.join('app', 'controllers', '*.rb')].each { |file| require file } -###################################################################### \ No newline at end of file +###################################################################### diff --git a/db/migrate/20170308190430_create_urls.rb b/db/migrate/20170308190430_create_urls.rb new file mode 100644 index 0000000..fd189cd --- /dev/null +++ b/db/migrate/20170308190430_create_urls.rb @@ -0,0 +1,12 @@ +class CreateUrls < ActiveRecord::Migration + + def change + create_table :urls do |t| + t.string :long_url + t.string :short_url + t.integer :click_count + + t.timestamps + end + end +end diff --git a/public/css/application.css b/public/css/application.css index e69de29..f14ddab 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -0,0 +1,13 @@ +body{ + background-image: + linear-gradient( + rgba(0, 0, 0, 0.75), + rgba(0, 0, 0, 0.01) + ), + url(../img/smile_lady.jpg); + background-size: cover; +} + +h1{ + text-align: center; +} diff --git a/public/img/smile_lady.jpg b/public/img/smile_lady.jpg new file mode 100644 index 0000000..d2488a0 Binary files /dev/null and b/public/img/smile_lady.jpg differ