Class: Ramaze::Request

Inherits:
Rack::Request
  • Object
show all
Defined in:
lib/ramaze/current/request.rb

Overview

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

Constant Summary collapse

LOCAL =
(ipv4 + ipv6).map{|a| IPAddr.new(a)}

Class Method Summary collapse

Instance Method Summary collapse

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



25
26
27
28
29
# File 'lib/ramaze/current/request.rb', line 25

def method_missing meth, *args
  key = meth.to_s.upcase
  return env[key] if env.has_key?(key)
  super
end

Class Method Details

.currentObject

get the current request out of Thread.current

You can call this from everywhere with Ramaze::Request.current



18
# File 'lib/ramaze/current/request.rb', line 18

def current() Current.request end

Instance Method Details

#[](key, *rest) ⇒ Object



58
59
60
61
62
63
# File 'lib/ramaze/current/request.rb', line 58

def [](key, *rest)
  value = params[key.to_s]
  return value if rest.empty?
  keys = rest.flatten.map{|k| k.to_s}
  Array[value, *params.values_at(*keys)]
end

#domain(path = '/') ⇒ Object



194
195
196
197
198
# File 'lib/ramaze/current/request.rb', line 194

def domain(path = '/')
  scheme = env['rack.url_scheme'] || 'http'
  host = env['HTTP_HOST']
  URI("#{scheme}://#{host}#{path}")
end

#http_varsObject

Interesting HTTP variables from env



152
153
154
155
156
# File 'lib/ramaze/current/request.rb', line 152

def http_vars
  env.reject{ |k,v|
    k.to_s !~ /USER|HOST|REQUEST|REMOTE|FORWARD|REFER|PATH|QUERY|VERSION|KEEP|CACHE/
  }
end

#ipObject



35
36
37
# File 'lib/ramaze/current/request.rb', line 35

def ip
  env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_ADDR']
end

#local_net?(address = ip) ⇒ Boolean

– Mongrel somehow puts together multiple IPs when proxy is involved. ++

Returns:

  • (Boolean)


50
51
52
53
54
55
56
# File 'lib/ramaze/current/request.rb', line 50

def local_net?(address = ip)
  address = address.to_s.split(',').first
  addr = IPAddr.new(address)
  LOCAL.find{|range| range.include?(addr) }
rescue ArgumentError => ex
  raise ArgumentError, ex unless ex.message == 'invalid address'
end

#localesObject



200
201
202
# File 'lib/ramaze/current/request.rb', line 200

def locales
  env['HTTP_ACCEPT_LANGUAGE'].to_s.split(/(?:,|;q=[\d.,]+)/)
end

#paramsObject

Wrapping Request#params to support a one-level hash notation. It doesn’t support anything really fancy, so be conservative in its use.

See if following provides something useful for us: redhanded.hobix.com/2006/01/25.html

Example Usage:

# Template:

<form action="/paste">
  <input type="text" name="paste[name]" />
  <input type="text" name="paste[syntax]" />
  <input type="submit" />
</form>

# In your Controller:

def paste
  name, syntax = request['paste'].values_at('name', 'syntax')
  paste = Paste.create_with(:name => name, :syntax => syntax)
  redirect '/'
end

# Or, easier:

def paste
  paste = Paste.create_with(request['paste'])
  redirect '/'
end


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ramaze/current/request.rb', line 107

def params
  return {} if put?
  return @ramaze_params if @ramaze_params

  begin
    @rack_params ||= rack_params
  rescue EOFError => ex
    @rack_params = {}
    Log.error(ex)
  end

  @ramaze_params = {}

  @rack_params.each do |key, value|
    if key =~ /^(.*?)(\[.*\])/
      prim, nested = $~.captures
      ref = @ramaze_params

      keys = nested.scan(/\[([^\]]+)\]/).flatten
      keys.unshift prim

      keys.each_with_index do |k, i|
        if i + 1 >= keys.size
          ref[k] = value
        else
          # in case the value is a string we cannot let it be ref next
          # time, so throw it away
          if ref[k].is_a?(String)
            ref = ref[k] = {}
          else
            ref = ref[k] ||= {}
          end
        end
      end
    else
      @ramaze_params[key] = value
    end
  end

  @ramaze_params
end

#pretty_print(pp) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ramaze/current/request.rb', line 164

def pretty_print pp
  p, c, e = params, cookies, http_vars
  pp.object_group(self){
    { 'params' => params,
      'cookies' => cookies,
      'env' => http_vars }.each do |name, hash|
      pp.breakable
      pp.text " @#{name}="
      pp.nest(name.length+3){ pp.pp_hash hash }
    end
  }
end

#rack_paramsObject



74
# File 'lib/ramaze/current/request.rb', line 74

alias rack_params params

#request_uriObject



31
32
33
# File 'lib/ramaze/current/request.rb', line 31

def request_uri
  env['REQUEST_URI'] || path_info
end

#subset(*keys) ⇒ Object

Answers with a subset of request.params with only the key/value pairs for which you pass the keys. Valid keys are objects that respond to :to_s

Example:

request.params
# => {'name' => 'jason', 'age' => '45', 'job' => 'lumberjack'}
request.sub('name')
# => {'name' => 'jason'}
request.sub(:name, :job)
# => {'name' => 'jason', 'job' => 'lumberjack'}


189
190
191
192
# File 'lib/ramaze/current/request.rb', line 189

def subset(*keys)
  keys = keys.map{|k| k.to_s }
  params.reject{|k,v| not keys.include?(k) }
end

#to_ivs(*args) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/ramaze/current/request.rb', line 65

def to_ivs(*args)
  instance = Action.current.instance
  args.each do |arg|
    next unless value = self[arg]
    instance.instance_variable_set("@#{arg}", value)
  end
end

#to_sObject Also known as: inspect



158
159
160
161
# File 'lib/ramaze/current/request.rb', line 158

def to_s
  p, c, e = params.inspect, cookies.inspect, http_vars.inspect
  %{#<Ramaze::Request params=#{p} cookies=#{c} env=#{e}>}
end