Skip to content
Browse files

shuffling some naming and make sure the response content type is set …

…correctly
  • Loading branch information...
1 parent eeaedb8 commit 51f2e5d182dd9ba8ac2bdca07006ba191dc51b0a @atmos atmos committed Nov 1, 2009
Showing with 47 additions and 25 deletions.
  1. +12 −3 lib/hancock/api.rb
  2. +12 −15 lib/hancock/api/consumer.rb
  3. +1 −4 lib/hancock/api/user.rb
  4. +13 −1 lib/hancock/models/consumer.rb
  5. +9 −2 spec/http/consumer_api_spec.rb
View
15 lib/hancock/api.rb
@@ -1,6 +1,3 @@
-require File.expand_path(File.join(File.dirname(__FILE__), 'api', 'user'))
-require File.expand_path(File.join(File.dirname(__FILE__), 'api', 'consumer'))
-
module Hancock
module API
def self.app
@@ -9,5 +6,17 @@ def self.app
run Consumers::App
end
end
+ module JSON
+ class App < Sinatra::Base
+ use Rack::AcceptFormat
+ enable :methodoverride
+
+ before do
+ response['Content-Type'] = 'application/json'
+ end
+ end
+ end
end
end
+require File.expand_path(File.join(File.dirname(__FILE__), 'api', 'user'))
+require File.expand_path(File.join(File.dirname(__FILE__), 'api', 'consumer'))
View
27 lib/hancock/api/consumer.rb
@@ -1,34 +1,31 @@
module Hancock
module API
module Consumers
- class App < Sinatra::Base
- use Rack::AcceptFormat
- enable :methodoverride
-
+ class App < Hancock::API::JSON::App
get '/consumers.json' do
- Hancock::Consumer.all.map { |user| user.attributes_for_api }.to_json
+ Hancock::Consumer.all.map { |consumer| consumer.attributes_for_api }.to_json
end
get '/consumers/:id.json' do |id|
- user = Hancock::Consumer.get(id)
- user.to_json
+ consumer = Hancock::Consumer.get(id)
+ consumer.to_json
end
post '/consumers.json' do
- user = Hancock::Consumer.signup(params)
- user.to_json
+ consumer = Hancock::Consumer.create_from_params(params)
+ consumer.to_json
end
put '/consumers/:id.json' do |id|
- user = Hancock::Consumer.get(id)
- user.update_from_params(params)
- user.to_json
+ consumer = Hancock::Consumer.get(id)
+ consumer.update_from_params(params)
+ consumer.to_json
end
delete '/consumers/:id.json' do |id|
- user = Hancock::Consumer.get(id)
- user.destroy unless user.admin?
- user.to_json
+ consumer = Hancock::Consumer.get(id)
+ consumer.destroy unless user.admin?
+ consumer.to_json
end
end
end
View
5 lib/hancock/api/user.rb
@@ -1,10 +1,7 @@
module Hancock
module API
module Users
- class App < Sinatra::Base
- use Rack::AcceptFormat
- enable :methodoverride
-
+ class App < Hancock::API::JSON::App
get '/users.json' do
Hancock::User.all.map { |user| user.attributes_for_api }.to_json
end
View
14 lib/hancock/models/consumer.rb
@@ -11,10 +11,14 @@ def self.attributes_for_api
%w(id url label internal)
end
- def attributes_for_update
+ def self.attributes_for_create
%w(url label internal)
end
+ def attributes_for_update
+ self.class.attributes_for_create
+ end
+
def self.allowed?(host)
!first(:url => host).nil?
end
@@ -30,5 +34,13 @@ def self.internal
c.label
end
end
+
+ def self.params_for_create(params)
+ params.reject { |key, value| !attributes_for_create.include?(key) }
+ end
+
+ def self.create_from_params(params)
+ create(params_for_create(params))
+ end
end
end
View
11 spec/http/consumer_api_spec.rb
@@ -14,12 +14,19 @@ def app
end
describe '(POST /consumers)' do
it 'creates a consumer' do
- pending
size = Hancock::Consumer.count
- params = { }
+ params = { :url => "http://#{/\w{4,8}/.gen}.example.com", :label => /\w{4,12}/.gen }
lambda { post('/consumers', params, { 'HTTP_ACCEPT' => 'application/json'}) }.should
change(Hancock::Consumer, :count).from(size).to(size + 1)
end
+ it 'creates an internal consumer' do
+ size = Hancock::Consumer.count
+ params = { :url => "http://#{/\w{4,8}/.gen}.example.com", :label => /\w{4,12}/.gen, :internal => 'true' }
+ lambda do
+ response = post('/consumers', params, { 'HTTP_ACCEPT' => 'application/json'})
+ pp response
+ end.should change(Hancock::Consumer, :count).from(size).to(size + 1)
+ end
end
describe "given an existing consumer" do
before(:each) { @consumer = Hancock::Consumer.gen(:internal) }

0 comments on commit 51f2e5d

Please sign in to comment.
Something went wrong with that request. Please try again.