class Ramaze::Request

The purpose of this class is to act as a simple wrapper for Rack::Request and provide some convinient methods for our own use.

Constants

INTERESTING_HTTP_VARIABLES
REQUEST_STRING_FORMAT

Public Instance Methods

accept_charset(default = 'UTF-8') click to toggle source
# 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']) click to toggle source

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']

@param [String to_s] string the value of HTTP_ACCEPT_LANGUAGE @return [Array] list of locales @see #accept_language_with_weight @author manveru

# 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
Also aliased as: locales
accept_language_with_weight(string = env['HTTP_ACCEPT_LANGUAGE']) click to toggle source

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]]

@param [String to_s] string the value of HTTP_ACCEPT_LANGUAGE @return [Array] array of [lang, weight] arrays @see #accept_language @author manveru

# 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() click to toggle source

Interesting HTTP variables from env

# File lib/ramaze/request.rb, line 96
def http_variables
  env.reject{|key, value| key.to_s !~ INTERESTING_HTTP_VARIABLES }
end
Also aliased as: http_vars
http_vars()
Alias for: http_variables
inspect()
Alias for: to_s
locales(string = env['HTTP_ACCEPT_LANGUAGE'])
Alias for: accept_language
method_missing(meth, *args) click to toggle source

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

Calls superclass method
# 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
pretty_print(pp) click to toggle source

Pretty prints current action with parameters, cookies and enviroment variables.

# File lib/ramaze/request.rb, line 110
def pretty_print(pp)
  pp.object_group(self) do
    group = {
      'params'  => params,
      'cookies' => 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) click to toggle source

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
# 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
Also aliased as: to_ivs
to_ivs(*args)
to_s() click to toggle source
# File lib/ramaze/request.rb, line 103
def to_s
  REQUEST_STRING_FORMAT % [self.class, params, cookies, http_variables]
end
Also aliased as: inspect