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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FormatParams.



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

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.



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

def parcels
  @parcels
end

#receiverObject

Returns the value of attribute receiver.



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

def receiver
  @receiver
end

#reference_codeObject

Returns the value of attribute reference_code.



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

def reference_code
  @reference_code
end

#senderObject

Returns the value of attribute sender.



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

def sender
  @sender
end

Instance Method Details

#executeObject



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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/deliveries/couriers/spring/shipments/create/format_params.rb', line 16

def execute
  shipment = {
    ShipperReference: reference_code,
    Service: Deliveries::Couriers::Spring.config("countries.#{receiver.country.downcase.to_sym}.service")
  }

  # Consignor address (from).
  shipment[:ConsignorAddress] = {
    Name: sender.name,
    Company: sender.name,
    AddressLine1: sender.street,
    AddressLine2: sender.street2,
    AddressLine3: sender.street3,
    City: sender.city,
    State: sender.state,
    Zip: sender.postcode,
    Country: sender.country,
    Phone: sender.phone,
    Email: sender.email
  }

  # Consignee address (to).
  shipment[:ConsigneeAddress] = {
    Name: receiver.name,
    Company: '',
    AddressLine1: receiver.street,
    AddressLine2: receiver.street2,
    AddressLine3: receiver.street3,
    City: receiver.city,
    State: receiver.state,
    Zip: receiver.postcode,
    Country: receiver.country,
    Phone: receiver.phone,
    Email: receiver.email
  }

  # Products collection.
  shipment[:Products] = 1.upto(parcels).map do
    {
      Description: Deliveries::Couriers::Spring.config('default_product.description'),
      Sku: '',
      HsCode: Deliveries::Couriers::Spring.config('default_product.hs_code'),
      OriginCountry: Deliveries::Couriers::Spring.config('default_product.origin_country'),
      PurchaseUrl: '',
      Quantity: Deliveries::Couriers::Spring.config('default_product.quantity'),
      Value: Deliveries::Couriers::Spring.config('default_product.value')
    }
  end

  params = {
    Apikey: Deliveries::Couriers::Spring.config(:api_key),
    Shipment: shipment
  }

  Defaults::PARAMS.deep_merge(params)
end