Class: Deliveries::Couriers::Spring::Shipments::Create

Inherits:
Object
  • Object
show all
Defined in:
lib/deliveries/couriers/spring/shipments/create.rb,
lib/deliveries/couriers/spring/shipments/create/defaults.rb,
lib/deliveries/couriers/spring/shipments/create/format_params.rb

Defined Under Namespace

Modules: Defaults Classes: FormatParams

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sender:, receiver:, parcels:, reference_code:) ⇒ Create

Returns a new instance of Create.



8
9
10
11
12
13
# File 'lib/deliveries/couriers/spring/shipments/create.rb', line 8

def initialize(sender:, receiver:, parcels:, reference_code:)
  self.sender = sender
  self.receiver = receiver
  self.parcels = parcels
  self.reference_code = reference_code
end

Instance Attribute Details

#parcelsObject

Returns the value of attribute parcels.



6
7
8
# File 'lib/deliveries/couriers/spring/shipments/create.rb', line 6

def parcels
  @parcels
end

#receiverObject

Returns the value of attribute receiver.



6
7
8
# File 'lib/deliveries/couriers/spring/shipments/create.rb', line 6

def receiver
  @receiver
end

#reference_codeObject

Returns the value of attribute reference_code.



6
7
8
# File 'lib/deliveries/couriers/spring/shipments/create.rb', line 6

def reference_code
  @reference_code
end

#senderObject

Returns the value of attribute sender.



6
7
8
# File 'lib/deliveries/couriers/spring/shipments/create.rb', line 6

def sender
  @sender
end

Instance Method Details

#executeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/deliveries/couriers/spring/shipments/create.rb', line 15

def execute
  params = Deliveries::Couriers::Spring::Shipments::Create::FormatParams.new(
    sender: sender.courierize(:spring),
    receiver: receiver.courierize(:spring),
    parcels: parcels,
    reference_code: reference_code
  ).execute

  response = Deliveries::Couriers::Spring::Request.execute(params: params)

  tracking_code = response[:Shipment][:TrackingNumber]
  label = Label.new(
    url: response[:Shipment][:CarrierTrackingUrl],
    raw: Base64.decode64(response[:Shipment][:LabelImage]).force_encoding('binary')
  )

  Deliveries::Delivery.new(
    courier_id: Deliveries::Couriers::Spring::COURIER_ID,
    sender: sender,
    receiver: receiver,
    parcels: parcels,
    reference_code: reference_code,
    tracking_code: tracking_code,
    label: label
  )
end