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.



11
12
13
14
# File 'lib/smartystreets_ruby_sdk/us_extract/client.rb', line 11

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

Instance Method Details

#add_parameter(request, key, value) ⇒ Object



51
52
53
54
55
# File 'lib/smartystreets_ruby_sdk/us_extract/client.rb', line 51

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

#build_request(lookup) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/smartystreets_ruby_sdk/us_extract/client.rb', line 31

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)
  if lookup.match !=  SmartyStreets::USStreet::MatchType::STRICT && lookup.match != nil
    add_parameter(request, 'match', lookup.match)
  end

  for key in lookup.custom_param_hash.keys do
    add_parameter(request, key, lookup.custom_param_hash[key])
  end

  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.



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

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