Class: Billomat::Gateway
- Inherits:
-
Object
- Object
- Billomat::Gateway
- Defined in:
- lib/billomat/gateway.rb
Overview
This class can be used by the gem to communicate with the API.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#config ⇒ Billomat::Configuration
:reek:UtilityFunction because it’s a shorthand.
-
#headers ⇒ Hash
The headers for the request.
-
#initialize(method, path, body = {}) ⇒ Gateway
constructor
Creates a new Gateway.
-
#response ⇒ RestClient::Response
Executes the API call and return the response.
-
#run ⇒ Hash
Executes the API call and parse the response.
- #timeout ⇒ Integer
-
#url ⇒ String
The complete URL for the request.
Constructor Details
#initialize(method, path, body = {}) ⇒ Gateway
Creates a new Gateway
39 40 41 42 43 |
# File 'lib/billomat/gateway.rb', line 39 def initialize(method, path, body = {}) @method = method.to_sym @path = path @body = body end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
28 29 30 |
# File 'lib/billomat/gateway.rb', line 28 def body @body end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
28 29 30 |
# File 'lib/billomat/gateway.rb', line 28 def method @method end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
28 29 30 |
# File 'lib/billomat/gateway.rb', line 28 def path @path end |
Instance Method Details
#config ⇒ Billomat::Configuration
:reek:UtilityFunction because it’s a shorthand
95 96 97 |
# File 'lib/billomat/gateway.rb', line 95 def config Billomat.configuration end |
#headers ⇒ Hash
Returns the headers for the request.
82 83 84 85 86 87 88 89 90 |
# File 'lib/billomat/gateway.rb', line 82 def headers { 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'X-BillomatApiKey' => config.api_key, 'X-AppId' => config.app_id, 'X-AppSecret' => config.app_secret }.compact end |
#response ⇒ RestClient::Response
Executes the API call and return the response.
61 62 63 64 65 66 67 68 69 |
# File 'lib/billomat/gateway.rb', line 61 def response RestClient::Request.execute( method: method, url: url, timeout: timeout, headers: headers, payload: body.to_json ).tap { |response| Billomat.configuration.after_response&.call(response) } end |
#run ⇒ Hash
Executes the API call and parse the response.
48 49 50 51 52 53 54 55 56 |
# File 'lib/billomat/gateway.rb', line 48 def run resp = response return nil if resp.body.empty? JSON.parse(resp.body) rescue RestClient::Exception => e raise GatewayError, e end |
#timeout ⇒ Integer
77 78 79 |
# File 'lib/billomat/gateway.rb', line 77 def timeout config.timeout || 5 end |
#url ⇒ String
Returns the complete URL for the request.
72 73 74 |
# File 'lib/billomat/gateway.rb', line 72 def url "https://#{config.subdomain}.billomat.net/api#{path}" end |