diff --git a/source/app/assets/stylesheets/urls.css.scss b/source/app/assets/stylesheets/urls.css.scss index a4281ec..e6e53e1 100644 --- a/source/app/assets/stylesheets/urls.css.scss +++ b/source/app/assets/stylesheets/urls.css.scss @@ -1,3 +1,31 @@ // 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/ + +$red: #8C2318; +$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; +} + +.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 ef26710..58c8f9f 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -1,2 +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 new file mode 100644 index 0000000..77c8f2e --- /dev/null +++ b/source/app/models/url.rb @@ -0,0 +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/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..24a3ecb --- /dev/null +++ b/source/app/views/urls/index.html.erb @@ -0,0 +1,10 @@ +

Welcome to LoRyder1's URL shortner

+ + <%= 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 3f66539..deb334a 100644 --- a/source/config/routes.rb +++ b/source/config/routes.rb @@ -8,6 +8,12 @@ # Example of regular route: # get 'products/:id' => 'catalog#view' + root 'urls#index' + + 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 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