From 0deaaaabf8c29d8cd3550d56fead3fba065bd1bf Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Mon, 1 Aug 2016 14:41:37 -0500 Subject: [PATCH] Add shipped labels to manifest to handle errors encountered when adding labels to a manifest --- lib/dhl/ecommerce/manifest.rb | 61 ++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/lib/dhl/ecommerce/manifest.rb b/lib/dhl/ecommerce/manifest.rb index 24009e1..9fcb5ad 100644 --- a/lib/dhl/ecommerce/manifest.rb +++ b/lib/dhl/ecommerce/manifest.rb @@ -1,7 +1,7 @@ module DHL module Ecommerce class Manifest < Base - attr_reader :id, :location_id + attr_reader :id, :location_id, :labels def location_id=(location_id) @location = nil @@ -20,30 +20,55 @@ def self.create(labels) labels.group_by(&:location_id).each.collect do |location_id, location_labels| closeout_id = DHL::Ecommerce.request :get, "https://api.dhlglobalmail.com/v1/#{DHL::Ecommerce::Location.resource_name.downcase}s/#{location_id}/closeout/id" - location_labels.each_slice(500) do |slice_labels| - xml = Builder::XmlMarkup.new - xml.instruct! :xml, version: "1.1", encoding: "UTF-8" + shipped_labels = add_labels_to_closeout(location_id, closeout_id, location_labels) - xml.ImpbList do - slice_labels.each do |label| - xml.Impb do - xml.Construct label.impb.construct - xml.Value label.impb.value - end + generate_manifest(location_id, closeout_id, shipped_labels) + end.flatten + end + + private + + def self.add_labels_to_closeout(location_id, closeout_id, labels) + shipped_labels = [] + labels.each_slice(500) do |slice_labels| + xml = Builder::XmlMarkup.new + xml.instruct! :xml, version: "1.1", encoding: "UTF-8" + + xml.ImpbList do + slice_labels.each do |label| + xml.Impb do + xml.Construct label.impb.construct + xml.Value label.impb.value end end + end - DHL::Ecommerce.request :post, "https://api.dhlglobalmail.com/v1/#{DHL::Ecommerce::Location.resource_name.downcase}s/#{location_id}/closeout/#{closeout_id}" do |request| - request.body = xml.target! - end + response_data = DHL::Ecommerce.request :post, "https://api.dhlglobalmail.com/v1/#{DHL::Ecommerce::Location.resource_name.downcase}s/#{location_id}/closeout/#{closeout_id}" do |request| + request.body = xml.target! end + shipped_labels << exclude_labels_with_errors(slice_labels, response_data) + end + shipped_labels.flatten + end + + def self.generate_manifest(location_id, closeout_id, labels) + response = DHL::Ecommerce.request :get, "https://api.dhlglobalmail.com/v1/#{DHL::Ecommerce::Location.resource_name.downcase}s/#{location_id}/closeout/#{closeout_id}" + response[:manifest_list][:manifest] = [response[:manifest_list][:manifest]] unless response[:manifest_list][:manifest].is_a? Array + response[:manifest_list][:manifest].each.collect do |attributes| + new attributes.merge(location_id: location_id, labels: labels) + end + end - response = DHL::Ecommerce.request :get, "https://api.dhlglobalmail.com/v1/#{DHL::Ecommerce::Location.resource_name.downcase}s/#{location_id}/closeout/#{closeout_id}" - response[:manifest_list][:manifest] = [response[:manifest_list][:manifest]] unless response[:manifest_list][:manifest].is_a? Array - response[:manifest_list][:manifest].each.collect do |attributes| - new attributes.merge(location_id: location_id) + def self.exclude_labels_with_errors(labels, response_data) + if response_data.error_list + impb_errors = response_data.error_list.impb_error + impb_errors = [impb_errors] unless impb_errors.is_a? Array + impb_errors.each do |error| + labels.delete_if { |label| label.impb.value == error.impb } end - end.flatten + end + + labels end end end