Module: Sailthru::Helpers
- Included in:
- SailthruClient
- Defined in:
- lib/sailthru.rb
Instance Method Summary collapse
-
#extract_param_values(params) ⇒ Object
params: params, Hash returns: Array, values of each item in the Hash (and nested hashes).
-
#flatten_nested_hash(hash, brackets = true) ⇒ Object
Flatten nested hash for GET / POST request.
-
#get_signature_hash(params, secret) ⇒ Object
params: params, Hash secret, String returns: String.
-
#get_signature_string(params, secret) ⇒ Object
params: params, Hash secret, String returns: String.
Instance Method Details
#extract_param_values(params) ⇒ Object
params:
params, Hash
returns:
Array, values of each item in the Hash (and nested hashes)
Extracts the values of a set of parameters, recursing into nested assoc arrays.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sailthru.rb', line 24 def extract_param_values(params) values = [] params.each do |k, v| if v.class == Hash values.concat extract_param_values(v) elsif v.class == Array temp_hash = Hash.new() v.each_with_index do |v_,i_| temp_hash[i_.to_s] = v_ end values.concat extract_param_values(temp_hash) else values.push v.to_s end end return values end |
#flatten_nested_hash(hash, brackets = true) ⇒ Object
Flatten nested hash for GET / POST request.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sailthru.rb', line 67 def flatten_nested_hash(hash, brackets = true) f = {} hash.each do |key, value| _key = brackets ? "[#{key}]" : key.to_s if value.class == Hash flatten_nested_hash(value).each do |k, v| f["#{_key}#{k}"] = v end elsif value.class == Array temp_hash = Hash.new() value.each_with_index do |v, i| temp_hash[i.to_s] = v end flatten_nested_hash(temp_hash).each do |k, v| f["#{_key}#{k}"] = v end else f[_key] = value end end return f end |
#get_signature_hash(params, secret) ⇒ Object
params:
params, Hash
secret, String
returns:
String
Returns an MD5 hash of the signature string for an API call.
61 62 63 |
# File 'lib/sailthru.rb', line 61 def get_signature_hash(params, secret) Digest::MD5.hexdigest(get_signature_string(params, secret)).to_s end |
#get_signature_string(params, secret) ⇒ Object
params:
params, Hash
secret, String
returns:
String
Returns the unhashed signature string (secret + sorted list of param values) for an API call.
49 50 51 |
# File 'lib/sailthru.rb', line 49 def get_signature_string(params, secret) return secret + extract_param_values(params).sort.join("") end |