-
Notifications
You must be signed in to change notification settings - Fork 40
Complete Release 3 #38
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?
Changes from 8 commits
817a193
4eca9cc
94b5e0b
bb19c2a
fb947e0
8ad65a0
0fa1957
0d7bc53
006bf22
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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --color | ||
| --require spec_helper |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,30 @@ | ||
| require 'net/http' | ||
|
|
||
| class UrlsController < ApplicationController | ||
| def index | ||
| @urls = Url.all | ||
| end | ||
|
|
||
| def create | ||
| url = Url.new(url_params) | ||
|
|
||
| if !url.save | ||
| flash[:error] = 'Invalid URL' | ||
| end | ||
|
|
||
| redirect_to urls_path | ||
| end | ||
|
|
||
| def show | ||
| url = Url.find(params[:id]) | ||
| url.click_count += 1 | ||
| url.save | ||
| redirect_to url.long_url | ||
| 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,33 @@ | ||
| require 'net/http' | ||
|
|
||
| class Url < ActiveRecord::Base | ||
| before_create :reset_click_count | ||
|
|
||
| validates :long_url, presence: true, format: { with: /http(|s):\/\/.+/ } | ||
| validate :long_url_is_uri_valid | ||
| validate :long_url_is_reachable | ||
|
|
||
| private | ||
|
|
||
| def reset_click_count | ||
| self.click_count = 0 | ||
|
||
| end | ||
|
|
||
| def long_url_is_uri_valid | ||
| unless long_url =~ /\A#{URI::regexp(['http', 'https'])}\z/ | ||
| errors.add(:long_url, 'must be valid uri') | ||
| end | ||
| end | ||
|
|
||
| def long_url_is_reachable | ||
| begin | ||
| response = HTTParty.get(long_url, timeout: 2) | ||
|
||
|
|
||
| if response.code.to_i != 200 | ||
| errors.add(:long_url, 'must return a 200 code') | ||
| end | ||
| rescue | ||
| errors.add(:long_url, 'must be accessible') | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <p>Shorten URL:</p> | ||
|
|
||
| <%= flash[:error] if !nil? %> | ||
|
|
||
| <%= form_for :url do |f| %> | ||
| <%= f.label :long_url %> | ||
| <%= f.text_field :long_url %> | ||
| <% end %> | ||
|
|
||
| <ul> | ||
| <% @urls.each do |url| %> | ||
| <li><%= url_url(url) ====> #{url.long_url} (clicked #{url.click_count} times)" %></li> | ||
| <% end %> | ||
| </ul> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class CreateUrls < ActiveRecord::Migration | ||
| def change | ||
| create_table :urls do |t| | ||
| t.string :long_url | ||
|
|
||
| t.timestamps | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class AddClickCounterToUrls < ActiveRecord::Migration | ||
| def change | ||
| add_column :urls, :click_count, :integer | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # 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: 20180514175827) do | ||
|
|
||
| create_table "urls", force: true do |t| | ||
| t.string "long_url" | ||
| t.datetime "created_at" | ||
| t.datetime "updated_at" | ||
| t.integer "click_count" | ||
| 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.
I could nitpick this
formatregexp, but I'm not sure if it is needed. Is theURI::regexpmatching sufficient?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.
You're right - this is leftover from before I implemented the URI matching.