-
Notifications
You must be signed in to change notification settings - Fork 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
PMueller solution #13
Open
petermueller
wants to merge
1
commit into
master
Choose a base branch
from
pmueller-solution
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 all commits
Commits
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 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 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 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,69 @@ | ||
body { | ||
background-color: #fff; | ||
color: #333; | ||
font-family: verdana, arial, helvetica, sans-serif; | ||
font-size: 13px; | ||
line-height: 18px; | ||
} | ||
|
||
p, ol, ul, td { | ||
font-family: verdana, arial, helvetica, sans-serif; | ||
font-size: 13px; | ||
line-height: 18px; | ||
} | ||
|
||
pre { | ||
background-color: #eee; | ||
padding: 10px; | ||
font-size: 11px; | ||
} | ||
|
||
a { | ||
color: #000; | ||
&:visited { | ||
color: #666; | ||
} | ||
&:hover { | ||
color: #fff; | ||
background-color: #000; | ||
} | ||
} | ||
|
||
div { | ||
&.field, &.actions { | ||
margin-bottom: 10px; | ||
} | ||
} | ||
|
||
#notice { | ||
color: green; | ||
} | ||
|
||
.field_with_errors { | ||
padding: 2px; | ||
background-color: red; | ||
display: table; | ||
} | ||
|
||
#error_explanation { | ||
width: 450px; | ||
border: 2px solid red; | ||
padding: 7px; | ||
padding-bottom: 0; | ||
margin-bottom: 20px; | ||
background-color: #f0f0f0; | ||
h2 { | ||
text-align: left; | ||
font-weight: bold; | ||
padding: 5px 5px 5px 15px; | ||
font-size: 12px; | ||
margin: -7px; | ||
margin-bottom: 0px; | ||
background-color: #c00; | ||
color: #fff; | ||
} | ||
ul li { | ||
font-size: 12px; | ||
list-style: square; | ||
} | ||
} |
This file contains 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,62 @@ | ||
class UrlsController < ApplicationController | ||
before_action :set_url, only: [:edit, :update, :destroy] | ||
before_action :set_url_by_shortened, only: [:show] | ||
|
||
def index | ||
@urls = Url.all | ||
end | ||
|
||
def show | ||
@url.increment(:click_count); @url.save | ||
redirect_to @url.original | ||
end | ||
|
||
def new | ||
@url = Url.new | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def create | ||
@url = Url.new(url_params) | ||
|
||
respond_to do |format| | ||
if @url.save | ||
format.html { redirect_to urls_path, notice: 'Url was successfully created.' } | ||
else | ||
format.html { render :new } | ||
end | ||
end | ||
end | ||
|
||
def update | ||
respond_to do |format| | ||
if @url.update(url_params) | ||
format.html { redirect_to urls_path, notice: 'Url was successfully updated.' } | ||
else | ||
format.html { render :edit } | ||
end | ||
end | ||
end | ||
|
||
def destroy | ||
@url.destroy | ||
respond_to do |format| | ||
format.html { redirect_to urls_path, notice: 'Url was successfully destroyed.' } | ||
end | ||
end | ||
|
||
private | ||
def set_url | ||
@url = Url.find(params[:id]) | ||
end | ||
|
||
def set_url_by_shortened | ||
@url = Url.find_by(shortened: params[:shortened]) | ||
end | ||
|
||
def url_params | ||
params.require(:url).permit(:shortened, :original) | ||
end | ||
end |
This file contains 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 @@ | ||
class Url < ActiveRecord::Base | ||
before_save :shorten | ||
|
||
validates :shortened, uniqueness: true | ||
validate :protocol_check, :resolvable_check | ||
|
||
def shorten | ||
self.shortened ||= SecureRandom.urlsafe_base64(5) | ||
end | ||
|
||
def protocol_check | ||
errors[:original] << "URL must begin with http:// or https://" unless original.match(/^https?:\/\//) | ||
end | ||
|
||
def resolvable_check | ||
begin | ||
HTTParty.get(original).code == 200 | ||
rescue | ||
errors[:original] << "not resolvable" | ||
end | ||
end | ||
end |
This file contains 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,21 @@ | ||
<%= form_for(@url) do |f| %> | ||
<% if @url.errors.any? %> | ||
<div id="error_explanation"> | ||
<h2><%= pluralize(@url.errors.count, "error") %> prohibited this url from being saved:</h2> | ||
|
||
<ul> | ||
<% @url.errors.full_messages.each do |message| %> | ||
<li><%= message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<div class="field"> | ||
<%= f.label :original %><br> | ||
<%= f.text_field :original %> | ||
</div> | ||
<div class="actions"> | ||
<%= f.submit %> | ||
</div> | ||
<% end %> |
This file contains 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,6 @@ | ||
<h1>Editing url</h1> | ||
|
||
<%= render 'form' %> | ||
|
||
<%= link_to 'Show', @url %> | | ||
<%= link_to 'Back', urls_path %> |
This file contains 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,28 @@ | ||
<h1>Listing urls</h1> | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<th>Clicks</th> | ||
<th>Shortened</th> | ||
<th>Original</th> | ||
<th colspan="3"></th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% @urls.each do |url| %> | ||
<tr> | ||
<td><%= url.click_count %></td> | ||
<td><%= link_to url.shortened, shortened_path(url.shortened) %></td> | ||
<td><%= url.original %></td> | ||
<td><%= link_to 'Edit', edit_url_path(url) %></td> | ||
<td><%= link_to 'Destroy', url, method: :delete, data: { confirm: 'Are you sure?' } %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<br> | ||
|
||
<%= link_to 'New Url', new_url_path %> |
This file contains 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>New url</h1> | ||
|
||
<%= render 'form' %> | ||
|
||
<%= link_to 'Back', urls_path %> |
This file contains 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 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,10 @@ | ||
class CreateUrls < ActiveRecord::Migration | ||
def change | ||
create_table :urls do |t| | ||
t.string :shortened | ||
t.string :original | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains 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 AddUrlsCount < ActiveRecord::Migration | ||
def change | ||
add_column :urls, :click_count, :integer, default: 0 | ||
end | ||
end |
This file contains 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,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: 20150601143819) do | ||
|
||
create_table "urls", force: true do |t| | ||
t.string "shortened" | ||
t.string "original" | ||
t.datetime "created_at" | ||
t.datetime "updated_at" | ||
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.
What if the user is hostile?
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 mean like sanitizing the original URL?
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.
Yeah and validation too. Let's talk about it at 4p. A couple of people made the same decision. Within the scope of this challenge, its sufficient but there's a bigger lesson about handling user input here.
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.
oooh. If they setup up the remote server's url to return something in the status field that would be interpreted and run, instead of the normal status codes?