smartystreets_ruby_sdk 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/gem-publish.yml +58 -0
- data/.gitignore +4 -0
- data/CHANGELOG.md +5 -0
- data/Dockerfile +10 -0
- data/Gemfile +4 -0
- data/LICENSE.md +202 -0
- data/Makefile +33 -0
- data/README.md +31 -0
- data/Rakefile +5 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/docker-compose.yml +12 -0
- data/examples/international_autocomplete_example.rb +47 -0
- data/examples/international_example.rb +53 -0
- data/examples/us_autocomplete_pro_example.rb +56 -0
- data/examples/us_extract_example.rb +63 -0
- data/examples/us_reverse_geo_example.rb +47 -0
- data/examples/us_street_multiple_address_example.rb +87 -0
- data/examples/us_street_single_address_example.rb +67 -0
- data/examples/us_zipcode_multiple_lookup_example.rb +80 -0
- data/examples/us_zipcode_single_lookup_example.rb +55 -0
- data/lib/smartystreets_ruby_sdk/batch.rb +59 -0
- data/lib/smartystreets_ruby_sdk/client_builder.rb +188 -0
- data/lib/smartystreets_ruby_sdk/custom_header_sender.rb +13 -0
- data/lib/smartystreets_ruby_sdk/errors.rb +27 -0
- data/lib/smartystreets_ruby_sdk/exceptions.rb +39 -0
- data/lib/smartystreets_ruby_sdk/international_autocomplete/client.rb +61 -0
- data/lib/smartystreets_ruby_sdk/international_autocomplete/lookup.rb +21 -0
- data/lib/smartystreets_ruby_sdk/international_autocomplete/suggestion.rb +16 -0
- data/lib/smartystreets_ruby_sdk/international_street/analysis.rb +17 -0
- data/lib/smartystreets_ruby_sdk/international_street/candidate.rb +23 -0
- data/lib/smartystreets_ruby_sdk/international_street/changes.rb +15 -0
- data/lib/smartystreets_ruby_sdk/international_street/client.rb +63 -0
- data/lib/smartystreets_ruby_sdk/international_street/components.rb +58 -0
- data/lib/smartystreets_ruby_sdk/international_street/language_mode.rb +9 -0
- data/lib/smartystreets_ruby_sdk/international_street/lookup.rb +82 -0
- data/lib/smartystreets_ruby_sdk/international_street/metadata.rb +17 -0
- data/lib/smartystreets_ruby_sdk/international_street/rootlevel.rb +25 -0
- data/lib/smartystreets_ruby_sdk/international_street.rb +12 -0
- data/lib/smartystreets_ruby_sdk/json_able.rb +19 -0
- data/lib/smartystreets_ruby_sdk/license_sender.rb +15 -0
- data/lib/smartystreets_ruby_sdk/logger.rb +7 -0
- data/lib/smartystreets_ruby_sdk/native_sender.rb +85 -0
- data/lib/smartystreets_ruby_sdk/native_serializer.rb +13 -0
- data/lib/smartystreets_ruby_sdk/proxy.rb +16 -0
- data/lib/smartystreets_ruby_sdk/request.rb +14 -0
- data/lib/smartystreets_ruby_sdk/response.rb +11 -0
- data/lib/smartystreets_ruby_sdk/retry_sender.rb +39 -0
- data/lib/smartystreets_ruby_sdk/shared_credentials.rb +13 -0
- data/lib/smartystreets_ruby_sdk/signing_sender.rb +13 -0
- data/lib/smartystreets_ruby_sdk/sleeper.rb +7 -0
- data/lib/smartystreets_ruby_sdk/static_credentials.rb +13 -0
- data/lib/smartystreets_ruby_sdk/status_code_sender.rb +41 -0
- data/lib/smartystreets_ruby_sdk/url_prefix_sender.rb +14 -0
- data/lib/smartystreets_ruby_sdk/us_autocomplete/client.rb +72 -0
- data/lib/smartystreets_ruby_sdk/us_autocomplete/geolocation_type.rb +9 -0
- data/lib/smartystreets_ruby_sdk/us_autocomplete/lookup.rb +38 -0
- data/lib/smartystreets_ruby_sdk/us_autocomplete/suggestion.rb +16 -0
- data/lib/smartystreets_ruby_sdk/us_autocomplete.rb +9 -0
- data/lib/smartystreets_ruby_sdk/us_autocomplete_pro/client.rb +78 -0
- data/lib/smartystreets_ruby_sdk/us_autocomplete_pro/geolocation_type.rb +8 -0
- data/lib/smartystreets_ruby_sdk/us_autocomplete_pro/lookup.rb +63 -0
- data/lib/smartystreets_ruby_sdk/us_autocomplete_pro/suggestion.rb +18 -0
- data/lib/smartystreets_ruby_sdk/us_autocomplete_pro.rb +10 -0
- data/lib/smartystreets_ruby_sdk/us_extract/address.rb +24 -0
- data/lib/smartystreets_ruby_sdk/us_extract/client.rb +49 -0
- data/lib/smartystreets_ruby_sdk/us_extract/lookup.rb +22 -0
- data/lib/smartystreets_ruby_sdk/us_extract/metadata.rb +17 -0
- data/lib/smartystreets_ruby_sdk/us_extract/result.rb +21 -0
- data/lib/smartystreets_ruby_sdk/us_extract.rb +10 -0
- data/lib/smartystreets_ruby_sdk/us_reverse_geo/address.rb +16 -0
- data/lib/smartystreets_ruby_sdk/us_reverse_geo/client.rb +38 -0
- data/lib/smartystreets_ruby_sdk/us_reverse_geo/coordinate.rb +25 -0
- data/lib/smartystreets_ruby_sdk/us_reverse_geo/lookup.rb +21 -0
- data/lib/smartystreets_ruby_sdk/us_reverse_geo/result.rb +20 -0
- data/lib/smartystreets_ruby_sdk/us_reverse_geo/us_reverse_geo_response.rb +17 -0
- data/lib/smartystreets_ruby_sdk/us_reverse_geo.rb +12 -0
- data/lib/smartystreets_ruby_sdk/us_street/analysis.rb +24 -0
- data/lib/smartystreets_ruby_sdk/us_street/candidate.rb +27 -0
- data/lib/smartystreets_ruby_sdk/us_street/client.rb +78 -0
- data/lib/smartystreets_ruby_sdk/us_street/components.rb +35 -0
- data/lib/smartystreets_ruby_sdk/us_street/lookup.rb +32 -0
- data/lib/smartystreets_ruby_sdk/us_street/match_type.rb +10 -0
- data/lib/smartystreets_ruby_sdk/us_street/metadata.rb +30 -0
- data/lib/smartystreets_ruby_sdk/us_street.rb +12 -0
- data/lib/smartystreets_ruby_sdk/us_zipcode/alternate_county.rb +15 -0
- data/lib/smartystreets_ruby_sdk/us_zipcode/city.rb +16 -0
- data/lib/smartystreets_ruby_sdk/us_zipcode/client.rb +72 -0
- data/lib/smartystreets_ruby_sdk/us_zipcode/lookup.rb +21 -0
- data/lib/smartystreets_ruby_sdk/us_zipcode/result.rb +47 -0
- data/lib/smartystreets_ruby_sdk/us_zipcode/zip_code.rb +33 -0
- data/lib/smartystreets_ruby_sdk/us_zipcode.rb +11 -0
- data/lib/smartystreets_ruby_sdk/version.rb +3 -0
- data/lib/smartystreets_ruby_sdk.rb +33 -0
- data/ruby-sdk-demo.json +354 -0
- data/smartystreets_ruby_sdk.gemspec +26 -0
- metadata +201 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
module SmartyStreets
|
2
|
+
module USReverseGeo
|
3
|
+
# In addition to holding all of the input data for this lookup, this class also will contain the
|
4
|
+
# result of the lookup after it comes back from the API.
|
5
|
+
#
|
6
|
+
# Note: Lookups must have certain required fields set with non-blank values.
|
7
|
+
# These can be found at the URL below.
|
8
|
+
#
|
9
|
+
# See "https://smartystreets.com/docs/cloud/us-reverse-geo-api#http-request-input-fields"
|
10
|
+
|
11
|
+
class Lookup
|
12
|
+
|
13
|
+
attr_accessor :latitude, :longitude, :response
|
14
|
+
|
15
|
+
def initialize(latitude, longitude)
|
16
|
+
@latitude = sprintf('%.8f', latitude)
|
17
|
+
@longitude = sprintf('%.8f', longitude)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative 'coordinate'
|
2
|
+
require_relative 'address'
|
3
|
+
|
4
|
+
module SmartyStreets
|
5
|
+
module USReverseGeo
|
6
|
+
# A result is a possible match for an geocode that was submitted. A lookup can have multiple results.
|
7
|
+
#
|
8
|
+
# See "https://smartystreets.com/docs/cloud/us-reverse-geo-api#result"
|
9
|
+
class Result
|
10
|
+
attr_reader :address, :coordinate, :distance
|
11
|
+
|
12
|
+
def initialize(obj)
|
13
|
+
@address = Address.new(obj.fetch('address', {}))
|
14
|
+
@coordinate = Coordinate.new(obj.fetch('coordinate', {}))
|
15
|
+
@distance = obj['distance']
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative 'result'
|
2
|
+
|
3
|
+
module SmartyStreets
|
4
|
+
module USReverseGeo
|
5
|
+
class Response
|
6
|
+
attr_reader :results
|
7
|
+
|
8
|
+
def initialize(obj)
|
9
|
+
@results = []
|
10
|
+
|
11
|
+
obj['results'].each do |result|
|
12
|
+
@results.push(Result.new(result))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative './us_reverse_geo/address'
|
2
|
+
require_relative './us_reverse_geo/client'
|
3
|
+
require_relative './us_reverse_geo/coordinate'
|
4
|
+
require_relative './us_reverse_geo/lookup'
|
5
|
+
require_relative './us_reverse_geo/result'
|
6
|
+
require_relative './us_reverse_geo/us_reverse_geo_response'
|
7
|
+
|
8
|
+
module SmartyStreets
|
9
|
+
module USReverseGeo
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module SmartyStreets
|
2
|
+
module USStreet
|
3
|
+
# See "https://smartystreets.com/docs/cloud/us-street-api#analysis"
|
4
|
+
class Analysis
|
5
|
+
attr_reader :lacs_link_code, :active, :footnotes, :lacs_link_indicator, :dpv_match_code, :is_suite_link_match,
|
6
|
+
:is_ews_match, :dpv_footnotes, :cmra, :vacant, :no_stat, :enhanced_match
|
7
|
+
|
8
|
+
def initialize(obj)
|
9
|
+
@dpv_match_code = obj['dpv_match_code']
|
10
|
+
@dpv_footnotes = obj['dpv_footnotes']
|
11
|
+
@cmra = obj['dpv_cmra']
|
12
|
+
@vacant = obj['dpv_vacant']
|
13
|
+
@no_stat = obj['dpv_no_stat']
|
14
|
+
@active = obj['active']
|
15
|
+
@is_ews_match = obj['ews_match']
|
16
|
+
@footnotes = obj['footnotes']
|
17
|
+
@lacs_link_code = obj['lacslink_code']
|
18
|
+
@lacs_link_indicator = obj['lacslink_indicator']
|
19
|
+
@is_suite_link_match = obj['suitelink_match']
|
20
|
+
@enhanced_match = obj['enhanced_match']
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'components'
|
2
|
+
require_relative 'metadata'
|
3
|
+
require_relative 'analysis'
|
4
|
+
|
5
|
+
module SmartyStreets
|
6
|
+
module USStreet
|
7
|
+
# See "https://smartystreets.com/docs/cloud/us-street-api#metadata"
|
8
|
+
class Candidate
|
9
|
+
attr_reader :input_id, :input_index, :candidate_index, :addressee, :delivery_line_1, :delivery_line_2, :delivery_point_barcode,
|
10
|
+
:last_line, :metadata, :components, :analysis
|
11
|
+
|
12
|
+
def initialize(obj)
|
13
|
+
@input_id = obj['input_id']
|
14
|
+
@input_index = obj['input_index']
|
15
|
+
@candidate_index = obj['candidate_index']
|
16
|
+
@addressee = obj['addressee']
|
17
|
+
@delivery_line_1 = obj['delivery_line_1']
|
18
|
+
@delivery_line_2 = obj['delivery_line_2']
|
19
|
+
@last_line = obj['last_line']
|
20
|
+
@delivery_point_barcode = obj['delivery_point_barcode']
|
21
|
+
@components = Components.new(obj.fetch('components', {}))
|
22
|
+
@metadata = Metadata.new(obj.fetch('metadata', {}))
|
23
|
+
@analysis = Analysis.new(obj.fetch('analysis', {}))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require_relative '../batch'
|
2
|
+
require_relative '../request'
|
3
|
+
require_relative 'candidate'
|
4
|
+
|
5
|
+
module SmartyStreets
|
6
|
+
module USStreet
|
7
|
+
# It is recommended to instantiate this class using ClientBuilder.build_us_street_api_client
|
8
|
+
class Client
|
9
|
+
def initialize(sender, serializer)
|
10
|
+
@sender = sender
|
11
|
+
@serializer = serializer
|
12
|
+
end
|
13
|
+
|
14
|
+
# Sends a Lookup object to the US Street API and stores the result in the Lookup's result field.
|
15
|
+
def send_lookup(lookup)
|
16
|
+
batch = SmartyStreets::Batch.new
|
17
|
+
batch.add(lookup)
|
18
|
+
send_batch(batch)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Sends a Batch object containing no more than 100 Lookup objects to the US Street API and stores the
|
22
|
+
# results in the result field of the Lookup object.
|
23
|
+
def send_batch(batch)
|
24
|
+
smarty_request = Request.new
|
25
|
+
|
26
|
+
return if batch.empty?
|
27
|
+
|
28
|
+
converted_lookups = remap_keys(batch.all_lookups)
|
29
|
+
|
30
|
+
if batch.size > 1
|
31
|
+
smarty_request.payload = @serializer.serialize(converted_lookups)
|
32
|
+
else
|
33
|
+
smarty_request.parameters = converted_lookups[0]
|
34
|
+
end
|
35
|
+
|
36
|
+
response = @sender.send(smarty_request)
|
37
|
+
|
38
|
+
raise response.error if response.error
|
39
|
+
|
40
|
+
candidates = @serializer.deserialize(response.payload)
|
41
|
+
candidates = [] if candidates.nil?
|
42
|
+
|
43
|
+
assign_candidates_to_lookups(batch, candidates)
|
44
|
+
end
|
45
|
+
|
46
|
+
def remap_keys(obj)
|
47
|
+
converted_obj = []
|
48
|
+
obj.each do |lookup|
|
49
|
+
converted_lookup = {}
|
50
|
+
lookup.candidates = 5 if lookup.match == "enhanced" && lookup.candidates == 0
|
51
|
+
|
52
|
+
converted_lookup['input_id'] = lookup.input_id
|
53
|
+
converted_lookup['street'] = lookup.street
|
54
|
+
converted_lookup['street2'] = lookup.street2
|
55
|
+
converted_lookup['secondary'] = lookup.secondary
|
56
|
+
converted_lookup['city'] = lookup.city
|
57
|
+
converted_lookup['state'] = lookup.state
|
58
|
+
converted_lookup['zipcode'] = lookup.zipcode
|
59
|
+
converted_lookup['lastline'] = lookup.lastline
|
60
|
+
converted_lookup['addressee'] = lookup.addressee
|
61
|
+
converted_lookup['urbanization'] = lookup.urbanization
|
62
|
+
converted_lookup['match'] = lookup.match
|
63
|
+
converted_lookup['candidates'] = lookup.candidates
|
64
|
+
|
65
|
+
converted_obj.push(converted_lookup)
|
66
|
+
end
|
67
|
+
converted_obj
|
68
|
+
end
|
69
|
+
|
70
|
+
def assign_candidates_to_lookups(batch, candidates)
|
71
|
+
candidates.each do |raw_candidate|
|
72
|
+
candidate = Candidate.new(raw_candidate)
|
73
|
+
batch[candidate.input_index].result.push(candidate)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module SmartyStreets
|
2
|
+
module USStreet
|
3
|
+
# This class contains the matched address broken down into its fundamental pieces.
|
4
|
+
#
|
5
|
+
# See "https://smartystreets.com/docs/cloud/us-street-api#components"
|
6
|
+
class Components
|
7
|
+
attr_reader :street_postdirection, :delivery_point_check_digit, :secondary_designator, :secondary_number, :zipcode,
|
8
|
+
:pmb_number, :state_abbreviation, :extra_secondary_designator, :urbanization, :street_name, :city_name,
|
9
|
+
:default_city_name, :street_suffix, :primary_number, :plus4_code, :street_predirection, :pmb_designator,
|
10
|
+
:extra_secondary_number, :delivery_point
|
11
|
+
|
12
|
+
def initialize(obj)
|
13
|
+
@urbanization = obj['urbanization']
|
14
|
+
@primary_number = obj['primary_number']
|
15
|
+
@street_name = obj['street_name']
|
16
|
+
@street_predirection = obj['street_predirection']
|
17
|
+
@street_postdirection = obj['street_postdirection']
|
18
|
+
@street_suffix = obj['street_suffix']
|
19
|
+
@secondary_number = obj['secondary_number']
|
20
|
+
@secondary_designator = obj['secondary_designator']
|
21
|
+
@extra_secondary_number = obj['extra_secondary_number']
|
22
|
+
@extra_secondary_designator = obj['extra_secondary_designator']
|
23
|
+
@pmb_designator = obj['pmb_designator']
|
24
|
+
@pmb_number = obj['pmb_number']
|
25
|
+
@city_name = obj['city_name']
|
26
|
+
@default_city_name = obj['default_city_name']
|
27
|
+
@state_abbreviation = obj['state_abbreviation']
|
28
|
+
@zipcode = obj['zipcode']
|
29
|
+
@plus4_code = obj['plus4_code']
|
30
|
+
@delivery_point = obj['delivery_point']
|
31
|
+
@delivery_point_check_digit = obj['delivery_point_check_digit']
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative '../json_able'
|
2
|
+
module SmartyStreets
|
3
|
+
module USStreet
|
4
|
+
# In addition to holding all of the input data for this lookup, this class also will contain
|
5
|
+
# the result of the lookup after it comes back from the API.
|
6
|
+
#
|
7
|
+
# See "https://smartystreets.com/docs/cloud/us-street-api#input-fields"
|
8
|
+
#
|
9
|
+
# @match:: Must be set to 'strict', 'range', or 'invalid'. Constants for these are in match_type.rb
|
10
|
+
class Lookup < JSONAble
|
11
|
+
attr_accessor :input_id, :street, :street2, :secondary, :city, :state, :zipcode, :lastline, :addressee, :urbanization,
|
12
|
+
:match, :candidates, :result
|
13
|
+
|
14
|
+
def initialize(street=nil, street2=nil, secondary=nil, city=nil, state=nil, zipcode=nil, lastline=nil,
|
15
|
+
addressee=nil, urbanization=nil, match=nil, candidates=0, input_id=nil)
|
16
|
+
@input_id = input_id
|
17
|
+
@street = street
|
18
|
+
@street2 = street2
|
19
|
+
@secondary = secondary
|
20
|
+
@city = city
|
21
|
+
@state = state
|
22
|
+
@zipcode = zipcode
|
23
|
+
@lastline = lastline
|
24
|
+
@addressee = addressee
|
25
|
+
@urbanization = urbanization
|
26
|
+
@match = match
|
27
|
+
@candidates = candidates
|
28
|
+
@result = []
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module SmartyStreets
|
2
|
+
module USStreet
|
3
|
+
# See "https://smartystreets.com/docs/cloud/us-street-api#metadata"
|
4
|
+
class Metadata
|
5
|
+
attr_reader :elot_sort, :longitude, :elot_sequence, :county_fips, :building_default_indicator, :rdi,
|
6
|
+
:congressional_district, :latitude, :precision, :time_zone, :zip_type, :county_name, :utc_offset,
|
7
|
+
:record_type, :carrier_route, :obeys_dst, :is_an_ews_match
|
8
|
+
|
9
|
+
def initialize(obj)
|
10
|
+
@record_type = obj['record_type']
|
11
|
+
@zip_type = obj['zip_type']
|
12
|
+
@county_fips = obj['county_fips']
|
13
|
+
@county_name = obj['county_name']
|
14
|
+
@carrier_route = obj['carrier_route']
|
15
|
+
@congressional_district = obj['congressional_district']
|
16
|
+
@building_default_indicator = obj['building_default_indicator']
|
17
|
+
@rdi = obj['rdi']
|
18
|
+
@elot_sequence = obj['elot_sequence']
|
19
|
+
@elot_sort = obj['elot_sort']
|
20
|
+
@latitude = obj['latitude']
|
21
|
+
@longitude = obj['longitude']
|
22
|
+
@precision = obj['precision']
|
23
|
+
@time_zone = obj['time_zone']
|
24
|
+
@utc_offset = obj['utc_offset']
|
25
|
+
@obeys_dst = obj['dst']
|
26
|
+
@is_an_ews_match = obj['ews_match']
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative './us_street/analysis'
|
2
|
+
require_relative './us_street/candidate'
|
3
|
+
require_relative './us_street/client'
|
4
|
+
require_relative './us_street/components'
|
5
|
+
require_relative './us_street/lookup'
|
6
|
+
require_relative './us_street/metadata'
|
7
|
+
require_relative './us_street/match_type'
|
8
|
+
|
9
|
+
module SmartyStreets
|
10
|
+
module USStreet
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module SmartyStreets
|
2
|
+
module USZipcode
|
3
|
+
# See "https://smartystreets.com/docs/cloud/us-zipcode-api#zipcodes"
|
4
|
+
class AlternateCounty
|
5
|
+
attr_reader :state_abbreviation, :state, :county_name, :county_fips
|
6
|
+
|
7
|
+
def initialize(obj)
|
8
|
+
@county_fips = obj.fetch('county_fips', nil)
|
9
|
+
@county_name = obj.fetch('county_name', nil)
|
10
|
+
@state_abbreviation = obj.fetch('state_abbreviation', nil)
|
11
|
+
@state = obj.fetch('state', nil)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module SmartyStreets
|
2
|
+
module USZipcode
|
3
|
+
# Known in the SmartyStreets US ZIP Code API documentation as a city_state
|
4
|
+
# See "https://smartystreets.com/docs/cloud/us-zipcode-api#cities"
|
5
|
+
class City
|
6
|
+
attr_reader :mailable_city, :state_abbreviation, :state, :city
|
7
|
+
|
8
|
+
def initialize(obj)
|
9
|
+
@city = obj['city']
|
10
|
+
@mailable_city = obj['mailable_city']
|
11
|
+
@state_abbreviation = obj['state_abbreviation']
|
12
|
+
@state = obj['state']
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require_relative 'result'
|
2
|
+
require_relative '../batch'
|
3
|
+
require_relative '../request'
|
4
|
+
|
5
|
+
module SmartyStreets
|
6
|
+
module USZipcode
|
7
|
+
# It is recommended to instantiate this class using ClientBuilder.build_us_zipcode_api_client.
|
8
|
+
class Client
|
9
|
+
def initialize(sender, serializer)
|
10
|
+
@sender = sender
|
11
|
+
@serializer = serializer
|
12
|
+
end
|
13
|
+
|
14
|
+
# Sends a Lookup object to the US ZIP Code API and stores the result in the Lookup's result field.
|
15
|
+
def send_lookup(lookup)
|
16
|
+
batch = Batch.new
|
17
|
+
batch.add(lookup)
|
18
|
+
send_batch(batch)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Sends a Batch object containing no more than 100 Lookup objects to the US ZIP Code API and stores the
|
22
|
+
# results in the result field of the Lookup object.
|
23
|
+
def send_batch(batch)
|
24
|
+
smarty_request = Request.new
|
25
|
+
|
26
|
+
return if batch.empty?
|
27
|
+
|
28
|
+
converted_lookups = remap_keys(batch.all_lookups)
|
29
|
+
|
30
|
+
if batch.size > 1
|
31
|
+
smarty_request.payload = @serializer.serialize(converted_lookups)
|
32
|
+
else
|
33
|
+
smarty_request.parameters = converted_lookups[0]
|
34
|
+
end
|
35
|
+
|
36
|
+
response = @sender.send(smarty_request)
|
37
|
+
|
38
|
+
raise response.error if response.error
|
39
|
+
|
40
|
+
results = @serializer.deserialize(response.payload)
|
41
|
+
results = [] if results.nil?
|
42
|
+
assign_results_to_lookups(batch, results)
|
43
|
+
end
|
44
|
+
|
45
|
+
def assign_results_to_lookups(batch, results)
|
46
|
+
results.each do |raw_result|
|
47
|
+
result = Result.new(raw_result)
|
48
|
+
batch[result.input_index].result = result
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def remap_keys(obj)
|
53
|
+
converted_obj = []
|
54
|
+
obj.each do |lookup|
|
55
|
+
converted_lookup = {}
|
56
|
+
|
57
|
+
add_field(converted_lookup, 'city', lookup.city)
|
58
|
+
add_field(converted_lookup, 'state', lookup.state)
|
59
|
+
add_field(converted_lookup, 'zipcode', lookup.zipcode)
|
60
|
+
|
61
|
+
converted_obj.push(converted_lookup)
|
62
|
+
end
|
63
|
+
|
64
|
+
converted_obj
|
65
|
+
end
|
66
|
+
|
67
|
+
def add_field(converted_lookup, key, value)
|
68
|
+
converted_lookup[key] = value unless value.nil? or value.empty?
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative '../json_able'
|
2
|
+
|
3
|
+
module SmartyStreets
|
4
|
+
module USZipcode
|
5
|
+
# In addition to holding all of the input data for this lookup, this class also
|
6
|
+
# will contain the result of the lookup after it comes back from the API.
|
7
|
+
#
|
8
|
+
# See "https://smartystreets.com/docs/cloud/us-zipcode-api#http-request-input-fields"
|
9
|
+
class Lookup < JSONAble
|
10
|
+
attr_accessor :result, :state, :zipcode, :input_id, :city
|
11
|
+
|
12
|
+
def initialize(city=nil, state=nil, zipcode=nil, input_id=nil)
|
13
|
+
@result = nil
|
14
|
+
@input_id = input_id
|
15
|
+
@city = city
|
16
|
+
@state = state
|
17
|
+
@zipcode = zipcode
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative 'city'
|
2
|
+
require_relative 'zip_code'
|
3
|
+
|
4
|
+
module SmartyStreets
|
5
|
+
module USZipcode
|
6
|
+
# See "https://smartystreets.com/docs/cloud/us-zipcode-api#root"
|
7
|
+
class Result
|
8
|
+
attr_reader :reason, :input_id, :input_index, :cities, :zipcodes, :status
|
9
|
+
|
10
|
+
def initialize(obj)
|
11
|
+
@status = obj['status']
|
12
|
+
@reason = obj['reason']
|
13
|
+
@input_id = obj['input_id']
|
14
|
+
@input_index = obj['input_index']
|
15
|
+
@cities = obj.fetch('city_states', [])
|
16
|
+
@zipcodes = obj.fetch('zipcodes', [])
|
17
|
+
|
18
|
+
@cities = convert_cities
|
19
|
+
@zipcodes = convert_zipcodes
|
20
|
+
end
|
21
|
+
|
22
|
+
def valid?
|
23
|
+
@status.nil? and @reason.nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
def convert_cities
|
27
|
+
converted_cities = []
|
28
|
+
|
29
|
+
@cities.each do |city|
|
30
|
+
converted_cities.push(City.new(city))
|
31
|
+
end
|
32
|
+
|
33
|
+
converted_cities
|
34
|
+
end
|
35
|
+
|
36
|
+
def convert_zipcodes
|
37
|
+
converted_zipcodes = []
|
38
|
+
|
39
|
+
@zipcodes.each do |zipcode|
|
40
|
+
converted_zipcodes.push(ZipCode.new(zipcode))
|
41
|
+
end
|
42
|
+
|
43
|
+
converted_zipcodes
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative 'alternate_county'
|
2
|
+
|
3
|
+
module SmartyStreets
|
4
|
+
module USZipcode
|
5
|
+
# See "https://smartystreets.com/docs/cloud/us-zipcode-api#zipcodes"
|
6
|
+
class ZipCode
|
7
|
+
attr_reader :longitude, :county_name, :zipcode, :zipcode_type, :county_fips,
|
8
|
+
:latitude, :precision, :default_city, :alternate_counties,
|
9
|
+
:state_abbreviation, :state
|
10
|
+
|
11
|
+
def initialize(obj)
|
12
|
+
@zipcode = obj['zipcode']
|
13
|
+
@zipcode_type = obj['zipcode_type']
|
14
|
+
@default_city = obj['default_city']
|
15
|
+
@county_fips = obj['county_fips']
|
16
|
+
@county_name = obj['county_name']
|
17
|
+
@latitude = obj['latitude']
|
18
|
+
@longitude = obj['longitude']
|
19
|
+
@precision = obj['precision']
|
20
|
+
@state = obj['state']
|
21
|
+
@state_abbreviation = obj['state_abbreviation']
|
22
|
+
alternate_counties = obj.fetch('alternate_counties', [])
|
23
|
+
|
24
|
+
@alternate_counties = []
|
25
|
+
|
26
|
+
alternate_counties.each do |county|
|
27
|
+
@alternate_counties.push(USZipcode::AlternateCounty.new(county))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require_relative './us_zipcode/city'
|
2
|
+
require_relative './us_zipcode/client'
|
3
|
+
require_relative './us_zipcode/lookup'
|
4
|
+
require_relative './us_zipcode/result'
|
5
|
+
require_relative './us_zipcode/zip_code'
|
6
|
+
require_relative './us_zipcode/alternate_county'
|
7
|
+
|
8
|
+
module SmartyStreets
|
9
|
+
module USZipcode
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'smartystreets_ruby_sdk/version'
|
2
|
+
require 'smartystreets_ruby_sdk/logger'
|
3
|
+
require 'smartystreets_ruby_sdk/sleeper'
|
4
|
+
|
5
|
+
require 'smartystreets_ruby_sdk/batch'
|
6
|
+
require 'smartystreets_ruby_sdk/client_builder'
|
7
|
+
require 'smartystreets_ruby_sdk/custom_header_sender'
|
8
|
+
require 'smartystreets_ruby_sdk/errors'
|
9
|
+
require 'smartystreets_ruby_sdk/exceptions'
|
10
|
+
require 'smartystreets_ruby_sdk/json_able'
|
11
|
+
require 'smartystreets_ruby_sdk/logger'
|
12
|
+
require 'smartystreets_ruby_sdk/native_sender'
|
13
|
+
require 'smartystreets_ruby_sdk/native_serializer'
|
14
|
+
require 'smartystreets_ruby_sdk/request'
|
15
|
+
require 'smartystreets_ruby_sdk/response'
|
16
|
+
require 'smartystreets_ruby_sdk/retry_sender'
|
17
|
+
require 'smartystreets_ruby_sdk/license_sender'
|
18
|
+
require 'smartystreets_ruby_sdk/shared_credentials'
|
19
|
+
require 'smartystreets_ruby_sdk/signing_sender'
|
20
|
+
require 'smartystreets_ruby_sdk/static_credentials'
|
21
|
+
require 'smartystreets_ruby_sdk/status_code_sender'
|
22
|
+
require 'smartystreets_ruby_sdk/url_prefix_sender'
|
23
|
+
|
24
|
+
require 'smartystreets_ruby_sdk/us_extract'
|
25
|
+
require 'smartystreets_ruby_sdk/us_street'
|
26
|
+
require 'smartystreets_ruby_sdk/us_zipcode'
|
27
|
+
require 'smartystreets_ruby_sdk/us_autocomplete'
|
28
|
+
require 'smartystreets_ruby_sdk/us_autocomplete_pro'
|
29
|
+
require 'smartystreets_ruby_sdk/international_street'
|
30
|
+
require 'smartystreets_ruby_sdk/us_reverse_geo'
|
31
|
+
|
32
|
+
module SmartyStreets
|
33
|
+
end
|