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
10 changes: 7 additions & 3 deletions source/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ gem 'spring', group: :development
# gem 'capistrano-rails', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'rspec-rails', group: [:development, :test]

group :development, :test do
gem 'byebug'
gem 'web-console', '2.0.0.beta3'
gem 'rspec-rails'
gem 'faker', '1.4.2'
gem 'pry'
end
27 changes: 27 additions & 0 deletions source/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,29 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
binding_of_caller (0.7.3.pre1)
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (3.5.1)
columnize (~> 0.8)
debugger-linecache (~> 1.2)
slop (~> 3.6)
coderay (1.1.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
coffee-script (2.3.0)
coffee-script-source
execjs
coffee-script-source (1.8.0)
columnize (0.9.0)
debug_inspector (0.0.2)
debugger-linecache (1.2.0)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.2.1)
faker (1.4.2)
i18n (~> 0.5)
hike (1.2.3)
i18n (0.6.11)
jbuilder (2.2.2)
Expand All @@ -50,9 +62,14 @@ GEM
json (1.8.1)
mail (2.6.1)
mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (2.4.1)
minitest (5.4.2)
multi_json (1.10.1)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.5.2)
rack-test (0.6.2)
rack (>= 1.0)
Expand Down Expand Up @@ -99,6 +116,7 @@ GEM
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
slop (3.6.0)
spring (1.1.3)
sprockets (2.11.0)
hike (~> 1.2)
Expand All @@ -120,14 +138,22 @@ GEM
uglifier (2.5.3)
execjs (>= 0.3.0)
json (>= 1.8.0)
web-console (2.0.0.beta3)
activemodel (~> 4.0)
binding_of_caller (= 0.7.3.pre1)
railties (~> 4.0)
sprockets-rails (>= 2.0, < 4.0)

PLATFORMS
ruby

DEPENDENCIES
byebug
coffee-rails (~> 4.0.0)
faker (= 1.4.2)
jbuilder (~> 2.0)
jquery-rails
pry
rails (= 4.1.6)
rspec-rails
sass-rails (~> 4.0.3)
Expand All @@ -136,3 +162,4 @@ DEPENDENCIES
sqlite3
turbolinks
uglifier (>= 1.3.0)
web-console (= 2.0.0.beta3)
9 changes: 9 additions & 0 deletions source/app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@
*= require_tree .
*= require_self
*/
.debug_dump {
clear: both;
float: left;
width: 100%;
margin-top: 45px;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
11 changes: 11 additions & 0 deletions source/app/controllers/gt_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

class GtController < ApplicationController

def show
@url = Url.find_by short_link: params[:id]
@url.click_count += 1
@url.save
redirect_to @url.link
end

end
28 changes: 28 additions & 0 deletions source/app/controllers/urls_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
class UrlsController < ApplicationController
def index
@urls = Url.all
end

def show
@url = Url.find(params[:id])
end

def create
@url = Url.new(url_params)
if @url.save
flash[:success] = "We created a shorter link! :)"
redirect_to @url
else
flash[:error] = "We could not create your link :("
render :edit
end
end

def new
@url = Url.new
end

private

def url_params
params.require(:url).permit(:link)
end
end
22 changes: 22 additions & 0 deletions source/app/models/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Url < ActiveRecord::Base
validates :link, presence: true
before_create :create_short_url
validate :url_is_valid, on: :create

private
def create_short_url
self.short_link = (SecureRandom.random_number(9999999) + 100000).to_s(36)
end

def url_is_valid
url = URI.parse(link)
req = Net::HTTP.new(url.host, url.port)
req.use_ssl = (url.scheme == 'https')
path = url.path if url.path.present?
res = req.request_head(path || '/')
errors.add(:link, 'must return status code < 400') if res.code.to_i >= 400
rescue URI::Error
errors.add(:link, 'could not be reached')
end

end
2 changes: 1 addition & 1 deletion source/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
<body>

<%= yield %>

<%= debug(params) if Rails.env.development? %>
</body>
</html>
5 changes: 5 additions & 0 deletions source/app/views/shared/_error_messages.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% if @url.errors.any? %>
This link could not be saved:
<%= @url.errors.full_messages %>
<% end %>
<%= console %>
3 changes: 3 additions & 0 deletions source/app/views/urls/_url.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<li>
<%= url.link %> - <%= url.short_link %>
</li>
8 changes: 8 additions & 0 deletions source/app/views/urls/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>A url!</h1>

<%= render 'shared/error_messages' %>
<%= @url.link %>
-
<%= @url.short_link %>

<%= console %>
7 changes: 7 additions & 0 deletions source/app/views/urls/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>All Urls</h1>


<ul class="urls">
<%= render @urls %>
</ul>

16 changes: 16 additions & 0 deletions source/app/views/urls/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1>A new url</h1>

<div>
<%= form_for(@url) do |f| %>


<%= f.label :link %>
<%= f.text_field :link %>

<%= f.label :short_link %>
<%= :short_link %>

<%= f.submit "Shorten my url" %>

<% end %>
</div>
9 changes: 9 additions & 0 deletions source/app/views/urls/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>A url!</h1>

Link:
<%= @url.link %>
<br/>
Short Link:
<%= @url.short_link %>
<br/>
Clicks: <%= @url.click_count %>
2 changes: 2 additions & 0 deletions source/config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

config.web_console.automount = true
end
5 changes: 5 additions & 0 deletions source/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
Rails.application.routes.draw do
root 'urls#index'
get 'urls/new'
get 'gt/:id' => 'gt#show'
resources :urls
get ':id' => 'gt#show'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
9 changes: 9 additions & 0 deletions source/db/migrate/20150115201035_create_urls.rb
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 :link

t.timestamps null:false
end
end
end
5 changes: 5 additions & 0 deletions source/db/migrate/20150116140521_add_short_link_to_urls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddShortLinkToUrls < ActiveRecord::Migration
def change
add_column :urls, :short_link, :string
end
end
5 changes: 5 additions & 0 deletions source/db/migrate/20150116163319_add_click_counter_to_urls.rb
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, default: 0
end
end
24 changes: 24 additions & 0 deletions source/db/schema.rb
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: 20150116163319) do

create_table "urls", force: true do |t|
t.string "link"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "short_link"
t.integer "click_count", default: 0
end

end
3 changes: 3 additions & 0 deletions source/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
99.times do |n|
Url.create!(link: Faker::Internet.url)
end
19 changes: 19 additions & 0 deletions source/test/controllers/gt_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'

class GtControllerTest < ActionController::TestCase
def setup
@url = urls(:google)
end

test "should redirect to link if given short_link" do
get :show, id: @url.short_link
assert_redirected_to @url.link
end

test "should increment click counter" do
assert_difference '@url.click_count', 1 do
get :show, id: @url.short_link
@url.reload
end
end
end
7 changes: 4 additions & 3 deletions source/test/controllers/urls_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'test_helper'

class UrlsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
def setup
@url = urls(:google)
end

end
4 changes: 4 additions & 0 deletions source/test/fixtures/urls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

google:
link: www.google.com
short_link: shrt12
16 changes: 16 additions & 0 deletions source/test/models/url_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'test_helper'

class UrlTest < ActiveSupport::TestCase
def setup
@url = Url.new(link: "www.google.com")
end

test "Url has a link" do
assert_equal @url.link, "www.google.com"
end

test "Url has a shortened link after saving" do
@url.save!
assert_not_nil @url.short_link, "test123"
end
end