Skip to content

Commit

Permalink
Support HTTP 204 no-content responses for put methods
Browse files Browse the repository at this point in the history
  • Loading branch information
John Feminella committed Sep 11, 2010
1 parent 762f505 commit 5ba2970
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/dupe/active_resource_extensions.rb
Expand Up @@ -57,8 +57,10 @@ def put(path, body = '', headers = {}) #:nodoc:

# if the request threw an exception
rescue
resource_hash = Hash.from_xml(body)
resource_hash = resource_hash[resource_hash.keys.first]
unless body.blank?
resource_hash = Hash.from_xml(body)
resource_hash = resource_hash[resource_hash.keys.first]
end
resource_hash = {} unless resource_hash.kind_of?(Hash)
resource_hash.symbolize_keys!

Expand Down
27 changes: 27 additions & 0 deletions spec/lib_specs/active_resource_extensions_spec.rb
Expand Up @@ -77,6 +77,33 @@ class Book < ActiveResource::Base
@ar_book.title = 'Rails!'
@ar_book.save
end

context "put methods that return HTTP 204" do
before(:each) do
class ExpirableBook < ActiveResource::Base
self.site = 'http://www.example.com'
attr_accessor :state

def expire_copyrights!
put(:expire)
end
end

Put %r{/expirable_books/(\d)+/expire.xml} do |id, body|
Dupe.find(:expirable_book) { |eb| eb.id == id.to_i }.tap { |book|
book.state = 'expired'
}
end

@e = Dupe.create :expirable_book, :title => 'Impermanence', :state => 'active'
end

it "should handle no-content responses" do
response = ExpirableBook.find(@e.id).expire_copyrights!
response.body.should be_blank
response.code.to_s.should == "204"
end
end

it "should parse the xml and turn the result into active resource objects" do
@book.title.should == "Rooby"
Expand Down

0 comments on commit 5ba2970

Please sign in to comment.