Sha256: bd3c6f6dec619ed8940305b1928fac1de4e130c205c65273524057eb5b5532d5

Contents?: true

Size: 963 Bytes

Versions: 8

Compression:

Stored size: 963 Bytes

Contents

require 'atol/request/get_token'
require 'atol/errors'

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

      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
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
atol-0.5.0 lib/atol/transaction/get_token.rb
atol-0.4.2 lib/atol/transaction/get_token.rb
atol-0.4.1 lib/atol/transaction/get_token.rb
atol-0.3 lib/atol/transaction/get_token.rb
atol-0.2 lib/atol/transaction/get_token.rb
atol-0.1.2 lib/atol/transaction/get_token.rb
atol-0.1.1 lib/atol/transaction/get_token.rb
atol-0.1 lib/atol/transaction/get_token.rb