Class: Deliveries::Couriers::CorreosExpress::Shipments::Create

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/deliveries/couriers/correos_express/shipments/create.rb,
lib/deliveries/couriers/correos_express/shipments/create/defaults.rb,
lib/deliveries/couriers/correos_express/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:, collection_point:, parcels:, reference_code:, shipment_date:, remarks:) ⇒ Create

Returns a new instance of Create.



13
14
15
16
17
18
19
20
21
22
# File 'lib/deliveries/couriers/correos_express/shipments/create.rb', line 13

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

Instance Attribute Details

#collection_pointObject

Returns the value of attribute collection_point.



10
11
12
# File 'lib/deliveries/couriers/correos_express/shipments/create.rb', line 10

def collection_point
  @collection_point
end

#parcelsObject

Returns the value of attribute parcels.



10
11
12
# File 'lib/deliveries/couriers/correos_express/shipments/create.rb', line 10

def parcels
  @parcels
end

#receiverObject

Returns the value of attribute receiver.



10
11
12
# File 'lib/deliveries/couriers/correos_express/shipments/create.rb', line 10

def receiver
  @receiver
end

#reference_codeObject

Returns the value of attribute reference_code.



10
11
12
# File 'lib/deliveries/couriers/correos_express/shipments/create.rb', line 10

def reference_code
  @reference_code
end

#remarksObject

Returns the value of attribute remarks.



10
11
12
# File 'lib/deliveries/couriers/correos_express/shipments/create.rb', line 10

def remarks
  @remarks
end

#senderObject

Returns the value of attribute sender.



10
11
12
# File 'lib/deliveries/couriers/correos_express/shipments/create.rb', line 10

def sender
  @sender
end

#shipment_dateObject

Returns the value of attribute shipment_date.



10
11
12
# File 'lib/deliveries/couriers/correos_express/shipments/create.rb', line 10

def shipment_date
  @shipment_date
end

Instance Method Details

#executeObject



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/correos_express/shipments/create.rb', line 24

def execute
  auth = {
    username: CorreosExpress.config(:username),
    password: CorreosExpress.config(:password)
  }

  params = FormatParams.new(
    sender: sender.courierize(:correos_express),
    receiver: receiver.courierize(:correos_express),
    collection_point: collection_point,
    parcels: parcels,
    reference_code: reference_code,
    shipment_date: shipment_date,
    remarks: remarks
  ).execute

  response = self.class.post(
    api_endpoint,
    basic_auth: auth,
    body: params,
    headers: headers,
    debug_output: Deliveries.debug ? Deliveries.logger : nil
  )
  raise Deliveries::ClientError unless response.success?

  parsed_response = JSON.parse(response.body, symbolize_names: true)
  if parsed_response[:codigoRetorno].zero? && parsed_response.key?(:datosResultado)
    tracking_code = parsed_response[:datosResultado]
    decoded_label = decode_label(parsed_response.dig(:etiqueta, 0, :etiqueta1))
    label = Label.new(raw: decoded_label) if decoded_label

    Deliveries::Shipment.new(
      courier_id: 'correos_express',
      sender: sender,
      receiver: receiver,
      parcels: parcels,
      reference_code: reference_code,
      tracking_code: tracking_code,
      shipment_date: shipment_date,
      label: label
    )
  else
    raise Deliveries::APIError.new(
      parsed_response[:mensajeRetorno],
      parsed_response[:codigoRetorno]
    )
  end
end