Sha256: 467be9ea897e6233e187a7a732687cda17aba70bf4a4527fa140620a8d1bb878

Contents?: true

Size: 934 Bytes

Versions: 5

Compression:

Stored size: 934 Bytes

Contents

# frozen_string_literal: true

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
atol-0.8.1 lib/atol/transaction/get_token.rb
atol-0.7.1 lib/atol/transaction/get_token.rb
atol-0.7 lib/atol/transaction/get_token.rb
atol-0.6 lib/atol/transaction/get_token.rb
atol-0.5.1 lib/atol/transaction/get_token.rb