Sha256: 8b30631539d8ab0eef2c468b6c1392171051a45fbac63a3942d1d4dbd1b5b327

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module CzechPostB2bClient
  module Services
    class ParcelsSendProcessUpdater < CzechPostB2bClient::Services::Communicator
      attr_reader :transaction_id

      def initialize(transaction_id:)
        @transaction_id = transaction_id
      end

      def steps
        super + %i[check_for_state_errors]
      end

      private

      def request_builder_args
        { transaction_id: transaction_id }
      end

      def request_builder_class
        CzechPostB2bClient::RequestBuilders::GetResultParcelsBuilder
      end

      def api_caller_class
        CzechPostB2bClient::Services::ApiCaller
      end

      def response_parser_class
        CzechPostB2bClient::ResponseParsers::GetResultParcelsParser
      end

      def endpoint_path
        '/getResultParcels'
      end

      def build_result_from(response_hash)
        OpenStruct.new(parcels_hash: response_hash[:parcels],
                       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)

        collect_parcel_errors

        fail! unless r_code.info?
      end

      def collect_parcel_errors
        result.parcels_hash.each_pair do |parcel_id, parcel_hash|
          add_errors_for_failed_states(parcel_id, parcel_hash[:states])
        end
      end

      def add_errors_for_failed_states(parcel_id, response_states)
        response_states.each do |response_state|
          response_code = response_state[:code]
          next if response_code == CzechPostB2bClient::ResponseCodes::Ok.code

          errors.add(:parcels, "Parcel[#{parcel_id}] => #{CzechPostB2bClient::ResponseCodes.new_by_code(response_code)}")
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
czech_post_b2b_client-1.2.7 lib/czech_post_b2b_client/services/parcels_send_process_updater.rb
czech_post_b2b_client-1.2.6 lib/czech_post_b2b_client/services/parcels_send_process_updater.rb
czech_post_b2b_client-1.2.5 lib/czech_post_b2b_client/services/parcels_send_process_updater.rb
czech_post_b2b_client-1.0.2 lib/czech_post_b2b_client/services/parcels_send_process_updater.rb