Module: Clutterbuck::Request

Defined in:
lib/clutterbuck/request.rb

Instance Method Summary collapse

Instance Method Details

#base_urlURI

Return the base URL of the app.

Returns:

  • (URI)


38
39
40
41
42
43
44
45
46
47
# File 'lib/clutterbuck/request.rb', line 38

def base_url
	URI("#{request.scheme}:/").tap do |uri|
		uri.host   = request.host
		uri.path   = request.script_name

		if uri.default_port != request.port
			uri.port = request.port
		end
	end
end

#body(mode = :hash) ⇒ Object

Parse and return the data sent in the body of the request.

This method is the "catch-all" means of accessing the request body. Since request bodies can come in all sorts of different flavours, there are several different "modes" of accessing the body. These vary the content types that will be accepted, as well as the type that is returned.

Parameters:

  • mode (Symbol) (defaults to: :hash)

    select what sort of body you're expecting. Valid values are:

    • :hash -- return some sort of hash containing request parameters. JSON documents containing an object at the top level, as well as HTML form submissions, are accepted in this mode.

    • :json -- Enforce that the request body must be JSON. Any valid JSON document will be returned, parsed into Ruby-native types -- so you could, in theory, end up with any of a Hash, Array, Integer, Float, true, false, or nil (yep, RFC7159 lets any of those things be a top-level document).

    • :raw -- The entire request body will be returned as one giant string. You're on your own when it comes to parsing the thing.

Returns:

  • (Object)

    depending on the mode, lots of different things can come back.

Raises:

  • (Clutterbuck::BadRequestError)

    something bad happened:

    • For mode == :hash, Parsing failed.

    • For mode == :json, no request body was given, or the JSON parser failed.

    • For mode == :raw, No body was provided.

  • (Clutterbuck::UnsupportedMediaTypeError)

    if mode == :json and something other than JSON was provided, or if mode == :hash and a content type we don't know how to turn into a hash was provided.



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/clutterbuck/request.rb', line 151

def body(mode=:hash)
	case mode
	when :hash
		hash_body
	when :json
		json_body
	when :raw
		raw_body
	else
		raise ArgumentError,
		      "Unknown body mode: #{mode.inspect}"
	end
end

#nested_query_paramsHash{String => String, Hash} Also known as: nqp

Note:

The query parameters are only those specified in the query string part of the URL; they explicitly do not include any data sent in the request body. The contents of the request body are available separately, via the #body method.

Note:

If you want the query parameters to be "flat", without any hashes as values for the top-level elements, you want #query_params.

Return the query parameters for this request as a nested hash.

This is useful in those situations where you want to get a bit "structured" with your query parameters. When a query parameter name contains square brackets ([ and ]), things get a bit tricky.

If there is something inside the square brackets (such as [blah]), then a hash will be created as the value of the part of the name before the brackets, and the contents of the square brackets will become the "key" in that new sub-hash, and the value of the query parameter will be the value in that subhash.

If the square brackets are empty (ie []), then an array will be created as the value of the part of the name before the brackets, and the value of the query parameter will be appended to the array.

All this is recursive too; this can end up with you getting very complicated nested data structures for your query params, which is rarely good thing, from a security perspective. But if you want to shoot yourself in the foot, it's here for you.

Returns:

  • (Hash{String => String, Hash})


102
103
104
105
106
# File 'lib/clutterbuck/request.rb', line 102

def nested_query_params
	@nested_query_params ||= begin
		Rack::Utils.parse_nested_query(request.query_string)
	end
end

#query_paramsHash<String, String> Also known as: qp

Note:

The query parameters are only those specified in the query string part of the URL; they explicitly do not include any data sent in the request body. The contents of the request body are available separately, via the #body method.

Note:

This method parses the variable names as a "flat" namespace. As an example, if the query string included foo[bar]=baz, then this method will return a hash of {"foo[bar]" => "baz"}. If you want the query parameters to be "nested", you want #nested_query_params.

Return the query parameters for this request.

Returns:

  • (Hash<String, String>)


63
64
65
66
67
# File 'lib/clutterbuck/request.rb', line 63

def query_params
	@query_params ||= begin
		Rack::Utils.parse_query(request.query_string)
	end
end

#requestRack::Request

Give an instance of Rack::Request generated from the environment of this request.

Returns:

  • (Rack::Request)


16
17
18
# File 'lib/clutterbuck/request.rb', line 16

def request
	@request ||= Rack::Request.new(env)
end

#url(path) ⇒ String

Generate an absolute URL for the path given, relative to the root of this application.

Parameters:

  • path (String)

    the path to append to the base URL of the app.

Returns:

  • (String)


27
28
29
30
31
32
# File 'lib/clutterbuck/request.rb', line 27

def url(path)
	base_url.tap do |url|
		url.path = url.path[0..-2] if url.path[-1] == "/"
		url.path += (path[0] == "/" ? "" : "/") + path
	end.to_s
end