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.



7
8
9
10
# File 'lib/atol/transaction/get_token.rb', line 7

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

Instance Method Details

#callObject



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

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

    case response.code
    when '200'
      return json['token']
    when '400'
      case json['code']
      when 19
        raise Atol::AuthUserOrPasswordError
      when 17
        raise Atol::AuthBadRequestError
      end
    when '500'
      next
    end
  end

  raise "#{response.code} #{response.body}"
end