-
Notifications
You must be signed in to change notification settings - Fork 25
/
client.rb
63 lines (51 loc) · 2.06 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require_relative '../request'
require_relative 'candidate'
module SmartyStreets
module InternationalStreet
# It is recommended to instantiate this class using ClientBuilder.build_international_street_api_client()
class Client
def initialize(sender, serializer)
@sender = sender
@serializer = serializer
end
# Sends a Lookup object to the International Street API and stores the result in the Lookup's result field.
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
def build_request(lookup)
request = SmartyStreets::Request.new
add_parameter(request, 'input_id', lookup.input_id)
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
def add_parameter(request, key, value)
request.parameters[key] = value unless value.nil? or value.empty?
end
def convert_candidates(raw_candidates)
candidates = []
unless raw_candidates.nil?
raw_candidates.each do |candidate|
candidates.push(Candidate.new(candidate))
end
end
candidates
end
end
end
end