Class: Yoti::RequestBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/yoti/http/request.rb

Overview

Builder for Request

Instance Method Summary collapse

Constructor Details

#initializeRequestBuilder

Returns a new instance of RequestBuilder.



198
199
200
201
# File 'lib/yoti/http/request.rb', line 198

def initialize
  @headers = {}
  @query_params = {}
end

Instance Method Details

#buildRequest

Returns:



288
289
290
291
292
293
294
295
296
297
# File 'lib/yoti/http/request.rb', line 288

def build
  request = Request.new
  request.base_url = @base_url
  request.endpoint = @endpoint
  request.query_params = @query_params
  request.http_method = @http_method
  request.payload = @payload
  @headers.map { |k, v| request.add_header(k, v) }
  request
end

#with_base_url(base_url) ⇒ self

Sets the base URL

Parameters:

  • base_url (String)

Returns:

  • (self)


210
211
212
213
214
# File 'lib/yoti/http/request.rb', line 210

def with_base_url(base_url)
  Validation.assert_is_a(String, base_url, 'base_url')
  @base_url = base_url
  self
end

#with_endpoint(endpoint) ⇒ self

Sets the API endpoint for the request

Parameters:

  • endpoint (String)

Returns:

  • (self)


266
267
268
269
270
# File 'lib/yoti/http/request.rb', line 266

def with_endpoint(endpoint)
  Validation.assert_is_a(String, endpoint, 'endpoint')
  @endpoint = endpoint
  self
end

#with_header(header, value) ⇒ self

Adds a HTTP header to the request

Parameters:

  • header (String)
  • value (String)

Returns:

  • (self)


224
225
226
227
228
229
# File 'lib/yoti/http/request.rb', line 224

def with_header(header, value)
  Validation.assert_is_a(String, header, 'header')
  Validation.assert_is_a(String, value, 'value')
  @headers[header] = value
  self
end

#with_http_method(http_method) ⇒ self

Sets the HTTP method

Parameters:

  • http_method (String)

Returns:

  • (self)


253
254
255
256
257
# File 'lib/yoti/http/request.rb', line 253

def with_http_method(http_method)
  Validation.assert_is_a(String, http_method, 'http_method')
  @http_method = http_method
  self
end

#with_payload(payload) ⇒ self

Sets the body sent with the request

Parameters:

  • payload (#to_json, String)

Returns:

  • (self)


279
280
281
282
283
# File 'lib/yoti/http/request.rb', line 279

def with_payload(payload)
  Validation.assert_respond_to(:to_json, payload, 'payload') unless payload.is_a?(String)
  @payload = payload
  self
end

#with_query_param(key, value) ⇒ self

Adds a query parameter to the request

Parameters:

  • key (String)
  • value (String)

Returns:

  • (self)


239
240
241
242
243
244
# File 'lib/yoti/http/request.rb', line 239

def with_query_param(key, value)
  Validation.assert_is_a(String, key, 'key')
  Validation.assert_is_a(String, value, 'value')
  @query_params[key] = value
  self
end