From ff4534c75b034185429154fd10a313273819c90d Mon Sep 17 00:00:00 2001 From: Lukasz Badura Date: Wed, 15 Aug 2012 15:30:14 +0200 Subject: [PATCH] Add a helper method to flatten nested params so that they can be used by Addressable. --- lib/oauth/helper.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/oauth/helper.rb b/lib/oauth/helper.rb index d9314400..83b30cd7 100644 --- a/lib/oauth/helper.rb +++ b/lib/oauth/helper.rb @@ -36,7 +36,7 @@ def generate_timestamp #:nodoc: # See Also: {OAuth core spec version 1.0, section 9.1.1}[http://oauth.net/core/1.0#rfc.section.9.1.1] def normalize(params) uri = Addressable::URI.new - uri.query_values = params + uri.query_values = flatten_params(params) query = uri.query # Addressable doesn't sort the params @@ -44,6 +44,22 @@ def normalize(params) query.gsub("[", "%5B").gsub("]", "%5D") end + # Flattens nested attributes. Required until Adressable adds support for it. + def flatten_params(hash, keys=nil) + new_hash = {} + hash.map do |k, v| + string_key = k.to_s + new_keys = keys ? "#{keys}[#{string_key}]" : string_key + if v.is_a?(Hash) + sub_hash = flatten_params v, new_keys + new_hash.merge! sub_hash + else + new_hash[new_keys] = v + end + end + new_hash + end + # Parse an Authorization / WWW-Authenticate header into a hash. Takes care of unescaping and # removing surrounding quotes. Raises a OAuth::Problem if the header is not parsable into a # valid hash. Does not validate the keys or values.