Class: InternationalPostcodeApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/international_postcode_api/client.rb

Class Method Summary collapse

Class Method Details

.autocomplete(term, context = 'nld') ⇒ Object



6
7
8
9
# File 'lib/international_postcode_api/client.rb', line 6

def self.autocomplete(term, context = 'nld')
  uri = generate_uri(:autocomplete, context.downcase, term)
  fetch(uri)
end

.details(context) ⇒ Object



11
12
13
14
# File 'lib/international_postcode_api/client.rb', line 11

def self.details(context)
  uri = generate_uri(:address, context)
  fetch(uri)
end

.dutch_postcode(postcode, house_number, house_number_addition = nil) ⇒ Object



16
17
18
19
# File 'lib/international_postcode_api/client.rb', line 16

def self.dutch_postcode(postcode, house_number, house_number_addition = nil)
  encoded_uri = URI.encode(['https://api.postcode.eu/nl/v1/addresses/postcode', postcode, house_number, house_number_addition].join('/'))
  fetch URI.parse(encoded_uri)
end

.postcode(postcode, house_number, lang = 'NLD') ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/international_postcode_api/client.rb', line 26

def self.postcode(postcode, house_number, lang = 'NLD')
  if lang == 'NLD' && InternationalPostcodeApi.configuration.dynamic_endpoints
    response = dutch_postcode(postcode, house_number)
    return { street: response.dig('street'), city: response.dig('city') }
  else
    query = autocomplete("#{postcode} #{house_number}", lang)
    context = query['matches'][0].dig('context')
    if context
      response = details(context)
      return { street: response.dig('address')&.dig('street'), city: response.dig('address')&.dig('locality')}
    end
  end
end

.supported_countriesObject



21
22
23
24
# File 'lib/international_postcode_api/client.rb', line 21

def self.supported_countries
  uri = generate_uri('supported-countries')
  fetch(uri)
end