Class: Deliveries::Couriers::Envialia::Shipments::Create

Inherits:
Object
  • Object
show all
Includes:
Authentication, HTTParty
Defined in:
lib/deliveries/couriers/envialia/shipments/create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Authentication

#login_body, #login_endpoint, #login_headers, #session_id

Constructor Details

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

Returns a new instance of Create.



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

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.



11
12
13
# File 'lib/deliveries/couriers/envialia/shipments/create.rb', line 11

def collection_point
  @collection_point
end

#parcelsObject

Returns the value of attribute parcels.



11
12
13
# File 'lib/deliveries/couriers/envialia/shipments/create.rb', line 11

def parcels
  @parcels
end

#receiverObject

Returns the value of attribute receiver.



11
12
13
# File 'lib/deliveries/couriers/envialia/shipments/create.rb', line 11

def receiver
  @receiver
end

#reference_codeObject

Returns the value of attribute reference_code.



11
12
13
# File 'lib/deliveries/couriers/envialia/shipments/create.rb', line 11

def reference_code
  @reference_code
end

#remarksObject

Returns the value of attribute remarks.



11
12
13
# File 'lib/deliveries/couriers/envialia/shipments/create.rb', line 11

def remarks
  @remarks
end

#senderObject

Returns the value of attribute sender.



11
12
13
# File 'lib/deliveries/couriers/envialia/shipments/create.rb', line 11

def sender
  @sender
end

#shipment_dateObject

Returns the value of attribute shipment_date.



11
12
13
# File 'lib/deliveries/couriers/envialia/shipments/create.rb', line 11

def shipment_date
  @shipment_date
end

Instance Method Details

#executeObject



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
# File 'lib/deliveries/couriers/envialia/shipments/create.rb', line 25

def execute
  response = self.class.post(
    api_endpoint,
    body: body,
    headers: headers,
    debug_output: Deliveries.debug ? Deliveries.logger : nil
  )

  raise Deliveries::ClientError unless response.success?

  tracking_code = response.dig('Envelope', 'Body', 'WebServService___GrabaEnvio8Response', 'strAlbaranOut')

  if tracking_code
    Deliveries::Shipment.new(
      courier_id: 'envialia',
      sender: sender,
      receiver: receiver,
      parcels: parcels,
      reference_code: reference_code,
      tracking_code: tracking_code,
      shipment_date: shipment_date,
      label: nil
    )
  else
    exception = response.dig('Envelope', 'Body', 'Fault')

    if exception['faultcode'].eql?('Exception')
      exception_code, exception_str = exception['faultstring'].split(':')
    else
      exception_code = 400
      exception_str = exception['faultstring']
    end

    raise Deliveries::APIError.new(
      exception_str.strip,
      exception_code.to_i
    )
  end
end