Class: CzechPostB2bClient::ResponseParsers::GetParcelStateParser

Inherits:
BaseParser show all
Defined in:
lib/czech_post_b2b_client/response_parsers/get_parcel_state_parser.rb

Instance Attribute Summary

Attributes inherited from SteppedService::Base

#result

Instance Method Summary collapse

Methods inherited from BaseParser

#initialize, #steps

Methods inherited from SteppedService::Base

call, #call, #errors, #failure?, #finished?, #steps, #success?

Constructor Details

This class inherits a constructor from CzechPostB2bClient::ResponseParsers::BaseParser

Instance Method Details

#build_resultObject



6
7
8
9
# File 'lib/czech_post_b2b_client/response_parsers/get_parcel_state_parser.rb', line 6

def build_result
  super
  @result[:parcels] = parcels_data_hash
end

#parcel_data_from(rp_hash) ⇒ Object

rubocop:disable Metrics/AbcSize



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/czech_post_b2b_client/response_parsers/get_parcel_state_parser.rb', line 27

def parcel_data_from(rp_hash) # rubocop:disable Metrics/AbcSize
  {
    parcel_type: rp_hash['parcelType'].to_s,
    weight_in_kg: rp_hash['weight'].nil? ? nil : rp_hash['weight'].to_f,
    cash_on_delivery: { amount: (rp_hash['amount'] || 0).to_f,
                        currency_iso_code: rp_hash['currency'].to_s },
    pieces: (rp_hash['quantityParcel'] || 1).to_i,
    deposited_until: rp_hash['depositTo'].nil? ? nil : Date.parse(rp_hash['depositTo']),
    deposited_for_days: rp_hash['timeDeposit'].nil? ? nil : rp_hash['timeDeposit'].to_i,
    country_of_origin: rp_hash['countryOfOrigin'].to_s,
    country_of_destination: rp_hash['countryOfDestination'].to_s,
    states: states_array_from(rp_hash)
  }
end

#parcels_data_hashObject



11
12
13
14
15
# File 'lib/czech_post_b2b_client/response_parsers/get_parcel_state_parser.rb', line 11

def parcels_data_hash
  response_parcel_hashes.each_with_object({}) do |rp_hash, result|
    result[rp_hash['idParcel']] = parcel_data_from(rp_hash)
  end
end

#response_parcel_hashesObject



21
22
23
24
25
# File 'lib/czech_post_b2b_client/response_parsers/get_parcel_state_parser.rb', line 21

def response_parcel_hashes
  return [] if response_root_node.nil?

  [response_root_node.dig('parcel')].flatten.compact # to always get array of hash(es)
end

#response_root_node_nameObject



17
18
19
# File 'lib/czech_post_b2b_client/response_parsers/get_parcel_state_parser.rb', line 17

def response_root_node_name
  'getParcelStateResponse'
end

#states_array_from(rp_hash) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/czech_post_b2b_client/response_parsers/get_parcel_state_parser.rb', line 42

def states_array_from(rp_hash)
  rp_states = [rp_hash.dig('states', 'state')].flatten.compact # to always get (empty?) array of hash(es)

  rp_states.collect do |rp_state_hash|
    { id: rp_state_hash['id'].to_s,
      date: Date.parse(rp_state_hash['date']),
      text: rp_state_hash['text'],
      post_code: rp_state_hash['postCode'],
      post_name: rp_state_hash['name'] }
  end
end