api_postcode_nl 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "api_postcode_nl"
8
- s.version = "0.1.4"
8
+ s.version = "0.1.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["rhomeister"]
12
- s.date = "2013-01-14"
12
+ s.date = "2013-01-15"
13
13
  s.description = "Retrieve a Dutch address based on housenumber and postcode"
14
14
  s.email = "r.stranders@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -34,7 +34,7 @@ Gem::Specification.new do |s|
34
34
  s.homepage = "http://github.com/rhomeister/api_postcode_nl"
35
35
  s.licenses = ["MIT"]
36
36
  s.require_paths = ["lib"]
37
- s.rubygems_version = "1.8.24"
37
+ s.rubygems_version = "1.8.23"
38
38
  s.summary = "Ruby gem for interfacing with api.postcode.nl"
39
39
 
40
40
  if s.respond_to? :specification_version then
@@ -7,6 +7,7 @@ require 'api_postcode_nl/invalid_postcode_exception'
7
7
  module ApiPostcodeNl
8
8
  class API
9
9
  BASE_URL = "https://api.postcode.nl/rest/addresses"
10
+ NIL_RESULT_CODE = "api_postcode_nl_NIL_RESULT_CODE"
10
11
 
11
12
  class << self
12
13
  def get_url(postcode, house_number, house_number_addition = nil)
@@ -37,7 +38,7 @@ module ApiPostcodeNl
37
38
  #puts result.message
38
39
  #puts result.body
39
40
  end
40
-
41
+
41
42
  def parse(response)
42
43
  result = {}
43
44
  parsed_response = JSON.parse(response)
@@ -53,12 +54,47 @@ module ApiPostcodeNl
53
54
  address_type: parsed_response["addressType"],
54
55
  purpose: parsed_response["purpose"],
55
56
  area: parsed_response["surfaceArea"],
56
- house_number_additions: parsed_response["houseNumberAdditions"]
57
+ house_number_additions: parsed_response["houseNumberAdditions"],
58
+ house_number_addition: parsed_response["houseNumberAddition"]
57
59
  }
58
60
  end
61
+
62
+ def cache_key(postcode, house_number, house_number_addition)
63
+ "api_postcode_nl_#{postcode.to_s.downcase}_#{house_number.to_s.downcase}_#{house_number_addition.to_s.downcase}"
64
+ end
59
65
 
60
66
  def address(postcode, house_number, house_number_addition = nil)
61
- parse(send(postcode, house_number, house_number_addition))
67
+ if house_number_addition.blank? && house_number
68
+ # attempt to get a house_number_addition from the house number
69
+ house_number = house_number.to_s
70
+ number = house_number.split(/[^0-9]/)[0]
71
+ house_number_addition = house_number.sub(number, "")
72
+ house_number = number
73
+ end
74
+
75
+ if postcode.blank? || house_number.blank?
76
+ return nil
77
+ end
78
+
79
+ key = cache_key(postcode, house_number, house_number_addition)
80
+ if cache && result = cache.read(key)
81
+ return nil if result == NIL_RESULT_CODE
82
+ return result
83
+ end
84
+
85
+ result = parse(send(postcode, house_number, house_number_addition))
86
+
87
+ cache.write(key, result ? result : NIL_RESULT_CODE, :expires_in => 1.week) if cache
88
+
89
+ result
90
+ end
91
+
92
+ def cache=(cache)
93
+ @@cache = cache
94
+ end
95
+
96
+ def cache
97
+ @@cache ||= nil
62
98
  end
63
99
 
64
100
  def key
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_postcode_nl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-14 00:00:00.000000000 Z
12
+ date: 2013-01-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
@@ -79,7 +79,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
79
  version: '0'
80
80
  segments:
81
81
  - 0
82
- hash: -2919040409656760397
82
+ hash: -3644844763917679694
83
83
  required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  version: '0'
89
89
  requirements: []
90
90
  rubyforge_project:
91
- rubygems_version: 1.8.24
91
+ rubygems_version: 1.8.23
92
92
  signing_key:
93
93
  specification_version: 3
94
94
  summary: Ruby gem for interfacing with api.postcode.nl