Skip to content

Commit

Permalink
Added yaml response parsing
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunemaker <nunemaker@gmail.com>
  • Loading branch information
mfilej authored and jnunemaker committed Feb 11, 2009
1 parent 577798b commit 3db19cb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ module HTTParty
'text/json' => :json,
'application/javascript' => :json,
'text/javascript' => :json,
'text/html' => :html
'text/html' => :html,
'application/x-yaml' => :yaml,
'text/yaml' => :yaml
} unless defined?(AllowedFormats)

def self.included(base)
Expand Down
2 changes: 2 additions & 0 deletions lib/httparty/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def parse_response(body)
HTTParty::Parsers::XML.parse(body)
when :json
HTTParty::Parsers::JSON.decode(body)
when :yaml
YAML::load(body)
else
body
end
Expand Down
6 changes: 6 additions & 0 deletions spec/httparty/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ def stub_response(body, code = 200)
@request.send(:parse_response, json).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
end

it 'should handle yaml automatically' do
yaml = "books: \n book: \n name: Foo Bar!\n id: \"1234\"\n"
@request.options[:format] = :yaml
@request.send(:parse_response, yaml).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
end

it "should include any HTTP headers in the returned response" do
@request.options[:format] = :html
response = stub_response "Content"
Expand Down
5 changes: 5 additions & 0 deletions spec/httparty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ def second_method
@klass.default_options[:format].should == :json
end

it "should allow yaml" do
@klass.format :yaml
@klass.default_options[:format].should == :yaml
end

it 'should not allow funky format' do
lambda do
@klass.format :foobar
Expand Down

0 comments on commit 3db19cb

Please sign in to comment.