Module: Brine::Requesting

Included in:
Brine
Defined in:
lib/brine/requesting.rb

Overview

Support constructing requests and saving responses.

Instance Method Summary collapse

Instance Method Details

#brine_root_urlString

Retrieve the root url to which Brine will send requests.

This will normally be the value of ENV, and that value should be directly usable after older ENV is end-of-lifed (at which point this can be removed).

Returns:

  • (String)

    Return the root URL to use or nil if none is provided.



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']
    deprecation_message('1.0', 'ROOT_URL is deprecated, replace with BRINE_ROOT_URL') if ENV['ROOT_URL']
    ENV['ROOT_URL']
  end
end

#clientFaraday::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.

Returns:

  • (Faraday::Connection, #run_request)

    Return the active HTTP client.



65
66
67
# File 'lib/brine/requesting.rb', line 65

def client
  @client ||= client_for_host(brine_root_url)
end

#headersHash

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.

Returns:

  • (Hash)

    Return the headers to use for the constructed request.



127
128
129
# File 'lib/brine/requesting.rb', line 127

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).

Parameters:

  • method (String)

    Provide a text representation of the HTTP method.

Returns:

  • (Symbol)

    Return ‘method` in a form potentially usable by the HTTP client library.



42
43
44
# File 'lib/brine/requesting.rb', line 42

def parse_method(method)
  method.downcase.to_sym
end

#request_paramsHash

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.

Returns:

  • (Hash)

    Return the query parameters to use for the constructed request.



149
150
151
# File 'lib/brine/requesting.rb', line 149

def request_params
  @request_params ||= {}
end

#reset_requestObject

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

#responseFaraday::Response

Return the response for the last sent request.

Returns:

  • (Faraday::Response)

    Return the most recent response.



115
116
117
# File 'lib/brine/requesting.rb', line 115

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.

Parameters:

  • method (Symbol)

    Provide the HTTP method for the request such as returned by #parse_method.

  • url (String)

    Specify the url to which the request will be sent.



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

#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.

Parameters:

  • client (Faraday::Connection, #run_request)

    Provide the client which will be used to issue HTTP requests.



53
54
55
# File 'lib/brine/requesting.rb', line 53

def set_client(client)
  @client = client
end

#set_header(k, v) ⇒ Object

Set the specified header to the provided value.

Parameters:

  • k (String)

    Specify the name of the header whose value will be set.

  • v (Object)

    Provide the value to set for the specified header. This should normally be a String, but some other types may work.



138
139
140
# File 'lib/brine/requesting.rb', line 138

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.

Parameters:

  • obj (Object)

    Provide the data to place in the request body.



86
87
88
# File 'lib/brine/requesting.rb', line 86

def set_request_body(obj)
  @body = obj
end

#set_request_param(k, v) ⇒ Object

Assign the provided value to the specified request query parameter.

Parameters:

  • k (String)

    Specify the name of the query parameter whose value is being assigned.

  • v (Object)

    Provide the value to assign the query parameter (normally a String).



159
160
161
# File 'lib/brine/requesting.rb', line 159

def set_request_param(k, v)
  request_params[k] = v
end