Class: Waves::Request
Overview
Waves::Request represents an HTTP request and provides convenient methods for accessing request attributes. See Rack::Request for documentation of any method not defined here.
Defined Under Namespace
Modules: Utilities Classes: Accept, ParseError, Query
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#traits ⇒ Object
readonly
Returns the value of attribute traits.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#accept ⇒ Object
this is a hack - need to incorporate browser variations for “accept” here …
- #accept_charset ⇒ Object
- #accept_language ⇒ Object
-
#initialize(env) ⇒ Request
constructor
Create a new request.
-
#method ⇒ Object
The request method.
-
#method_missing(name, *args, &body) ⇒ Object
access HTTP headers as methods.
-
#not_found ⇒ Object
Raise a not found exception.
-
#path ⇒ Object
The request path (PATH_INFO).
-
#query ⇒ Object
(also: #params)
Access to “params” - aka the query string - as a hash.
- #rack_request ⇒ Object
-
#redirect(path, status = '302') ⇒ Object
Issue a redirect for the given path.
Constructor Details
#initialize(env) ⇒ Request
Create a new request. Takes a env parameter representing the request passed in from Rack. You shouldn’t need to call this directly.
16 17 18 19 20 21 |
# File 'lib/runtime/request.rb', line 16 def initialize( env ) @traits = Class.new { include Attributes }.new( :waves => {} ) @request = Rack::Request.new( env ).freeze @response = Waves::Response.new( self ) @session = Waves::Session.new( self ) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &body) ⇒ Object
access HTTP headers as methods
55 56 57 58 59 |
# File 'lib/runtime/request.rb', line 55 def method_missing( name, *args, &body ) return super unless args.empty? and body.nil? key = "HTTP_#{name.to_s.upcase}" @request.env[ key ] if @request.env.has_key?( key ) end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
12 13 14 |
# File 'lib/runtime/request.rb', line 12 def response @response end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
12 13 14 |
# File 'lib/runtime/request.rb', line 12 def session @session end |
#traits ⇒ Object (readonly)
Returns the value of attribute traits.
12 13 14 |
# File 'lib/runtime/request.rb', line 12 def traits @traits end |
Instance Method Details
#[](key) ⇒ Object
52 |
# File 'lib/runtime/request.rb', line 52 def []( key ) ; @request.env[ key.to_s.upcase ] ; end |
#accept ⇒ Object
this is a hack - need to incorporate browser variations for “accept” here … def accept ; Accept.parse(@request.env).unshift( Waves.config.mime_types[ path ] ).compact.uniq ; end
103 |
# File 'lib/runtime/request.rb', line 103 def accept ; @accept ||= Accept.parse( Waves.config.mime_types[ path.downcase ] || 'text/html' ) ; end |
#accept_charset ⇒ Object
104 |
# File 'lib/runtime/request.rb', line 104 def accept_charset ; @charset ||= Accept.parse(@request.env['HTTP_ACCEPT_CHARSET']) ; end |
#accept_language ⇒ Object
105 |
# File 'lib/runtime/request.rb', line 105 def accept_language ; @lang ||= Accept.parse(@request.env['HTTP_ACCEPT_LANGUAGE']) ; end |
#method ⇒ Object
The request method. Because browsers can’t send PUT or DELETE requests this can be simulated by sending a POST with a hidden field named ‘_method’ and a value with ‘PUT’ or ‘DELETE’. Also accepted is when a query parameter named ‘_method’ is provided.
47 48 49 50 |
# File 'lib/runtime/request.rb', line 47 def method @method ||= ( ( ( m = @request.request_method.downcase ) == 'post' and ( n = @request['_method'] ) ) ? n.downcase : m ).intern end |
#not_found ⇒ Object
Raise a not found exception.
62 63 64 |
# File 'lib/runtime/request.rb', line 62 def not_found raise Waves::Dispatchers::NotFoundError, "#{@request.url} not found." end |
#path ⇒ Object
The request path (PATH_INFO). Ex: /entry/2008-01-17
31 |
# File 'lib/runtime/request.rb', line 31 def path ; @request.path_info ; end |
#query ⇒ Object Also known as: params
Access to “params” - aka the query string - as a hash
34 |
# File 'lib/runtime/request.rb', line 34 def query ; @request.params ; end |
#rack_request ⇒ Object
23 |
# File 'lib/runtime/request.rb', line 23 def rack_request; @request; end |
#redirect(path, status = '302') ⇒ Object
Issue a redirect for the given path.
67 68 69 |
# File 'lib/runtime/request.rb', line 67 def redirect( path, status = '302' ) raise Waves::Dispatchers::Redirect.new( path, status ) end |