Sha256: bf0305073d3573c136648206c911c03c8e6f7f2d68c103a77aefb59bef8242a8

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module CzechPostB2bClient
  module Services
    class AddressSheetsGenerator < CzechPostB2bClient::Services::Communicator
      attr_reader :parcel_codes, :options

      AddressSheetsGeneratorResult = Struct.new(:pdf_content, :zpl_content, :state_text, :state_code, keyword_init: true)

      def initialize(parcel_codes:, options: {})
        super()
        @parcel_codes = parcel_codes
        @options = options
      end

      def steps
        super + %i[check_for_state_errors]
      end

      private

      def request_builder_args
        { parcel_codes: parcel_codes, options: options }
      end

      def request_builder_class
        CzechPostB2bClient::RequestBuilders::GetParcelsPrintingBuilder
      end

      def api_caller_class
        CzechPostB2bClient::Services::ApiCaller
      end

      def response_parser_class
        CzechPostB2bClient::ResponseParsers::GetParcelsPrintingParser
      end

      def endpoint_path
        '/getParcelsPrinting'
      end

      def build_result_from(response_hash)
        AddressSheetsGeneratorResult.new(pdf_content: response_hash.dig(:printings, :pdf_content),
                                         zpl_content: response_hash.dig(:printings, :zpl_content),
                                         state_text: response_hash.dig(:response, :state, :text),
                                         state_code: response_hash.dig(:response, :state, :code))
      end

      def check_for_state_errors
        return if result.state_code == CzechPostB2bClient::ResponseCodes::Ok.code

        r_code = CzechPostB2bClient::ResponseCodes.new_by_code(result.state_code)
        errors.add(:response_state, r_code.to_s)

        fail! unless r_code.info?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
czech_post_b2b_client-1.4.0 lib/czech_post_b2b_client/services/address_sheets_generator.rb
czech_post_b2b_client-1.3.0 lib/czech_post_b2b_client/services/address_sheets_generator.rb