Skip to content

Commit

Permalink
Add actual redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
drapergeek committed Dec 16, 2012
1 parent d16bab2 commit e3dffe9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/controllers/short_urls_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ def create


redirect_to new_short_url_path redirect_to new_short_url_path
end end

def show
short_url = ShortUrl.find_by_name(params[:name])
redirect_to short_url.url
end
end end
11 changes: 11 additions & 0 deletions app/models/short_url.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def save
end end
end end


def self.find_by_name(name)
stored_url = redis.get(name)
if stored_url
ShortUrl.new(name: name, url: stored_url)
end
end

def full_url def full_url
"http://#{HOSTNAME}/#{name}" "http://#{HOSTNAME}/#{name}"
end end
Expand Down Expand Up @@ -48,4 +55,8 @@ def generate_random_name
def redis def redis
@redis ||= Redis.new(:driver => :hiredis) @redis ||= Redis.new(:driver => :hiredis)
end end

def self.redis
@redis ||= Redis.new(:driver => :hiredis)
end
end end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,6 @@
Shortstuff::Application.routes.draw do Shortstuff::Application.routes.draw do
resources :short_urls, only: [:new, :create] resources :short_urls, only: [:new, :create]
root to: "short_urls#new" root to: "short_urls#new"

match '/:name' => 'short_urls#show'
end end
12 changes: 12 additions & 0 deletions spec/features/visit_short_url_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'spec_helper'

feature 'redirecting users to short links', js: true do
scenario 'A valid link is visited' do
short_url = ShortUrl.new(url: "http://www.google.com", name: "tester")
short_url.save

visit "/tester"

page.should have_content 'Google'
end
end
20 changes: 20 additions & 0 deletions spec/models/short_url_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -53,4 +53,24 @@
expect(shorturl.full_url).to eq('http://example.com/mypage') expect(shorturl.full_url).to eq('http://example.com/mypage')
end end
end end

describe ".find_by_name" do
context "when there is an instance with the name" do
it "returns the instance" do
short_url = ShortUrl.new(name: 'test', url: 'http://www.bing.com').save

searched_short_url = ShortUrl.find_by_name('test')

expect(searched_short_url.name).to eq('test')
end
end

context 'when there is not an instance with the name' do
it "returns nil" do
searched_short_url = ShortUrl.find_by_name('test')

expect(searched_short_url).to be_nil
end
end
end
end end

0 comments on commit e3dffe9

Please sign in to comment.