Class: SmartyStreets::InternationalStreet::Client

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

Overview

It is recommended to instantiate this class using ClientBuilder.build_international_street_api_client()

Instance Method Summary collapse

Constructor Details

#initialize(sender, serializer) ⇒ Client

Returns a new instance of Client.



8
9
10
11
# File 'lib/smartystreets_ruby_sdk/international_street/client.rb', line 8

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

Instance Method Details

#add_parameter(request, key, value) ⇒ Object



45
46
47
# File 'lib/smartystreets_ruby_sdk/international_street/client.rb', line 45

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

#build_request(lookup) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/smartystreets_ruby_sdk/international_street/client.rb', line 26

def build_request(lookup)
  request = SmartyStreets::Request.new

  add_parameter(request, 'country', lookup.country)
  add_parameter(request, 'geocode', lookup.geocode.to_s)
  add_parameter(request, 'language', lookup.language)
  add_parameter(request, 'freeform', lookup.freeform)
  add_parameter(request, 'address1', lookup.address1)
  add_parameter(request, 'address2', lookup.address2)
  add_parameter(request, 'address3', lookup.address3)
  add_parameter(request, 'address4', lookup.address4)
  add_parameter(request, 'organization', lookup.organization)
  add_parameter(request, 'locality', lookup.locality)
  add_parameter(request, 'administrative_area', lookup.administrative_area)
  add_parameter(request, 'postal_code', lookup.postal_code)

  request
end

#convert_candidates(raw_candidates) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/smartystreets_ruby_sdk/international_street/client.rb', line 49

def convert_candidates(raw_candidates)
  candidates = []

  raw_candidates.each do |candidate|
    candidates.push(Candidate.new(candidate))
  end

  candidates
end

#send(lookup) ⇒ Object

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



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/smartystreets_ruby_sdk/international_street/client.rb', line 14

def send(lookup)
  lookup.ensure_enough_info
  request = build_request(lookup)

  response = @sender.send(request)

  raise response.error if response.error

  candidates = convert_candidates(@serializer.deserialize(response.payload))
  lookup.result = candidates
end