-
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
Open
lnestor
wants to merge
9
commits into
paircolumbus:master
Choose a base branch
from
lnestor:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
817a193
Complete Release 0
4eca9cc
Add form for user to shorten urls
94b5e0b
Complete Release 2
bb19c2a
Complete Release 2
fb947e0
Complete Release 3
8ad65a0
Add click count to view
0fa1957
Add additional url validations and moved behavior from controller to …
0d7bc53
Switch to HTTParty gem to decrease spec running times
006bf22
Remove unnecessary validations and let rails do the work for me
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --color | ||
| --require spec_helper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| 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 | ||
| url = URI(long_url) | ||
| Net::HTTP.get_response(url) | ||
| rescue | ||
| errors.add(:long_url, 'must be accessible') | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
5 changes: 5 additions & 0 deletions
5
source/db/migrate/20180514175827_add_click_counter_to_urls.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| require 'rails_helper' | ||
|
|
||
| describe UrlsController do | ||
| let(:url) { Url.create(long_url: 'https://www.google.com/', click_count: 0) } | ||
|
|
||
| describe 'GET index' do | ||
| it 'responds with a 200 OK' do | ||
| get :index | ||
| expect(response.status).to eq 200 | ||
| end | ||
|
|
||
| it 'assigns @urls' do | ||
| get :index | ||
| expect(assigns[:urls]).to eq [url] | ||
| end | ||
|
|
||
| it 'renders the index template' do | ||
| get :index | ||
| expect(response).to render_template('index') | ||
| end | ||
| end | ||
|
|
||
| describe 'POST create' do | ||
| context 'with valid params' do | ||
| it 'creates a url entry' do | ||
| expect do | ||
| post :create, url: { long_url: 'https://www.google.com/' } | ||
| end.to change { Url.count }.by 1 | ||
| end | ||
|
|
||
| it 'redirects to the index page' do | ||
| post :create, url: { long_url: 'https://www.google.com/' } | ||
| expect(response).to redirect_to urls_path | ||
| end | ||
|
|
||
| it 'sets the click counter to 0' do | ||
| post :create, url: { long_url: 'https://www.google.com/' } | ||
| expect(Url.first.click_count).to eq 0 | ||
| end | ||
| end | ||
|
|
||
| context 'with blantantly invalid params' do | ||
| it 'does not create a url entry' do | ||
| expect do | ||
| post :create, url: { long_url: 'invalid URL' } | ||
| end.to_not change { Url.count } | ||
| end | ||
|
|
||
| it 'redirects to the index page' do | ||
| post :create, url: { long_url: 'invalid URL' } | ||
| expect(response).to redirect_to urls_path | ||
| end | ||
|
|
||
| it 'displays an error message' do | ||
| post :create, url: { long_url: 'invalid URL' } | ||
| expect(flash[:error]).to eq 'Invalid URL' | ||
| end | ||
| end | ||
|
|
||
| context 'with valid params but unaccessible webpage' do | ||
| it 'does not create a url entry' do | ||
| expect do | ||
| post :create, url: { long_url: 'https://blah.com' } | ||
| end.to_not change { Url.count } | ||
| end | ||
|
|
||
| it 'redirects to the index page' do | ||
| post :create, url: { long_url: 'https://blah.com' } | ||
| expect(response).to redirect_to urls_path | ||
| end | ||
|
|
||
| it 'displays an error message' do | ||
| post :create, url: { long_url: 'https://blah.com' } | ||
| expect(flash[:error]).to eq 'Invalid URL' | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'GET show' do | ||
| it 'redirects to the long url' do | ||
| get :show, { id: url.id } | ||
| expect(response).to redirect_to 'https://www.google.com/' | ||
| end | ||
|
|
||
| it 'increases the click_counter' do | ||
| expect do | ||
| get :show, { id: url.id } | ||
| end.to change { url.reload.click_count }.by 1 | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.