-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi.rb
More file actions
121 lines (97 loc) · 3.19 KB
/
api.rb
File metadata and controls
121 lines (97 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#encoding: utf-8
require 'net/http'
require 'net/https'
require 'json'
require 'active_support'
require 'active_support/core_ext/object'
require 'api_postcode_nl/invalid_postcode_exception'
module ApiPostcodeNl
class API
class Fetcher
def fetch(uri, key, secret)
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)
}
result
end
end
BASE_URL = "https://api.postcode.nl/rest/addresses"
NIL_RESULT_CODE = "api_postcode_nl_NIL_RESULT_CODE"
class << self
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
def fetch(postcode, house_number, house_number_addition = nil)
uri = URI.parse(get_url(postcode, house_number, house_number_addition))
result = fetcher.fetch(uri, key, secret)
handle_errors(result)
result.body
end
def handle_errors(result)
if result.is_a?(Net::HTTPNotFound)
raise ApiPostcodeNl::InvalidPostcodeException, JSON.parse(result.body)["exception"]
end
end
def parse(response)
parsed_response = JSON.parse(response)
parsed_response.map do |k, v|
[k.underscore, v]
end.to_h.symbolize_keys
end
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
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]
if number
house_number_addition = house_number.sub(number, "")
house_number = number
end
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(fetch(postcode, house_number, house_number_addition))
cache.write(key, result ? result : NIL_RESULT_CODE, :expires_in => 1.week) if cache
result
end
def cache=(cache)
@@cache = cache
end
def cache
@@cache ||= nil
end
def fetcher=(fetcher)
@@fetcher = fetcher
end
def fetcher
@@fetcher ||= Fetcher.new
end
def key
@@key
end
def key=(value)
@@key = value
end
def secret
@@secret
end
def secret=(value)
@@secret = value
end
end
end
end