class Deliveries::Couriers::CorreosExpress::Pickups::CutoffTime

Attributes

country[RW]
postcode[RW]

Public Class Methods

new(country:, postcode:) click to toggle source
# File lib/deliveries/couriers/correos_express/pickups/cutoff_time.rb, line 12
def initialize(country:, postcode:)
  self.country = country
  self.postcode = postcode
end

Public Instance Methods

execute() click to toggle source
# 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

Private Instance Methods

api_endpoint() click to toggle source
# File lib/deliveries/couriers/correos_express/pickups/cutoff_time.rb, line 50
def api_endpoint
  CorreosExpress::CUTOFF_TIME_ENDPOINT_LIVE
end
headers() click to toggle source
# File lib/deliveries/couriers/correos_express/pickups/cutoff_time.rb, line 54
def headers
  { 'Content-Type' => 'application/json' }
end