Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Mashify responses that contain hashes and non-hashes.

  • Loading branch information...
commit c3218d1c0cbcb16455def270956bb9b6ec5e7f29 1 parent e3da4be
@nicolasblanco nicolasblanco authored sferik committed
Showing with 10 additions and 2 deletions.
  1. +2 −2 lib/faraday/mashify.rb
  2. +8 −0 test/mashify_test.rb
View
4 lib/faraday/mashify.rb
@@ -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
8 test/mashify_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.