Class: Deliveries::Couriers::Envialia::Pickups::Create

Inherits:
Object
  • Object
show all
Includes:
Authentication, HTTParty
Defined in:
lib/deliveries/couriers/envialia/pickups/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:, parcels:, reference_code:, pickup_date:, remarks:, tracking_code:) ⇒ Create

Returns a new instance of Create.



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

def initialize(sender:, receiver:, parcels:,
               reference_code:, pickup_date:, remarks:, tracking_code:)

  self.sender = sender
  self.receiver = receiver
  self.parcels = parcels
  self.reference_code = reference_code
  self.pickup_date = pickup_date
  self.remarks = remarks
  self.tracking_code = tracking_code
end

Instance Attribute Details

#parcelsObject

Returns the value of attribute parcels.



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

def parcels
  @parcels
end

#pickup_dateObject

Returns the value of attribute pickup_date.



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

def pickup_date
  @pickup_date
end

#receiverObject

Returns the value of attribute receiver.



11
12
13
# File 'lib/deliveries/couriers/envialia/pickups/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/pickups/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/pickups/create.rb', line 11

def remarks
  @remarks
end

#senderObject

Returns the value of attribute sender.



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

def sender
  @sender
end

#tracking_codeObject

Returns the value of attribute tracking_code.



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

def tracking_code
  @tracking_code
end

Instance Method Details

#executeObject



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/pickups/create.rb', line 26

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?

  pickup_number = response.dig('Envelope', 'Body', 'WebServService___GrabaRecogida3Response', 'strCodOut')

  if pickup_number
    Deliveries::Pickup.new(
      courier_id: 'envialia',
      sender: sender,
      receiver: receiver,
      parcels: parcels,
      reference_code: reference_code,
      tracking_code: pickup_number,
      pickup_date: pickup_date
    )
  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