Class: BillyApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/billy_api/client.rb

Constant Summary collapse

BASE_URL =
'https://api.billysbilling.com/v2/'.freeze

Class Method Summary collapse

Class Method Details

.request(request_method, url, id = nil, payload = {}, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/billy_api/client.rb', line 8

def self.request(request_method, url, id = nil, payload = {}, &block)
  url = "#{url}/#{id}" if request_method == :get && !id.nil?

  response = RestClient::Request.execute(
    {
      method: request_method,
      url: BASE_URL + url,
      headers: {
        'X-Access-Token' => BillyApi.configuration.api_key,
        'Content-Type' => 'application/json'
      }
    }.merge(request_method == :get ? {} : { payload: MultiJson.dump(payload) })
  )

  if block_given?
    yield MultiJson.load(response.body)
  else
    return MultiJson.load(response.body)
  end
rescue RuntimeError => e # Todo, find correct exceptions
  puts e.response.body
  # Reraise until better handling, of ResponseObjects are in place.
  raise e
end