Class: OpinionatedHTTP::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/opinionated_http/request.rb

Overview

The request object

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#actionObject

Returns the value of attribute action.



4
5
6
# File 'lib/opinionated_http/request.rb', line 4

def action
  @action
end

#bodyObject

Returns the value of attribute body.



4
5
6
# File 'lib/opinionated_http/request.rb', line 4

def body
  @body
end

#error_classObject

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_dataObject

Returns the value of attribute form_data.



4
5
6
# File 'lib/opinionated_http/request.rb', line 4

def form_data
  @form_data
end

#formatObject

Returns the value of attribute format.



4
5
6
# File 'lib/opinionated_http/request.rb', line 4

def format
  @format
end

#headersObject

Returns the value of attribute headers.



4
5
6
# File 'lib/opinionated_http/request.rb', line 4

def headers
  @headers
end

#loggerObject

Returns the value of attribute logger.



4
5
6
# File 'lib/opinionated_http/request.rb', line 4

def logger
  @logger
end

#metric_prefixObject

Returns the value of attribute metric_prefix.



4
5
6
# File 'lib/opinionated_http/request.rb', line 4

def metric_prefix
  @metric_prefix
end

#parametersObject

Returns the value of attribute parameters.



4
5
6
# File 'lib/opinionated_http/request.rb', line 4

def parameters
  @parameters
end

#passwordObject

Returns the value of attribute password.



4
5
6
# File 'lib/opinionated_http/request.rb', line 4

def password
  @password
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/opinionated_http/request.rb', line 4

def path
  @path
end

#usernameObject

Returns the value of attribute username.



4
5
6
# File 'lib/opinionated_http/request.rb', line 4

def username
  @username
end

#verbObject

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_requestObject

Raises:

  • (ArgumentError)


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