Permalink
Browse files

Use helper to correctly serialize

  • Loading branch information...
1 parent e4681b5 commit 76291130f0fd9c3aab89ef43168d82213dc6a456 @vanstee committed Jul 16, 2012
@@ -1,5 +1,6 @@
require 'hovercraft/loader'
-require 'hovercraft/actions'
+require 'hovercraft/routes'
+require 'hovercraft/helpers'
require 'sinatra/base'
require 'rack/contrib'
require 'forwardable'
@@ -22,7 +23,8 @@ def application
end
def configure(application)
- application.register(Hovercraft::Actions)
+ application.register(Hovercraft::Helpers)
+ application.register(Hovercraft::Routes)
application.use(Rack::PostBodyContentTypeParser)
application
end
@@ -1,46 +1,44 @@
-require 'sinatra/base'
-
module Hovercraft
- module Actions
+ module Routes
def generate_index(model_class, model_name, plural_model_name)
- get("/#{plural_model_name}.:format") do
+ get(%r{^/#{plural_model_name}(\.(?<format>[^\./?#]+))?$}) do
model_instances = model_class.all
status 200
- model_instances.to_json
+ respond_with model_instances
end
end
def generate_create(model_class, model_name, plural_model_name)
- post("/#{plural_model_name}.:format") do
+ post(%r{^/#{plural_model_name}(\.(?<format>[^\./?#]+))?$}) do
model_instance = model_class.create(params[model_name.to_sym])
status model_instance.valid? ? 201 : 400
- model_instance.to_json
+ respond_with model_instance
end
end
def generate_show(model_class, model_name, plural_model_name)
- get("/#{plural_model_name}/:id.:format") do
+ get(%r{^/#{plural_model_name}/(?<id>[^\./?#]+)(\.(?<format>[^\./?#]+))?$}) do
model_instance = model_class.find(params[:id])
status 200
- model_instance.to_json
+ respond_with model_instance
end
end
def generate_update(model_class, model_name, plural_model_name)
- put("/#{plural_model_name}/:id.:format") do
+ put(%r{^/#{plural_model_name}/(?<id>[^\./?#]+)(\.(?<format>[^\./?#]+))?$}) do
model_instance = model_class.find(params[:id])
model_instance.update_attributes(params[model_name.to_sym])
status model_instance.valid? ? 204 : 400
- model_instance.to_json
+ respond_with model_instance
end
end
def generate_destroy(model_class, model_name, plural_model_name)
- delete("/#{plural_model_name}/:id.:format") do
+ delete(%r{^/#{plural_model_name}/(?<id>[^\./?#]+)(\.(?<format>[^\./?#]+))?$}) do
model_instance = model_class.find(params[:id])
model_instance.destroy
status 204
- model_instance.to_json
+ respond_with model_instance
end
end
end
@@ -33,8 +33,14 @@
describe '#configure' do
let(:application) { stub(register: nil, use: nil) }
+ it 'registers the helper methods' do
+ application.should_receive(:register).with(Hovercraft::Helpers)
+
+ subject.configure(application)
+ end
+
it 'registers the methods to generate actions' do
- application.should_receive(:register).with(Hovercraft::Actions)
+ application.should_receive(:register).with(Hovercraft::Routes)
subject.configure(application)
end
@@ -1,7 +1,9 @@
-require 'hovercraft/actions'
+require 'hovercraft/routes'
+require 'hovercraft/helpers'
+require 'sinatra'
require 'rack/test'
-describe Hovercraft::Actions do
+describe Hovercraft::Routes do
include Rack::Test::Methods
let(:application) { Sinatra.new }
@@ -50,7 +52,8 @@
alias :app :application
before do
- application.register(Hovercraft::Actions)
+ application.register(Hovercraft::Helpers)
+ application.register(Hovercraft::Routes)
model_class.stub(all: models, create: model, find: model)
end
@@ -61,7 +64,7 @@
end
it 'generates a GET collection route' do
- application.routes['GET'][0][0].should == %r{^/employees(?:\.|%2E)([^/?#]+)$}
+ application.routes['GET'][0][0].should == %r{^/employees(\.(?<format>[^\./?#]+))?$}
application.routes['GET'][0][1].should == ['format']
end
@@ -78,7 +81,7 @@
end
it 'generates a POST collection route' do
- application.routes['POST'][0][0].should == %r{^/employees(?:\.|%2E)([^/?#]+)$}
+ application.routes['POST'][0][0].should == %r{^/employees(\.(?<format>[^\./?#]+))?$}
application.routes['POST'][0][1].should == ['format']
end
@@ -103,7 +106,7 @@
end
it 'generates a GET member route' do
- application.routes['GET'][0][0].should == %r{^/employees/([^/?#]+)(?:\.|%2E)([^/?#]+)$}
+ application.routes['GET'][0][0].should == %r{^/employees/(?<id>[^\./?#]+)(\.(?<format>[^\./?#]+))?$}
application.routes['GET'][0][1].should == ['id', 'format']
end
@@ -120,7 +123,7 @@
end
it 'generates a PUT member route' do
- application.routes['PUT'][0][0].should == %r{^/employees/([^/?#]+)(?:\.|%2E)([^/?#]+)$}
+ application.routes['PUT'][0][0].should == %r{^/employees/(?<id>[^\./?#]+)(\.(?<format>[^\./?#]+))?$}
application.routes['PUT'][0][1].should == ['id', 'format']
end
@@ -145,7 +148,7 @@
end
it 'generates a DELETE member route' do
- application.routes['DELETE'][0][0].should == %r{^/employees/([^/?#]+)(?:\.|%2E)([^/?#]+)$}
+ application.routes['DELETE'][0][0].should == %r{^/employees/(?<id>[^\./?#]+)(\.(?<format>[^\./?#]+))?$}
application.routes['DELETE'][0][1].should == ['id', 'format']
end

0 comments on commit 7629113

Please sign in to comment.