Class: EasybillRestClient::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/easybill_rest_client/api_client.rb

Constant Summary collapse

DEFAULT_RETRY_COOL_OFF_TIME =
15
DEFAULT_TRIES =
5
MAX_PAGE_SIZE =
1000

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ApiClient

Returns a new instance of ApiClient.



17
18
19
20
21
22
# File 'lib/easybill_rest_client/api_client.rb', line 17

def initialize(options = {})
  @api_key = options.fetch(:api_key)
  @retry_cool_off_time = options.fetch(:retry_cool_off_time, DEFAULT_RETRY_COOL_OFF_TIME)
  @tries = options.fetch(:tries, DEFAULT_TRIES)
  @logger = options.fetch(:logger, Logger.new($stdout))
end

Instance Method Details

#request(method, endpoint, params = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/easybill_rest_client/api_client.rb', line 30

def request(method, endpoint, params = {})
  request = Request.new(
    api_key: api_key,
    method: method,
    endpoint: endpoint,
    params: params,
    logger: logger,
    tries: tries,
    retry_cool_off_time: retry_cool_off_time
  )
  Response.new(request.run).body
end

#request_collection(method, endpoint, params = {}) ⇒ Object



24
25
26
27
28
# File 'lib/easybill_rest_client/api_client.rb', line 24

def request_collection(method, endpoint, params = {})
  fetch_pages do |page|
    request(method, endpoint, params.merge(page: page, limit: MAX_PAGE_SIZE))
  end
end