Module: Clutterbuck::Request
- Defined in:
- lib/clutterbuck/request.rb
Instance Method Summary collapse
-
#base_url ⇒ URI
Return the base URL of the app.
-
#body(mode = :hash) ⇒ Object
Parse and return the data sent in the body of the request.
-
#nested_query_params ⇒ Hash{String => String, Hash}
(also: #nqp)
Return the query parameters for this request as a nested hash.
-
#query_params ⇒ Hash<String, String>
(also: #qp)
Return the query parameters for this request.
-
#request ⇒ Rack::Request
Give an instance of
Rack::Requestgenerated from the environment of this request. -
#url(path) ⇒ String
Generate an absolute URL for the path given, relative to the root of this application.
Instance Method Details
#base_url ⇒ URI
Return the base URL of the app.
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.
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_params ⇒ Hash{String => String, Hash} Also known as: nqp
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.
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.
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_params ⇒ Hash<String, String> Also known as: qp
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.
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.
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 |
#request ⇒ Rack::Request
Give an instance of Rack::Request generated from the environment
of this 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.
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 |