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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cde414194b3d3c80d275a26918bd7f850503c16b
4
- data.tar.gz: 5b459b6916551fb80cfe2a400998f8897faaf818
3
+ metadata.gz: 147512e323a471a816a84556d570323472beff6d
4
+ data.tar.gz: 717cb2e23ec416da1e57a2dc313f7a36d7458d2e
5
5
  SHA512:
6
- metadata.gz: 541783d7ac308d6fce8c46cc3fc91676ee1a47fcd784ab23b5a9032b80d0ddc5fadbdd3e46a496228c48f5c4a4da2c79a95d5c92dc3712b77d925ae7b2605c12
7
- data.tar.gz: 930731fab471c15d5bbdb62511f18607daac9962c5399e7c86668cf154e563e3edbeee86f5f9718c77b019b19fd27a230a10e648b131ca480f553152b68b93f2
6
+ metadata.gz: 1a3c7bc57cd3161cfbc2ba12499c211a11d253a3581578a6a6db37022ce9751a83a300c40b46081311c8ba25d44ae5936d360a80e5cc0d25dd5246a6b181e680
7
+ data.tar.gz: 91fa4119686d09b07ac4eef012501afd0c6c36280719549d0efe1e46d8dc60a464d0d0d7f7bbbdb3ddacc1afeb97abbe98ea0bfca4e0a5688ebb86835c2b798f
@@ -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'
@@ -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
- HTTParty.get(usps_verification_url(usps_verification_xml(address)), timeout: 5)
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>
@@ -1,3 +1,3 @@
1
1
  class AddressVerifier
2
- VERSION = "0.1.2"
2
+ VERSION = '0.1.4'
3
3
  end
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.2
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-02 00:00:00.000000000 Z
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