Class: Postcode::API
- Inherits:
-
Object
- Object
- Postcode::API
- Defined in:
- lib/postcodeapi/api.rb
Overview
You’re required to sign up for an api key at www.postcodeapi.nu/
Constant Summary collapse
- BASE_URI =
'https://postcode-api.apiwise.nl'
Instance Method Summary collapse
-
#addresses(postcode, house_number = nil, options = {}) ⇒ Object
Returns an address list for a given postcode with all raw data that the api disposes.
-
#initialize(api_key) ⇒ API
constructor
A new instance of API.
-
#simple_addresses(postcode, house_number = nil, options = {}) ⇒ Object
Returns simplified addresses for a given postcode with only essential filtered data.
Constructor Details
#initialize(api_key) ⇒ API
Returns a new instance of API.
6 7 8 |
# File 'lib/postcodeapi/api.rb', line 6 def initialize(api_key) @api_key = api_key end |
Instance Method Details
#addresses(postcode, house_number = nil, options = {}) ⇒ Object
Returns an address list for a given postcode with all raw data that the api disposes
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/postcodeapi/api.rb', line 11 def addresses(postcode, house_number = nil, = {}) postcode = sanitize(postcode) uri = URI.parse([BASE_URI, 'v2', 'addresses'].compact.join('/')) uri.query = URI.encode_www_form( {'postcode' => postcode, 'number' => house_number} ) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(uri.request_uri) request.add_field('X-Api-Key', @api_key) response = http.request(request) content = Hashie::Mash.new(JSON.parse(response.body)) return content if content.error content. end |
#simple_addresses(postcode, house_number = nil, options = {}) ⇒ Object
Returns simplified addresses for a given postcode with only essential filtered data
31 32 33 34 35 36 |
# File 'lib/postcodeapi/api.rb', line 31 def simple_addresses(postcode, house_number = nil, = {}) content = addresses(postcode, house_number, ) return content if content.error simplify(content) end |