-
Notifications
You must be signed in to change notification settings - Fork 40
finished #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
finished #40
Changes from 5 commits
0aa6d5a
03138b3
3309367
f379b73
413ec2a
ef06625
cb2861d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,35 @@ | ||
| class UrlsController < ApplicationController | ||
|
|
||
| def index | ||
| @urls = Url.all | ||
| end | ||
|
|
||
| def show | ||
| if params[:id].start_with?("!") | ||
| @url = Url.find_by(:short_url => "http://localhost:3000/urls/#{params[:id]}") | ||
| @url.click_count += 1 | ||
|
||
| redirect_to @url[:long_url] | ||
| else | ||
| @url = Url.find(params[:id]) | ||
| url_path(@url) | ||
| end | ||
| end | ||
|
|
||
| def new | ||
| @url = Url.new | ||
| end | ||
|
|
||
| def create | ||
| @url = Url.new(url_params) | ||
| if @url.save | ||
| redirect_to url_path(@url) | ||
| else | ||
| render 'new' | ||
| end | ||
| end | ||
|
|
||
| private | ||
| def url_params | ||
| params.require(:url).permit(:long_url) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| require 'net/http' | ||
|
|
||
| class Url < ActiveRecord::Base | ||
| before_save :shorten_url | ||
| validates :long_url, presence: true, allow_blank: false | ||
| validate :validate_url | ||
|
|
||
| def validate_url | ||
| uri = URI("#{self.long_url}") | ||
| # this doesn't work if the input isn't a url, i.e., it only works if an error code is returned | ||
| res = Net::HTTP.get_response(uri) | ||
| self.errors.add(:base, "This URL is invalid, #{res.message}") if res.code != "200" | ||
| end | ||
|
|
||
| def shorten_url | ||
| self.short_url = "http://localhost:3000/urls/!#{(0..3).map{ rand(36).to_s(36) }.join}" | ||
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <h1>All the URLs</h1> | ||
|
|
||
| <% @urls.each do |url| %> | ||
| <%= url.long_url %><br /> | ||
| <% end %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <% if @url.errors.any? %> | ||
| <% @url.errors.full_messages.each do |message| %> | ||
| <li><%= message %></li> | ||
| <% end %> | ||
| <% end %> | ||
|
|
||
| <%= form_for @url do |f| %> | ||
| <%= f.label "Enter a URL" %> | ||
| <%= f.url_field :long_url, :required => true %> | ||
| <%= f.submit %> | ||
| <% end %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| The URL you entered: <%= @url.long_url %><br /> | ||
| Its short version: <%= link_to @url.short_url, @url.short_url %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class Urls < ActiveRecord::Migration | ||
| def change | ||
| create_table :urls do |t| | ||
| t.string :long_url | ||
| t.string :short_url | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class AddClickCountToUrls < ActiveRecord::Migration | ||
| def change | ||
| add_column :urls, :click_count, :integer, default: 0 | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # 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: 20180515131738) do | ||
|
|
||
| create_table "urls", force: true do |t| | ||
| t.string "long_url" | ||
| t.string "short_url" | ||
| t.integer "click_count", default: 0 | ||
| end | ||
|
|
||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's try to make the router send these two kinds of requests to two controller actions. Let me know if you'd like some guidance on that.