Sha256: 8e4e3f50b34e46ec7184b0bd56ec930a38ecf9f71060e20fc4d3708328a95678

Contents?: true

Size: 698 Bytes

Versions: 1

Compression:

Stored size: 698 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 = {})
      postcode = sanitize(postcode)
      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

    def sanitize(postcode)
      postcode.gsub(/\s+/, '').upcase
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
postcodeapi-1.1.0 lib/postcodeapi/api.rb