Permalink
Browse files

Mashify responses that contain hashes and non-hashes.

  • Loading branch information...
1 parent e3da4be commit c3218d1c0cbcb16455def270956bb9b6ec5e7f29 @nicolasblanco nicolasblanco committed with sferik Nov 30, 2010
Showing with 10 additions and 2 deletions.
  1. +2 −2 lib/faraday/mashify.rb
  2. +8 −0 test/mashify_test.rb
View
@@ -13,8 +13,8 @@ def self.register_on_complete(env)
response_body = response[:body]
if response_body.is_a?(Hash)
response[:body] = ::Hashie::Mash.new(response_body)
- elsif response_body.is_a?(Array) and response_body.first.is_a?(Hash)
- response[:body] = response_body.map{|item| ::Hashie::Mash.new(item)}
+ elsif response_body.is_a?(Array)
+ response[:body] = response_body.map{|item| item.is_a?(Hash) ? ::Hashie::Mash.new(item) : item}
end
end
end
View
@@ -31,5 +31,13 @@ class MashifyTest < Test::Unit::TestCase
assert_equal 123, values.first
assert_equal 456, values.last
end
+
+ should 'handle arrays of hashes and non-hashes' do
+ @stubs.get('/array/simple') {[200, {'content-type' => 'application/json; charset=utf-8'}, '[123, {"username":"slainer68"}, 42]']}
+ values = @conn.get("/array/simple").body
+ assert_equal 123, values[0]
+ assert_equal "slainer68", values[1].username
+ assert_equal 42, values[2]
+ end
end
end

0 comments on commit c3218d1

Please sign in to comment.