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

Instance Method Details

#brine_root_urlString

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)

    The root URL to use or nil if none is provided.



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']
    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

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)

    The currently active client object.



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

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

#headersHash

The headers for the request currently being built.

Will be initialized as needed on first access, with a default specifying JSON content-type.

Returns:

  • (Hash)

    The headers to use for the constructed request.



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

Parameters:

  • method (String)

    A text representation of the HTTP method.

Returns:

  • (Symbol)

    A representation of ‘method` usable by the HTTP client library.



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

def parse_method(method)
  method.downcase.to_sym
end

#request_paramsHash

The query parameters which will be attached to the constructeed request.

Will be initialized to an empty hash as needed upon first access.

Returns:

  • (Hash)

    The query parameters to use for request construction.



151
152
153
# File 'lib/brine/requesting.rb', line 151

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.



76
77
78
# File 'lib/brine/requesting.rb', line 76

def reset_request
  @params = @headers = @body = nil
end

#responseFaraday::Response

The response for the last sent request.

Returns:

  • (Faraday::Response)

    The most recent response.



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.

Parameters:

  • method (Symbol)

    The client friendly representation of the HTTP method for the request (@see #parse_method).

  • url (String)

    The url to which the request will be sent.



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.

Parameters:

  • client (Faraday::Connection, #run_request)

    The client which will be used to issue HTTP requests.



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.

Parameters:

  • k (String)

    The name of the header whose value will be set.

  • v (Object)

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



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.

Parameters:

  • The (Object)

    new data to be placed in the request body.



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.

Parameters:

  • k (String)

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

  • v (Object)

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



161
162
163
# File 'lib/brine/requesting.rb', line 161

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