Module: Brine::Requesting
- Included in:
- Brine
- Defined in:
- lib/brine/requesting.rb
Overview
Support constructing requests and saving responses.
Instance Method Summary collapse
-
#brine_root_url ⇒ String
Retrieve the root url to which Brine will send requests.
-
#client ⇒ Faraday::Connection, #run_request
Return the currently active client which will be used to issue HTTP calls.
-
#headers ⇒ Hash
Expose 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
Expose 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
Return 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
permalink #brine_root_url ⇒ String
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/brine/requesting.rb', line 25 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 |
permalink #client ⇒ Faraday::Connection, #run_request
Return 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.
65 66 67 |
# File 'lib/brine/requesting.rb', line 65 def client @client ||= client_for_host(brine_root_url) end |
permalink #headers ⇒ Hash
Expose the headers for the request currently being built.
This will be initialized as needed on first access, with a default specifying JSON content-type.
127 128 129 |
# File 'lib/brine/requesting.rb', line 127 def headers @headers ||= {content_type: 'application/json'} end |
permalink #parse_method(method) ⇒ Symbol
Normalize an HTTP method for the HTTP client library (to a lowercased symbol).
42 43 44 |
# File 'lib/brine/requesting.rb', line 42 def parse_method(method) method.downcase.to_sym end |
permalink #request_params ⇒ Hash
Expose the query parameters which will be attached to the constructeed request.
This will be initialized to an empty hash as needed upon first access.
149 150 151 |
# File 'lib/brine/requesting.rb', line 149 def request_params @request_params ||= {} end |
permalink #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.
75 76 77 |
# File 'lib/brine/requesting.rb', line 75 def reset_request @params = @headers = @body = nil end |
permalink #response ⇒ Faraday::Response
Return the response for the last sent request.
115 116 117 |
# File 'lib/brine/requesting.rb', line 115 def response @response end |
permalink #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.
104 105 106 107 108 |
# File 'lib/brine/requesting.rb', line 104 def send_request(method, url) @response = client.run_request(method, url, @body, headers) do |req| req.params = request_params end end |
permalink #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.
53 54 55 |
# File 'lib/brine/requesting.rb', line 53 def set_client(client) @client = client end |
permalink #set_header(k, v) ⇒ Object
Set the specified header to the provided value.
138 139 140 |
# File 'lib/brine/requesting.rb', line 138 def set_header(k, v) headers[k] = v end |
permalink #set_request_body(obj) ⇒ Object
Store the provided body in the request being built.
This will override any previous body value.
86 87 88 |
# File 'lib/brine/requesting.rb', line 86 def set_request_body(obj) @body = obj end |
permalink #set_request_param(k, v) ⇒ Object
Assign the provided value to the specified request query parameter.
159 160 161 |
# File 'lib/brine/requesting.rb', line 159 def set_request_param(k, v) request_params[k] = v end |