Module: Requesting

Includes:
ClientBuilding
Included in:
Brine, Requester
Defined in:
lib/brine/requester.rb

Overview

Module in charge of constructing requests and saving responses

Instance Method Summary collapse

Methods included from ClientBuilding

#client_for_host, #connection_handlers, #use_oauth2_token, #with_oauth2_token

Instance Method Details

#add_header(k, v) ⇒ Object



120
121
122
# File 'lib/brine/requester.rb', line 120

def add_header(k, v)
  headers[k] = v
end

#add_request_param(k, v) ⇒ Object



128
129
130
# File 'lib/brine/requester.rb', line 128

def add_request_param(k, v)
  params[k] = v
end

#clientObject

return Faraday client object so that it could be used directly or passed to another object



87
88
89
90
# File 'lib/brine/requester.rb', line 87

def client
  @client ||= client_for_host((ENV['ROOT_URL'] || 'http://localhost:8080'),
                              logging: ENV['BRINE_LOG_HTTP'])
end

#headersObject



116
117
118
# File 'lib/brine/requester.rb', line 116

def headers
  @headers ||= {content_type: 'application/json'}
end

#paramsObject



124
125
126
# File 'lib/brine/requester.rb', line 124

def params
  @params ||= {}
end

#parse_method(method) ⇒ Object

Utility Methods

Normalize an HTTP method passed from a specification into a form expected by the HTTP client library (lowercased symbol)



77
78
79
# File 'lib/brine/requester.rb', line 77

def parse_method(method)
  method.downcase.to_sym
end

#reset_requestObject

clear out any previously built request state and set defaults



93
94
95
# File 'lib/brine/requester.rb', line 93

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

#responseObject

getter for the latest response returned



112
113
114
# File 'lib/brine/requester.rb', line 112

def response
  @response
end

#send_request(method, url) ⇒ Object

send a request using method to url using whatever options have been built for the present request



105
106
107
108
109
# File 'lib/brine/requester.rb', line 105

def send_request(method, url)
  @response = client.run_request(method, url, @body, headers) do |req|
    req.params = params
  end
end

#set_client(client) ⇒ Object



81
82
83
# File 'lib/brine/requester.rb', line 81

def set_client(client)
  @client = client
end

#set_request_body(obj) ⇒ Object

store the provided body in the request options being built overriding any previously provided object



99
100
101
# File 'lib/brine/requester.rb', line 99

def set_request_body(obj)
  @body = obj
end