Class: Waves::Request

Inherits:
Object show all
Defined in:
lib/waves/request/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: 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.



17
18
19
20
21
# File 'lib/waves/request/request.rb', line 17

def initialize(env)
  @traits = Class.new { include Attributes }.new( :waves => {} )
  @request = Rack::Request.new(env).freeze
  @response = Waves::Response.new( self )
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#sessionObject (readonly)

Returns the value of attribute session.



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

def session
  @session
end

#traitsObject (readonly)

Returns the value of attribute traits.



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

def traits
  @traits
end

Instance Method Details

#[](key) ⇒ Object



46
# File 'lib/waves/request/request.rb', line 46

def []( key ) ; @request.env[ key.to_s ] ; end

#acceptObject

Requested representation MIME type



71
72
73
# File 'lib/waves/request/request.rb', line 71

def accept()
  @accept ||= Accept.parse(@request.env['HTTP_ACCEPT'])
end

#accept_charsetObject

Requested charset(s).

See Also:

  • Waves::Request.matchers/acceptmatchers/accept.rb


100
101
102
# File 'lib/waves/request/request.rb', line 100

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

#accept_languageObject

Requested language(s).

See Also:

  • Waves::Request.matchers/acceptmatchers/accept.rb


108
109
110
# File 'lib/waves/request/request.rb', line 108

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

#basenameObject



119
120
121
# File 'lib/waves/request/request.rb', line 119

def basename
  @basename ||= File.basename( path )
end

#extensionObject Also known as: ext

File extension of path, with leading dot



113
114
115
# File 'lib/waves/request/request.rb', line 113

def extension
  @ext ||= ( ( e = File.extname( path ) ).empty? ? nil : e )
end

#if_modified_sinceObject



40
41
42
43
44
# File 'lib/waves/request/request.rb', line 40

def if_modified_since
  @if_modified_since ||= 
    ( Time.parse( @request.env[ 'HTTP_IF_MODIFIED_SINCE' ] ) if 
      @request.env.has_key?( 'HTTP_IF_MODIFIED_SINCE' ) ) 
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.



65
66
67
68
# File 'lib/waves/request/request.rb', line 65

def method
  @method ||= ( ( ( m = @request.request_method.downcase ) == 'post' and
    ( n = @request['_method'] ) ) ? n.downcase : m ).intern
end

#pathObject

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



49
# File 'lib/waves/request/request.rb', line 49

def path ; @request.path_info ; end

#queryObject Also known as: params

Access to “params” - aka the query string - as a hash



52
# File 'lib/waves/request/request.rb', line 52

def query ; @request.params ; end

#rack_requestObject

Rack request object.



25
26
27
# File 'lib/waves/request/request.rb', line 25

def rack_request()
  @request
end

#requestedObject

Combination of Accept and file extension for matching.

A file extension takes precedence over the Accept header, the Accept is ignored.

The absence of a file extension is indicated using the special MIME type MimeTypes::Unspecified, which allows specialised handling thereof. The resource must specifically accept Unspecified for it to have an effect.

See Also:

  • Waves::Request.matchers/requestedmatchers/requested.rb
  • #accept
  • #ext
  • for the actual definition of the Unspecified type.


92
93
94
# File 'lib/waves/request/request.rb', line 92

def requested()
  @requested ||= ( extension ? Accept.new( Accept.parse( MimeTypes[ extension ].join(",") ) + accept ).uniq : accept )
end