-
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
Open
carakane
wants to merge
7
commits into
paircolumbus:master
Choose a base branch
from
carakane: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
finished #40
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0aa6d5a
finished release 0
03138b3
finished release 1
3309367
finished release 2
f379b73
cleaned up files, added comment about url validation
413ec2a
changed comment about error
ef06625
refactored
cb2861d
removed extraneous line of code
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
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,35 @@ | ||
| class UrlsController < ApplicationController | ||
|
|
||
| def short | ||
| @url = Url.find_by(:short_url => params[:id]) | ||
| @url.increment(:click_count) | ||
| redirect_to @url[:long_url] | ||
| end | ||
|
|
||
| def index | ||
| @urls = Url.all | ||
| end | ||
|
|
||
| def show | ||
| @url = Url.find(params[:id]) | ||
| url_path(@url) | ||
| 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 | ||
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,20 @@ | ||
| require 'net/http' | ||
|
|
||
| class Url < ActiveRecord::Base | ||
| before_create :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 | ||
| if res = Net::HTTP.get_response(uri) | ||
| else res.code = "x" | ||
| end | ||
| self.errors.add(:base, "This URL is invalid, #{res.message}") if res.code != "200" | ||
| end | ||
|
|
||
| def shorten_url | ||
| self.short_url = "!#{(0..3).map{ rand(36).to_s(36) }.join}" | ||
| 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,5 @@ | ||
| <h1>All the URLs</h1> | ||
|
|
||
| <% @urls.each do |url| %> | ||
| <%= url.long_url %><br /> | ||
| <% 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,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 %> |
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 @@ | ||
| The URL you entered: <%= @url.long_url %><br /> | ||
| Its short version: <%= link_to "http://localhost:3000/urls/#{@url.short_url}", @url.short_url %> |
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 |
|---|---|---|
| @@ -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 |
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 AddClickCountToUrls < ActiveRecord::Migration | ||
| def change | ||
| add_column :urls, :click_count, :integer, default: 0 | ||
| 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,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 |
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 don't think this line does anything.