Class: SmartyStreets::USAutocompletePro::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/smartystreets_ruby_sdk/us_autocomplete_pro/client.rb

Overview

It is recommended to instantiate this class using ClientBuilder.build_us_autocomplete_pro_api_client

Instance Method Summary collapse

Constructor Details

#initialize(sender, serializer) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/smartystreets_ruby_sdk/us_autocomplete_pro/client.rb', line 10

def initialize(sender, serializer)
  @sender = sender
  @serializer = serializer
end

Instance Method Details

#add_parameter(request, key, value) ⇒ Object



72
73
74
# File 'lib/smartystreets_ruby_sdk/us_autocomplete_pro/client.rb', line 72

def add_parameter(request, key, value)
  request.parameters[key] = value unless value.nil? or value.empty?
end

#build_filter_string(filter_list) ⇒ Object



57
58
59
# File 'lib/smartystreets_ruby_sdk/us_autocomplete_pro/client.rb', line 57

def build_filter_string(filter_list)
  filter_list ? filter_list.join(';') : nil
end

#build_request(lookup) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/smartystreets_ruby_sdk/us_autocomplete_pro/client.rb', line 33

def build_request(lookup)
  request = Request.new

  add_parameter(request, 'search', lookup.search)
  add_parameter(request, 'max_results', lookup.max_results.to_s)
  add_parameter(request, 'include_only_cities', build_filter_string(lookup.city_filter))
  add_parameter(request, 'include_only_states', build_filter_string(lookup.state_filter))
  add_parameter(request, 'include_only_zip_codes', build_filter_string(lookup.zip_filter))
  add_parameter(request, 'exclude_states', build_filter_string(lookup.exclude_states))
  add_parameter(request, 'prefer_cities', build_filter_string(lookup.prefer_cities))
  add_parameter(request, 'prefer_states', build_filter_string(lookup.prefer_states))
  add_parameter(request, 'prefer_zip_codes', build_filter_string(lookup.prefer_zip_codes))
  add_parameter(request, 'prefer_ratio', lookup.prefer_ratio.to_s)
  add_parameter(request, 'source', lookup.source)
  if lookup.prefer_zip_codes or lookup.zip_filter
    request.parameters['prefer_geolocation'] = GeolocationType::NONE
  else
    add_parameter(request, 'prefer_geolocation', lookup.prefer_geolocation)
  end
  add_parameter(request, 'selected', lookup.selected)

  request
end

#convert_suggestions(suggestion_hashes) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/smartystreets_ruby_sdk/us_autocomplete_pro/client.rb', line 61

def convert_suggestions(suggestion_hashes)
  converted_suggestions = []
  return converted_suggestions if suggestion_hashes.nil?

  suggestion_hashes.each do |suggestion|
    converted_suggestions.push(USAutocompletePro::Suggestion.new(suggestion))
  end

  converted_suggestions
end

#send(lookup) ⇒ Object

Sends a Lookup object to the US Autocomplete Pro API and stores the result in the Lookup’s result field.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/smartystreets_ruby_sdk/us_autocomplete_pro/client.rb', line 16

def send(lookup)
  if not lookup or not lookup.search
    raise SmartyStreets::SmartyError, 'Send() must be passed a Lookup with the prefix field set.'
  end

  request = build_request(lookup)

  response = @sender.send(request)

  raise response.error if response.error

  result = @serializer.deserialize(response.payload)
  suggestions = convert_suggestions(result.fetch('suggestions', []))
  lookup.result = suggestions
end