Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'


gem 'pry'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.6'
# Use sqlite3 as the database for Active Record
Expand Down
9 changes: 9 additions & 0 deletions source/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ GEM
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
builder (3.2.2)
coderay (1.1.2)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
Expand All @@ -50,9 +51,13 @@ GEM
json (1.8.1)
mail (2.6.1)
mime-types (>= 1.16, < 3)
method_source (0.9.0)
mime-types (2.4.1)
minitest (5.4.2)
multi_json (1.10.1)
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rack (1.5.2)
rack-test (0.6.2)
rack (>= 1.0)
Expand Down Expand Up @@ -128,6 +133,7 @@ DEPENDENCIES
coffee-rails (~> 4.0.0)
jbuilder (~> 2.0)
jquery-rails
pry
rails (= 4.1.6)
rspec-rails
sass-rails (~> 4.0.3)
Expand All @@ -136,3 +142,6 @@ DEPENDENCIES
sqlite3
turbolinks
uglifier (>= 1.3.0)

BUNDLED WITH
1.16.1
32 changes: 32 additions & 0 deletions source/app/controllers/urls_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,34 @@
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])
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
20 changes: 20 additions & 0 deletions source/app/models/url.rb
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
5 changes: 5 additions & 0 deletions source/app/views/urls/index.html.erb
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 %>
11 changes: 11 additions & 0 deletions source/app/views/urls/new.html.erb
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 %>
2 changes: 2 additions & 0 deletions source/app/views/urls/show.html.erb
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 %>
1 change: 1 addition & 0 deletions source/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
require 'pry'

module Source
class Application < Rails::Application
Expand Down
6 changes: 6 additions & 0 deletions source/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
Rails.application.routes.draw do

get 'urls/:id', to: 'urls#short', id: /[:!].{4}/

resources :urls


# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
8 changes: 8 additions & 0 deletions source/db/migrate/20180514183131_urls.rb
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
5 changes: 5 additions & 0 deletions source/db/migrate/20180515131738_add_click_count_to_urls.rb
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
22 changes: 22 additions & 0 deletions source/db/schema.rb
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