Class: DHL::Ecommerce::Manifest

Inherits:
Base
  • Object
show all
Defined in:
lib/dhl/ecommerce/manifest.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from DHL::Ecommerce::Base

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/dhl/ecommerce/manifest.rb', line 4

def id
  @id
end

#location_idObject

Returns the value of attribute location_id.



4
5
6
# File 'lib/dhl/ecommerce/manifest.rb', line 4

def location_id
  @location_id
end

Class Method Details

.create(labels) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dhl/ecommerce/manifest.rb', line 19

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"

      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
    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)
    end
  end.flatten
end

Instance Method Details

#fileObject



15
16
17
# File 'lib/dhl/ecommerce/manifest.rb', line 15

def file
  @base64_decoded_file ||= StringIO.new(Base64.decode64(@file))
end

#locationObject



11
12
13
# File 'lib/dhl/ecommerce/manifest.rb', line 11

def location
  @location ||= DHL::Ecommerce::Location.find location_id
end