Class: Deliveries::Couriers::Envialia::Shipments::Trace

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

Defined Under Namespace

Classes: FormatResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Authentication

#login_body, #login_endpoint, #login_headers, #session_id

Constructor Details

#initialize(tracking_code:) ⇒ Trace

Returns a new instance of Trace.



13
14
15
# File 'lib/deliveries/couriers/envialia/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/envialia/shipments/trace.rb', line 11

def tracking_code
  @tracking_code
end

Instance Method Details

#api_endpointObject



60
61
62
63
64
65
66
# File 'lib/deliveries/couriers/envialia/shipments/trace.rb', line 60

def api_endpoint
  if Envialia.live?
    Envialia::ENVIALIA_ENDPOINT_LIVE
  else
    Envialia::ENVIALIA_ENDPOINT_TEST
  end
end

#bodyObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/deliveries/couriers/envialia/shipments/trace.rb', line 37

def body
  <<~XML
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:Header>
        <tns:ROClientIDHeader xmlns:tns="http://tempuri.org/">
          <tns:ID>#{session_id}</tns:ID>
        </tns:ROClientIDHeader>
      </soapenv:Header>
      <soapenv:Body>
        <tns:WebServService___ConsEnvEstados xmlns:tns="http://tempuri.org/">
          <tns:strCodAgeCargo>#{Deliveries.courier(:envialia).config(:agency_code)}</tns:strCodAgeCargo>
          <tns:strCodAgeOri>#{Deliveries.courier(:envialia).config(:agency_code)}</tns:strCodAgeOri>
          <tns:strAlbaran>#{tracking_code}</tns:strAlbaran>
        </tns:WebServService___ConsEnvEstados>
      </soapenv:Body>
    </soapenv:Envelope>
  XML
end

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/deliveries/couriers/envialia/shipments/trace.rb', line 17

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?

  if response.dig('Envelope', 'Body', 'WebServService___ConsEnvEstadosResponse', 'strEnvEstados').nil?
    raise Deliveries::APIError.new(
      'No se han encontrado datos para este envĂ­o',
      '402'
    )
  else
    response
  end
end

#headersObject



56
57
58
# File 'lib/deliveries/couriers/envialia/shipments/trace.rb', line 56

def headers
  { 'Content-Type' => 'application/json;charset=UTF-8', 'Accept' => 'application/json' }
end