Permalink
Browse files

GET /:hash works.

  • Loading branch information...
Gabe Berke-Williams
Gabe Berke-Williams committed Apr 7, 2012
1 parent f0248a6 commit 0637262bdcfb50b7e048e61d3964e2232c8ac899
View
@@ -2,5 +2,11 @@ source :rubygems
gem 'sinatra', '~> 1.3.2'
gem 'redis', '~> 2.2.2'
-gem 'rspec', '~> 2.9.0'
gem 'rake', '~> 0.9.2'
+gem 'ohm', '~> 0.1.5'
+
+group :test do
+ gem 'rack-test'
+ gem 'rspec', '~> 2.9.0'
+ gem 'factory_girl', '~> 3.1.0'
+end
View
@@ -1,10 +1,23 @@
GEM
remote: http://rubygems.org/
specs:
+ activesupport (3.2.3)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
diff-lcs (1.1.3)
+ factory_girl (3.1.0)
+ activesupport (>= 3.0.0)
+ i18n (0.6.0)
+ multi_json (1.2.0)
+ nest (1.1.1)
+ redis
+ ohm (0.1.5)
+ nest (~> 1.0)
rack (1.4.1)
rack-protection (1.2.0)
rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
rake (0.9.2.2)
redis (2.2.2)
rspec (2.9.0)
@@ -25,6 +38,9 @@ PLATFORMS
ruby
DEPENDENCIES
+ factory_girl (~> 3.1.0)
+ ohm (~> 0.1.5)
+ rack-test
rake (~> 0.9.2)
redis (~> 2.2.2)
rspec (~> 2.9.0)
View
@@ -0,0 +1,17 @@
+require 'bundler/setup'
+require 'sinatra/base'
+
+require_relative '../lib/encoder'
+require_relative '../lib/decoder'
+require_relative '../lib/ohm_setup'
+require_relative 'models/link'
+
+module Shawtie
+ class Application < Sinatra::Base
+ get '/:hash' do |hash|
+ link = Link.find(hash: hash).first
+
+ redirect link.url
+ end
+ end
+end
View
@@ -0,0 +1,11 @@
+class Link < Ohm::Model
+ attribute :url
+ attribute :hash
+
+ index :hash
+
+ def validate
+ assert_present :url
+ assert_present :hash
+ end
+end
View
@@ -0,0 +1,3 @@
+require 'ohm'
+
+# Ohm.connect
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe Shawtie::Application do
+ context 'GET /:hash' do
+ let(:link) { FactoryGirl.build(:link) }
+
+ before do
+ get "/#{link.hash}"
+ end
+
+ it 'is a redirect' do
+ last_response.should be_redirect
+ end
+
+ it 'redirects to the URL' do
+ last_response.location.should == link.url
+ end
+ end
+end
View
@@ -0,0 +1,10 @@
+FactoryGirl.define do
+ factory :link do
+ ignore do
+ url 'http://example.com'
+ sequence(:hash) { |n| "hash#{n}" }
+ end
+
+ initialize_with { Link.create(url: url, hash: hash) }
+ end
+end
View
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe Link do
+ context 'validations' do
+ it 'requires url to be present' do
+ link = Link.create(url: nil)
+ link.errors.assoc(:url)[1].should == :not_present
+ end
+
+ it 'requires hash to be present' do
+ link = Link.create(hash: nil)
+ link.errors.assoc(:hash)[1].should == :not_present
+ end
+ end
+end
View
@@ -1,5 +1,10 @@
-$LOAD_PATH << File.expand_path('../lib', __FILE__)
+$LOAD_PATH << File.dirname(__FILE__)
+require 'bundler/setup'
require 'rspec'
-require 'encoder'
-require 'decoder'
+require 'rack/test'
+require 'factory_girl'
+
+require_relative '../app/application'
+
+Dir['spec/support/**/*.rb'].each { |f| require File.expand_path(f) }
@@ -0,0 +1 @@
+FactoryGirl.find_definitions
View
@@ -0,0 +1,7 @@
+RSpec.configure do |config|
+ config.include Rack::Test::Methods
+
+ def app
+ Shawtie::Application
+ end
+end
View
@@ -0,0 +1,5 @@
+RSpec.configure do |config|
+ config.before do
+ Ohm.redis.flushdb
+ end
+end

0 comments on commit 0637262

Please sign in to comment.