Class: Wfirma::Drivers::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/wfirma/drivers/http.rb

Overview

Real transport. POSTs JSON to api2.wfirma.pl with API Key auth headers (accessKey / secretKey / appKey) and parses the response.

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api2.wfirma.pl".freeze
PDF_MAGIC =
"%PDF-".freeze

Instance Method Summary collapse

Constructor Details

#initialize(access_key:, secret_key:, app_key:, company_id:, base_url: DEFAULT_BASE_URL, open_timeout: 5, read_timeout: 30) ⇒ Http

Returns a new instance of Http.



13
14
15
16
17
18
19
20
21
22
# File 'lib/wfirma/drivers/http.rb', line 13

def initialize(access_key:, secret_key:, app_key:, company_id:,
               base_url: DEFAULT_BASE_URL, open_timeout: 5, read_timeout: 30)
  @access_key = access_key
  @secret_key = secret_key
  @app_key = app_key
  @company_id = company_id
  @base_url = base_url
  @open_timeout = open_timeout
  @read_timeout = read_timeout
end

Instance Method Details

#call(module_name, action, payload, params = {}, id = nil) ⇒ Object

id appends a record id to the path, as required by the per-record actions (invoices/send/123, invoices/download/123, invoices/get/123).



26
27
28
29
# File 'lib/wfirma/drivers/http.rb', line 26

def call(module_name, action, payload, params = {}, id = nil)
  uri = build_uri(module_name, action, params, id)
  parse(perform(uri, payload))
end

#download(module_name, action, payload, params = {}, id = nil) ⇒ Object

Binary actions (invoices/download) answer with the raw file; only failures come back as JSON. Returns the file bytes as a binary String.



33
34
35
36
37
38
39
40
# File 'lib/wfirma/drivers/http.rb', line 33

def download(module_name, action, payload, params = {}, id = nil)
  uri = build_uri(module_name, action, params, id)
  response = perform(uri, payload, accept: "application/pdf")
  body = response.body.to_s.dup.force_encoding(Encoding::BINARY)
  return body if body.start_with?(PDF_MAGIC)

  raise_download_error(body, response)
end