Class: Deliveries::Couriers::CorreosExpress::CollectionPoints::Search

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/deliveries/couriers/correos_express/collection_points/search.rb,
lib/deliveries/couriers/correos_express/collection_points/search/format_response.rb

Defined Under Namespace

Classes: FormatResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(postcode:) ⇒ Search

Returns a new instance of Search.



12
13
14
# File 'lib/deliveries/couriers/correos_express/collection_points/search.rb', line 12

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

Instance Attribute Details

#postcodeObject

Returns the value of attribute postcode.



10
11
12
# File 'lib/deliveries/couriers/correos_express/collection_points/search.rb', line 10

def postcode
  @postcode
end

Instance Method Details

#executeObject



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
# 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