Module: HashkeyGenerator

Included in:
BasicApiAuth::Middleware
Defined in:
lib/hashkey_generator.rb

Instance Method Summary collapse

Instance Method Details

#concat_params(params) ⇒ Object

create a string representation of passed hash, which can be encoded in a URL



5
6
7
8
9
10
# File 'lib/hashkey_generator.rb', line 5

def concat_params(params)
  result = ""
  return result if params.empty?
  params.map{|key,val| result += "#{key}=#{val}&" unless val.nil? or val == ''}
  result.chomp!('&')
end

#generate_hashkey_for(params, api_key) ⇒ Object

Generate a hashkey using SHA1 for a provided API key List of parameters *params : a list of parameters as a hash, that needs to be used for hashkey generation *api_key: a unique API key provided to you for authenticating the API calls



21
22
23
24
25
# File 'lib/hashkey_generator.rb', line 21

def generate_hashkey_for(params, api_key)
  sorted_params = sort_params(params)
  params_string = concat_params(sorted_params)
  Digest::SHA1.hexdigest "#{params_string}&#{api_key}"
end

#sort_params(params) ⇒ Object

sort the parameters in alphabetical order of keys



13
14
15
# File 'lib/hashkey_generator.rb', line 13

def sort_params(params)
  Hash[params.sort]
end