Created
July 9, 2009 20:18
-
-
Save julesfern/143966 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # BEFORE | |
| # ---------------------------------------------------------------------- | |
| # Returns the signature_params as a normalised string in line with | |
| # http://oauth.net/core/1.0#signing_process | |
| def normalise_signature_params(p=signature_params, *hash_path) | |
| p.sort {|a,b| a.to_s<=>b.to_s}.collect do |key, value| | |
| path = hash_path.dup | |
| path << key | |
| if value.is_a?(Hash) or value.is_a?(Mash) | |
| normalise_signature_params(value, *path) | |
| else | |
| key_path = path.first + path[1..(path.length-1)].collect {|h| "[#{h}]"}.join("") | |
| "#{CGI.escape key_path.to_s}=#{CGI.escape value.to_s}" rescue raise("The key/value pair #{key_path.inspect} / #{value.inspect} could not be included in the signature.") | |
| end | |
| end.join("&") | |
| end | |
| # AFTER | |
| # ---------------------------------------------------------------------- | |
| # Returns the signature_params as a normalised string in line with | |
| # http://oauth.net/core/1.0#signing_process | |
| def normalise_signature_params(params=signature_params, *hash_path) | |
| flatten_params(params).sort {|a,b| a.to_s <=> b.to_s}.collect {|k, v| "#{CGI.escape(k)}=#{CGI.escape(v.to_s)}" }.join("&") | |
| end | |
| def flatten_params(params, *hash_path) | |
| op = {} | |
| params.sort {|a,b| a.to_s<=>b.to_s}.each do |key, value| | |
| path = hash_path.dup | |
| path << key.to_s | |
| if value.is_a?(Hash) | |
| op.merge! flatten_params(value, *path) | |
| elsif value | |
| key_path = path.first + path[1..(path.length-1)].collect {|h| "[#{h}]"}.join("") | |
| op[key_path] = value | |
| end | |
| end | |
| return op | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment