addressfinder 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d2d3c4923e027e17a94375f9ace07771fc20644
4
- data.tar.gz: e17d6c8121c855971b817eaf1807805eeaa708b4
3
+ metadata.gz: 3e898a98cb65edda0b68b91cf7dd80f5b1678618
4
+ data.tar.gz: ae753e8e1f56f6aee705b7ee8f7fcb58d6063056
5
5
  SHA512:
6
- metadata.gz: 58ea4d86be9dc5cab93385a9482247a88d934c0582f9b246f47abef938d97fe2de6bed37c14e110604d448dcf5edf405e0afdbcab66be338fef16d2da3005a3e
7
- data.tar.gz: 3568aa198161346b55abe3e941e7ddf027134dcf54ec46e2bd6f46be4d1358ce356eb29e83a70e77776c9f6ef165ec5c973f6f40b5a37ea947940de4c16e9bb4
6
+ metadata.gz: 6bfb48dfc96843b4000dd91bd9df7e78d443fb271c753457b296bb8d1ddabe3944b4f27bad1b3ef8949a8058073a468189ae8d11116df5fda45b4dbcabb42f7f
7
+ data.tar.gz: 24efefe0cfc07c40148605f527d57591b7beff0122448e7e7e16fde443dd9f289d449e05e609b0d7301781adee93e3def00a5c939ff20dcde7a2b880325a926b
@@ -1,3 +1,8 @@
1
+ # AddressFinder 1.4.0 (October 19, 2015) #
2
+
3
+ * Add support for Address Search API
4
+ * Add support for Address Info API
5
+
1
6
  # AddressFinder 1.3.0 (October 16, 2015) #
2
7
 
3
8
  * Add optional domain parameter to configuration and API calls
data/README.md CHANGED
@@ -89,7 +89,7 @@ end
89
89
 
90
90
  See documentation on the available parameters and expected response here:
91
91
 
92
- https://addressfinder.nz/docs/address_info_api/
92
+ https://addressfinder.nz/docs/location_info_api/
93
93
 
94
94
  Usage example:
95
95
 
@@ -107,6 +107,50 @@ rescue AddressFinder::RequestRejectedError => e
107
107
  end
