address_verifier 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/address_verifier.gemspec +1 -0
- data/lib/address_verifier.rb +35 -4
- data/lib/address_verifier/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 147512e323a471a816a84556d570323472beff6d
|
4
|
+
data.tar.gz: 717cb2e23ec416da1e57a2dc313f7a36d7458d2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a3c7bc57cd3161cfbc2ba12499c211a11d253a3581578a6a6db37022ce9751a83a300c40b46081311c8ba25d44ae5936d360a80e5cc0d25dd5246a6b181e680
|
7
|
+
data.tar.gz: 91fa4119686d09b07ac4eef012501afd0c6c36280719549d0efe1e46d8dc60a464d0d0d7f7bbbdb3ddacc1afeb97abbe98ea0bfca4e0a5688ebb86835c2b798f
|
data/address_verifier.gemspec
CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_runtime_dependency 'httparty', '~> 0'
|
22
22
|
spec.add_runtime_dependency 'queryparams', '~> 0'
|
23
|
+
spec.add_runtime_dependency 'madison', '~> 0'
|
23
24
|
spec.add_development_dependency 'bundler', '~> 1.11'
|
24
25
|
spec.add_development_dependency 'rake', '~> 10.0'
|
25
26
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
data/lib/address_verifier.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'httparty'
|
2
|
+
require 'madison'
|
2
3
|
require 'queryparams'
|
3
4
|
|
4
5
|
class AddressVerifier
|
5
6
|
attr_accessor :usps_user_id
|
6
7
|
attr_accessor :google_api_key
|
8
|
+
attr_reader :last_usps_response
|
7
9
|
|
8
10
|
def initialize(usps_user_id = nil, google_api_key = nil)
|
9
11
|
@usps_user_id = usps_user_id
|
@@ -16,17 +18,48 @@ class AddressVerifier
|
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
21
|
+
def state_abbr(state)
|
22
|
+
return nil unless state
|
23
|
+
Madison.get_abbrev(state) ||
|
24
|
+
Madison.get_abbrev(Madison.get_name(state).to_s)
|
25
|
+
end
|
26
|
+
|
19
27
|
def usps_verification(address)
|
20
|
-
|
28
|
+
address.merge! state: state_abbr(address[:state])
|
29
|
+
request_xml = usps_verification_xml address
|
30
|
+
request_url = usps_verification_url request_xml
|
31
|
+
res = HTTParty.get request_url, timeout: 5
|
32
|
+
@last_usps_response = res['AddressValidateResponse']['Address']
|
33
|
+
res
|
21
34
|
end
|
22
35
|
|
23
36
|
def verified?(address)
|
24
37
|
res = usps_verification(address)['AddressValidateResponse']['Address']
|
25
|
-
res['ReturnText'].nil? && res['Error'].nil?
|
38
|
+
res['ReturnText'].nil? && res['Error'].nil? && !differ?(address, res)
|
26
39
|
end
|
27
40
|
|
28
41
|
private
|
29
42
|
|
43
|
+
# Returns true if the request differs significantly from the response.
|
44
|
+
# If a city and state were provided as part of the request, returns false if
|
45
|
+
# they match the response. If city and state were not both provided, returns
|
46
|
+
# false if the request zipcode matches the response.
|
47
|
+
def differ?(request, response)
|
48
|
+
req_city = request[:city]
|
49
|
+
req_state = request[:state]
|
50
|
+
req_zip = request[:zip5]
|
51
|
+
res_city = response['City']
|
52
|
+
res_state = response['State']
|
53
|
+
res_zip = response['Zip5']
|
54
|
+
|
55
|
+
if req_city && req_state
|
56
|
+
return req_city.downcase != res_city.downcase ||
|
57
|
+
req_state.downcase != res_state.downcase
|
58
|
+
elsif req_zip
|
59
|
+
return req_zip.downcase != res_zip.downcase
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
30
63
|
def suggestions_url(address)
|
31
64
|
params = QueryParams.encode(
|
32
65
|
address: address,
|
@@ -38,8 +71,6 @@ class AddressVerifier
|
|
38
71
|
def usps_verification_xml(address)
|
39
72
|
"
|
40
73
|
<AddressValidateRequest USERID='#{@usps_user_id}'>
|
41
|
-
<IncludeOptionalElements>true</IncludeOptionalElements>
|
42
|
-
<ReturnCarrierRoute>true</ReturnCarrierRoute>
|
43
74
|
<Address ID=\"0\">
|
44
75
|
<FirmName />
|
45
76
|
<Address1>#{address[:address1]}</Address1>
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: address_verifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Jesuele
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: madison
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: bundler
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|