Class: DHL::Ecommerce::Label

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

Constant Summary collapse

FACILITIES =
{
  auburn: "USSEA1",
  compton: "USLAX1",
  denver: "USDEN1",
  edgewood: "USISP1",
  elkridge: "USBWI1",
  forest_park: "USATL1",
  franklin: "USBOS1",
  grand_prairie: "USDFW1",
  hebron: "USCVG1",
  melrose_park: "USORD1",
  memphis: "USMEM1",
  orlando: "USMCO1",
  phoenix: "USPHX1",
  salt_lake_city: "USSLC1",
  secaucus: "USEWR1",
  st_louis: "USSTL1",
  union_city: "USSFO1"
}
MAIL_TYPES =
{
  bound_printed_matter: 6,
  irregular_parcel: 2,
  machinable_parcel: 3,
  marketing_parcel_gte_6oz: 30,
  marketing_parcel_lt_6oz: 20,
  media_mail: 9,
  parcel_select_machinable: 7,
  parcel_select_nonmachinable: 8
}
SERVICES =
{
  delivery_confirmation: "DELCON",
  signature_confirmation: "SIGCON"
}
SERVICE_ENDORSEMENTS =
{
  address_service: 1,
  change_service: 3,
  forwarding_service: 2,
  return_service: 4
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Label

Returns a new instance of Label.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dhl/ecommerce/label.rb', line 101

def initialize(attributes = {})
  super attributes

  unless attributes.empty?
    if attributes[:mail]
      @id = attributes[:mail][:mailIdentifier] if attributes[:mail][:mailIdentifier]
      @weight = attributes[:mail][:weight] if attributes[:mail][:weight]
      @product_id = attributes[:mail][:product_id] if attributes[:mail][:product_id]
      @reference = attributes[:mail][:customer_reference] if attributes[:mail][:customer_reference]
      @batch = attributes[:mail][:batch_reference] if attributes[:mail][:batch_reference]
      @impb = attributes[:mail][:intelligent_mail_barcode] if attributes[:mail][:intelligent_mail_barcode]
      @customer_confirmation_number = attributes[:mail][:customer_confirmation_number] if attributes[:mail][:customer_confirmation_number]

      @services = :delivery_confirmation if attributes[:mail][:delivery_confirmation_flag] == '1'
      @services = :signature_confirmation if attributes[:mail][:signature_confirmation_flag] == '1'
    end

    if attributes[:pickup]
      @location_id = attributes[:pickup][:pickup] if attributes[:pickup][:pickup]
    end

    if attributes[:recipient]
      @consignee_address = StandardAddress.new attributes[:recipient]
    end

    if attributes[:events]
      @events = []

      attributes[:events][:event] = [attributes[:events][:event]] unless attributes[:events][:event].is_a? Array
      attributes[:events][:event].each do |event_attributes|
        event = TrackedEvent.new event_attributes
        event.instance_variable_set :@event, Event.new(event_attributes)

        @events << event
      end
    end
  end
end

Instance Attribute Details

#batchObject

Returns the value of attribute batch.



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

def batch
  @batch
end

#consignee_addressObject

Returns the value of attribute consignee_address.



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

def consignee_address
  @consignee_address
end

#customer_confirmation_numberObject

Returns the value of attribute customer_confirmation_number.



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

def customer_confirmation_number
  @customer_confirmation_number
end

#eventsObject (readonly)

Returns the value of attribute events.



5
6
7
# File 'lib/dhl/ecommerce/label.rb', line 5

def events
  @events
end

#expected_ship_dateObject

Returns the value of attribute expected_ship_date.



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

def expected_ship_date
  @expected_ship_date
end

#facilityObject

Returns the value of attribute facility.



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

def facility
  @facility
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/dhl/ecommerce/label.rb', line 5

def id
  @id
end

#impbObject (readonly)

Returns the value of attribute impb.



5
6
7
# File 'lib/dhl/ecommerce/label.rb', line 5

def impb
  @impb
end

#location_idObject

Returns the value of attribute location_id.



5
6
7
# File 'lib/dhl/ecommerce/label.rb', line 5

def location_id
  @location_id
end

#mail_typeObject

Returns the value of attribute mail_type.



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

def mail_type
  @mail_type
end

#product_idObject

Returns the value of attribute product_id.



5
6
7
# File 'lib/dhl/ecommerce/label.rb', line 5

def product_id
  @product_id
end

#referenceObject

Returns the value of attribute reference.



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

def reference
  @reference
end

#return_addressObject

Returns the value of attribute return_address.



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

def return_address
  @return_address
end

#serviceObject

Returns the value of attribute service.



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

def service
  @service
end

#service_endorsementObject

Returns the value of attribute service_endorsement.



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

def service_endorsement
  @service_endorsement
end

#service_typeObject (readonly)

Returns the value of attribute service_type.



5
6
7
# File 'lib/dhl/ecommerce/label.rb', line 5

def service_type
  @service_type
end

#weightObject

Returns the value of attribute weight.



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

def weight
  @weight
end

Class Method Details

.create(attributes) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/dhl/ecommerce/label.rb', line 82

def self.create(attributes)
  array = attributes.is_a? Array
  attributes = [attributes] unless array

  labels = self.create_in_batches attributes

  array ? labels : labels.first
end

.find(id) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/dhl/ecommerce/label.rb', line 91

def self.find(id)
  attributes = DHL::Ecommerce.request :get, "https://api.dhlglobalmail.com/v1/mailitems/track" do |request|
    request.params[:number] = id
  end

  attributes[:mail_items][:mail_item] = attributes[:mail_items][:mail_item].first if attributes[:mail_items][:mail_item].is_a? Array

  new attributes[:mail_items][:mail_item]
end

Instance Method Details

#fileObject



78
79
80
# File 'lib/dhl/ecommerce/label.rb', line 78

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

#locationObject



55
56
57
# File 'lib/dhl/ecommerce/label.rb', line 55

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

#location=(location) ⇒ Object



59
60
61
62
# File 'lib/dhl/ecommerce/label.rb', line 59

def location=(location)
  @location = nil if @location_id != location.id
  @location_id = location.id
end

#productObject



69
70
71
# File 'lib/dhl/ecommerce/label.rb', line 69

def product
  @product ||= DHL::Ecommerce::Product.find product_id
end

#product=(product) ⇒ Object



73
74
75
76
# File 'lib/dhl/ecommerce/label.rb', line 73

def product=(product)
  @product = nil if @product_id != product.id
  @product_id = product.id
end