From c51cb3e2d7b9a28c75feb1f5dede793160254b66 Mon Sep 17 00:00:00 2001 From: John Mortlock Date: Mon, 19 Mar 2018 21:02:46 +1030 Subject: [PATCH] Correctly handle nested params --- lib/httparty/hash_conversions.rb | 2 +- spec/httparty/hash_conversions_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/httparty/hash_conversions.rb b/lib/httparty/hash_conversions.rb index 0df6035..f32d30c 100644 --- a/lib/httparty/hash_conversions.rb +++ b/lib/httparty/hash_conversions.rb @@ -26,7 +26,7 @@ module HTTParty def self.normalize_param(key, value) normalized_keys = normalize_keys(key, value) - normalized_keys.inject('') do |string, (k, v)| + normalized_keys.flatten.each_slice(2).inject('') do |string, (k, v)| string + "#{k}=#{ERB::Util.url_encode(v.to_s)}&" end end diff --git a/spec/httparty/hash_conversions_spec.rb b/spec/httparty/hash_conversions_spec.rb index a403391..0027f25 100644 --- a/spec/httparty/hash_conversions_spec.rb +++ b/spec/httparty/hash_conversions_spec.rb @@ -11,6 +11,13 @@ RSpec.describe HTTParty::HashConversions do } expect(HTTParty::HashConversions.to_params(hash)).to eq("name=bob&address[street]=111%20ruby%20ave.&address[city]=ruby%20central&address[phones][]=111-111-1111&address[phones][]=222-222-2222") end + + context "nested params" do + it 'creates a params string from a hash' do + hash = { marketing_event: { marketed_resources: [ {type:"product", id: 57474842640 } ] } } + expect(HTTParty::HashConversions.to_params(hash)).to eq("marketing_event[marketed_resources][][type]=product&marketing_event[marketed_resources][][id]=57474842640") + end + end end describe ".normalize_param" do