Class: Zero::Request::Parameter
- Inherits:
-
Object
- Object
- Zero::Request::Parameter
- Defined in:
- lib/zero/request/parameter.rb
Overview
represents all parameter set in a session
This class holds all parameters available in the rack environment, split on query and payload parameters.
Constant Summary collapse
- ENV_KEY_QUERY =
they key for the query string
'QUERY_STRING'
- ENV_KEY_PAYLOAD =
the key for the payload
'rack.input'
- ENV_KEY_CUSTOM =
the key for custom parameters
'zero.params.custom'
- ENV_KEY_CONTENT_TYPE =
the key for the content type
'CONTENT_TYPE'
- PAYLOAD_CONTENT_TYPES =
all content types which used for using the body as a parameter input
[ 'application/x-www-form-urlencoded', 'multipart/form-data' ].to_set
Instance Attribute Summary collapse
-
#custom ⇒ Object
readonly
get all custom parameters.
-
#payload ⇒ Object
(also: #post)
readonly
get the payload or form data parameters.
-
#query ⇒ Object
(also: #get)
readonly
get the query parameters.
Instance Method Summary collapse
-
#[](key) ⇒ String
get a parameter.
-
#[]=(key, value) ⇒ Object
set a custom key/value pair.
-
#initialize(environment) ⇒ Parameter
constructor
creates a new parameter instance.
Constructor Details
#initialize(environment) ⇒ Parameter
creates a new parameter instance
This should never be called directly, as it will be generated for you. This instance gives you the options to get query parameters (mostly called GET parameters) and payload parameters (or POST parameters).
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/zero/request/parameter.rb', line 42 def initialize(environment) @query = extract_query_params(environment) @payload = extract_payload_params(environment) if environment.has_key?(ENV_KEY_CUSTOM) @custom = environment[ENV_KEY_CUSTOM] else @custom = {} environment[ENV_KEY_CUSTOM] = @custom end end |
Instance Attribute Details
#custom ⇒ Object (readonly)
get all custom parameters
34 35 36 |
# File 'lib/zero/request/parameter.rb', line 34 def custom @custom end |
#payload ⇒ Object (readonly) Also known as: post
get the payload or form data parameters
30 31 32 |
# File 'lib/zero/request/parameter.rb', line 30 def payload @payload end |
#query ⇒ Object (readonly) Also known as: get
get the query parameters
26 27 28 |
# File 'lib/zero/request/parameter.rb', line 26 def query @query end |
Instance Method Details
#[](key) ⇒ String
get a parameter
With this method you can get the value of a parameter. First the custom parameters are checked, then payload and after that the query ones.
*Beware, that this may lead to security holes!*
63 64 65 |
# File 'lib/zero/request/parameter.rb', line 63 def [](key) @custom[key] || @payload[key] || @query[key] end |
#[]=(key, value) ⇒ Object
set a custom key/value pair
Use this method if you want to set a custom parameter for later use. If the key was already set it will be overwritten.
73 74 75 |
# File 'lib/zero/request/parameter.rb', line 73 def []=(key, value) @custom[key] = value end |