addressfinder 1.1.2 → 1.2.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: 4a4f13013752f2897a65b1ae37cdf75432e48d65
4
- data.tar.gz: 736cb348b5e219a390025fa85054cc639a4f97cc
3
+ metadata.gz: dd55287eea55045c57b60b4a33a650c25aba42b0
4
+ data.tar.gz: 88d85b667c45378a01c370d9b7453845ee1e403b
5
5
  SHA512:
6
- metadata.gz: f913ac44544240ce9270c202a5916e26d323669f41c494438560316f548657f39c2ee78591ea55a4c3e87eb3be8ca96e4f9ff8995826f094a8782d874818404b
7
- data.tar.gz: cf7ce295d772e3902e79f023bb4ecfac1f61129b23f1c714d79e31294760e50a16d6f467a64781d04339d85cbb1acc0e365b839712b67ef24871049dc4a0de99
6
+ metadata.gz: 05ae9738123018521f49e009ffc0485574a246e2879459b9736170ece70b1923dd50aae07303c28a2e853dc54e7757be42df54a4690acf24dd79338f79fc97ac
7
+ data.tar.gz: c591f3700cbc86b90cf447a16d79b49e1487a8e1db7b43e45f4b1dc314fb66271b1e8e086edab3118fc2b364f2b474bbed623c80a0dab636d0cb32bf15d8985a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # AddressFinder 1.2.0 (October 1, 2015) #
2
+
3
+ * Add support for Location Search API
4
+ * Add support for Location Info API
5
+
1
6
  # AddressFinder 1.1.2 (September 23, 2015) #
2
7
 
3
8
  * Minor bugfixes
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard :rspec, cmd: 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/README.md CHANGED
@@ -62,6 +62,50 @@ else
62
62
  end
