Class: Ramaze::Request
- 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
-
.current ⇒ Object
get the current request out of STATE.
Instance Method Summary collapse
- #[](key, *rest) ⇒ Object
- #domain(path = '/') ⇒ Object
-
#http_vars ⇒ Object
Interesting HTTP variables from env.
-
#ip ⇒ Object
the IP address(s) making the request provided by Rack::Request.
-
#local_net?(address = ip) ⇒ Boolean
returns true if the IP address making the request is from local network.
-
#locales ⇒ Object
Returns and array of locales 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.
-
#params ⇒ Object
Wrapping Request#params to support a one-level hash notation.
-
#pretty_print(pp) ⇒ Object
Pretty prints current action with parameters, cookies and enviroment variables.
-
#protocol ⇒ Object
Returns ‘https’ if this is an SSL request and ‘http’ otherwise.
-
#request_uri ⇒ Object
the full request URI provided by Rack::Request e.g.
-
#ssl? ⇒ Boolean
Is this an SSL request?.
-
#subset(*keys) ⇒ Object
Answers with a subset of request.params with only the key/value pairs for which you pass the keys.
-
#to_ivs(*args) ⇒ Object
Sets any arguments passed as @instance_variables for the current action.
-
#to_s ⇒ Object
(also: #inspect)
Returns a string presentation of the request, useful for debugging parameters of the action.
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
Instance Method Details
#[](key, *rest) ⇒ Object
64 65 66 67 |
# File 'lib/ramaze/current/request.rb', line 64 def [](key, *rest) return params[key.to_s] if rest.empty? [key, *rest].map{|k| params[k.to_s] } end |
#domain(path = '/') ⇒ Object
219 220 221 222 |
# File 'lib/ramaze/current/request.rb', line 219 def domain(path = '/') host = env['HTTP_HOST'] URI("#{protocol}://#{host}#{path}") end |
#http_vars ⇒ Object
Interesting HTTP variables from env
161 162 163 164 165 |
# File 'lib/ramaze/current/request.rb', line 161 def http_vars env.reject{ |k,v| k.to_s !~ /USER|HOST|REQUEST|REMOTE|FORWARD|REFER|PATH|QUERY|VERSION|KEEP|CACHE/ } end |
#ip ⇒ Object
the IP address(s) making the request provided by Rack::Request. You shouldn’t trust it
39 40 41 42 43 44 45 |
# File 'lib/ramaze/current/request.rb', line 39 def ip if addr = env['HTTP_X_FORWARDED_FOR'] addr.split(',').last.strip else env['REMOTE_ADDR'] end end |
#local_net?(address = ip) ⇒ Boolean
returns true if the IP address making the request is from local network. Optional argument address can be used to check any IP address.
57 58 59 60 61 62 |
# File 'lib/ramaze/current/request.rb', line 57 def local_net?(address = ip) addr = IPAddr.new(address) LOCAL.find{|range| range.include?(addr) } rescue ArgumentError => ex raise ArgumentError, ex unless ex. == 'invalid address' end |
#locales ⇒ Object
Returns and array of locales from env. e.g. [“fi”, “en”, “ja”, “fr”, “de”, “es”, “it”, “nl”, “sv”]
227 228 229 |
# File 'lib/ramaze/current/request.rb', line 227 def locales env['HTTP_ACCEPT_LANGUAGE'].to_s.split(/(?:,|;q=[\d.,]+)/) end |
#params ⇒ Object
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
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 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/ramaze/current/request.rb', line 117 def params return {} if put? return @ramaze_params if @ramaze_params begin @rack_params ||= super 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
Pretty prints current action with parameters, cookies and enviroment variables.
179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/ramaze/current/request.rb', line 179 def pretty_print pp p, c, e = params, , http_vars pp.object_group(self){ { 'params' => params, 'cookies' => , 'env' => http_vars }.each do |name, hash| pp.breakable pp.text " @#{name}=" pp.nest(name.length+3){ pp.pp_hash hash } end } end |
#protocol ⇒ Object
Returns ‘https’ if this is an SSL request and ‘http’ otherwise.
215 216 217 |
# File 'lib/ramaze/current/request.rb', line 215 def protocol ssl? ? 'https' : 'http' end |
#request_uri ⇒ Object
the full request URI provided by Rack::Request e.g. localhost:7000/controller/action?foo=bar.xhtml
33 34 35 |
# File 'lib/ramaze/current/request.rb', line 33 def request_uri env['REQUEST_URI'] || path_info end |
#ssl? ⇒ Boolean
Is this an SSL request?
210 211 212 |
# File 'lib/ramaze/current/request.rb', line 210 def ssl? env['HTTPS'] == 'on' || env['HTTP_X_FORWARDED_PROTO'] == 'https' 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.subset('name')
# => {'name' => 'jason'}
request.subset(:name, :job)
# => {'name' => 'jason', 'job' => 'lumberjack'}
204 205 206 207 |
# File 'lib/ramaze/current/request.rb', line 204 def subset(*keys) keys = keys.map{|k| k.to_s } params.reject{|k,v| not keys.include?(k) } end |
#to_ivs(*args) ⇒ Object
Sets any arguments passed as @instance_variables for the current action.
Usage:
request.params # => {'name' => 'manveru', 'q' => 'google', 'lang' => 'de'}
to_ivs(:name, :q)
@q # => 'google'
@name # => 'manveru'
@lang # => nil
78 79 80 81 82 83 84 |
# File 'lib/ramaze/current/request.rb', line 78 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_s ⇒ Object Also known as: inspect
Returns a string presentation of the request, useful for debugging parameters of the action.
170 171 172 173 |
# File 'lib/ramaze/current/request.rb', line 170 def to_s p, c, e = params.inspect, .inspect, http_vars.inspect %{#<Ramaze::Request params=#{p} cookies=#{c} env=#{e}>} end |