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
- HTTP_HEADERS_MATCH =
/^(?:[a-z0-9])+(?:_[a-z0-9]+)*$/i.freeze
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.
22 23 24 25 |
# File 'lib/webmachine/request.rb', line 22 def initialize(method, uri, headers, body) @method, @headers, @body = method, headers, body @uri = build_uri(uri, headers) 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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/webmachine/request.rb', line 32 def method_missing(m, *args, &block) if m =~ HTTP_HEADERS_MATCH # Access headers more easily as underscored methods. header_name = m.to_s.tr(UNDERSCORE, DASH) if (header_value = headers[header_name]) # Make future lookups faster. self.class.class_eval <<-RUBY, __FILE__, __LINE__ def #{m} headers["#{header_name}"] end RUBY end header_value else super end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
13 14 15 |
# File 'lib/webmachine/request.rb', line 13 def body @body end |
#disp_path ⇒ Object
Returns the value of attribute disp_path.
14 15 16 |
# File 'lib/webmachine/request.rb', line 14 def disp_path @disp_path end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
13 14 15 |
# File 'lib/webmachine/request.rb', line 13 def headers @headers end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
13 14 15 |
# File 'lib/webmachine/request.rb', line 13 def method @method end |
#path_info ⇒ Object
Returns the value of attribute path_info.
14 15 16 |
# File 'lib/webmachine/request.rb', line 14 def path_info @path_info end |
#path_tokens ⇒ Object
Returns the value of attribute path_tokens.
14 15 16 |
# File 'lib/webmachine/request.rb', line 14 def path_tokens @path_tokens end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
13 14 15 |
# File 'lib/webmachine/request.rb', line 13 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.
58 59 60 61 62 63 |
# File 'lib/webmachine/request.rb', line 58 def base_uri @base_uri ||= uri.dup.tap do |u| u.path = SLASH u.query = nil end end |
#connect? ⇒ Boolean
Is this a CONNECT request?
154 155 156 |
# File 'lib/webmachine/request.rb', line 154 def connect? method == CONNECT_METHOD end |
#cookies ⇒ Hash
The cookies sent in the request.
87 88 89 90 91 92 |
# File 'lib/webmachine/request.rb', line 87 def unless @cookies @cookies = Webmachine::Cookie.parse(headers['Cookie']) end @cookies end |
#delete? ⇒ Boolean
Is this a DELETE request?
138 139 140 |
# File 'lib/webmachine/request.rb', line 138 def delete? method == DELETE_METHOD end |
#get? ⇒ Boolean
Is this a GET request?
106 107 108 |
# File 'lib/webmachine/request.rb', line 106 def get? method == GET_METHOD end |
#has_body? ⇒ Boolean
@return[true, false] Whether the request body is present.
51 52 53 |
# File 'lib/webmachine/request.rb', line 51 def has_body? !(body.nil? || body.empty?) end |
#head? ⇒ Boolean
Is this a HEAD request?
114 115 116 |
# File 'lib/webmachine/request.rb', line 114 def head? method == HEAD_METHOD end |
#https? ⇒ Boolean
Is this an HTTPS request?
98 99 100 |
# File 'lib/webmachine/request.rb', line 98 def https? uri.scheme == "https" end |
#options? ⇒ Boolean
Is this an OPTIONS request?
162 163 164 |
# File 'lib/webmachine/request.rb', line 162 def method == OPTIONS_METHOD end |
#post? ⇒ Boolean
Is this a POST request?
122 123 124 |
# File 'lib/webmachine/request.rb', line 122 def post? method == POST_METHOD end |
#put? ⇒ Boolean
Is this a PUT request?
130 131 132 |
# File 'lib/webmachine/request.rb', line 130 def put? method == PUT_METHOD 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.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/webmachine/request.rb', line 69 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?
146 147 148 |
# File 'lib/webmachine/request.rb', line 146 def trace? method == TRACE_METHOD end |