Class: Postcode::API

Inherits:
Object
  • Object
show all
Defined in:
lib/postcodeapi/api.rb

Overview

You’re required to sign up for an api key at postcodeapi.nu

Constant Summary collapse

BASE_URI =
"http://api.postcodeapi.nu"

Instance Method Summary collapse

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

#postcode(postcode, house_number = nil, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/postcodeapi/api.rb', line 10

def postcode(postcode, house_number = nil, options = {})
  uri = URI.parse([BASE_URI, postcode, house_number].compact.join('/'))

  req = Net::HTTP::Get.new(uri.path)
  req.add_field('Api-Key', @api_key)

  res = Net::HTTP.new(uri.host, uri.port).start do |http|
    http.request(req)
  end

  Hashie::Mash.new(JSON.parse(res.body))
end