Class: Ramaze::Request
- Inherits:
-
Innate::Request
- Object
- Innate::Request
- Ramaze::Request
- Defined in:
- lib/ramaze/request.rb
Overview
The purpose of this class is to act as a simple wrapper for Rack::Request and provide some convenient methods for our own use.
Constant Summary collapse
- INTERESTING_HTTP_VARIABLES =
(/USER|HOST|REQUEST|REMOTE|FORWARD|REFER|PATH|QUERY|VERSION|KEEP|CACHE/)
- REQUEST_STRING_FORMAT =
"#<%s params=%p cookies=%p env=%p>"
Instance Method Summary collapse
- #accept_charset(default = 'UTF-8') ⇒ Object
-
#accept_language(string = env['HTTP_ACCEPT_LANGUAGE']) ⇒ Array
(also: #locales)
Try to find out which languages the client would like to have and sort them by weight, (most wanted first).
-
#accept_language_with_weight(string = ) ⇒ Array
Transform the HTTP_ACCEPT_LANGUAGE header into an Array with:.
-
#http_variables ⇒ Object
(also: #http_vars)
Interesting HTTP variables from env.
-
#method_missing(meth, *args) ⇒ Object
you can access the original @request via this method_missing, first it tries to match your method with any of the HTTP parameters then, in case that fails, it will relay to @request.
-
#pretty_print(pp) ⇒ Object
Pretty prints current action with parameters, cookies and environment variables.
-
#to_instance_variables(*args) ⇒ Object
(also: #to_ivs)
Sets any arguments passed as @instance_variables for the current action.
- #to_s ⇒ Object (also: #inspect)
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
you can access the original @request via this method_missing, first it tries to match your method with any of the HTTP parameters then, in case that fails, it will relay to @request
12 13 14 15 16 |
# File 'lib/ramaze/request.rb', line 12 def method_missing meth, *args key = meth.to_s.upcase return env[key] if env.has_key?(key) super end |
Instance Method Details
#accept_charset(default = 'UTF-8') ⇒ Object
39 40 41 42 43 |
# File 'lib/ramaze/request.rb', line 39 def accept_charset(default = 'UTF-8') return default unless charsets = env['HTTP_ACCEPT_CHARSET'] charset = charsets.split(',', 2).first charset == '*' ? default : charset end |
#accept_language(string = env['HTTP_ACCEPT_LANGUAGE']) ⇒ Array Also known as: locales
Try to find out which languages the client would like to have and sort them by weight, (most wanted first).
Returns and array of locales from env. e.g. [“fi”, “en”, “ja”, “fr”, “de”, “es”, “it”, “nl”, “sv”]
Usage:
request.accept_language # => ['en-us', 'en', 'de-at', 'de']
61 62 63 64 65 |
# File 'lib/ramaze/request.rb', line 61 def accept_language(string = env['HTTP_ACCEPT_LANGUAGE']) return [] unless string accept_language_with_weight(string).map{|lang, weight| lang } end |
#accept_language_with_weight(string = ) ⇒ Array
Transform the HTTP_ACCEPT_LANGUAGE header into an Array with:
[[lang, weight], [lang, weight], ...]
This algorithm was taken and improved from the locales library.
Usage:
request.accept_language_with_weight
# => [["en-us", 1.0], ["en", 0.8], ["de-at", 0.5], ["de", 0.3]]
85 86 87 88 89 90 |
# File 'lib/ramaze/request.rb', line 85 def accept_language_with_weight(string = env['HTTP_ACCEPT_LANGUAGE']) string.to_s.gsub(/\s+/, '').split(','). map{|chunk| chunk.split(';q=', 2) }. map{|lang, weight| [lang, weight ? weight.to_f : 1.0] }. sort_by{|lang, weight| -weight } end |
#http_variables ⇒ Object Also known as: http_vars
Interesting HTTP variables from env
96 97 98 |
# File 'lib/ramaze/request.rb', line 96 def http_variables env.reject{|key, value| key.to_s !~ INTERESTING_HTTP_VARIABLES } end |
#pretty_print(pp) ⇒ Object
Pretty prints current action with parameters, cookies and environment variables.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ramaze/request.rb', line 110 def pretty_print(pp) pp.object_group(self) do group = { 'params' => params, 'cookies' => , 'env' => http_variables } group.each do |name, hash| pp.breakable pp.text " @#{name}=" pp.nest(name.size + 3){ pp.pp_hash(hash) } end end end |
#to_instance_variables(*args) ⇒ Object Also known as: to_ivs
Sets any arguments passed as @instance_variables for the current action.
Usage:
request.params # => {'name' => 'manveru', 'q' => 'google', 'lang' => 'de'}
request.to_ivs(:name, :q)
@q # => 'google'
@name # => 'manveru'
@lang # => nil
30 31 32 33 34 35 36 |
# File 'lib/ramaze/request.rb', line 30 def to_instance_variables(*args) instance = Current.action.instance args.each do |arg| next unless value = self[arg] instance.instance_variable_set("@#{arg}", value) end end |
#to_s ⇒ Object Also known as: inspect
103 104 105 |
# File 'lib/ramaze/request.rb', line 103 def to_s REQUEST_STRING_FORMAT % [self.class, params, , http_variables] end |