108
108
  ```
109
109
 
110
+ ### Address Search
111
+
112
+ See documentation on the available parameters and expected response here:
113
+
114
+ https://addressfinder.nz/docs/address_api/
115
+
116
+ Usage example:
117
+
118
+ ```ruby
119
+ begin
120
+ results = AddressFinder.address_search(q: '186 Willis Street')
121
+ if results.any?
122
+ $standout.puts "Success: #{results}"
123
+ else
124
+ $standout.puts "Sorry, there were no address matches"
125
+ end
126
+ rescue AddressFinder::RequestRejectedError => e
127
+ response = JSON.parse(e.body)
128
+ $standout.puts response['message']
129
+ end
130
+ ```
131
+
132
+ ### Address Info
133
+
134
+ See documentation on the available parameters and expected response here:
135
+
136
+ https://addressfinder.nz/docs/address_info_api/
137
+
138
+ Usage example:
139
+
140
+ ```ruby
141
+ begin
142
+ result = AddressFinder.address_info(pxid: '1-.B.3l')
143
+ if result
144
+ $standout.puts "Success: #{result.a}"
145
+ else
146
+ $standout.puts "Sorry, can't find that address"
147
+ end
148
+ rescue AddressFinder::RequestRejectedError => e
149
+ response = JSON.parse(e.body)
150
+ $standout.puts response['message']
151
+ end
152
+ ```
153
+
110
154
  ### Bulk Operations
111
155
 
112
156
  If you have a series of calls you need to make to AddressFinder, you can use the
@@ -5,6 +5,8 @@ require 'addressfinder/configuration'
5
5
  require 'addressfinder/cleanse'
6
6
  require 'addressfinder/location_info'
7
7
  require 'addressfinder/location_search'
8
+ require 'addressfinder/address_info'
9
+ require 'addressfinder/address_search'
8
10
  require 'addressfinder/bulk'
9
11
  require 'addressfinder/errors'
10
12
 
@@ -36,6 +38,14 @@ module AddressFinder
36
38
  AddressFinder::LocationInfo.new(params: args, http: configure_http).perform.result
37
39
  end
38
40
 
41
+ def address_search(args={})
42
+ AddressFinder::AddressSearch.new(params: args, http: configure_http).perform.results
43
+ end
44
+
45
+ def address_info(args={})
46
+ AddressFinder::AddressInfo.new(params: args, http: configure_http).perform.result
47
+ end
48
+
39
49
  def bulk(&block)
40
50
  # TODO include parameter http: configure_http
41
51
  AddressFinder::Bulk.new(&block).perform
@@ -0,0 +1,71 @@
1
+ require 'ostruct'
2
+
3
+ module AddressFinder
4
+ class AddressInfo
5
+
6
+ attr_reader :result
7
+
8
+ def initialize(params:, http:)
9
+ @http = http
10
+ @country = params.delete(:country) || config.default_country
11
+
12
+ @params = params
13
+ @params['key'] = config.api_key
14
+ @params['secret'] = config.api_secret
15
+ end
16
+
17
+ def perform
18
+ build_request
19
+ execute_request
20
+ build_result
21
+
22
+ self
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :request_uri, :params, :country, :http
28
+ attr_accessor :response_body, :response_status
29
+ attr_writer :result
30
+
31
+ def build_request
32
+ @request_uri = "/api/#{country}/address/info.json?#{encoded_params}"
33
+ end
34
+
35
+ def encoded_params
36
+ query = params.map{|k,v| "#{k}=#{v}"}.join('&')
37
+ URI::encode(query)
38
+ end
39
+
40
+ def execute_request
41
+ request = Net::HTTP::Get.new(request_uri)
42
+
43
+ response = http.request(request)
44
+
45
+ self.response_body = response.body
46
+ self.response_status = response.code
47
+ end
48
+
49
+ def build_result
50
+ case response_status
51
+ when '200'
52
+ self.result = Result.new(response_hash)
53
+ when '404'
54
+ raise AddressFinder::NotFoundError.new(@response_status, @response_body, @pxid)
55
+ else
56
+ raise AddressFinder::RequestRejectedError.new(@response_status, @response_body)
57
+ end
58
+ end
59
+
60
+ def response_hash
61
+ @_response_hash ||= MultiJson.load(response_body)
62
+ end
63
+
64
+ def config
65
+ @_config ||= AddressFinder.configuration
66
+ end
67
+
68
+ class Result < OpenStruct
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,71 @@
1
+ require 'ostruct'
2
+
3
+ module AddressFinder
4
+ class AddressSearch
5
+
6
+ attr_reader :results
7
+
8
+ def initialize(params:, http:)
9
+ @http = http
10
+ @country = params.delete(:country) || config.default_country
11
+
12
+ @params = params
13
+ @params['key'] = config.api_key
14
+ @params['secret'] = config.api_secret
15
+ end
16
+
17
+ def perform
18
+ build_request
19
+ execute_request
20
+ build_result
21
+
22
+ self
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :request_uri, :params, :country, :http
28
+ attr_accessor :response_body, :response_status
29
+ attr_writer :results
30
+
31
+ def build_request
32
+ @request_uri = "/api/#{country}/address.json?#{encoded_params}"
33
+ end
34
+
35
+ def encoded_params
36
+ query = params.map{|k,v| "#{k}=#{v}"}.join('&')
37
+ URI::encode(query)
38
+ end
39
+
40
+ def execute_request
41
+ request = Net::HTTP::Get.new(request_uri)
42
+
43
+ response = http.request(request)
44
+
45
+ self.response_body = response.body
46
+ self.response_status = response.code
47
+ end
48
+
49
+ def build_result
50
+ case response_status
51
+ when '200'
52
+ self.results = response_hash['completions'].map do |result_hash|
53
+ Result.new(result_hash)
54
+ end
55
+ else
56
+ raise AddressFinder::RequestRejectedError.new(@response_status, @response_body)
57
+ end
58
+ end
59
+
60
+ def response_hash
61
+ @_response_hash ||= MultiJson.load(response_body)
62
+ end
63
+
64
+ def config
65
+ @_config ||= AddressFinder.configuration
66
+ end
67
+
68
+ class Result < OpenStruct
69
+ end
70
+ end
71
+ end
@@ -60,8 +60,11 @@ module AddressFinder
60
60
  end
61
61
 
62
62
  def encoded_params
63
- query = params.map{|k,v| "#{k}=#{v}"}.join('&')
64
- URI::encode(query)
63
+ query_params = params.map do |k,v|
64
+ "#{k}=#{ERB::Util.url_encode(v)}"
65
+ end
66
+
67
+ query_params.join('&')
65
68
  end
66
69
 
67
70
  def response_hash
@@ -1,3 +1,3 @@
1
1
  module AddressFinder
2
- VERSION = '1.3.0'
2
+ VERSION = '1.4.0'
3
3
  end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe AddressFinder::AddressInfo do
4
+ before do
5
+ AddressFinder.configure do |af|
6
+ af.api_key = 'XXX'
7
+ af.api_secret = 'YYY'
8
+ af.default_country = 'au'
9
+ end
10
+ end
11
+
12
+ describe '#build_request' do
13
+ let(:locator){ AddressFinder::AddressInfo.new(params: args, http: http) }
14
+ let(:http){ AddressFinder.send(:configure_http) }
15
+
16
+ subject(:request_uri){ locator.send(:build_request) }
17
+
18
+ context 'with a simple PXID' do
19
+ let(:args){ {pxid: '123'} }
20
+
21
+ it { expect(request_uri).to eq('/api/au/address/info.json?pxid=123&key=XXX&secret=YYY') }
22
+ end
23
+
24
+ context 'with a country override' do
25
+ let(:args){ {pxid: '123'} }
26
+
27
+ it { expect(request_uri).to eq('/api/au/address/info.json?pxid=123&key=XXX&secret=YYY') }
28
+ end
29
+ end
30
+
31
+ describe '#build_result' do
32
+ let(:locator){ AddressFinder::AddressInfo.new(params: {q: 'ignored'}, http: nil) }
33
+
34
+ before do
35
+ locator.send('response_body=', body)
36
+ locator.send('response_status=', status)
37
+ end
38
+
39
+ subject(:result){ locator.send(:build_result) }
40
+
41
+ context 'with a successful result' do
42
+ let(:body){ '{"pxid":"2-.9.2U.F.F.2I","number":"184","container_only":"false","x":"174.314855382547","y":"-35.6946659390092","postcode":"0112","a":"184 William Jones Drive, Otangarei, Whangarei 0112","postal":"184 William Jones Drive, Otangarei, Whangarei 0112","mailtown":"Whangarei","post_suburb":"Otangarei","ta":"Whangarei District","sufi":932755,"street_type":"drive","city":"Whangarei","suburb":"Otangarei","region":"Northland Region","street":"William Jones Drive","postal_line_1":"184 William Jones Drive","postal_line_2":"Otangarei","postal_line_3":"Whangarei 0112","meshblock":"93700","dpid":"681856"}' }
43
+ let(:status){ '200' }
44
+
45
+ it { expect(result.class).to eq(AddressFinder::AddressInfo::Result) }
46
+ it { expect(result.a).to eq("184 William Jones Drive, Otangarei, Whangarei 0112") }
47
+ it { expect(result.pxid).to eq("2-.9.2U.F.F.2I") }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe AddressFinder::AddressSearch do
4
+ before do
5
+ AddressFinder.configure do |af|
6
+ af.api_key = 'XXX'
7
+ af.api_secret = 'YYY'
8
+ af.default_country = 'nz'
9
+ end
10
+ end
11
+
12
+ describe '#build_request' do
13
+ let(:locator){ AddressFinder::AddressSearch.new(params: args, http: http) }
14
+ let(:http){ AddressFinder.send(:configure_http) }
15
+
16
+ subject(:request_uri){ locator.send(:build_request) }
17
+
18
+ context 'with minimal arguments' do
19
+ let(:args){ {q: '186 willis'} }
20
+
21
+ it { expect(request_uri).to eq('/api/nz/address.json?q=186%20willis&key=XXX&secret=YYY') }
22
+ end
23
+
24
+ context 'with more arguments' do
25
+ let(:args){ {q: '186 willis st', delivered: 1, max: 10} }
26
+
27
+ it { expect(request_uri).to eq('/api/nz/address.json?q=186%20willis%20st&delivered=1&max=10&key=XXX&secret=YYY') }
28
+ end
29
+
30
+ context 'with a country override' do
31
+ let(:args){ {q: '186 willis st', country: 'au'} }
32
+
33
+ it { expect(request_uri).to eq('/api/au/address.json?q=186%20willis%20st&key=XXX&secret=YYY') }
34
+ end
35
+ end
36
+
37
+ describe '#build_result' do
38
+ let(:locator){ AddressFinder::AddressSearch.new(params: {q: 'ignored'}, http: nil) }
39
+
40
+ before do
41
+ locator.send('response_body=', body)
42
+ locator.send('response_status=', status)
43
+ locator.send(:build_result)
44
+ end
45
+
46
+ subject(:results){ locator.results }
47
+
48
+ context 'with completions' do
49
+ let(:body){ '{"completions":[{"a":"184 William Jones Drive, Otangarei, Whangarei 0112","pxid":"2-.9.2U.F.F.2I","v":1},{"a":"184 Williams Street, Kaiapoi 7630","pxid":"2-.3.1q.2.4G.4c","v":0},{"a":"184 Willis Street, Te Aro, Wellington 6011","pxid":"2-.F.1W.p.1D.1W","v":0}],"paid":true}' }
50
+ let(:status){ '200' }
51
+
52
+ it { expect(results.size).to eq(3) }
53
+ it { expect(results.first.class).to eq(AddressFinder::AddressSearch::Result) }
54
+ it { expect(results.first.a).to eq("184 William Jones Drive, Otangarei, Whangarei 0112") }
55
+ end
56
+
57
+ context 'with no completions' do
58
+ let(:body){ '{"completions":[],"paid":true}' }
59
+ let(:status){ '200' }
60
+
61
+ it { expect(results).to eq([]) }
62
+ end
63
+ end
64
+ end
@@ -79,4 +79,32 @@ RSpec.describe AddressFinder::Cleanse do
79
79
  it { expect(result).to eq(nil) }
80
80
  end
81
81
  end
82
+
83
+ describe '#encoded_params' do
84
+ subject(:encoded_params){ AddressFinder::Cleanse.new(q: q, http: nil).send(:encoded_params) }
85
+
86
+ context 'with a question mark value' do
87
+ let(:q){ '?' }
88
+
89
+ it { expect(encoded_params).to eq('q=%3F&format=json&key=XXX&secret=YYY') }
90
+ end
91
+
92
+ context 'with a normal address value' do
93
+ let(:q){ '12 high' }
94
+
95
+ it { expect(encoded_params).to eq('q=12%20high&format=json&key=XXX&secret=YYY') }
96
+ end
97
+
98
+ context 'with a blank value' do
99
+ let(:q){ '' }
100
+
101
+ it { expect(encoded_params).to eq('q=&format=json&key=XXX&secret=YYY') }
102
+ end
103
+
104
+ context 'with a nil value' do
105
+ let(:q){ nil }
106
+
107
+ it { expect(encoded_params).to eq('q=&format=json&key=XXX&secret=YYY') }
108
+ end
109
+ end
82
110
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: addressfinder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nigel Ramsay
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-10-15 00:00:00.000000000 Z
13
+ date: 2015-10-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multi_json
@@ -101,6 +101,8 @@ files:
101
101
  - Rakefile
102
102
  - addressfinder.gemspec
103
103
  - lib/addressfinder.rb
104
+ - lib/addressfinder/address_info.rb
105
+ - lib/addressfinder/address_search.rb
104
106
  - lib/addressfinder/bulk.rb
105
107
  - lib/addressfinder/cleanse.rb
106
108
  - lib/addressfinder/configuration.rb
@@ -108,6 +110,8 @@ files:
108
110
  - lib/addressfinder/location_info.rb
109
111
  - lib/addressfinder/location_search.rb
110
112
  - lib/addressfinder/version.rb
113
+ - spec/lib/addressfinder/address_info_spec.rb
114
+ - spec/lib/addressfinder/address_search_spec.rb
111
115
  - spec/lib/addressfinder/cleanse_spec.rb
112
116
  - spec/lib/addressfinder/location_info_spec.rb
113
117
  - spec/lib/addressfinder/location_search_spec.rb
@@ -132,11 +136,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
136
  version: '0'
133
137
  requirements: []
134
138
  rubyforge_project:
135
- rubygems_version: 2.4.2
139
+ rubygems_version: 2.4.7
136
140
  signing_key:
137
141
  specification_version: 4
138
142
  summary: Provides easy access to AddressFinder APIs
139
143
  test_files:
144
+ - spec/lib/addressfinder/address_info_spec.rb
145
+ - spec/lib/addressfinder/address_search_spec.rb
140
146
  - spec/lib/addressfinder/cleanse_spec.rb
141
147
  - spec/lib/addressfinder/location_info_spec.rb
142
148
  - spec/lib/addressfinder/location_search_spec.rb