diff --git a/Gemfile b/Gemfile index a918448..ee1840f 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 5cecadf..a8a4fcf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/app/application.rb b/app/application.rb new file mode 100644 index 0000000..cd954bd --- /dev/null +++ b/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 diff --git a/app/models/link.rb b/app/models/link.rb new file mode 100644 index 0000000..670db34 --- /dev/null +++ b/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 diff --git a/lib/ohm_setup.rb b/lib/ohm_setup.rb new file mode 100644 index 0000000..e942ca2 --- /dev/null +++ b/lib/ohm_setup.rb @@ -0,0 +1,3 @@ +require 'ohm' + +# Ohm.connect diff --git a/spec/app/application_spec.rb b/spec/app/application_spec.rb new file mode 100644 index 0000000..3418d14 --- /dev/null +++ b/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 diff --git a/spec/factories.rb b/spec/factories.rb new file mode 100644 index 0000000..b26dc65 --- /dev/null +++ b/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 diff --git a/spec/models/link_spec.rb b/spec/models/link_spec.rb new file mode 100644 index 0000000..819d5fa --- /dev/null +++ b/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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b668b09..edcdf0d 100644 --- a/spec/spec_helper.rb +++ b/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 'encoder' -require 'decoder' +require 'rack/test' +require 'factory_girl' + +require_relative '../app/application' + +Dir['spec/support/**/*.rb'].each { |f| require File.expand_path(f) } diff --git a/spec/support/factory_girl.rb b/spec/support/factory_girl.rb new file mode 100644 index 0000000..95774f9 --- /dev/null +++ b/spec/support/factory_girl.rb @@ -0,0 +1 @@ +FactoryGirl.find_definitions diff --git a/spec/support/rack.rb b/spec/support/rack.rb new file mode 100644 index 0000000..48c4500 --- /dev/null +++ b/spec/support/rack.rb @@ -0,0 +1,7 @@ +RSpec.configure do |config| + config.include Rack::Test::Methods + + def app + Shawtie::Application + end +end diff --git a/spec/support/redis.rb b/spec/support/redis.rb new file mode 100644 index 0000000..805d7cf --- /dev/null +++ b/spec/support/redis.rb @@ -0,0 +1,5 @@ +RSpec.configure do |config| + config.before do + Ohm.redis.flushdb + end +end