-
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
Levi's solution #35
base: master
Are you sure you want to change the base?
Levi's solution #35
Changes from all commits
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 |
---|---|---|
|
@@ -12,5 +12,4 @@ | |
// | ||
//= require jquery | ||
//= require jquery_ujs | ||
//= require turbolinks | ||
//= require_tree . |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,37 @@ | ||
class UrlsController < ApplicationController | ||
|
||
# GET /urls | ||
def index | ||
@urls = Url.all | ||
@url = Url.new | ||
end | ||
|
||
# POST /urls | ||
def create | ||
@url = Url.new(url_params) | ||
|
||
respond_to do |format| | ||
if @url.save | ||
format.html { redirect_to root_path, notice: 'Url was successfully created.' } | ||
else | ||
format.html { redirect_to root_path, notice: @url.errors.full_messages } | ||
end | ||
end | ||
end | ||
|
||
# GET /:shortened_url | ||
def redirect | ||
@url = Url.where(shortened_url: params[:shortened_url]).take | ||
if @url != nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
@url.update_attribute(:click_count, @url.click_count + 1) | ||
redirect_to @url.original_url | ||
else | ||
redirect_to root_path, notice: 'We couldn\'t find a link for the bitly URL you clicked.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switching to double quotes is preferable over escaping the embedded single quote. |
||
end | ||
end | ||
|
||
private | ||
def url_params | ||
params.require(:url).permit(:original_url, :shortened_url, :click_count) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'uri' | ||
require 'net/http' | ||
|
||
class Url < ActiveRecord::Base | ||
ALPHANUMERIC_CHARACTERS = (('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 extracting a constant for this |
||
|
||
validates_presence_of :original_url | ||
validates_format_of :original_url, :with => URI::regexp, :message => "must be a valid Ruby URI" | ||
validate :original_url_starts_with_http_or_https | ||
validate :original_url_must_return_a_response | ||
|
||
before_save :shorten_url | ||
|
||
def original_url_starts_with_http_or_https | ||
if not original_url =~ /^https?:\/\// | ||
errors.add(:original_url, "must start with http:// or https://") | ||
end | ||
end | ||
|
||
def original_url_must_return_a_response | ||
return unless original_url.present? && original_url =~ URI::regexp(['http', 'https']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Matching the url against |
||
uri = URI.parse(original_url) | ||
response = Net::HTTP.get_response(uri) | ||
rescue URI::InvalidURIError | ||
errors.add(:original_url, "must respond to a HTTP request") | ||
rescue Errno::ECONNREFUSED | ||
errors.add(:original_url, "must respond to a HTTP request") | ||
rescue SocketError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect Net::HTTP is capable of raise many other errors. I'd switch to rescuing all subclasses of |
||
errors.add(:original_url, "must respond to a HTTP request") | ||
end | ||
|
||
def shorten_url | ||
self.shortened_url = generate_key unless self.shortened_url | ||
end | ||
|
||
def generate_key | ||
key = "" | ||
7.times { key << ALPHANUMERIC_CHARACTERS.sample } | ||
key | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<%= 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.text_field :original_url %> | ||
<%= f.hidden_field :click_count, value: 0 %> | ||
<%= f.submit 'Shorten' %> | ||
</div> | ||
<% end %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<h2>Shorten a link</h2> | ||
|
||
<%= render 'form' %> | ||
|
||
<br><hr> | ||
|
||
<p id="notice"><%= notice %></p> | ||
|
||
<h2>Your shortened links</h2> | ||
|
||
<table> | ||
<tbody> | ||
<% @urls.each do |url| %> | ||
<tr> | ||
<td><%= url.original_url %></td> | ||
</tr> | ||
<tr> | ||
<td><%= link_to "http://localhost:3000/#{url.shortened_url}", "http://localhost:3000/#{url.shortened_url}" %></td> | ||
<td> </td> | ||
<td><b>Clicks:</b> <%= url.click_count %></td> | ||
</tr> | ||
<tr> | ||
<td> </td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,5 @@ | ||
Rails.application.routes.draw do | ||
# The priority is based upon order of creation: first created -> highest priority. | ||
# See how all your routes lay out with "rake routes". | ||
|
||
# You can have the root of your site routed with "root" | ||
# root 'welcome#index' | ||
|
||
# Example of regular route: | ||
# get 'products/:id' => 'catalog#view' | ||
|
||
# Example of named route that can be invoked with purchase_url(id: product.id) | ||
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase | ||
|
||
# Example resource route (maps HTTP verbs to controller actions automatically): | ||
# resources :products | ||
|
||
# Example resource route with options: | ||
# resources :products do | ||
# member do | ||
# get 'short' | ||
# post 'toggle' | ||
# end | ||
# | ||
# collection do | ||
# get 'sold' | ||
# end | ||
# end | ||
|
||
# Example resource route with sub-resources: | ||
# resources :products do | ||
# resources :comments, :sales | ||
# resource :seller | ||
# end | ||
|
||
# Example resource route with more complex sub-resources: | ||
# resources :products do | ||
# resources :comments | ||
# resources :sales do | ||
# get 'recent', on: :collection | ||
# end | ||
# end | ||
|
||
# Example resource route with concerns: | ||
# concern :toggleable do | ||
# post 'toggle' | ||
# end | ||
# resources :posts, concerns: :toggleable | ||
# resources :photos, concerns: :toggleable | ||
|
||
# Example resource route within a namespace: | ||
# namespace :admin do | ||
# # Directs /admin/products/* to Admin::ProductsController | ||
# # (app/controllers/admin/products_controller.rb) | ||
# resources :products | ||
# end | ||
resources :urls, only: [:index, :create] | ||
get '/:shortened_url', to: 'urls#redirect' | ||
root 'urls#index' | ||
end |
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 :original_url | ||
t.string :shortened_url | ||
|
||
t.timestamps | ||
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 | ||
end | ||
end |
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: 20180125194335) do | ||
|
||
create_table "urls", force: true do |t| | ||
t.string "original_url" | ||
t.string "shortened_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.
take
with no argument seems very out of place to me. I think most Rails devs would expect it's alias,first
.find_by
simplifies this a bit more though.