Sha256: fe4e93f7cf2553fde413bd3cad59212f7bf8ece3b9e36e2a7ab7d190859376b6

Contents?: true

Size: 588 Bytes

Versions: 2

Compression:

Stored size: 588 Bytes

Contents

module Postcode
  # You're required to sign up for an api key at http://postcodeapi.nu
  class API
    BASE_URI = "http://api.postcodeapi.nu"

    def initialize(api_key)
      @api_key = api_key
    end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
postcodeapi-1.0.0 lib/postcodeapi/api.rb
postcodeapi-0.0.2 lib/postcodeapi/api.rb