Sha256: 79e23a9b3f0a2328387796c602e59b2a08977937067b4aaa1f1333608404b8c8

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module CzechPostB2bClient
  module ResponseParsers
    class GetResultParcelsParser < BaseParser
      def build_result
        super
        @result[:response][:state] = state_hash_from(response_state_response)
        @result[:parcels] = parcels_data_hash
      end

      def parcels_data_hash
        response_parcel_hashes.each_with_object({}) do |rp_hash, result|
          parcel_id = parcel_parcel_id_from(rp_hash)
          result[parcel_id] = updated_result_value_for(result[parcel_id], rp_hash)
        end
      end

      def response_root_node_name
        'getResultParcelsResponse'
      end

      def response_state_response
        response_root_node.dig('doParcelHeaderResult', 'doParcelStateResponse')
      end

      def response_parcel_hashes
        [response_root_node['doParcelParamResult']].flatten.compact # to always get array of hash(es)
      end

      def parcel_parcel_id_from(rp_hash)
        rp_hash['recordNumber'].to_s
      end

      def parcel_data_from(rp_hash)
        { parcel_code: rp_hash['parcelCode'],
          states: [state_hash_from(rp_hash['doParcelStateResponse'])] }
      end

      def updated_result_value_for(value, parcel_params_result_hash)
        pd_hash = parcel_data_from(parcel_params_result_hash)

        return pd_hash if value.nil?

        # merging states
        value[:states] = (value[:states] + pd_hash[:states]).sort { |a, b| a[:code] <=> b[:code] }

        # more parcel_codes for one parcel_id => probably errors
        old_p_code = value[:parcel_code]
        new_p_code = pd_hash[:parcel_code]
        raise "Two different parcel_codes [#{old_p_code}, #{new_p_code}] for parcel_id:'#{parcel_id}'" if old_p_code != new_p_code

        value
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
czech_post_b2b_client-1.4.0 lib/czech_post_b2b_client/response_parsers/get_result_parcels_parser.rb
czech_post_b2b_client-1.3.0 lib/czech_post_b2b_client/response_parsers/get_result_parcels_parser.rb
czech_post_b2b_client-1.2.8 lib/czech_post_b2b_client/response_parsers/get_result_parcels_parser.rb