Class: Yoti::RequestBuilder
- Inherits:
-
Object
- Object
- Yoti::RequestBuilder
- Defined in:
- lib/yoti/http/request.rb
Overview
Builder for Request
Instance Method Summary collapse
- #build ⇒ Request
-
#initialize ⇒ RequestBuilder
constructor
A new instance of RequestBuilder.
-
#with_base_url(base_url) ⇒ self
Sets the base URL.
-
#with_endpoint(endpoint) ⇒ self
Sets the API endpoint for the request.
-
#with_header(header, value) ⇒ self
Adds a HTTP header to the request.
-
#with_http_method(http_method) ⇒ self
Sets the HTTP method.
-
#with_payload(payload) ⇒ self
Sets the body sent with the request.
-
#with_query_param(key, value) ⇒ self
Adds a query parameter to the request.
Constructor Details
#initialize ⇒ RequestBuilder
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
#build ⇒ Request
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
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
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
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
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
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
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 |