Class: Deliveries::Couriers::Envialia::Labels::Generate

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Authentication

#login_body, #login_endpoint, #login_headers, #session_id

Constructor Details

#initialize(tracking_codes:) ⇒ Generate

Returns a new instance of Generate.



13
14
15
# File 'lib/deliveries/couriers/envialia/labels/generate.rb', line 13

def initialize(tracking_codes:)
  self.tracking_codes = tracking_codes.respond_to?(:each) ? tracking_codes : [tracking_codes]
end

Instance Attribute Details

#tracking_codesObject

Returns the value of attribute tracking_codes.



11
12
13
# File 'lib/deliveries/couriers/envialia/labels/generate.rb', line 11

def tracking_codes
  @tracking_codes
end

Instance Method Details

#executeObject



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
# File 'lib/deliveries/couriers/envialia/labels/generate.rb', line 17

def execute
  decoded_labels = []

  tracking_codes.each do |tracking_code|
    response = self.class.post(
      api_endpoint,
      body: body(tracking_code),
      headers: headers,
      debug_output: Deliveries.debug ? Deliveries.logger : nil
    )

    raise Deliveries::ClientError unless response.success?

    labels = response.dig('Envelope', 'Body', 'WebServService___ConsEtiquetaEnvio6Response', 'strEtiquetas')

    if labels.blank?
      raise Deliveries::APIError.new(
        'No hay etiqutas disponibles',
        404
      )
    else
      decoded_labels << Base64.decode64(labels).force_encoding('binary')
    end
  end

  decoded_labels
end