class Deliveries::Couriers::CorreosExpress::CollectionPoints::Search

Attributes

postcode[RW]

Public Class Methods

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

Public Instance Methods

execute() click to toggle source
# File lib/deliveries/couriers/correos_express/collection_points/search.rb, line 16
def execute
  auth = {
    username: CorreosExpress.config(:username),
    password: CorreosExpress.config(:password)
  }

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

  response = self.class.post(
    api_endpoint,
    basic_auth: auth,
    body: { cod_postal: postcode }.to_json,
    headers: headers,
    debug_output: Deliveries.debug ? Deliveries.logger : nil
  )
  parsed_response = JSON.parse(response.body)

  if parsed_response['tipoRespuesta'] == 'KO'
    raise Deliveries::APIError.new(
      parsed_response['listaErrores'].first['descError'],
      parsed_response['listaErrores'].first['codError']
    )
  end

  parsed_response['oficinas']
end

Private Instance Methods

api_endpoint() click to toggle source
# File lib/deliveries/couriers/correos_express/collection_points/search.rb, line 45
def api_endpoint
  if CorreosExpress.live?
    CorreosExpress::COLLECTION_POINTS_ENDPOINT_LIVE
  else
    CorreosExpress::COLLECTION_POINTS_ENDPOINT_TEST
  end
end