Module: Brine::Requesting
- Included in:
- Brine
- Defined in:
- lib/brine/requesting.rb
Overview
Module in charge of constructing requests and saving responses.
Instance Method Summary collapse
-
#brine_root_url ⇒ String
The root url to which Brine will send requests.
-
#client ⇒ Faraday::Connection, #run_request
The currently active client which will be used to issue HTTP calls.
-
#headers ⇒ Hash
The headers for the request currently being built.
-
#parse_method(method) ⇒ Symbol
Normalize an HTTP method for the HTTP client library (to a lowercased symbol).
-
#request_params ⇒ Hash
The query parameters which will be attached to the constructeed request.
-
#reset_request ⇒ Object
Clear any previously built request state and set defaults.
-
#response ⇒ Faraday::Response
The response for the last sent request.
-
#send_request(method, url) ⇒ Object
Send a ‘method` request to `url` including any current builder options and store response.
-
#set_client(client) ⇒ Object
Set the client which will be used to send HTTP requests.
-
#set_header(k, v) ⇒ Object
Set the specified header to the provided value.
-
#set_request_body(obj) ⇒ Object
Store the provided body in the request being built.
-
#set_request_param(k, v) ⇒ Object
Assign the provided value to the specified request query parameter.
Instance Method Details
#brine_root_url ⇒ String
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/brine/requesting.rb', line 26 def brine_root_url if @brine_root_url @brine_root_url elsif ENV['BRINE_ROOT_URL'] ENV['BRINE_ROOT_URL'] elsif ENV['ROOT_URL'] ('1.0', 'ROOT_URL is deprecated, replace with BRINE_ROOT_URL') if ENV['ROOT_URL'] ENV['ROOT_URL'] end end |
#client ⇒ Faraday::Connection, #run_request
The currently active client which will be used to issue HTTP calls.
This will be initialized as neded on first access to a default client constructed by the ClientBuilding module.
66 67 68 |
# File 'lib/brine/requesting.rb', line 66 def client @client ||= client_for_host(brine_root_url) end |
#headers ⇒ Hash
The headers for the request currently being built.
Will be initialized as needed on first access, with a default specifying JSON content-type.
129 130 131 |
# File 'lib/brine/requesting.rb', line 129 def headers @headers ||= {content_type: 'application/json'} end |
#parse_method(method) ⇒ Symbol
Normalize an HTTP method for the HTTP client library (to a lowercased symbol).
43 44 45 |
# File 'lib/brine/requesting.rb', line 43 def parse_method(method) method.downcase.to_sym end |
#request_params ⇒ Hash
The query parameters which will be attached to the constructeed request.
Will be initialized to an empty hash as needed upon first access.
151 152 153 |
# File 'lib/brine/requesting.rb', line 151 def request_params @request_params ||= {} end |
#reset_request ⇒ Object
Clear any previously built request state and set defaults.
This should be called upon request completion or when constructing a new request so no extant state inadvertently pollutes the new construction.
76 77 78 |
# File 'lib/brine/requesting.rb', line 76 def reset_request @params = @headers = @body = nil end |
#response ⇒ Faraday::Response
The response for the last sent request.
117 118 119 |
# File 'lib/brine/requesting.rb', line 117 def response @response end |
#send_request(method, url) ⇒ Object
Send a ‘method` request to `url` including any current builder options and store response.
The response will be available as the ‘#response` property.
For requests such as simple GETs that only require a url this method may be self-contained; for more complex requests the values collected through request building are likely required. Any data present from the builder methods will always be inclued in the request and therefore such data should be cleared using ‘#reset_request` if it is not desired.
106 107 108 109 110 |
# File 'lib/brine/requesting.rb', line 106 def send_request(method, url) @response = client.run_request(method, url, @body, headers) do |req| req.params = request_params end end |
#set_client(client) ⇒ Object
Set the client which will be used to send HTTP requests.
This will generally be a connection as created by the ClientBuilding module.
54 55 56 |
# File 'lib/brine/requesting.rb', line 54 def set_client(client) @client = client end |
#set_header(k, v) ⇒ Object
Set the specified header to the provided value.
140 141 142 |
# File 'lib/brine/requesting.rb', line 140 def set_header(k, v) headers[k] = v end |
#set_request_body(obj) ⇒ Object
Store the provided body in the request being built.
This will override any previous body value.
87 88 89 |
# File 'lib/brine/requesting.rb', line 87 def set_request_body(obj) @body = obj end |
#set_request_param(k, v) ⇒ Object
Assign the provided value to the specified request query parameter.
161 162 163 |
# File 'lib/brine/requesting.rb', line 161 def set_request_param(k, v) request_params[k] = v end |