@@ -89,37 +89,49 @@ address_to = {
8989 :email => 'hippo@goshippo.com' }
9090
9191# Create parcel object
92- parcel = {
92+ parcel1 = {
9393 :length => 5 ,
9494 :width => 2 ,
9595 :height => 5 ,
9696 :distance_unit => :in ,
9797 :weight => 2 ,
9898 :mass_unit => :lb }
99+ parcel2 = {
100+ :length => 4 ,
101+ :width => 8 ,
102+ :height => 4 ,
103+ :distance_unit => :in ,
104+ :weight => 6 ,
105+ :mass_unit => :lb }
106+
99107
100- hash = { :default_carrier_account => DEFAULT_CARRIER_ACCOUNT ,
101- :default_servicelevel_token => 'usps_priority' ,
102- :label_filetype => 'ZPLII' ,
103- :metadata => 'BATCH #170' ,
104- :batch_shipments => [
105- {
106- :shipment => {
107- :address_from => address_from ,
108- :address_to => address_to ,
109- :parcel => parcel ,
110- :async => false
111- }
112- }
113- ]
114- }
115-
116- shipment_params = { :address_from => address_from ,
117- :address_to => address_to ,
118- :parcel => parcel ,
119- :async => false }
108+ shipment1 = { :address_from => address_from ,
109+ :address_to => address_to ,
110+ :parcels => [ parcel1 ] ,
111+ :async => false }
112+
113+ shipment2 = { :address_from => address_from ,
114+ :address_to => address_to ,
115+ :parcels => [ parcel2 ] ,
116+ :async => false }
120117
121118begin
122- batch = ExampleHelper . log_operation 'Making first API call to create a batch...' do
119+ shipment_1 = ExampleHelper . log_operation 'Making API call for shipment... ' do
120+ Shippo ::Shipment . create ( shipment1 )
121+ end
122+ # **** NOTE: Known issue with TEST API KEY and BATCH, must create shipment first
123+ hash = { :default_carrier_account => DEFAULT_CARRIER_ACCOUNT ,
124+ :default_servicelevel_token => 'usps_priority' ,
125+ :label_filetype => 'ZPLII' ,
126+ :metadata => 'BATCH #170' ,
127+ :batch_shipments => [
128+ {
129+ :shipment => shipment_1 [ :object_id ]
130+ }
131+ ]
132+ }
133+
134+ batch = ExampleHelper . log_operation 'Making API call to create a batch...' do
123135 Shippo ::Batch . create ( hash )
124136 end
125137
@@ -131,26 +143,26 @@ begin
131143 puts "Batch shipment count = #{ retrieve [ :batch_shipments ] [ :count ] } "
132144 puts
133145
134- shipment = ExampleHelper . log_operation 'Making API call to create a shipment... ' do
135- Shippo ::Shipment . create ( shipment_params )
146+ shipment_2 = ExampleHelper . log_operation 'Making API call to create a shipment... ' do
147+ Shippo ::Shipment . create ( shipment2 )
136148 end
137- raise Shippo ::Exceptions ::UnsuccessfulResponseError . new ( shipment . object . inspect ) unless shipment . success?
149+ raise Shippo ::Exceptions ::UnsuccessfulResponseError . new ( shipment_2 . object . inspect ) unless shipment_2 . success?
138150 File . open ( 'example-shipment.json' , 'w' ) do |file |
139- file . puts JSON . dump ( shipment . to_hash )
151+ file . puts JSON . dump ( shipment_2 . to_hash )
140152 end
141153
142154 # Adding shipments
143155 shipments = Array . new
144- shipments . push ( { "shipment" => shipment [ :object_id ] } )
156+ shipments . push ( { "shipment" => shipment_2 [ :object_id ] } )
145157 added = ExampleHelper . log_operation 'Making API call to add a new shipment to batch...' do
146158 Shippo ::Batch ::add_shipment ( retrieve [ :object_id ] , shipments )
147159 end
148160 puts "Batch shipment count = #{ added [ :batch_shipments ] [ :count ] } "
149161 puts
150-
162+
151163 # Removing shipments
152164 shipments_to_remove = Array . new
153- shipments_to_remove . push ( added . batch_shipments . results [ 0 ] [ :object_id ] )
165+ shipments_to_remove . push ( added . batch_shipments . results . last [ :object_id ] )
154166 removed = ExampleHelper . log_operation 'Making API call to remove the new shipment from batch...' do
155167 Shippo ::Batch ::remove_shipment ( retrieve [ :object_id ] , shipments_to_remove )
156168 end
0 commit comments