Class: Metropol::Request
- Inherits:
-
Object
- Object
- Metropol::Request
- 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
Constants included from LegalId
Constants included from Default
Default::API_VERSION, Default::HEADERS, Default::HOST, Default::PORT
Instance Method Summary collapse
- #generate_api_hash(timestamp) ⇒ Object
- #generate_headers ⇒ Object
- #generate_timestamp ⇒ Object
- #generate_url ⇒ Object
-
#initialize(public_key:, private_key:, path:, port:, api_version:, payload: {}) ⇒ Request
constructor
A new instance of Request.
- #post ⇒ Object
Methods included from ReportReason
#method_missing, #valid_reason?
Methods included from IdModifier
Methods included from LegalId
Methods included from Default
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() json = JSON.generate(@payload) str = @private_key + json + @public_key + Digest::SHA256.hexdigest(str.encode('UTF-8')) end |
#generate_headers ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/metropol/request.rb', line 34 def generate_headers = api_hash = generate_api_hash() headers = default_headers headers['X-METROPOL-REST-API-KEY'] = @public_key headers['X-METROPOL-REST-API-TIMESTAMP'] = headers['X-METROPOL-REST-API-HASH'] = api_hash headers end |
#generate_timestamp ⇒ Object
45 46 47 48 |
# File 'lib/metropol/request.rb', line 45 def t = Time.now.utc t.strftime('%Y%m%d%H%M%S%6N') end |
#generate_url ⇒ Object
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 |
#post ⇒ Object
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 |