class CzechPostB2bClient::ResponseParsers::ParcelServiceSyncParser

Public Instance Methods

build_result() click to toggle source
# File lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb, line 8
def build_result
  super
  @result[:response][:state] = state_hash_from(response_state_response)
  @result[:parcel] = parcel_data_hash
end
parcel_data_from(rp_hash) click to toggle source
# File lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb, line 45
def parcel_data_from(rp_hash)
  { parcel_code: rp_hash['parcelCode'],
    states: parcel_states_form(rp_hash.dig('parcelDataResponse')) }
end
parcel_data_hash() click to toggle source
# File lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb, line 14
def parcel_data_hash
  parcel_id = nil
  pdh = 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
  pdh[parcel_id].merge!(printings: print_data_from(response_print_hash))

  pdh
end
parcel_parcel_id_from(rp_hash) click to toggle source
# File lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb, line 41
def parcel_parcel_id_from(rp_hash)
  rp_hash['recordNumber'].to_s
end
parcel_states_form(data_responses) click to toggle source
# File lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb, line 50
def parcel_states_form(data_responses)
  [data_responses].flatten.collect { |data_response| state_hash_from(data_response) }
end
pdf_content_from(pdf_content_encoded) click to toggle source
# File lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb, line 61
def pdf_content_from(pdf_content_encoded)
  return nil if pdf_content_encoded.nil?

  ::Base64.decode64(pdf_content_encoded)
end
print_data_from(print_hash) click to toggle source
response_parcel_hashes() click to toggle source
# File lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb, line 33
def response_parcel_hashes
  [response_root_node.dig('responseHeader', 'resultParcelData')].flatten.compact # to always get array of hash(es)
end
response_print_hash() click to toggle source
# File lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb, line 37
def response_print_hash
  response_root_node.dig('responsePrintParams')
end
response_root_node_name() click to toggle source
# File lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb, line 25
def response_root_node_name
  'parcelServiceSyncResponse'
end
response_state_response() click to toggle source
# File lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb, line 29
def response_state_response
  response_root_node.dig('responseHeader', 'resultHeader')
end
updated_result_value_for(value, parcel_params_result_hash) click to toggle source
# File lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb, line 67
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