Class: OpinionatedHTTP::Request
- Inherits:
-
Object
- Object
- OpinionatedHTTP::Request
- Defined in:
- lib/opinionated_http/request.rb
Overview
The request object
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#body ⇒ Object
Returns the value of attribute body.
-
#error_class ⇒ Object
Returns the value of attribute error_class.
-
#form_data ⇒ Object
Returns the value of attribute form_data.
-
#format ⇒ Object
Returns the value of attribute format.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#metric_prefix ⇒ Object
Returns the value of attribute metric_prefix.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#password ⇒ Object
Returns the value of attribute password.
-
#path ⇒ Object
Returns the value of attribute path.
-
#username ⇒ Object
Returns the value of attribute username.
-
#verb ⇒ Object
Returns the value of attribute verb.
Instance Method Summary collapse
- #http_request ⇒ Object
-
#initialize(action:, verb: nil, path: nil, format: nil, headers: {}, body: nil, form_data: nil, username: nil, password: nil, parameters: nil) ⇒ Request
constructor
A new instance of Request.
Constructor Details
#initialize(action:, verb: nil, path: nil, format: nil, headers: {}, body: nil, form_data: nil, username: nil, password: nil, parameters: nil) ⇒ Request
Returns a new instance of Request.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/opinionated_http/request.rb', line 7 def initialize(action:, verb: nil, path: nil, format: nil, headers: {}, body: nil, form_data: nil, username: nil, password: nil, parameters: nil) @action = action @path = if path.nil? "/#{action}" elsif path.start_with?("/") path else "/#{path}" end @verb = verb @format = format @headers = headers @body = body @form_data = form_data @username = username @password = password @parameters = parameters end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def action @action end |
#body ⇒ Object
Returns the value of attribute body.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def body @body end |
#error_class ⇒ Object
Returns the value of attribute error_class.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def error_class @error_class end |
#form_data ⇒ Object
Returns the value of attribute form_data.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def form_data @form_data end |
#format ⇒ Object
Returns the value of attribute format.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def format @format end |
#headers ⇒ Object
Returns the value of attribute headers.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def headers @headers end |
#logger ⇒ Object
Returns the value of attribute logger.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def logger @logger end |
#metric_prefix ⇒ Object
Returns the value of attribute metric_prefix.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def metric_prefix @metric_prefix end |
#parameters ⇒ Object
Returns the value of attribute parameters.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def parameters @parameters end |
#password ⇒ Object
Returns the value of attribute password.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def password @password end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def path @path end |
#username ⇒ Object
Returns the value of attribute username.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def username @username end |
#verb ⇒ Object
Returns the value of attribute verb.
4 5 6 |
# File 'lib/opinionated_http/request.rb', line 4 def verb @verb end |
Instance Method Details
#http_request ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/opinionated_http/request.rb', line 27 def http_request unless headers_and_form_data_compatible?(headers, form_data) raise(ArgumentError, "Setting form data will overwrite supplied content-type") end raise(ArgumentError, "Cannot supply both form_data and a body") if body && form_data path_with_params = parameters ? "#{path}?#{URI.encode_www_form(parameters)}" : path body = format_body if self.body request = Net::HTTP.const_get(verb).new(path_with_params, headers) if body && !request.request_body_permitted? raise(ArgumentError, "#{request.class.name} does not support a request body") end if parameters && !request.response_body_permitted? raise(ArgumentError, ":parameters cannot be supplied for #{request.class.name}") end request.body = body if body request.set_form_data form_data if form_data request.basic_auth(username, password) if username && password request end |