us_address_client 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b042f259b793875c82a530f33a50da311e319c87dc78af15e699e618388c95ba
4
+ data.tar.gz: 4444911ea6ed25edaf5b165b1f3bdad47a15fa993cc969d851dd0443a3ddd17f
5
+ SHA512:
6
+ metadata.gz: 015420e49d95d20c0f64a754f01656bc22d9b6c25a26fc108cc18884a40bce5a16baf20448ee197de3359ecce2ee96f44138723989df58a45705cbf3328df893
7
+ data.tar.gz: 4258c022309fb3b69876f0cce661bcb245bc9eaa5184e1e7f729b93bde62ae23f2d8f12ad3980cad15b581f18d81303852b45594294799a6c407f0a8a6fb0b1c
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,165 @@
1
+ # US Address Client
2
+
3
+ Ruby Client to call the privately hosted US Postal Address Service to verify and cleanse addresses.
4
+
5
+ ## Status
6
+
7
+ Not for production use. US Postal Address Service is not yet open source to be able to use this client.
8
+
9
+ ## Examples
10
+
11
+ Verify and cleanse an address:
12
+ ~~~ruby
13
+ address = USAddressClient.verify(address: "2811 Safe Harbor Drive", city: "Tampa", state: "FL", zip: "33618")
14
+ # => #<USAddressClient::Address:0x00007feab8c06570 @zip="33618", @time_zone_code="05", @time_zone="Eastern Time", @suite_range="", @suite_name="", @suite="", @suffix="Dr", @street_name="Safe Harbor", @state="FL", @result_codes=["AS01"], @private_mailbox_number="", @private_mailbox_name="", @pre_direction="", @post_direction="", @plus4="4534", @melissa_address_key_base="", @melissa_address_key="", @garbage="", @delivery_point="33618453411", @city="Tampa", @address_type_code="S", @address_type="Street", @address_range="2811", @address2="", @address="2811 Safe Harbor Dr">
15
+
16
+ address.valid?
17
+ # => true
18
+
19
+ address.city
20
+ # => "Tampa"
21
+
22
+ address.time_zone_offset
23
+ # => 300
24
+
25
+ address.attributes
26
+ # =>
27
+ {
28
+ :address => "2811 Safe Harbor Dr",
29
+ :address2 => "",
30
+ :suite => "",
31
+ :city => "Tampa",
32
+ :state => "FL",
33
+ :zip => "33618",
34
+ :plus4 => "4534",
35
+ :melissa_address_key => "",
36
+ :melissa_address_key_base => "",
37
+ :delivery_point => "33618453411",
38
+ :time_zone_code => "05",
39
+ :time_zone => "Eastern Time",
40
+ :address_type => "Street",
41
+ :address_type_code => "S",
42
+ :suite_name => "",
43
+ :suite_range => "",
44
+ :address_range => "2811",
45
+ :pre_direction => "",
46
+ :post_direction => "",
47
+ :street_name => "Safe Harbor",
48
+ :suffix => "Dr",
49
+ :private_mailbox_name => "",
50
+ :private_mailbox_number => "",
51
+ :garbage => "",
52
+ :result_codes => ["AS01"]
53
+ }
54
+ ~~~
55
+
56
+ Obtain Melissa Data Quality Suite version and licensing information:
57
+ ~~~ruby
58
+ USAddressClient.version
59
+ ~~~
60
+
61
+ Output:
62
+ ~~~ruby
63
+ {
64
+ :license_expiration_date => Tue, 24 Sep 2019,
65
+ :initialize_status => "No error.",
66
+ :expiration_date => Thu, 31 Jan 2019,
67
+ :database_date => Mon, 15 Oct 2018,
68
+ :build_number => "3240"
69
+ }
70
+ ~~~
71
+
72
+ ### Configuration
73
+
74
+ Use Secret Config to supply the configuration settings for the Address Service.
75
+ `url` is mandatory, below are the default settings for the remaining values:
76
+
77
+ ~~~yaml
78
+ us_address_client:
79
+ url: http://address.service.test.com
80
+ pool_size: 100
81
+ open_timeout: 10
82
+ read_timeout: 10
83
+ idle_timeout: 300
84
+ keep_alive: 300
85
+ pool_timeout: 5
86
+ warn_timeout: 0.25
87
+ proxy: :ENV
88
+ force_retry: true
89
+ mocked: false
90
+ ~~~
91
+
92
+ Mock out calls to Address Service:
93
+ ~~~yaml
94
+ us_address_client:
95
+ mocked: true
96
+ ~~~
97
+
98
+ #### Notes:
99
+
100
+ - If `url` is not supplied then it will fall back to the local melissa data installation using the `melissa` gem.
101
+ - Configuration changes _must_ be made prior to any calls being made to this service.
102
+
103
+ ### Logging
104
+
105
+ The Address Service logs to `USAddressClient.logger`. The log level can be changed at any time to increase
106
+ or decrease the logging output.
107
+
108
+ Below is sample output for each log level setting.
109
+
110
+ #### Info Logging
111
+
112
+ ~~~ruby
113
+ USAddressClient.logger.level = :info
114
+ ~~~
115
+
116
+ Output:
117
+
118
+ ~~~
119
+ 2018-12-19 19:30:05.257225 I [28643:70323013957920 client.rb:8] (7.351ms) USAddressClient -- #verify
120
+ ~~~
121
+
122
+ #### Debug Logging
123
+
124
+ ~~~ruby
125
+ USAddressClient.logger.level = :debug
126
+ ~~~
127
+
128
+ Output:
129
+
130
+ ~~~
131
+ 2018-12-19 19:30:34.619759 D [28643:70323013957920 transport.rb:25] (8.319ms) USAddressClient -- #http_get -- { :path => "/address?address=2811+Safe+Harbor+Drive&city=Tampa&state=FL&zip=33618" }
132
+ 2018-12-19 19:30:34.620862 I [28643:70323013957920 client.rb:8] (9.562ms) USAddressClient -- #verify -- { :input => { :address => "2811 Safe Harbor Drive", :city => "Tampa", :state => "FL", :zip => "33618" }, :output => { :zip => "33618", :time_zone_code => "05", :time_zone => "Eastern Time", :suite_range => "", :suite_name => "", :suite => "", :suffix => "Dr", :street_name => "Safe Harbor", :state => "FL", :result_codes => [ "AS01" ], :private_mailbox_number => "", :private_mailbox_name => "", :pre_direction => "", :post_direction => "", :plus4 => "4534", :melissa_address_key_base => "", :melissa_address_key => "", :garbage => "", :delivery_point => "33618453411", :city => "Tampa", :address_type_code => "S", :address_type => "Street", :address_range => "2811", :address2 => "", :address => "2811 Safe Harbor Dr" } }
133
+ ~~~
134
+
135
+ #### Trace Logging
136
+
137
+ ~~~ruby
138
+ USAddressClient.logger.level = :trace
139
+ ~~~
140
+
141
+ With trace level logging PersistentHTTP and GenePool log messages are made available in the standard log files.
142
+
143
+ Output:
144
+
145
+ ~~~
146
+ 2018-12-19 19:34:08.118208 T [28643:70323013957920 transport.rb:56] USAddressClient -- HTTP: Checkout connection HTTP:0.0.0.0:4000(70323063570580) self=connections=70323063570580 checked_out=70323063570580 with_map=
147
+ 2018-12-19 19:34:08.118455 T [28643:70323013957920 transport.rb:52] USAddressClient -- Conn close because of keep_alive_timeout
148
+ 2018-12-19 19:34:08.119208 T [28643:70323013957920 transport.rb:52] USAddressClient -- opening connection to 0.0.0.0:4000...
149
+ 2018-12-19 19:34:08.120509 T [28643:70323013957920 transport.rb:52] USAddressClient -- opened
150
+ 2018-12-19 19:34:08.121268 T [28643:70323013957920 transport.rb:52] USAddressClient -- <-
151
+ 2018-12-19 19:34:08.121478 T [28643:70323013957920 transport.rb:52] USAddressClient -- "GET /address?address=2811+Safe+Harbor+Drive&city=Tampa&state=FL&zip=33618 HTTP/1.1\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nUser-Agent: Ruby\r\nContent-Type: application/json\r\nConnection: keep-alive\r\nKeep-Alive: 300\r\nHost: 0.0.0.0:4000\r\n\r\n"
152
+ 2018-12-19 19:34:08.133798 T [28643:70323013957920 transport.rb:52] USAddressClient -- -> "HTTP/1.1 200 OK\r\n"
153
+ 2018-12-19 19:34:08.133938 T [28643:70323013957920 transport.rb:52] USAddressClient -- -> "cache-control: max-age=0, private, must-revalidate\r\n"
154
+ 2018-12-19 19:34:08.134063 T [28643:70323013957920 transport.rb:52] USAddressClient -- -> "content-length: 524\r\n"
155
+ 2018-12-19 19:34:08.134126 T [28643:70323013957920 transport.rb:52] USAddressClient -- -> "date: Wed, 19 Dec 2018 19:34:07 GMT\r\n"
156
+ 2018-12-19 19:34:08.134182 T [28643:70323013957920 transport.rb:52] USAddressClient -- -> "server: Cowboy\r\n"
157
+ 2018-12-19 19:34:08.134236 T [28643:70323013957920 transport.rb:52] USAddressClient -- -> "\r\n"
158
+ 2018-12-19 19:34:08.134361 T [28643:70323013957920 transport.rb:52] USAddressClient -- reading 524 bytes...
159
+ 2018-12-19 19:34:08.134422 T [28643:70323013957920 transport.rb:52] USAddressClient -- -> "{\"zip\":\"33618\",\"time_zone_code\":\"05\",\"time_zone\":\"Eastern Time\",\"suite_range\":\"\",\"suite_name\":\"\",\"suite\":\"\",\"suffix\":\"Dr\",\"street_name\":\"Safe Harbor\",\"state\":\"FL\",\"result_codes\":\"AS01\",\"private_mailbox_number\":\"\",\"private_mailbox_name\":\"\",\"pre_direction\":\"\",\"post_direction\":\"\",\"plus4\":\"4534\",\"melissa_address_key_base\":\"\",\"melissa_address_key\":\"\",\"garbage\":\"\",\"delivery_point\":\"33618453411\",\"city\":\"Tampa\",\"address_type_code\":\"S\",\"address_type\":\"Street\",\"address_range\":\"2811\",\"address2\":\"\",\"address\":\"2811 Safe Harbor Dr\"}"
160
+ 2018-12-19 19:34:08.134470 T [28643:70323013957920 transport.rb:52] USAddressClient -- read 524 bytes
161
+ 2018-12-19 19:34:08.134537 T [28643:70323013957920 transport.rb:52] USAddressClient -- Conn keep-alive
162
+ 2018-12-19 19:34:08.134662 T [28643:70323013957920 transport.rb:56] USAddressClient -- HTTP: Checkin connection HTTP:0.0.0.0:4000(70323063570580) self=connections=70323063570580 checked_out= with_map=
163
+ 2018-12-19 19:34:08.134716 D [28643:70323013957920 transport.rb:25] (16.6ms) USAddressClient -- #http_get -- { :path => "/address?address=2811+Safe+Harbor+Drive&city=Tampa&state=FL&zip=33618" }
164
+ 2018-12-19 19:34:08.134845 I [28643:70323013957920 client.rb:8] (16.8ms) USAddressClient -- #verify -- { :input => { :address => "2811 Safe Harbor Drive", :city => "Tampa", :state => "FL", :zip => "33618" }, :output => { :zip => "33618", :time_zone_code => "05", :time_zone => "Eastern Time", :suite_range => "", :suite_name => "", :suite => "", :suffix => "Dr", :street_name => "Safe Harbor", :state => "FL", :result_codes => [ "AS01" ], :private_mailbox_number => "", :private_mailbox_name => "", :pre_direction => "", :post_direction => "", :plus4 => "4534", :melissa_address_key_base => "", :melissa_address_key => "", :garbage => "", :delivery_point => "33618453411", :city => "Tampa", :address_type_code => "S", :address_type => "Street", :address_range => "2811", :address2 => "", :address => "2811 Safe Harbor Dr" } }
165
+ ~~~
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ # Setup bundler to avoid having to run bundle exec all the time.
2
+ require "rubygems"
3
+ require "bundler/setup"
4
+
5
+ require "rake/testtask"
6
+ require_relative "lib/us_address_client/version"
7
+
8
+ task :gem do
9
+ system "gem build us_address_client.gemspec"
10
+ end
11
+
12
+ task publish: :gem do
13
+ system "git tag -a v#{USAddressClient::VERSION} -m 'Tagging #{USAddressClient::VERSION}'"
14
+ system "git push --tags"
15
+ system "gem push us_address_client-#{USAddressClient::VERSION}.gem"
16
+ system "rm us_address_client-#{USAddressClient::VERSION}.gem"
17
+ end
18
+
19
+ Rake::TestTask.new(:test) do |t|
20
+ t.pattern = "test/**/*_test.rb"
21
+ t.verbose = true
22
+ t.warning = false
23
+ end
24
+
25
+ task default: :test
@@ -0,0 +1,98 @@
1
+ module USAddressClient
2
+ class Address
3
+ TIME_ZONES = {
4
+ "04" => "Atlantic/Bermuda",
5
+ "05" => "US/Eastern",
6
+ "06" => "US/Central",
7
+ "07" => "US/Mountain",
8
+ "08" => "US/Pacific",
9
+ "09" => "US/Alaska",
10
+ "10" => "US/Hawaii",
11
+ "11" => "US/Samoa",
12
+ "13" => "Pacific/Majuro",
13
+ "14" => "Pacific/Guam",
14
+ "15" => "Pacific/Palau"
15
+ }.freeze
16
+
17
+ # See http://wiki.melissadata.com/index.php?title=Result_Code_Details#Address_Object
18
+ def good_codes
19
+ @good_codes ||= SecretConfig.fetch(
20
+ "us_address_client/good_codes",
21
+ default: %w[AS01 AS02 AS03],
22
+ separator: ","
23
+ ).freeze
24
+ end
25
+
26
+ def bad_codes
27
+ @bad_codes ||= SecretConfig.fetch(
28
+ "us_address_client/bad_codes",
29
+ default: %w[AC02 AC03],
30
+ separator: ","
31
+ ).freeze
32
+ end
33
+
34
+ ATTRIBUTES = %i[
35
+ address
36
+ address2
37
+ suite
38
+ city
39
+ state
40
+ zip
41
+ plus4
42
+ address_key
43
+ delivery_point
44
+ time_zone_code
45
+ time_zone
46
+ address_type
47
+ address_type_code
48
+ suite_name
49
+ suite_range
50
+ address_range
51
+ pre_direction
52
+ post_direction
53
+ street_name
54
+ suffix
55
+ private_mailbox_name
56
+ private_mailbox_number
57
+ garbage
58
+ result_codes
59
+ ].freeze
60
+
61
+ attr_accessor(*ATTRIBUTES)
62
+
63
+ def initialize(attributes = {}, safe = false)
64
+ safe ? safe_assign_attributes(attributes) : self.attributes = attributes
65
+ end
66
+
67
+ # At least 1 good code and no bad codes
68
+ def valid?
69
+ (result_codes & good_codes).present? && (result_codes & bad_codes).empty?
70
+ end
71
+
72
+ def time_zone_offset
73
+ time_zone = TIME_ZONES[time_zone_code]
74
+ return unless time_zone
75
+
76
+ time_zone = "US/Arizona" if state == "AZ"
77
+ Time.now.in_time_zone(time_zone).utc_offset / -60
78
+ end
79
+
80
+ def attributes=(attributes)
81
+ attributes.each_pair { |key, value| public_send("#{key}=".to_sym, value) }
82
+ end
83
+
84
+ def attributes
85
+ ATTRIBUTES.each_with_object({}) { |key, attributes| attributes[key] = public_send(key) }
86
+ end
87
+
88
+ private
89
+
90
+ def safe_assign_attributes(attributes)
91
+ attributes.each_pair do |key, value|
92
+ next unless ATTRIBUTES.include?(key.to_sym)
93
+
94
+ public_send("#{key}=".to_sym, value)
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,29 @@
1
+ require "active_support"
2
+ require "active_support/core_ext"
3
+
4
+ module USAddressClient
5
+ class AddressMock < Address
6
+ def initialize(fields)
7
+ @result_codes = ["AS01"]
8
+ @address_type = "Street"
9
+ @time_zone_code = "05"
10
+ @plus4 = "1234"
11
+ super(fields)
12
+ end
13
+
14
+ def valid?
15
+ zip.present?
16
+ end
17
+
18
+ def delivery_point
19
+ return if zip.blank?
20
+
21
+ delivery_point_code = zip[3..5]
22
+ "#{zip}#{plus4}#{delivery_point_code}"
23
+ end
24
+
25
+ def address_key
26
+ delivery_point
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,39 @@
1
+ require "date"
2
+
3
+ module USAddressClient
4
+ class Client
5
+ # Ensure that only one instance is created by using a mutex.
6
+ # Once created a mutex is not used again since there is no writer accessor.
7
+ sync_cattr_reader :http do
8
+ OpinionatedHTTP.new(
9
+ header: {"Content-Type" => "application/json"},
10
+ secret_config_prefix: "us_address_client",
11
+ metric_prefix: "USAddressClient",
12
+ logger: USAddressClient.logger,
13
+ error_class: ServiceError
14
+ )
15
+ end
16
+
17
+ def self.verify(input_address)
18
+ return AddressMock.new(input_address) if USAddressClient.mocked?
19
+
20
+ response = http.get(action: "address", parameters: input_address)
21
+ hash = Parser.parse_verify_response(response)
22
+ USAddressClient.logger.trace(payload: hash)
23
+ Address.new(hash, true)
24
+ end
25
+
26
+ def self.version
27
+ if USAddressClient.mocked?
28
+ return {
29
+ license_expiration_date: Date.today + 365,
30
+ expiration_date: Date.today + 365,
31
+ mock: true
32
+ }
33
+ end
34
+
35
+ response = http.get(action: "version")
36
+ Parser.parse_version_response(response)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,28 @@
1
+ require "date"
2
+ module USAddressClient
3
+ class Parser
4
+ def self.parse_verify_response(response)
5
+ hash = JSON.parse(response, symbolize_names: true)
6
+ hash[:result_codes] = hash[:result_codes].split(",") if hash.key?(:result_codes)
7
+ hash
8
+ rescue StandardError => e
9
+ message = "Invalid data returned from Address Service. #{e.class.name}: #{e.message}"
10
+ USAddressClient.logger.error(message: message, metric: "Supplier/USAddressClient/exception", exception: e, payload: hash)
11
+ raise(ServiceError, message)
12
+ end
13
+
14
+ def self.parse_version_response(response)
15
+ hash = JSON.parse(response, symbolize_names: true)
16
+ hash[:database_date] = Date.parse(hash[:database_date])
17
+ # Date when the current U.S. data files expire. This date enables you to confirm that the
18
+ # data files you are using are the latest available.
19
+ hash[:expiration_date] = Date.strptime(hash[:expiration_date], "%m-%d-%Y")
20
+ hash[:license_expiration_date] = Date.parse(hash[:license_expiration_date])
21
+ hash
22
+ rescue StandardError => e
23
+ message = "Invalid data returned from Address Service#verify. #{e.class.name}: #{e.message}"
24
+ USAddressClient.logger.error(message: message, metric: "Supplier/USAddressClient/exception", exception: e, payload: hash)
25
+ raise(ServiceError, message)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ module USAddressClient
2
+ class ServiceError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module USAddressClient
2
+ VERSION = "1.0.0".freeze
3
+ end
@@ -0,0 +1,36 @@
1
+ require "us_address_client/version"
2
+ require "opinionated_http"
3
+ require "secret_config"
4
+ module USAddressClient
5
+ autoload :Address, "us_address_client/address"
6
+ autoload :AddressMock, "us_address_client/address_mock"
7
+ autoload :Client, "us_address_client/client"
8
+ autoload :Parser, "us_address_client/parser"
9
+ autoload :ServiceError, "us_address_client/service_error"
10
+
11
+ include SemanticLogger::Loggable
12
+
13
+ # Verify, and cleanse the address, also returning the delivery_point.
14
+ #
15
+ # Example:
16
+ # USAddressClient.verify(address: "2811 Safe Harbor Drive", city: "Tampa", state: "FL", zip: "33618")
17
+ def self.verify(address)
18
+ Client.verify(address)
19
+ rescue ServiceError
20
+ # Fallback to no delivery_point and continue processing as though the address was not matched.
21
+ Address.new(address, true)
22
+ end
23
+
24
+ # Returns version and license information from the Address Service.
25
+ #
26
+ # Example:
27
+ # USAddressClient.version
28
+ def self.version
29
+ Client.version
30
+ end
31
+
32
+ # Returns whether to use mocked calls for address verification.
33
+ def self.mocked?
34
+ SecretConfig.fetch("us_address_client/mocked", type: :boolean, default: false)
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
2
+ ENV["TZ"] = "UTC"
3
+ ENV["SECRET_CONFIG_PATH"] = "test"
4
+
5
+ require "yaml"
6
+ require "minitest/autorun"
7
+ require "amazing_print"
8
+ require "sync_attr"
9
+ require "semantic_logger"
10
+ require "us_address_client"
11
+
12
+ SemanticLogger.add_appender(file_name: "test.log", formatter: :color)
13
+ SemanticLogger.default_level = :debug
@@ -0,0 +1,42 @@
1
+ require "net/http"
2
+ require "json"
3
+ require_relative "test_helper"
4
+
5
+ class USAddressClientTest < Minitest::Test
6
+ describe USAddressClient do
7
+ it "successful verify" do
8
+ address = {address: "2811 Safe Harbor Dr", city: "Tampa", state: "FL", zip: "33618"}
9
+ body = address.to_json
10
+ response = stub_request(Net::HTTPSuccess, 200, "OK", body) do
11
+ USAddressClient.verify(address)
12
+ end
13
+ expected = USAddressClient::Address.new(address: "2811 Safe Harbor Dr", city: "Tampa", state: "FL", zip: "33618")
14
+ assert_equal expected.attributes, response.attributes
15
+ end
16
+
17
+ it "failed verify" do
18
+ address = {address: "2811 Safe Harbor Dr", city: "Tampa", state: "FL", zip: "33618"}
19
+ response = stub_request(Net::HTTPInternalServerError, 500, "ERROR", "") do
20
+ USAddressClient.verify(address)
21
+ end
22
+ expected = USAddressClient::Address.new(address: "2811 Safe Harbor Dr", city: "Tampa", state: "FL", zip: "33618")
23
+ assert_equal expected.attributes, response.attributes
24
+ end
25
+
26
+ it "successful version" do
27
+ response = USAddressClient::Client.version
28
+ assert response[:mock]
29
+ assert response[:license_expiration_date]
30
+ assert response[:expiration_date]
31
+ end
32
+
33
+ def stub_request(klass, code, msg, body, &block)
34
+ response = klass.new("1.1", code, msg)
35
+ response.stub(:body, body) do
36
+ USAddressClient.stub(:mocked?, false) do
37
+ USAddressClient::Client.http.driver.stub(:request, response, &block)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: us_address_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Reid Morrison
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: opinionated_http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: secret_config
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sync_attr
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'
55
+ description: US Address Client that calls the privately hosted US Address Service
56
+ that calls Melissa Data Address Object
57
+ email:
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/us_address_client.rb
66
+ - lib/us_address_client/address.rb
67
+ - lib/us_address_client/address_mock.rb
68
+ - lib/us_address_client/client.rb
69
+ - lib/us_address_client/parser.rb
70
+ - lib/us_address_client/service_error.rb
71
+ - lib/us_address_client/version.rb
72
+ - test/test_helper.rb
73
+ - test/us_address_client_test.rb
74
+ homepage: https://github.com/reidmorrison/us_address_client
75
+ licenses:
76
+ - Apache-2.0
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '2.5'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubygems_version: 3.2.22
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: US Address Service Client
97
+ test_files:
98
+ - test/test_helper.rb
99
+ - test/us_address_client_test.rb