Class: Deliveries::Couriers::CorreosExpress::Pickups::CutoffTime

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

Defined Under Namespace

Classes: FormatParams

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(country:, postcode:) ⇒ CutoffTime

Returns a new instance of CutoffTime.



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

def initialize(country:, postcode:)
  self.country = country
  self.postcode = postcode
end

Instance Attribute Details

#countryObject

Returns the value of attribute country.



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

def country
  @country
end

#postcodeObject

Returns the value of attribute postcode.



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

def postcode
  @postcode
end

Instance Method Details

#executeObject

Raises:



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

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

  params = FormatParams.new(
    country: country,
    postcode: postcode
  ).execute

  response = self.class.post(
    api_endpoint,
    basic_auth: auth,
    body: params,
    headers: headers,
    debug_output: Deliveries.debug ? Deliveries.logger : nil
  )
  raise Deliveries::Error unless response.success?

  parsed_response = JSON.parse(response.body, symbolize_names: true)
  if (parsed_response[:codError]).zero? && parsed_response[:horaCorte].present?
    parsed_response[:horaCorte]
  else
    raise Deliveries::APIError.new(
      parsed_response[:mensError],
      parsed_response[:codError]
    )
  end
end