Class: ApiPostcodeNl::API
- Inherits:
-
Object
- Object
- ApiPostcodeNl::API
- Defined in:
- lib/api_postcode_nl/api.rb
Constant Summary collapse
- BASE_URL =
"https://api.postcode.nl/rest/addresses"
- NIL_RESULT_CODE =
"api_postcode_nl_NIL_RESULT_CODE"
Class Method Summary collapse
- .address(postcode, house_number, house_number_addition = nil) ⇒ Object
- .cache ⇒ Object
- .cache=(cache) ⇒ Object
- .cache_key(postcode, house_number, house_number_addition) ⇒ Object
- .get_url(postcode, house_number, house_number_addition = nil) ⇒ Object
- .handle_errors(result) ⇒ Object
- .key ⇒ Object
- .key=(value) ⇒ Object
- .parse(response) ⇒ Object
- .secret ⇒ Object
- .secret=(value) ⇒ Object
- .send(postcode, house_number, house_number_addition = nil) ⇒ Object
Class Method Details
.address(postcode, house_number, house_number_addition = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/api_postcode_nl/api.rb', line 66 def address(postcode, house_number, house_number_addition = nil) if house_number_addition.blank? && house_number # attempt to get a house_number_addition from the house number house_number = house_number.to_s number = house_number.split(/[^0-9]/)[0] house_number_addition = house_number.sub(number, "") house_number = number end if postcode.blank? || house_number.blank? return nil end key = cache_key(postcode, house_number, house_number_addition) if cache && result = cache.read(key) return nil if result == NIL_RESULT_CODE return result end result = parse(send(postcode, house_number, house_number_addition)) cache.write(key, result ? result : NIL_RESULT_CODE, :expires_in => 1.week) if cache result end |
.cache ⇒ Object
96 97 98 |
# File 'lib/api_postcode_nl/api.rb', line 96 def cache @@cache ||= nil end |
.cache=(cache) ⇒ Object
92 93 94 |
# File 'lib/api_postcode_nl/api.rb', line 92 def cache=(cache) @@cache = cache end |
.cache_key(postcode, house_number, house_number_addition) ⇒ Object
62 63 64 |
# File 'lib/api_postcode_nl/api.rb', line 62 def cache_key(postcode, house_number, house_number_addition) "api_postcode_nl_#{postcode.to_s.downcase}_#{house_number.to_s.downcase}_#{house_number_addition.to_s.downcase}" end |
.get_url(postcode, house_number, house_number_addition = nil) ⇒ Object
13 14 15 16 |
# File 'lib/api_postcode_nl/api.rb', line 13 def get_url(postcode, house_number, house_number_addition = nil) postcode, house_number, house_number_addition = [postcode, house_number, house_number_addition].map{|c| c.to_s.delete(" ")} "#{BASE_URL}/#{postcode}/#{house_number}/#{house_number_addition}" end |
.handle_errors(result) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/api_postcode_nl/api.rb', line 33 def handle_errors(result) if result.is_a?(Net::HTTPNotFound) raise ApiPostcodeNl::InvalidPostcodeException, JSON.parse(result.body)["exception"] end #puts result.code.class #puts result.message #puts result.body end |
.key ⇒ Object
100 101 102 |
# File 'lib/api_postcode_nl/api.rb', line 100 def key @@key end |
.key=(value) ⇒ Object
104 105 106 |
# File 'lib/api_postcode_nl/api.rb', line 104 def key=(value) @@key = value end |
.parse(response) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/api_postcode_nl/api.rb', line 42 def parse(response) result = {} parsed_response = JSON.parse(response) { street_name: parsed_response["street"], house_number: parsed_response["houseNumber"], postcode: parsed_response["postcode"], city: parsed_response["city"], municipality: parsed_response["municipality"], province: parsed_response["province"], latitude: parsed_response["latitude"], longitude: parsed_response["longitude"], address_type: parsed_response["addressType"], purpose: parsed_response["purpose"], area: parsed_response["surfaceArea"], house_number_additions: parsed_response["houseNumberAdditions"], house_number_addition: parsed_response["houseNumberAddition"] } end |
.secret ⇒ Object
108 109 110 |
# File 'lib/api_postcode_nl/api.rb', line 108 def secret @@secret end |
.secret=(value) ⇒ Object
112 113 114 |
# File 'lib/api_postcode_nl/api.rb', line 112 def secret=(value) @@secret = value end |
.send(postcode, house_number, house_number_addition = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/api_postcode_nl/api.rb', line 18 def send(postcode, house_number, house_number_addition = nil) uri = URI.parse(get_url(postcode, house_number, house_number_addition)) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Get.new(uri.path) request.basic_auth key, secret result = http.start {|http| http.request(request) } handle_errors(result) result.body end |