class InternationalPostcodeApi::Client

Public Class Methods

autocomplete(term, context = 'nld') click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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') click to toggle source
# 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_countries() click to toggle source
# File lib/international_postcode_api/client.rb, line 21
def self.supported_countries
  uri = generate_uri('supported-countries')
  fetch(uri)
end

Private Class Methods

fetch(uri) click to toggle source
# File lib/international_postcode_api/client.rb, line 47
def self.fetch(uri)
  config = InternationalPostcodeApi.configuration

  req = Net::HTTP::Get.new(uri)
  req['X-Autocomplete-Session'] = config.session_token
  req.basic_auth config.api_key, config.secret_key

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request req
  end

  JSON.parse(response.body)
end
generate_uri(*path) click to toggle source
# File lib/international_postcode_api/client.rb, line 42
def self.generate_uri(*path)
  encoded_uri = URI.encode(InternationalPostcodeApi.configuration.base_uri + path.join('/'))
  URI.parse encoded_uri
end