Sha256: 206f1f69c7b5ec7cd557aa41b5f545ce9701473da423d8defa6c3f47e8c401b4

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

require 'atol'
require 'atol/errors'
require 'atol/request/post_document/item/body'
require 'atol/request/post_document/sell/body'

module Atol
  module Request
    class PostDocument
      OPERATIONS = %i[sell sell_refund sell_correction buy buy_refund buy_correction].freeze
      HEADERS = { 'Content-Type' => 'application/json; charset=utf-8' }.freeze

      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)
        raise(Atol::MissingConfigError, 'group_code missing') if @config.group_code.empty?
        raise(Atol::UnknownOperationError, operation.to_s) unless OPERATIONS.include?(operation.to_sym)

        @operation = operation
        @token = token
        @body = body
        @req_logger = req_logger
        @res_logger = res_logger
      end

      def call
        http_client = @config.http_client
        uri = URI("#{Atol::URL}/#{@config.group_code}/#{@operation}")
        req_headers = HEADERS.merge('Token' => @token)
        req = http_client::Post.new(uri, req_headers)
        req.body = @body

        @req_logger.call(req) if @req_logger.respond_to?(:call)

        res = http_client.start(uri.hostname, uri.port, use_ssl: true) do |http|
          http.request(req)
        end
        
        @res_logger.call(res) if @res_logger.respond_to?(:call)

        res
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
atol-0.5.0 lib/atol/request/post_document.rb
atol-0.4.2 lib/atol/request/post_document.rb
atol-0.4.1 lib/atol/request/post_document.rb