Sha256: cb5d5ec476e1de2990b7acc6d628ed3d1f15e43e6cdd2bb7ac5ea1d00799b6c0

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

require 'atol'
require 'atol/errors'
require 'net/http'
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' }.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)

        @url = "#{Atol::URL}/#{@config.group_code}/#{operation}?tokenid=#{token}"
        @body = body
        @req_logger = req_logger
        @res_logger = res_logger
      end

      def call
        uri = URI(url)
        req = Net::HTTP::Post.new(uri, HEADERS)
        req.body = body

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

        res = Net::HTTP.start(uri.hostname, uri.port) do |http|
          http.request(req)
        end

        if res_logger.respond_to?(:call)
          res_logger.call(res)
        end

        res
      end

      private

      attr_reader :url, :body, :req_logger, :res_logger
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
atol-0.1.2 lib/atol/request/post_document.rb
atol-0.1.1 lib/atol/request/post_document.rb
atol-0.1 lib/atol/request/post_document.rb