Class: Waves::Request

Inherits:
Object show all
Defined in:
lib/runtime/request.rb

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

Instance Method Summary collapse

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

#responseObject (readonly)

Returns the value of attribute response.



12
13
14
# File 'lib/runtime/request.rb', line 12

def response
  @response
end

#sessionObject (readonly)

Returns the value of attribute session.



12
13
14
# File 'lib/runtime/request.rb', line 12

def session
  @session
end

#traitsObject (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

#acceptObject

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_charsetObject



104
# File 'lib/runtime/request.rb', line 104

def accept_charset ; @charset ||= Accept.parse(@request.env['HTTP_ACCEPT_CHARSET']) ; end

#accept_languageObject



105
# File 'lib/runtime/request.rb', line 105

def accept_language ; @lang ||= Accept.parse(@request.env['HTTP_ACCEPT_LANGUAGE']) ; end

#methodObject

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_foundObject

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

#pathObject

The request path (PATH_INFO). Ex: /entry/2008-01-17



31
# File 'lib/runtime/request.rb', line 31

def path ; @request.path_info ; end

#queryObject 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_requestObject



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