Class: SmartyStreets::USExtract::Client

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

Overview

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

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

Instance Method Details

#add_parameter(request, key, value) ⇒ Object



42
43
44
45
46
# File 'lib/smartystreets_ruby_sdk/us_extract/client.rb', line 42

def add_parameter(request, key, value)
  if value and not value.empty?
    request.parameters[key] = value
  end
end

#build_request(lookup) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/smartystreets_ruby_sdk/us_extract/client.rb', line 29

def build_request(lookup)
  request = Request.new
  request.content_type = 'text/plain'
  request.payload = lookup.text

  add_parameter(request, 'html', lookup.html.to_s)
  add_parameter(request, 'aggressive', lookup.aggressive.to_s)
  add_parameter(request, 'addr_line_breaks', lookup.addresses_have_line_breaks.to_s)
  add_parameter(request, 'addr_per_line', lookup.addresses_per_line.to_s)

  request
end

#send(lookup) ⇒ Object

Sends a Lookup object to the US Extract Code API and stores the result in the Lookup’s result field. It also returns the result directly.



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

def send(lookup)
  if lookup.nil? or lookup.text.nil? or not lookup.text.is_a? String or lookup.text.empty?
    raise SmartyError, 'Client.send() requires a Lookup with the "text" field set'
  end

  request = build_request(lookup)
  response = @sender.send(request)
  raise response.error if response.error
  result = USExtract::Result.new(@serializer.deserialize(response.payload))

  lookup.result = result
end