Class: Billomat::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/billomat/gateway.rb

Overview

This class can be used by the gem to communicate with the API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path, body = {}) ⇒ Gateway

Creates a new Gateway

Examples:

Billomat::Gateway.new(:get, '/invoices')
Billomat::Gateway.new(:post, '/invoices', { 'invoice' => { ... } })

Parameters:

  • method (Symbol)

    The HTTP verb

  • path (String)

    The path of the resource

  • body (Hash) (defaults to: {})

    The payload for the request



42
43
44
45
46
# File 'lib/billomat/gateway.rb', line 42

def initialize(method, path, body = {})
  @method   = method.to_sym
  @path     = path
  @body     = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



31
32
33
# File 'lib/billomat/gateway.rb', line 31

def body
  @body
end

#methodObject (readonly)

Returns the value of attribute method.



31
32
33
# File 'lib/billomat/gateway.rb', line 31

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



31
32
33
# File 'lib/billomat/gateway.rb', line 31

def path
  @path
end

Instance Method Details

#configBillomat::Configuration

:reek:UtilityFunction because it’s a shorthand

Returns:



98
99
100
# File 'lib/billomat/gateway.rb', line 98

def config
  Billomat.configuration
end

#headersHash

Returns the headers for the request.

Returns:

  • (Hash)

    the headers for the request



85
86
87
88
89
90
91
92
93
# File 'lib/billomat/gateway.rb', line 85

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

#responseRestClient::Response

Executes the API call and return the response.

Returns:

  • (RestClient::Response)

    the API response



64
65
66
67
68
69
70
71
72
# File 'lib/billomat/gateway.rb', line 64

def response
  RestClient::Request.execute(
    method: method,
    url: url,
    timeout: timeout,
    headers: headers,
    payload: body.to_json
  )
end

#runHash

Executes the API call and parse the response.

Returns:

  • (Hash)

    the response body



51
52
53
54
55
56
57
58
59
# File 'lib/billomat/gateway.rb', line 51

def run
  resp = response

  return nil if resp.body.empty?

  JSON.parse(resp.body)
rescue RestClient::Exception => e
  raise GatewayError, e
end

#timeoutInteger

Returns:

  • (Integer)


80
81
82
# File 'lib/billomat/gateway.rb', line 80

def timeout
  config.timeout || 5
end

#urlString

Returns the complete URL for the request.

Returns:

  • (String)

    the complete URL for the request



75
76
77
# File 'lib/billomat/gateway.rb', line 75

def url
  "https://#{config.subdomain}.billomat.net/api#{path}"
end