Class: Zero::Request::Parameter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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).

Parameters:

  • environment (Hash)

    the rack environment



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

#customObject (readonly)

get all custom parameters



34
35
36
# File 'lib/zero/request/parameter.rb', line 34

def custom
  @custom
end

#payloadObject (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

#queryObject (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!*

Parameters:

  • key (String)

    a key to look for

Returns:

  • (String)

    the value of the key



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.

Parameters:

  • key (String)

    the key to use for saving the parameter

  • value (Object)

    the value for the key



73
74
75
# File 'lib/zero/request/parameter.rb', line 73

def []=(key, value)
  @custom[key] = value
end