addressfinder 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -0
- data/CHANGELOG.md +8 -0
- data/README.md +45 -15
- data/Rakefile +7 -0
- data/addressfinder.gemspec +8 -0
- data/lib/addressfinder.rb +23 -2
- data/lib/addressfinder/bulk.rb +37 -0
- data/lib/addressfinder/cleanse.rb +20 -16
- data/lib/addressfinder/configuration.rb +1 -0
- data/lib/addressfinder/version.rb +1 -1
- data/spec/lib/addressfinder/cleanse_spec.rb +65 -0
- data/spec/spec_helper.rb +17 -0
- metadata +84 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75748dbec5997618df1a9aef78d9bfabced2f496
|
4
|
+
data.tar.gz: 77750d90c4738a6fcfd099837d76cae0dd9ff825
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfcd83c4a5a3fcde6b12a71587a8f60b5a2044f1fffc4eba479baa14ab51300d1537ee80a376076e896a3d05f297f1171a1d5bbd64e624fe1ef798524eb98440
|
7
|
+
data.tar.gz: 67b503303c3c5360112b77e256187492c952bf4da48ae9881943aaed606d24fa916e55d7f43d7e44ba65e26eacc0f8b0f0e87f332b8ef6682e4a8d45d30c56a6
|
data/.travis.yml
ADDED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# AddressFinder Ruby Gem
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/addressfinder.svg)](http://badge.fury.io/rb/addressfinder)
|
4
|
+
[![Build Status](https://travis-ci.org/AbleTech/addressfinder-ruby.svg)](https://travis-ci.org/AbleTech/addressfinder-ruby)
|
5
|
+
|
3
6
|
A client library for accessing the AddressFinder APIs.
|
4
7
|
|
5
8
|
## Installation
|
@@ -23,19 +26,23 @@ Or install it yourself as:
|
|
23
26
|
You should call the configure block on startup of your app. In a Ruby on Rails application this
|
24
27
|
is normally performed in an initializer file. For example `./config/initializers/addressfinder.rb`
|
25
28
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
29
|
+
```ruby
|
30
|
+
AddressFinder.configure do |af|
|
31
|
+
# Mandatory configuration
|
32
|
+
af.api_key = 'XXXXXXXXXX'
|
33
|
+
af.api_secret = 'YYYYYYYYYY'
|
34
|
+
af.default_country = 'nz'
|
35
|
+
|
36
|
+
# Optional configuration
|
37
|
+
af.timeout = 10 # seconds
|
38
|
+
af.proxy_host = 'corp.proxy.com'
|
39
|
+
af.proxy_port = 8080
|
40
|
+
af.proxy_user = 'username'
|
41
|
+
af.proxy_password = 'password'
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
You can obtain your API key and secret from the AddressFinder Portal.
|
39
46
|
|
40
47
|
### Address Cleansing
|
41
48
|
|
@@ -45,10 +52,33 @@ https://addressfinder.nz/docs/address_cleanse_api/
|
|
45
52
|
|
46
53
|
Usage example:
|
47
54
|
|
48
|
-
|
55
|
+
```ruby
|
56
|
+
result = AddressFinder.cleanse(q: '186 Willis St, Wellington')
|
57
|
+
|
58
|
+
if result
|
59
|
+
$stdout.puts "Success: #{result.postal}"
|
60
|
+
else
|
61
|
+
$stdout.puts "Sorry, can't find that address"
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
### Bulk Operations
|
66
|
+
|
67
|
+
If you have a series of calls you need to make to AddressFinder, you can use the
|
68
|
+
bulk method which re-uses the HTTP connection.
|
69
|
+
|
70
|
+
Usage example:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
AddressFinder.bulk do |af|
|
74
|
+
CSV.foreach('auckland_addresses.csv') do |row|
|
75
|
+
result = af.cleanse(q: row[0], region_code: '1')
|
49
76
|
|
50
77
|
if result
|
51
|
-
$stdout.puts "
|
78
|
+
$stdout.puts "Success: #{result.postal}"
|
52
79
|
else
|
53
80
|
$stdout.puts "Sorry, can't find that address"
|
54
81
|
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
```
|
data/Rakefile
ADDED
data/addressfinder.gemspec
CHANGED
@@ -14,4 +14,12 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
15
|
gem.test_files = gem.files.grep(%r{^(spec|features)/})
|
16
16
|
gem.require_paths = ["lib"]
|
17
|
+
|
18
|
+
gem.required_ruby_version = '~> 2.1'
|
19
|
+
gem.add_runtime_dependency 'multi_json', '~> 1.0'
|
20
|
+
|
21
|
+
gem.add_development_dependency 'rspec', '>= 3.3.0'
|
22
|
+
gem.add_development_dependency 'bundler'
|
23
|
+
gem.add_development_dependency 'rake'
|
24
|
+
gem.add_development_dependency 'webmock'
|
17
25
|
end
|
data/lib/addressfinder.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'multi_json'
|
1
3
|
require 'addressfinder/version'
|
2
4
|
require 'addressfinder/configuration'
|
3
5
|
require 'addressfinder/cleanse'
|
6
|
+
require 'addressfinder/bulk'
|
4
7
|
require 'addressfinder/errors'
|
5
8
|
|
6
9
|
module AddressFinder
|
@@ -19,8 +22,26 @@ module AddressFinder
|
|
19
22
|
@configuration ||= AddressFinder::Configuration.new
|
20
23
|
end
|
21
24
|
|
22
|
-
def cleanse(
|
23
|
-
AddressFinder::Cleanse.new(
|
25
|
+
def cleanse(args={})
|
26
|
+
AddressFinder::Cleanse.new(args.merge(http: configure_http)).perform.result
|
27
|
+
end
|
28
|
+
|
29
|
+
def bulk(&block)
|
30
|
+
# TODO include parameter http: configure_http
|
31
|
+
AddressFinder::Bulk.new(&block).perform
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def configure_http
|
37
|
+
http = Net::HTTP.new(configuration.hostname, configuration.port,
|
38
|
+
configuration.proxy_host, configuration.proxy_port,
|
39
|
+
configuration.proxy_user, configuration.proxy_password)
|
40
|
+
http.open_timeout = configuration.timeout
|
41
|
+
http.read_timeout = configuration.timeout
|
42
|
+
http.use_ssl = true
|
43
|
+
|
44
|
+
http
|
24
45
|
end
|
25
46
|
end
|
26
47
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module AddressFinder
|
2
|
+
class Bulk
|
3
|
+
def initialize(&block)
|
4
|
+
@block = block
|
5
|
+
end
|
6
|
+
|
7
|
+
def perform
|
8
|
+
Net::HTTP.start(config.hostname, config.port, use_ssl: true,
|
9
|
+
open_timeout: config.timeout,
|
10
|
+
read_timeout: config.timeout) do |http|
|
11
|
+
block.call ClientProxy.new(http: http)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
attr_reader :block
|
18
|
+
|
19
|
+
def config
|
20
|
+
@_config ||= AddressFinder.configuration
|
21
|
+
end
|
22
|
+
|
23
|
+
class ClientProxy
|
24
|
+
def initialize(http:)
|
25
|
+
@http = http
|
26
|
+
end
|
27
|
+
|
28
|
+
def cleanse(args={})
|
29
|
+
AddressFinder::Cleanse.new(args.merge(http: http)).perform
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
attr_reader :http
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -1,6 +1,11 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
1
3
|
module AddressFinder
|
2
4
|
class Cleanse
|
3
|
-
|
5
|
+
|
6
|
+
attr_reader :result
|
7
|
+
|
8
|
+
def initialize(q:, country: nil, delivered: nil, post_box: nil, rural: nil, region_code: nil, http:)
|
4
9
|
@params = {}
|
5
10
|
@params['q'] = q
|
6
11
|
@params['delivered'] = delivered if delivered
|
@@ -11,35 +16,34 @@ module AddressFinder
|
|
11
16
|
@params['key'] = config.api_key
|
12
17
|
@params['secret'] = config.api_secret
|
13
18
|
@country = country || config.default_country
|
19
|
+
@http = http
|
14
20
|
end
|
15
21
|
|
16
22
|
def perform
|
17
23
|
build_request
|
18
24
|
execute_request
|
19
25
|
build_result
|
26
|
+
|
27
|
+
self
|
20
28
|
end
|
21
29
|
|
22
30
|
private
|
23
31
|
|
24
|
-
attr_reader :
|
32
|
+
attr_reader :request_uri, :params, :result, :country, :http
|
33
|
+
attr_accessor :response_body, :response_status
|
34
|
+
attr_writer :result
|
25
35
|
|
26
36
|
def build_request
|
27
|
-
@
|
37
|
+
@request_uri = "/api/#{country}/address/cleanse?#{encoded_params}"
|
28
38
|
end
|
29
39
|
|
30
40
|
def execute_request
|
31
|
-
|
32
|
-
http = Net::HTTP.new(uri.host, uri.port, config.proxy_host, config.proxy_port, config.proxy_user, config.proxy_password)
|
33
|
-
http.open_timeout = config.timeout
|
34
|
-
http.read_timeout = config.timeout
|
35
|
-
http.use_ssl = (uri.scheme == "https")
|
36
|
-
|
37
|
-
request = Net::HTTP::Get.new(uri.request_uri)
|
41
|
+
request = Net::HTTP::Get.new(request_uri)
|
38
42
|
|
39
43
|
response = http.request(request)
|
40
44
|
|
41
|
-
|
42
|
-
|
45
|
+
self.response_body = response.body
|
46
|
+
self.response_status = response.code
|
43
47
|
end
|
44
48
|
|
45
49
|
def build_result
|
@@ -48,10 +52,10 @@ module AddressFinder
|
|
48
52
|
end
|
49
53
|
|
50
54
|
if response_hash['matched']
|
51
|
-
|
55
|
+
self.result = Result.new(response_hash)
|
56
|
+
else
|
57
|
+
self.result = nil
|
52
58
|
end
|
53
|
-
|
54
|
-
nil
|
55
59
|
end
|
56
60
|
|
57
61
|
def encoded_params
|
@@ -60,7 +64,7 @@ module AddressFinder
|
|
60
64
|
end
|
61
65
|
|
62
66
|
def response_hash
|
63
|
-
@_response_hash ||=
|
67
|
+
@_response_hash ||= MultiJson.load(response_body)
|
64
68
|
end
|
65
69
|
|
66
70
|
def config
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe AddressFinder::Cleanse 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(:cleanser){ AddressFinder::Cleanse.new(args) }
|
14
|
+
let(:http){ AddressFinder.send(:configure_http) }
|
15
|
+
|
16
|
+
subject(:request_uri){ cleanser.send(:build_request) }
|
17
|
+
|
18
|
+
context 'with minimal arguments' do
|
19
|
+
let(:args){ {q: '186 willis st', http: http} }
|
20
|
+
|
21
|
+
it { expect(request_uri).to eq('/api/nz/address/cleanse?q=186%20willis%20st&format=json&key=XXX&secret=YYY') }
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'with more arguments' do
|
25
|
+
let(:args){ {q: '186 willis st', delivered: true, region_code: 'A', http: http} }
|
26
|
+
|
27
|
+
it { expect(request_uri).to eq('/api/nz/address/cleanse?q=186%20willis%20st&delivered=true®ion_code=A&format=json&key=XXX&secret=YYY') }
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'with a country override' do
|
31
|
+
let(:args){ {q: '186 willis st', country: 'au', http: http} }
|
32
|
+
|
33
|
+
it { expect(request_uri).to eq('/api/au/address/cleanse?q=186%20willis%20st&format=json&key=XXX&secret=YYY') }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#build_result' do
|
38
|
+
let(:cleanser){ AddressFinder::Cleanse.new(q: 'ignored', http: nil) }
|
39
|
+
|
40
|
+
before do
|
41
|
+
cleanser.send('response_body=', body)
|
42
|
+
cleanser.send('response_status=', status)
|
43
|
+
end
|
44
|
+
|
45
|
+
subject(:result){ cleanser.send(:build_result) }
|
46
|
+
|
47
|
+
context 'with a successful result' do
|
48
|
+
let(:body){ '{"matched": true, "postal_address": "Texas"}' }
|
49
|
+
let(:status){ '200' }
|
50
|
+
|
51
|
+
it { expect(result.class).to eq(AddressFinder::Cleanse::Result) }
|
52
|
+
|
53
|
+
it { expect(result.matched).to eq(true) }
|
54
|
+
|
55
|
+
it { expect(result.postal_address).to eq("Texas") }
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'with an unfound result' do
|
59
|
+
let(:body){ '{"matched": false}' }
|
60
|
+
let(:status){ '200' }
|
61
|
+
|
62
|
+
it { expect(result).to eq(nil) }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'bundler'
|
10
|
+
require 'webmock/rspec'
|
11
|
+
require 'addressfinder'
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.mock_with :rspec
|
15
|
+
end
|
16
|
+
|
17
|
+
WebMock.disable_net_connect!
|
metadata
CHANGED
@@ -1,15 +1,85 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: addressfinder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
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-
|
12
|
-
dependencies:
|
11
|
+
date: 2015-09-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: multi_json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.3.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.3.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
13
83
|
description: Ruby client library for AddressFinder
|
14
84
|
email:
|
15
85
|
- nigel@abletech.nz
|
@@ -18,16 +88,21 @@ extensions: []
|
|
18
88
|
extra_rdoc_files: []
|
19
89
|
files:
|
20
90
|
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
21
92
|
- CHANGELOG.md
|
22
93
|
- Gemfile
|
23
94
|
- LICENSE
|
24
95
|
- README.md
|
96
|
+
- Rakefile
|
25
97
|
- addressfinder.gemspec
|
26
98
|
- lib/addressfinder.rb
|
99
|
+
- lib/addressfinder/bulk.rb
|
27
100
|
- lib/addressfinder/cleanse.rb
|
28
101
|
- lib/addressfinder/configuration.rb
|
29
102
|
- lib/addressfinder/errors.rb
|
30
103
|
- lib/addressfinder/version.rb
|
104
|
+
- spec/lib/addressfinder/cleanse_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
31
106
|
homepage: https://github.com/AbleTech/addressfinder-ruby
|
32
107
|
licenses: []
|
33
108
|
metadata: {}
|
@@ -37,9 +112,9 @@ require_paths:
|
|
37
112
|
- lib
|
38
113
|
required_ruby_version: !ruby/object:Gem::Requirement
|
39
114
|
requirements:
|
40
|
-
- - "
|
115
|
+
- - "~>"
|
41
116
|
- !ruby/object:Gem::Version
|
42
|
-
version: '
|
117
|
+
version: '2.1'
|
43
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
119
|
requirements:
|
45
120
|
- - ">="
|
@@ -47,8 +122,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
122
|
version: '0'
|
48
123
|
requirements: []
|
49
124
|
rubyforge_project:
|
50
|
-
rubygems_version: 2.4.
|
125
|
+
rubygems_version: 2.4.8
|
51
126
|
signing_key:
|
52
127
|
specification_version: 4
|
53
128
|
summary: Provides easy access to AddressFinder APIs
|
54
|
-
test_files:
|
129
|
+
test_files:
|
130
|
+
- spec/lib/addressfinder/cleanse_spec.rb
|
131
|
+
- spec/spec_helper.rb
|