From 0f4b24deed039b1454596b29c96c6c210b8fee94 Mon Sep 17 00:00:00 2001 From: Lovinder Pnag Date: Sun, 31 May 2015 17:14:52 -0400 Subject: [PATCH 1/2] started making views for Bitly clone --- source/app/assets/stylesheets/urls.css.scss | 14 +++++++++++ source/app/controllers/urls_controller.rb | 3 +++ source/app/models/url.rb | 2 ++ source/app/views/layouts/application.html.erb | 4 +++- source/app/views/urls/index.html.erb | 3 +++ source/config/routes.rb | 4 ++++ .../db/migrate/20150531210037_create_urls.rb | 11 +++++++++ source/db/schema.rb | 24 +++++++++++++++++++ source/spec/models/url_spec.rb | 5 ++++ 9 files changed, 69 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/db/migrate/20150531210037_create_urls.rb create mode 100644 source/db/schema.rb create mode 100644 source/spec/models/url_spec.rb diff --git a/source/app/assets/stylesheets/urls.css.scss b/source/app/assets/stylesheets/urls.css.scss index a4281ec..2362870 100644 --- a/source/app/assets/stylesheets/urls.css.scss +++ b/source/app/assets/stylesheets/urls.css.scss @@ -1,3 +1,17 @@ // 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/ + +$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; +} \ No newline at end of file diff --git a/source/app/controllers/urls_controller.rb b/source/app/controllers/urls_controller.rb index ef26710..c46a628 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -1,2 +1,5 @@ class UrlsController < ApplicationController + def index + + end end diff --git a/source/app/models/url.rb b/source/app/models/url.rb new file mode 100644 index 0000000..e4834ff --- /dev/null +++ b/source/app/models/url.rb @@ -0,0 +1,2 @@ +class Url < ActiveRecord::Base +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 %> +
+ <%= yield %> +
diff --git a/source/app/views/urls/index.html.erb b/source/app/views/urls/index.html.erb new file mode 100644 index 0000000..e912075 --- /dev/null +++ b/source/app/views/urls/index.html.erb @@ -0,0 +1,3 @@ +

Welcome to LoRyder1's URL shortner

+ + <%= link_to 'New URL', new_url_path %> \ No newline at end of file diff --git a/source/config/routes.rb b/source/config/routes.rb index 3f66539..17b19b9 100644 --- a/source/config/routes.rb +++ b/source/config/routes.rb @@ -8,6 +8,10 @@ # Example of regular route: # get 'products/:id' => 'catalog#view' + root 'urls#index' + + resources :urls + # Example of named route that can be invoked with purchase_url(id: product.id) # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase diff --git a/source/db/migrate/20150531210037_create_urls.rb b/source/db/migrate/20150531210037_create_urls.rb new file mode 100644 index 0000000..f9af684 --- /dev/null +++ b/source/db/migrate/20150531210037_create_urls.rb @@ -0,0 +1,11 @@ +class CreateUrls < ActiveRecord::Migration + def change + create_table :urls do |t| + t.string :orig + t.string :short + t.integer :counter + + t.timestamps + end + end +end diff --git a/source/db/schema.rb b/source/db/schema.rb new file mode 100644 index 0000000..f42c1b4 --- /dev/null +++ b/source/db/schema.rb @@ -0,0 +1,24 @@ +# 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: 20150531210037) do + + create_table "urls", force: true do |t| + t.string "orig" + t.string "short" + t.integer "counter" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/source/spec/models/url_spec.rb b/source/spec/models/url_spec.rb new file mode 100644 index 0000000..209ca4c --- /dev/null +++ b/source/spec/models/url_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Url, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end From c27cd1c8dcee22c2e61f984a4b4beef2d898380b Mon Sep 17 00:00:00 2001 From: Lovinder Pnag Date: Sun, 31 May 2015 19:27:54 -0400 Subject: [PATCH 2/2] working bitly clone with error handling --- source/app/assets/stylesheets/urls.css.scss | 14 ++++++++ source/app/controllers/urls_controller.rb | 40 ++++++++++++++++++++- source/app/models/url.rb | 12 +++++++ source/app/views/urls/index.html.erb | 9 ++++- source/app/views/urls/new.html.erb | 28 +++++++++++++++ source/app/views/urls/show.html.erb | 8 +++++ source/config/routes.rb | 2 ++ 7 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 source/app/views/urls/new.html.erb create mode 100644 source/app/views/urls/show.html.erb diff --git a/source/app/assets/stylesheets/urls.css.scss b/source/app/assets/stylesheets/urls.css.scss index 2362870..e6e53e1 100644 --- a/source/app/assets/stylesheets/urls.css.scss +++ b/source/app/assets/stylesheets/urls.css.scss @@ -2,6 +2,7 @@ // 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; @@ -14,4 +15,17 @@ $my_grey: #F5F5F5; 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 c46a628..58c8f9f 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -1,5 +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 index e4834ff..77c8f2e 100644 --- a/source/app/models/url.rb +++ b/source/app/models/url.rb @@ -1,2 +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/urls/index.html.erb b/source/app/views/urls/index.html.erb index e912075..24a3ecb 100644 --- a/source/app/views/urls/index.html.erb +++ b/source/app/views/urls/index.html.erb @@ -1,3 +1,10 @@

Welcome to LoRyder1's URL shortner

- <%= link_to 'New URL', new_url_path %> \ No newline at end of file + <%= link_to 'New URL', new_url_path %> + + \ 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..07aff09 --- /dev/null +++ b/source/app/views/urls/new.html.erb @@ -0,0 +1,28 @@ +

URL shortner

+ +<% if @url.errors.any? %> + <%= pluralize(@url.errors.count, "error") %> + prohibited this url from being shortenend: + <% @url.errors.full_messages.each do |msg| %> + + <% end %> + + <% if flash[:error] %> +

<%= flash[:error] %>

+ <% end %> + +<% end %> + + +<%= form_for @url do |f| %> + +

+
<%= f.text_field :orig, placeholder: 'Enter your 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..b932300 --- /dev/null +++ b/source/app/views/urls/show.html.erb @@ -0,0 +1,8 @@ +

Your URL shortened:

+ + +

Original: <%= @url.orig %>

+ +

Short: + <%= link_to "localhost:3000/url/#{@url.short}", link_path(@url.short), :method => :post %> +

diff --git a/source/config/routes.rb b/source/config/routes.rb index 17b19b9..deb334a 100644 --- a/source/config/routes.rb +++ b/source/config/routes.rb @@ -12,6 +12,8 @@ resources :urls + post "url/:short" => "urls#link", as: "link" + # Example of named route that can be invoked with purchase_url(id: product.id) # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase