Class: SmartyStreets::InternationalAutocomplete::Client

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

Overview

It is recommended to instantiate this class using ClientBuilder.build_international_autocomplete_api_client

Instance Method Summary collapse

Constructor Details

#initialize(sender, serializer) ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/smartystreets_ruby_sdk/international_autocomplete/client.rb', line 9

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

Instance Method Details

#add_parameter(request, key, value) ⇒ Object



55
56
57
# File 'lib/smartystreets_ruby_sdk/international_autocomplete/client.rb', line 55

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

#build_request(lookup) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/smartystreets_ruby_sdk/international_autocomplete/client.rb', line 32

def build_request(lookup)
  request = Request.new

  add_parameter(request, 'search', lookup.search)
  add_parameter(request, 'country', lookup.country)
  add_parameter(request, 'include_only_administrative_area', lookup.administrative_area)
  add_parameter(request, 'include_only_locality', lookup.locality)
  add_parameter(request, 'include_only_postal_code', lookup.postal_code)

  request
end

#convert_suggestions(suggestion_hashes) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/smartystreets_ruby_sdk/international_autocomplete/client.rb', line 44

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

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

  converted_suggestions
end

#send(lookup) ⇒ Object

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



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

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('candidates', []))
  lookup.result = suggestions
end