Class: App42::Client::RestUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/app42/client/rest_util.rb

Instance Method Summary collapse

Instance Method Details

#compute_hmac(secret_key, sorted_params) ⇒ Object

Encoding and decoding of the queries are done using the Hmac Algorithm.

Parameters:

  • baseString
  • key


58
59
60
61
62
# File 'lib/app42/client/rest_util.rb', line 58

def compute_hmac(secret_key, sorted_params)
  signature =  Base64.encode64(HMAC::SHA1::digest(secret_key, sorted_params)).strip
  computed_hmac = CGI.escape(signature)
  computed_hmac
end

#get_timestamp_utcObject

Finds the current time

Returns:

  • time format



15
16
17
18
# File 'lib/app42/client/rest_util.rb', line 15

def get_timestamp_utc
  timestamp = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
  return timestamp.freeze
end

#sign(secret_key, params) ⇒ Object

It signs the request that has to be sent in the Hmac format.

Parameters:

  • secretKey
  • params


28
29
30
31
32
# File 'lib/app42/client/rest_util.rb', line 28

def sign secret_key, params
  sorted_params =  sort_convert_table(params)
  signature = compute_hmac(secret_key, sorted_params)
  signature
end

#sort_convert_table(table) ⇒ Object

This method sorts all the values that are stored in the table.

Parameters:

  • table

Returns:

  • sorted string



41
42
43
44
45
46
47
48
# File 'lib/app42/client/rest_util.rb', line 41

def sort_convert_table(table)
  sorted_params = ""
  table.sort {|a,b| a[0] <=> b[0]}.each{ |key, val|
    sorted_params << key
    sorted_params << val
  }
  sorted_params
end