Class: SmartyStreets::USAutocomplete::Client
- Inherits:
-
Object
- Object
- SmartyStreets::USAutocomplete::Client
- Defined in:
- lib/smartystreets_ruby_sdk/us_autocomplete/client.rb
Overview
It is recommended to instantiate this class using ClientBuilder.build_us_autocomplete_api_client
Instance Method Summary collapse
- #add_parameter(request, key, value) ⇒ Object
- #build_filter_string(filter_list) ⇒ Object
- #build_request(lookup) ⇒ Object
- #convert_suggestions(suggestion_hashes) ⇒ Object
-
#initialize(sender, serializer) ⇒ Client
constructor
A new instance of Client.
-
#send(lookup) ⇒ Object
Sends a Lookup object to the US Autocomplete API and stores the result in the Lookup’s result field.
Constructor Details
#initialize(sender, serializer) ⇒ Client
Returns a new instance of Client.
10 11 12 13 |
# File 'lib/smartystreets_ruby_sdk/us_autocomplete/client.rb', line 10 def initialize(sender, serializer) @sender = sender @serializer = serializer end |
Instance Method Details
#add_parameter(request, key, value) ⇒ Object
65 66 67 |
# File 'lib/smartystreets_ruby_sdk/us_autocomplete/client.rb', line 65 def add_parameter(request, key, value) request.parameters[key] = value unless value.nil? or value.empty? end |
#build_filter_string(filter_list) ⇒ Object
50 51 52 |
# File 'lib/smartystreets_ruby_sdk/us_autocomplete/client.rb', line 50 def build_filter_string(filter_list) filter_list ? filter_list.join(',') : nil end |
#build_request(lookup) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/smartystreets_ruby_sdk/us_autocomplete/client.rb', line 31 def build_request(lookup) request = Request.new add_parameter(request, 'prefix', lookup.prefix) add_parameter(request, 'suggestions', lookup.max_suggestions.to_s) add_parameter(request, 'city_filter', build_filter_string(lookup.city_filter)) add_parameter(request, 'state_filter', build_filter_string(lookup.state_filter)) add_parameter(request, 'prefer', build_filter_string(lookup.prefer)) add_parameter(request, 'prefer_ratio', lookup.prefer_ratio.to_s) if lookup.geolocate_type != GeolocationType::NONE request.parameters['geolocate'] = 'true' request.parameters['geolocate_precision'] = lookup.geolocate_type else request.parameters['geolocate'] = 'false' end request end |
#convert_suggestions(suggestion_hashes) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/smartystreets_ruby_sdk/us_autocomplete/client.rb', line 54 def convert_suggestions(suggestion_hashes) converted_suggestions = [] return converted_suggestions if suggestion_hashes.nil? suggestion_hashes.each do |suggestion| converted_suggestions.push(USAutocomplete::Suggestion.new(suggestion)) end converted_suggestions end |
#send(lookup) ⇒ Object
Sends a Lookup object to the US Autocomplete API and stores the result in the Lookup’s result field.
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/smartystreets_ruby_sdk/us_autocomplete/client.rb', line 16 def send(lookup) if not lookup or not lookup.prefix raise SmartyStreets::SmartyError, 'Send() must be passed a Lookup with the prefix field set.' end request = build_request(lookup) response = @sender.send(request) result = @serializer.deserialize(response.payload) suggestions = convert_suggestions(result.fetch('suggestions', [])) lookup.result = suggestions end |