Class: Atol::Transaction::PostDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/atol/transaction/post_document.rb

Instance Method Summary collapse

Constructor Details

#initialize(operation:, token:, body:, config: nil, req_logger: nil, res_logger: nil) ⇒ PostDocument

Returns a new instance of PostDocument.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/atol/transaction/post_document.rb', line 9

def initialize(operation:, token:, body:, config: nil, req_logger: nil, res_logger: nil)
  @config = config || Atol.config
  raise(Atol::ConfigExpectedError) unless @config.is_a?(Atol::Config)

  @params = {
    operation: operation,
    token: token,
    body: body,
    config: config,
    req_logger: req_logger,
    res_logger: res_logger
  }
end

Instance Method Details

#callObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/atol/transaction/post_document.rb', line 23

def call
  request = Atol::Request::PostDocument.new(@params)
  response = request.call
  encoded_body = response.body.dup.force_encoding(Atol::ENCODING)
  json = JSON.parse(encoded_body)

  if response.code == '200' && json['error'].nil?
    json
  elsif ERRORS[json['error']['code']]
    raise(ERRORS[json['error']['code']], encoded_body)
  else
    raise(encoded_body)
  end
end