module Lalamove::Helper

Public Class Methods

generate_raw_signature(method, timestamp,path, payload) click to toggle source
# File lib/lalamove/helper.rb, line 27
def self.generate_raw_signature(method, timestamp,path, payload)
  "#{timestamp}\r\n#{method}\r\n#{path}\r\n\r\n#{payload}"
end
generate_signature(path, method, timestamp, payload) click to toggle source
# File lib/lalamove/helper.rb, line 20
def self.generate_signature(path, method, timestamp, payload)
  raw_signature = generate_raw_signature(method, timestamp, path, payload)
  puts "Key config"
  puts Lalamove.config.secret_key
  OpenSSL::HMAC.hexdigest('sha256', Lalamove.config.secret_key, raw_signature)
end
get_header(token, timestamp) click to toggle source
# File lib/lalamove/helper.rb, line 35
def self.get_header(token, timestamp)
  {
    "Authorization" => "hmac #{token}",
    "X-LLM-Country" => Lalamove.config.country_code,
    "X-Request-ID" => timestamp.to_s
  }
end
get_timestamp() click to toggle source
# File lib/lalamove/helper.rb, line 31
def self.get_timestamp
  (Time.now.to_f * 1000).to_i
end
get_token(key, timestamp, signature) click to toggle source
# File lib/lalamove/helper.rb, line 43
def self.get_token(key, timestamp, signature)
  "#{key}:#{timestamp}:#{signature}"
end
request(path, payload, method) click to toggle source
# File lib/lalamove/helper.rb, line 10
def self.request(path, payload, method)
  timestamp = get_timestamp
  puts timestamp
  signature = generate_signature(path, method, timestamp, payload)
  token = get_token(Lalamove.config.key, timestamp, signature)
  headers = get_header(token, timestamp.to_s)
  url = request_url(path)
  HTTParty.post(url, :headers => headers, :body => payload.to_json.to_s)
end
request_url(path) click to toggle source
# File lib/lalamove/helper.rb, line 47
def self.request_url(path)
  Lalamove::Config.base_url + path
end