63
63
  ```
64
64
 
65
+ ### Location Search
66
+
67
+ See documentation on the available parameters and expected response here:
68
+
69
+ https://addressfinder.nz/docs/location_api/
70
+
71
+ Usage example:
72
+
73
+ ```ruby
74
+ begin
75
+ results = AddressFinder.location_search(q: 'Queen Street')
76
+ if results.any?
77
+ $standout.puts "Success: #{results}"
78
+ else
79
+ $standout.puts "Sorry, there were no location matches"
80
+ end
81
+ rescue AddressFinder::RequestRejectedError => e
82
+ response = JSON.parse(e.body)
83
+ $standout.puts response['message']
84
+ end
85
+ ```
86
+
87
+ ### Location Info
88
+
89
+ See documentation on the available parameters and expected response here:
90
+
91
+ https://addressfinder.nz/docs/address_info_api/
92
+
93
+ Usage example:
94
+
95
+ ```ruby
96
+ begin
97
+ result = AddressFinder.location_info(pxid: '1-.B.3l')
98
+ if result
99
+ $standout.puts "Success: #{result.a}"
100
+ else
101
+ $standout.puts "Sorry, can't find that location"
102
+ end
103
+ rescue AddressFinder::RequestRejectedError => e
104
+ response = JSON.parse(e.body)
105
+ $standout.puts response['message']
106
+ end
107
+ ```
108
+
65
109
  ### Bulk Operations
66
110
 
67
111
  If you have a series of calls you need to make to AddressFinder, you can use the
@@ -19,8 +19,9 @@ Gem::Specification.new do |gem|
19
19
  gem.required_ruby_version = '~> 2.1'
20
20
  gem.add_runtime_dependency 'multi_json', '~> 1.0'
21
21
 
22
- gem.add_development_dependency 'rspec', '>= 3.3.0'
23
- gem.add_development_dependency 'bundler'
24
- gem.add_development_dependency 'rake'
25
- gem.add_development_dependency 'webmock'
22
+ gem.add_development_dependency 'rspec', '~> 3.3'
23
+ gem.add_development_dependency 'guard-rspec', '~> 4.5'
24
+ gem.add_development_dependency 'bundler', '~> 0'
25
+ gem.add_development_dependency 'rake', '~> 0'
26
+ gem.add_development_dependency 'webmock', '~> 0'
26
27
  end
data/lib/addressfinder.rb CHANGED
@@ -3,6 +3,8 @@ require 'multi_json'
3
3
  require 'addressfinder/version'
4
4
  require 'addressfinder/configuration'
5
5
  require 'addressfinder/cleanse'
6
+ require 'addressfinder/location_info'
7
+ require 'addressfinder/location_search'
6
8
  require 'addressfinder/bulk'
7
9
  require 'addressfinder/errors'
8
10
 
@@ -26,6 +28,14 @@ module AddressFinder
26
28
  AddressFinder::Cleanse.new(args.merge(http: configure_http)).perform.result
27
29
  end
28
30
 
31
+ def location_search(args={})
32
+ AddressFinder::LocationSearch.new(params: args, http: configure_http).perform.results
33
+ end
34
+
35
+ def location_info(args={})
36
+ AddressFinder::LocationInfo.new(params: args, http: configure_http).perform.result
37
+ end
38
+
29
39
  def bulk(&block)
30
40
  # TODO include parameter http: configure_http
31
41
  AddressFinder::Bulk.new(&block).perform
@@ -10,4 +10,17 @@ module AddressFinder
10
10
  super("Request rejected with status code: #{status}\n#{body}")
11
11
  end
12
12
  end
13
+
14
+ class NotFoundError < StandardError
15
+
16
+ attr_reader :status, :body, :pxid
17
+
18
+ def initialize(status, body, pxid)
19
+ @status = status
20
+ @body = body
21
+ @pxid = pxid
22
+
23
+ super("The address or location you have requested could not be found.\n#{body}")
24
+ end
25
+ end
13
26
  end
@@ -0,0 +1,71 @@
1
+ require 'ostruct'
2
+
3
+ module AddressFinder
4
+ class LocationInfo
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}/location/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 LocationSearch
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}/location.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
@@ -1,3 +1,3 @@
1
1
  module AddressFinder
2
- VERSION = '1.1.2'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe AddressFinder::LocationInfo 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::LocationInfo.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/location/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/location/info.json?pxid=123&key=XXX&secret=YYY') }
28
+ end
29
+ end
30
+
31
+ describe '#build_result' do
32
+ let(:locator){ AddressFinder::LocationInfo.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){ '{"a":"Seaview Road, Glenfield, Auckland","city":"Auckland","suburb":"Glenfield","region":"Auckland Region","x":174.713938691835,"y":-36.7894885545157,"pxid":"1-.1.6.j.1F","street":"Seaview Road"}' }
43
+ let(:status){ '200' }
44
+
45
+ it { expect(result.class).to eq(AddressFinder::LocationInfo::Result) }
46
+ it { expect(result.a).to eq("Seaview Road, Glenfield, Auckland") }
47
+ it { expect(result.pxid).to eq("1-.1.6.j.1F") }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe AddressFinder::LocationSearch 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::LocationSearch.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: 'willis'} }
20
+
21
+ it { expect(request_uri).to eq('/api/nz/location.json?q=willis&key=XXX&secret=YYY') }
22
+ end
23
+
24
+ context 'with more arguments' do
25
+ let(:args){ {q: 'willis st', street: 1, max: 10} }
26
+
27
+ it { expect(request_uri).to eq('/api/nz/location.json?q=willis%20st&street=1&max=10&key=XXX&secret=YYY') }
28
+ end
29
+
30
+ context 'with a country override' do
31
+ let(:args){ {q: 'willis st', country: 'au'} }
32
+
33
+ it { expect(request_uri).to eq('/api/au/location.json?q=willis%20st&key=XXX&secret=YYY') }
34
+ end
35
+ end
36
+
37
+ describe '#build_result' do
38
+ let(:locator){ AddressFinder::LocationSearch.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":"Willowbank","pxid":"1-.B.3l","v":0},{"a":"Willowby","pxid":"1-.3.4O","v":0}],"paid":true}' }
50
+ let(:status){ '200' }
51
+
52
+ it { expect(results.size).to eq(2) }
53
+ it { expect(results.first.class).to eq(AddressFinder::LocationSearch::Result) }
54
+ it { expect(results.first.a).to eq("Willowbank") }
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
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: addressfinder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nigel Ramsay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-23 00:00:00.000000000 Z
11
+ date: 2015-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -28,56 +28,70 @@ dependencies:
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 3.3.0
33
+ version: '3.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 3.3.0
40
+ version: '3.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.5'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - ">="
59
+ - - "~>"
46
60
  - !ruby/object:Gem::Version
47
61
  version: '0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - ">="
66
+ - - "~>"
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rake
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - ">="
73
+ - - "~>"
60
74
  - !ruby/object:Gem::Version
61
75
  version: '0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - ">="
80
+ - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: webmock
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - ">="
87
+ - - "~>"
74
88
  - !ruby/object:Gem::Version
75
89
  version: '0'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - ">="
94
+ - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
83
97
  description: Ruby client library for AddressFinder
@@ -91,6 +105,7 @@ files:
91
105
  - ".travis.yml"
92
106
  - CHANGELOG.md
93
107
  - Gemfile
108
+ - Guardfile
94
109
  - LICENSE
95
110
  - README.md
96
111
  - Rakefile
@@ -100,8 +115,12 @@ files:
100
115
  - lib/addressfinder/cleanse.rb
101
116
  - lib/addressfinder/configuration.rb
102
117
  - lib/addressfinder/errors.rb
118
+ - lib/addressfinder/location_info.rb
119
+ - lib/addressfinder/location_search.rb
103
120
  - lib/addressfinder/version.rb
104
121
  - spec/lib/addressfinder/cleanse_spec.rb
122
+ - spec/lib/addressfinder/location_info_spec.rb
123
+ - spec/lib/addressfinder/location_search_spec.rb
105
124
  - spec/spec_helper.rb
106
125
  homepage: https://github.com/AbleTech/addressfinder-ruby
107
126
  licenses:
@@ -123,10 +142,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
142
  version: '0'
124
143
  requirements: []
125
144
  rubyforge_project:
126
- rubygems_version: 2.4.8
145
+ rubygems_version: 2.4.7
127
146
  signing_key:
128
147
  specification_version: 4
129
148
  summary: Provides easy access to AddressFinder APIs
130
149
  test_files:
131
150
  - spec/lib/addressfinder/cleanse_spec.rb
151
+ - spec/lib/addressfinder/location_info_spec.rb
152
+ - spec/lib/addressfinder/location_search_spec.rb
132
153
  - spec/spec_helper.rb