Skip to content

Commit

Permalink
GET /:hash works.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Berke-Williams committed Apr 7, 2012
1 parent f0248a6 commit 0637262
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 4 deletions.
8 changes: 7 additions & 1 deletion Gemfile
Expand Up @@ -2,5 +2,11 @@ source :rubygems


gem 'sinatra', '~> 1.3.2' gem 'sinatra', '~> 1.3.2'
gem 'redis', '~> 2.2.2' gem 'redis', '~> 2.2.2'
gem 'rspec', '~> 2.9.0'
gem 'rake', '~> 0.9.2' 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
16 changes: 16 additions & 0 deletions Gemfile.lock
@@ -1,10 +1,23 @@
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
diff-lcs (1.1.3) 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 (1.4.1)
rack-protection (1.2.0) rack-protection (1.2.0)
rack rack
rack-test (0.6.1)
rack (>= 1.0)
rake (0.9.2.2) rake (0.9.2.2)
redis (2.2.2) redis (2.2.2)
rspec (2.9.0) rspec (2.9.0)
Expand All @@ -25,6 +38,9 @@ PLATFORMS
ruby ruby


DEPENDENCIES DEPENDENCIES
factory_girl (~> 3.1.0)
ohm (~> 0.1.5)
rack-test
rake (~> 0.9.2) rake (~> 0.9.2)
redis (~> 2.2.2) redis (~> 2.2.2)
rspec (~> 2.9.0) rspec (~> 2.9.0)
Expand Down
17 changes: 17 additions & 0 deletions app/application.rb
@@ -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
11 changes: 11 additions & 0 deletions app/models/link.rb
@@ -0,0 +1,11 @@
class Link < Ohm::Model
attribute :url
attribute :hash

index :hash

def validate
assert_present :url
assert_present :hash
end
end
3 changes: 3 additions & 0 deletions lib/ohm_setup.rb
@@ -0,0 +1,3 @@
require 'ohm'

# Ohm.connect
19 changes: 19 additions & 0 deletions spec/app/application_spec.rb
@@ -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
10 changes: 10 additions & 0 deletions spec/factories.rb
@@ -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
15 changes: 15 additions & 0 deletions spec/models/link_spec.rb
@@ -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
11 changes: 8 additions & 3 deletions spec/spec_helper.rb
@@ -1,5 +1,10 @@
$LOAD_PATH << File.expand_path('../lib', __FILE__) $LOAD_PATH << File.dirname(__FILE__)


require 'bundler/setup'
require 'rspec' require 'rspec'
require 'encoder' require 'rack/test'
require 'decoder' require 'factory_girl'

require_relative '../app/application'

Dir['spec/support/**/*.rb'].each { |f| require File.expand_path(f) }
1 change: 1 addition & 0 deletions spec/support/factory_girl.rb
@@ -0,0 +1 @@
FactoryGirl.find_definitions
7 changes: 7 additions & 0 deletions spec/support/rack.rb
@@ -0,0 +1,7 @@
RSpec.configure do |config|
config.include Rack::Test::Methods

def app
Shawtie::Application
end
end
5 changes: 5 additions & 0 deletions spec/support/redis.rb
@@ -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.