Class: Atol::Transaction::GetToken

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

Instance Method Summary collapse

Constructor Details

#initialize(config: nil) ⇒ GetToken

Returns a new instance of GetToken.



9
10
11
12
# File 'lib/atol/transaction/get_token.rb', line 9

def initialize(config: nil)
  @config = config || Atol.config
  raise(Atol::ConfigExpectedError) unless @config.is_a?(Atol::Config)
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/atol/transaction/get_token.rb', line 14

def call
  @config.req_tries_number.times do
    request = Atol::Request::GetToken.new(config: @config)
    response = request.call
    encoded_body = response.body.dup.force_encoding(Atol::ENCODING)
    json = JSON.parse(encoded_body)

    case response.code
    when '200'
      return json['token']
    when '400'
      raise Atol::AuthBadRequestError
    when '401'
      raise Atol::AuthUserOrPasswordError
    when '500'
      next
    end
    raise "#{response.code} #{response.body}"
  end
end