Class: SmartyStreets::USStreet::Client

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

Overview

It is recommended to instantiate this class using ClientBuilder.build_us_street_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/us_street/client.rb', line 9

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

Instance Method Details

#assign_candidates_to_lookups(batch, candidates) ⇒ Object



70
71
72
73
74
75
# File 'lib/smartystreets_ruby_sdk/us_street/client.rb', line 70

def assign_candidates_to_lookups(batch, candidates)
  candidates.each do |raw_candidate|
    candidate = Candidate.new(raw_candidate)
    batch[candidate.input_index].result.push(candidate)
  end
end

#remap_keys(obj) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/smartystreets_ruby_sdk/us_street/client.rb', line 46

def remap_keys(obj)
  converted_obj = []
  obj.each do |lookup|
    converted_lookup = {}
    lookup.candidates = 5 if lookup.match == "enhanced" && lookup.candidates == 0

    converted_lookup['input_id'] = lookup.input_id
    converted_lookup['street'] = lookup.street
    converted_lookup['street2'] = lookup.street2
    converted_lookup['secondary'] = lookup.secondary
    converted_lookup['city'] = lookup.city
    converted_lookup['state'] = lookup.state
    converted_lookup['zipcode'] = lookup.zipcode
    converted_lookup['lastline'] = lookup.lastline
    converted_lookup['addressee'] = lookup.addressee
    converted_lookup['urbanization'] = lookup.urbanization
    converted_lookup['match'] = lookup.match
    converted_lookup['candidates'] = lookup.candidates
    converted_lookup['format'] = lookup.format
    converted_obj.push(converted_lookup)
  end
  converted_obj
end

#send_batch(batch) ⇒ Object

Sends a Batch object containing no more than 100 Lookup objects to the US Street API and stores the results in the result field of the Lookup object.



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

def send_batch(batch)
  smarty_request = Request.new

  return if batch.empty?

  converted_lookups = remap_keys(batch.all_lookups)

  if batch.size > 1
    smarty_request.payload = @serializer.serialize(converted_lookups)
  else
    smarty_request.parameters = converted_lookups[0]
  end

  response = @sender.send(smarty_request)

  raise response.error if response.error

  candidates = @serializer.deserialize(response.payload)
  candidates = [] if candidates.nil?

  assign_candidates_to_lookups(batch, candidates)
end

#send_lookup(lookup) ⇒ Object

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



15
16
17
18
19
# File 'lib/smartystreets_ruby_sdk/us_street/client.rb', line 15

def send_lookup(lookup)
  batch = SmartyStreets::Batch.new
  batch.add(lookup)
  send_batch(batch)
end