Class: Metropol::Request

Inherits:
Object
  • Object
show all
Includes:
Default, LegalId, IdModifier, ReportReason
Defined in:
lib/metropol/request.rb,
lib/metropol/request/id_modifier.rb,
lib/metropol/request/report_reason.rb

Defined Under Namespace

Modules: IdModifier, ReportReason

Constant Summary

Constants included from ReportReason

ReportReason::REASON_TYPES

Constants included from LegalId

LegalId::ID_TYPES

Constants included from Default

Default::API_VERSION, Default::HEADERS, Default::HOST, Default::PORT

Instance Method Summary collapse

Methods included from ReportReason

#method_missing, #valid_reason?

Methods included from IdModifier

#method_missing

Methods included from LegalId

#code_for, #valid_id?

Methods included from Default

#default_headers

Constructor Details

#initialize(public_key:, private_key:, path:, port:, api_version:, payload: {}) ⇒ Request

Returns a new instance of Request.



17
18
19
20
21
22
23
24
# File 'lib/metropol/request.rb', line 17

def initialize(public_key:, private_key:, path:, port:, api_version:, payload: {})
  @public_key = public_key
  @private_key = private_key
  @path = path
  @port = port
  @api_version = api_version
  @payload = payload
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Metropol::Request::ReportReason

Instance Method Details

#generate_api_hash(timestamp) ⇒ Object



50
51
52
53
54
# File 'lib/metropol/request.rb', line 50

def generate_api_hash(timestamp)
  json = JSON.generate(@payload)
  str = @private_key + json + @public_key + timestamp
  Digest::SHA256.hexdigest(str.encode('UTF-8'))
end

#generate_headersObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/metropol/request.rb', line 34

def generate_headers
  timestamp = generate_timestamp
  api_hash = generate_api_hash(timestamp)

  headers = default_headers
  headers['X-METROPOL-REST-API-KEY'] = @public_key
  headers['X-METROPOL-REST-API-TIMESTAMP'] = timestamp
  headers['X-METROPOL-REST-API-HASH'] = api_hash
  headers
end

#generate_timestampObject



45
46
47
48
# File 'lib/metropol/request.rb', line 45

def generate_timestamp
  t = Time.now.utc
  t.strftime('%Y%m%d%H%M%S%6N')
end

#generate_urlObject



26
27
28
29
30
31
32
# File 'lib/metropol/request.rb', line 26

def generate_url
  URI::HTTPS.build(
    host: HOST,
    port: port,
    path: "/#{api_version}/#{@path}"
  ).to_s
end

#postObject



56
57
58
59
60
61
# File 'lib/metropol/request.rb', line 56

def post
  url = generate_url
  headers = generate_headers
  resp = RestClient.post(url, payload_json, headers)
  JSON.parse resp.body
end