Module: Lalamove::Helper

Defined in:
lib/lalamove/helper.rb

Class Method Summary collapse

Class Method Details

.generate_raw_signature(method, timestamp, path, payload) ⇒ Object



27
28
29
# 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) ⇒ Object



20
21
22
23
24
25
# 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) ⇒ Object



35
36
37
38
39
40
41
# 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_timestampObject



31
32
33
# File 'lib/lalamove/helper.rb', line 31

def self.get_timestamp
  (Time.now.to_f * 1000).to_i
end

.get_token(key, timestamp, signature) ⇒ Object



43
44
45
# File 'lib/lalamove/helper.rb', line 43

def self.get_token(key, timestamp, signature)
  "#{key}:#{timestamp}:#{signature}"
end

.request(path, payload, method) ⇒ Object



10
11
12
13
14
15
16
17
18
# 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) ⇒ Object



47
48
49
# File 'lib/lalamove/helper.rb', line 47

def self.request_url(path)
  Lalamove::Config.base_url + path
end