Class: CzechPostB2bClient::ResponseParsers::ParcelServiceSyncParser
- Inherits:
-
BaseParser
show all
- Defined in:
- lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb
Instance Attribute Summary
#result
Instance Method Summary
collapse
Methods inherited from BaseParser
#initialize, #steps
call, #call, #errors, #failure?, #finished?, #steps, #success?
Instance Method Details
#build_result ⇒ Object
8
9
10
11
12
|
# 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) ⇒ Object
45
46
47
48
|
# 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['parcelDataResponse']) }
end
|
#parcel_data_hash ⇒ Object
14
15
16
17
18
19
20
21
22
23
|
# 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) ⇒ Object
41
42
43
|
# 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
|
50
51
52
|
# 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) ⇒ Object
61
62
63
64
65
|
# 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) ⇒ Object
54
55
56
57
58
59
|
# File 'lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb', line 54
def print_data_from(print_hash)
return nil if print_hash.nil? || print_hash.empty?
{ pdf_content: pdf_content_from(print_hash['file']),
state: state_hash_from(print_hash['printParamsResponse']) }
end
|
#response_parcel_hashes ⇒ Object
33
34
35
|
# 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 end
|
#response_print_hash ⇒ Object
37
38
39
|
# File 'lib/czech_post_b2b_client/response_parsers/parcel_service_sync_parser.rb', line 37
def response_print_hash
response_root_node['responsePrintParams']
end
|
#response_root_node_name ⇒ Object
25
26
27
|
# 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 ⇒ Object
29
30
31
|
# 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) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# 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?
value[:states] = (value[:states] + pd_hash[:states]).sort { |a, b| a[:code] <=> b[:code] }
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
|