Class: Deliveries::Couriers::CorreosExpress::Shipments::Trace

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/deliveries/couriers/correos_express/shipments/trace.rb,
lib/deliveries/couriers/correos_express/shipments/trace/format_response.rb

Defined Under Namespace

Classes: FormatResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracking_code:) ⇒ Trace

Returns a new instance of Trace.



13
14
15
# File 'lib/deliveries/couriers/correos_express/shipments/trace.rb', line 13

def initialize(tracking_code:)
  self.tracking_code = tracking_code
end

Instance Attribute Details

#tracking_codeObject

Returns the value of attribute tracking_code.



11
12
13
# File 'lib/deliveries/couriers/correos_express/shipments/trace.rb', line 11

def tracking_code
  @tracking_code
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
44
45
46
47
48
49
50
# File 'lib/deliveries/couriers/correos_express/shipments/trace.rb', line 17

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

  xml = "<?xml version='1.0'?>
    <SeguimientoEnviosRequest xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xsi:noNamespaceSchemaLocation='SeguimientoEnviosRequest.xsd'>
                      <Solicitante>#{solicitante}</Solicitante>
                      <Dato>#{tracking_code}</Dato>
    </SeguimientoEnviosRequest>"

  response = self.class.post(
    api_endpoint,
    basic_auth: auth,
    body: xml,
    headers: { 'Content-Type' => 'application/xml' },
    debug_output: Deliveries.debug ? Deliveries.logger : nil
  )

  raise Deliveries::ClientError unless response.success?

  result = Hash.from_xml response.force_encoding(Encoding::ISO_8859_1).encode(Encoding::UTF_8)
  if result['SeguimientoEnviosResponse']['Error'] == '0'
    result['SeguimientoEnviosResponse']
  else
    raise Deliveries::APIError.new(
      result['SeguimientoEnviosResponse']['MensajeError'],
      result['SeguimientoEnviosResponse']['Error']
    )
  end
end