Class: SmartyStreets::ClientBuilder

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

Overview

The ClientBuilder class helps you build a client object for one of the supported SmartyStreets APIs. You can use ClientBuilder’s methods to customize settings like maximum retries or timeout duration. These methods are chainable, so you can usually get set up with one line of code.

Constant Summary collapse

INTERNATIONAL_STREET_API_URL =
'https://international-street.api.smarty.com/verify'.freeze
INTERNATIONAL_AUTOCOMPLETE_API_URL =
"https://international-autocomplete.api.smarty.com/v2/lookup".freeze
US_AUTOCOMPLETE_PRO_API_URL =
'https://us-autocomplete-pro.api.smarty.com/lookup'.freeze
US_EXTRACT_API_URL =
'https://us-extract.api.smarty.com/'.freeze
US_STREET_API_URL =
'https://us-street.api.smarty.com/street-address'.freeze
US_ZIP_CODE_API_URL =
'https://us-zipcode.api.smarty.com/lookup'.freeze
US_REVERSE_GEO_API_URL =
'https://us-reverse-geo.api.smarty.com/lookup'.freeze
US_ENRICHMENT_API_URL =
'https://us-enrichment.api.smarty.com/lookup'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(signer) ⇒ ClientBuilder

Returns a new instance of ClientBuilder.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 35

def initialize(signer)
  @signer = signer
  @serializer = NativeSerializer.new
  @http_sender = nil
  @max_retries = 5
  @max_timeout = 10
  @url_prefix = nil
  @proxy = nil
  @header = nil
  @licenses = %w()
  @debug = nil
end

Instance Method Details

#build_international_autocomplete_api_clientObject



130
131
132
133
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 130

def build_international_autocomplete_api_client
  ensure_url_prefix_not_null(INTERNATIONAL_AUTOCOMPLETE_API_URL)
  InternationalAutocomplete::Client.new(build_sender, @serializer)
end

#build_international_street_api_clientObject

<editor-fold desc=“Build methods”>



125
126
127
128
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 125

def build_international_street_api_client
  ensure_url_prefix_not_null(INTERNATIONAL_STREET_API_URL)
  InternationalStreet::Client.new(build_sender, @serializer)
end

#build_senderObject

</editor-fold>



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 167

def build_sender
  return @http_sender unless @http_sender.nil?

  sender = NativeSender.new(@max_timeout, @proxy, @debug)

  sender = StatusCodeSender.new(sender)

  sender = CustomHeaderSender.new(sender, @header) unless @header.nil?

  sender = SigningSender.new(@signer, sender) unless @signer.nil?

  sender = RetrySender.new(@max_retries, sender, SmartyStreets::Sleeper.new,SmartyStreets::Logger.new) if @max_retries > 0

  sender = LicenseSender.new(sender, @licenses)

  URLPrefixSender.new(@url_prefix, sender)
end

#build_us_autocomplete_pro_api_clientObject



135
136
137
138
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 135

def build_us_autocomplete_pro_api_client
  ensure_url_prefix_not_null(US_AUTOCOMPLETE_PRO_API_URL)
  USAutocompletePro::Client.new(build_sender, @serializer)
end

#build_us_enrichment_api_clientObject



160
161
162
163
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 160

def build_us_enrichment_api_client
  ensure_url_prefix_not_null(US_ENRICHMENT_API_URL)
  USEnrichment::Client.new(build_sender, @serializer)
end

#build_us_extract_api_clientObject



140
141
142
143
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 140

def build_us_extract_api_client
  ensure_url_prefix_not_null(US_EXTRACT_API_URL)
  USExtract::Client.new(build_sender, @serializer)
end

#build_us_reverse_geo_api_clientObject



155
156
157
158
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 155

def build_us_reverse_geo_api_client
  ensure_url_prefix_not_null(US_REVERSE_GEO_API_URL)
  USReverseGeo::Client.new(build_sender, @serializer)
end

#build_us_street_api_clientObject



145
146
147
148
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 145

def build_us_street_api_client
  ensure_url_prefix_not_null(US_STREET_API_URL)
  USStreet::Client.new(build_sender, @serializer)
end

#build_us_zipcode_api_clientObject



150
151
152
153
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 150

def build_us_zipcode_api_client
  ensure_url_prefix_not_null(US_ZIP_CODE_API_URL)
  USZipcode::Client.new(build_sender, @serializer)
end

#ensure_url_prefix_not_null(url) ⇒ Object



185
186
187
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 185

def ensure_url_prefix_not_null(url)
  @url_prefix = url if @url_prefix.nil?
end

#retry_at_most(max_retries) ⇒ Object

Sets the maximum number of times to retry sending the request to the API. (Default is 5)

Returns self to accommodate method chaining.



51
52
53
54
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 51

def retry_at_most(max_retries)
  @max_retries = max_retries
  self
end

#with_base_url(base_url) ⇒ Object

This may be useful when using a local installation of the SmartyStreets APIs. base_url is a string that defaults to the URL for the API corresponding to the Client object being built.

Returns self to accommodate method chaining.



84
85
86
87
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 84

def with_base_url(base_url)
  @url_prefix = base_url
  self
end

#with_custom_headers(header) ⇒ Object

Allows you to submit custom headers using a Hash. headers is a Hash object.

Returns self to accommodate method chaining.



102
103
104
105
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 102

def with_custom_headers(header)
  @header = header
  self
end

#with_debugObject

Enables debug mode, which will print information about the HTTP request and response to $stdout.

Returns self to accommodate method chaining.



118
119
120
121
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 118

def with_debug
  @debug = true
  self
end

#with_licenses(licenses) ⇒ Object

Allows the caller to specify the subscription license (aka “track”) they wish to use.

Returns self to accommodate method chaining.



110
111
112
113
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 110

def with_licenses(licenses)
  @licenses.concat licenses
  self
end

#with_max_timeout(max_timeout) ⇒ Object

The maximum time (in seconds) to wait for the response to be read. (Default is 10)

Returns self to accommodate method chaining.



59
60
61
62
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 59

def with_max_timeout(max_timeout)
  @max_timeout = max_timeout
  self
end

#with_proxy(host, port, username, password) ⇒ Object

Assigns a proxy through which all requests will be sent. proxy is a Proxy object from this module.

Returns self to accommodate method chaining.



93
94
95
96
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 93

def with_proxy(host, port, username, password)
  @proxy = SmartyStreets::Proxy.new(host, port, username, password)
  self
end

#with_sender(sender) ⇒ Object

Default is a series of nested senders. (See build_sender()

Returns self to accommodate method chaining.



67
68
69
70
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 67

def with_sender(sender)
  @http_sender = sender
  self
end

#with_serializer(serializer) ⇒ Object

Changes the Serializer from the default.

Returns self to accommodate method chaining.



75
76
77
78
# File 'lib/smartystreets_ruby_sdk/client_builder.rb', line 75

def with_serializer(serializer)
  @serializer = serializer
  self
end