Skip to content

Commit

Permalink
Merge pull request oauth-xx#1 from lbadura/nested_params_support
Browse files Browse the repository at this point in the history
Add a helper method to flatten nested params so that they can be used by...
  • Loading branch information
Michał Bugno committed Aug 15, 2012
2 parents eba957c + ff4534c commit 3a74788
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/oauth/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,30 @@ 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
query = query.split("&").sort { |a, b| a.split("=")[0] <=> b.split("=")[0] }.join("&")
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.
Expand Down

0 comments on commit 3a74788

Please sign in to comment.