Class: SmartyStreets::USZipcode::Client

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

Overview

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

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

Instance Method Details

#add_field(converted_lookup, key, value) ⇒ Object



62
63
64
# File 'lib/smartystreets_ruby_sdk/us_zipcode/client.rb', line 62

def add_field(converted_lookup, key, value)
  converted_lookup[key] = value unless value.nil? or value.empty?
end

#assign_results_to_lookups(batch, results) ⇒ Object



40
41
42
43
44
45
# File 'lib/smartystreets_ruby_sdk/us_zipcode/client.rb', line 40

def assign_results_to_lookups(batch, results)
  results.each do |raw_result|
    result = Result.new(raw_result)
    batch[result.input_index].result = result
  end
end

#remap_keys(obj) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/smartystreets_ruby_sdk/us_zipcode/client.rb', line 47

def remap_keys(obj)
  converted_obj = []
  obj.each do |lookup|
    converted_lookup = {}

    add_field(converted_lookup, 'city', lookup.city)
    add_field(converted_lookup, 'state', lookup.state)
    add_field(converted_lookup, 'zipcode', lookup.zipcode)

    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 ZIP Code 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
# File 'lib/smartystreets_ruby_sdk/us_zipcode/client.rb', line 23

def send_batch(batch)
  smarty_request = Request.new

  return if batch.empty?

  converted_lookups = remap_keys(batch.all_lookups)
  smarty_request.payload = @serializer.serialize(converted_lookups)

  response = @sender.send(smarty_request)

  raise response.error if response.error

  results = @serializer.deserialize(response.payload)
  results = [] if results == nil
  assign_results_to_lookups(batch, results)
end

#send_lookup(lookup) ⇒ Object

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



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

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