Permalink
Browse files

Add actual redirection

  • Loading branch information...
1 parent d16bab2 commit e3dffe95341f9cf10e0bad81369406df3ede6a27 @drapergeek committed Dec 16, 2012
@@ -12,4 +12,9 @@ def create
redirect_to new_short_url_path
end
+
+ def show
+ short_url = ShortUrl.find_by_name(params[:name])
+ redirect_to short_url.url
+ end
end
View
@@ -17,6 +17,13 @@ def save
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
"http://#{HOSTNAME}/#{name}"
end
@@ -48,4 +55,8 @@ def generate_random_name
def redis
@redis ||= Redis.new(:driver => :hiredis)
end
+
+ def self.redis
+ @redis ||= Redis.new(:driver => :hiredis)
+ end
end
View
@@ -1,4 +1,6 @@
Shortstuff::Application.routes.draw do
resources :short_urls, only: [:new, :create]
root to: "short_urls#new"
+
+ match '/:name' => 'short_urls#show'
end
@@ -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
@@ -53,4 +53,24 @@
expect(shorturl.full_url).to eq('http://example.com/mypage')
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

0 comments on commit e3dffe9

Please sign in to comment.