Class: Deliveries::Couriers::CorreosExpress::Pickups::Create

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/deliveries/couriers/correos_express/pickups/create.rb,
lib/deliveries/couriers/correos_express/pickups/create/defaults.rb,
lib/deliveries/couriers/correos_express/pickups/create/format_params.rb

Defined Under Namespace

Modules: Defaults Classes: FormatParams

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:) ⇒ Create

Returns a new instance of Create.



12
13
14
# File 'lib/deliveries/couriers/correos_express/pickups/create.rb', line 12

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

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



10
11
12
# File 'lib/deliveries/couriers/correos_express/pickups/create.rb', line 10

def params
  @params
end

Instance Method Details

#executeObject

Raises:



16
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
# File 'lib/deliveries/couriers/correos_express/pickups/create.rb', line 16

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

  response = self.class.post(
    api_endpoint,
    basic_auth: auth,
    body: params,
    headers: headers,
    debug_output: Deliveries.debug ? Deliveries.logger : nil
  )
  raise ClientError, "Failed with status code #{response.code}" unless response.success?

  parsed_response = JSON.parse(response.body, symbolize_names: true)
  if parsed_response[:codigoRetorno]&.zero? && parsed_response[:numRecogida].present?
    parsed_response[:numRecogida]
  else
    exception_class =
      case parsed_response[:codigoRetorno]
      when 105 then InvalidDateError
      when 154 then InvalidTimeIntervalError
      else APIError
      end

    raise exception_class.new(
      parsed_response[:mensajeRetorno],
      parsed_response[:codigoRetorno]
    )
  end
end