Class: Webmachine::Request
- Inherits:
-
Object
- Object
- Webmachine::Request
- Extended by:
- Forwardable
- Defined in:
- lib/webmachine/request.rb
Overview
Request represents a single HTTP request sent from a client. It should be instantiated by Adapters when a request is received
Constant Summary collapse
- STANDARD_HTTP_METHODS =
%w[GET HEAD POST PUT DELETE TRACE CONNECT OPTIONS]
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#disp_path ⇒ Object
Returns the value of attribute disp_path.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#path_info ⇒ Object
Returns the value of attribute path_info.
-
#path_tokens ⇒ Object
Returns the value of attribute path_tokens.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#base_uri ⇒ URI
The root URI for the request, ignoring path and query.
-
#connect? ⇒ Boolean
Is this a CONNECT request?.
-
#cookies ⇒ Hash
The cookies sent in the request.
-
#delete? ⇒ Boolean
Is this a DELETE request?.
-
#get? ⇒ Boolean
Is this a GET request?.
-
#has_body? ⇒ Boolean
@return[true, false] Whether the request body is present.
-
#head? ⇒ Boolean
Is this a HEAD request?.
-
#https? ⇒ Boolean
Is this an HTTPS request?.
-
#initialize(method, uri, headers, body) ⇒ Request
constructor
A new instance of Request.
-
#method_missing(m, *args, &block) ⇒ Object
Enables quicker access to request headers by using a lowercased-underscored version of the header name, e.g.
-
#options? ⇒ Boolean
Is this an OPTIONS request?.
-
#post? ⇒ Boolean
Is this a POST request?.
-
#put? ⇒ Boolean
Is this a PUT request?.
-
#query ⇒ Hash
Returns a hash of query parameters (they come after the ? in the URI).
-
#trace? ⇒ Boolean
Is this a TRACE request?.
Constructor Details
#initialize(method, uri, headers, body) ⇒ Request
Returns a new instance of Request.
20 21 22 |
# File 'lib/webmachine/request.rb', line 20 def initialize(method, uri, headers, body) @method, @uri, @headers, @body = method, uri, headers, body end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
Enables quicker access to request headers by using a lowercased-underscored version of the header name, e.g. ‘if_unmodified_since`.
29 30 31 32 33 34 35 36 |
# File 'lib/webmachine/request.rb', line 29 def method_missing(m, *args, &block) if m.to_s =~ /^(?:[a-z0-9])+(?:_[a-z0-9]+)*$/i # Access headers more easily as underscored methods. self[m.to_s.tr('_', '-')] else super end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
9 10 11 |
# File 'lib/webmachine/request.rb', line 9 def body @body end |
#disp_path ⇒ Object
Returns the value of attribute disp_path.
10 11 12 |
# File 'lib/webmachine/request.rb', line 10 def disp_path @disp_path end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
9 10 11 |
# File 'lib/webmachine/request.rb', line 9 def headers @headers end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
9 10 11 |
# File 'lib/webmachine/request.rb', line 9 def method @method end |
#path_info ⇒ Object
Returns the value of attribute path_info.
10 11 12 |
# File 'lib/webmachine/request.rb', line 10 def path_info @path_info end |
#path_tokens ⇒ Object
Returns the value of attribute path_tokens.
10 11 12 |
# File 'lib/webmachine/request.rb', line 10 def path_tokens @path_tokens end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
9 10 11 |
# File 'lib/webmachine/request.rb', line 9 def uri @uri end |
Instance Method Details
#base_uri ⇒ URI
The root URI for the request, ignoring path and query. This is useful for calculating relative paths to resources.
46 47 48 49 50 51 |
# File 'lib/webmachine/request.rb', line 46 def base_uri @base_uri ||= uri.dup.tap do |u| u.path = "/" u.query = nil end end |
#connect? ⇒ Boolean
Is this a CONNECT request?
142 143 144 |
# File 'lib/webmachine/request.rb', line 142 def connect? method == "CONNECT" end |
#cookies ⇒ Hash
The cookies sent in the request.
75 76 77 78 79 80 |
# File 'lib/webmachine/request.rb', line 75 def unless @cookies @cookies = Webmachine::Cookie.parse(headers['Cookie']) end @cookies end |
#delete? ⇒ Boolean
Is this a DELETE request?
126 127 128 |
# File 'lib/webmachine/request.rb', line 126 def delete? method == "DELETE" end |
#get? ⇒ Boolean
Is this a GET request?
94 95 96 |
# File 'lib/webmachine/request.rb', line 94 def get? method == "GET" end |
#has_body? ⇒ Boolean
@return[true, false] Whether the request body is present.
39 40 41 |
# File 'lib/webmachine/request.rb', line 39 def has_body? !(body.nil? || body.empty?) end |
#head? ⇒ Boolean
Is this a HEAD request?
102 103 104 |
# File 'lib/webmachine/request.rb', line 102 def head? method == "HEAD" end |
#https? ⇒ Boolean
Is this an HTTPS request?
86 87 88 |
# File 'lib/webmachine/request.rb', line 86 def https? uri.scheme == "https" end |
#options? ⇒ Boolean
Is this an OPTIONS request?
150 151 152 |
# File 'lib/webmachine/request.rb', line 150 def method == "OPTIONS" end |
#post? ⇒ Boolean
Is this a POST request?
110 111 112 |
# File 'lib/webmachine/request.rb', line 110 def post? method == "POST" end |
#put? ⇒ Boolean
Is this a PUT request?
118 119 120 |
# File 'lib/webmachine/request.rb', line 118 def put? method == "PUT" end |
#query ⇒ Hash
Returns a hash of query parameters (they come after the ? in the URI). Note that this does NOT work in the same way as Rails, i.e. it does not support nested arrays and hashes.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/webmachine/request.rb', line 57 def query unless @query @query = {} (uri.query || '').split(/&/).each do |kv| key, value = kv.split(/=/) if key && value key, value = CGI.unescape(key), CGI.unescape(value) @query[key] = value end end end @query end |
#trace? ⇒ Boolean
Is this a TRACE request?
134 135 136 |
# File 'lib/webmachine/request.rb', line 134 def trace? method == "TRACE" end |