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
Direct Known Subclasses
Constant Summary collapse
- HTTP_HEADERS_MATCH =
/^(?:[a-z0-9])+(?:_[a-z0-9]+)*$/i.freeze
- ROUTING_PATH_MATCH =
/^\/(.*)/.freeze
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
-
#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.
-
#routing_tokens ⇒ Object
readonly
Returns the value of attribute routing_tokens.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#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, routing_tokens = nil, base_uri = nil) ⇒ 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, routing_tokens = nil, base_uri = nil) ⇒ Request
Returns a new instance of Request.
24 25 26 27 28 29 30 31 32 |
# File 'lib/webmachine/request.rb', line 24 def initialize(method, uri, headers, body, routing_tokens = nil, base_uri = nil) @method, @headers, @body = method, headers, body @uri = build_uri(uri, headers) @routing_tokens = routing_tokens || @uri.path.match(ROUTING_PATH_MATCH)[1].split(SLASH) @base_uri = base_uri || @uri.dup.tap do |u| u.path = SLASH u.query = nil end 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`.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/webmachine/request.rb', line 39 def method_missing(m, *args, &block) if HTTP_HEADERS_MATCH.match?(m) # 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__ + 1 def #{m} @headers["#{header_name}"] end RUBY end header_value else super end end |
Instance Attribute Details
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
15 16 17 |
# File 'lib/webmachine/request.rb', line 15 def base_uri @base_uri end |
#body ⇒ Object (readonly)
Returns the value of attribute body.
15 16 17 |
# File 'lib/webmachine/request.rb', line 15 def body @body end |
#disp_path ⇒ Object
Returns the value of attribute disp_path.
16 17 18 |
# File 'lib/webmachine/request.rb', line 16 def disp_path @disp_path end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
15 16 17 |
# File 'lib/webmachine/request.rb', line 15 def headers @headers end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
15 16 17 |
# File 'lib/webmachine/request.rb', line 15 def method @method end |
#path_info ⇒ Object
Returns the value of attribute path_info.
16 17 18 |
# File 'lib/webmachine/request.rb', line 16 def path_info @path_info end |
#path_tokens ⇒ Object
Returns the value of attribute path_tokens.
16 17 18 |
# File 'lib/webmachine/request.rb', line 16 def path_tokens @path_tokens end |
#routing_tokens ⇒ Object (readonly)
Returns the value of attribute routing_tokens.
15 16 17 |
# File 'lib/webmachine/request.rb', line 15 def routing_tokens @routing_tokens end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
15 16 17 |
# File 'lib/webmachine/request.rb', line 15 def uri @uri end |
Instance Method Details
#connect? ⇒ Boolean
Is this a CONNECT request?
149 150 151 |
# File 'lib/webmachine/request.rb', line 149 def connect? method == CONNECT_METHOD end |
#cookies ⇒ Hash
The cookies sent in the request.
84 85 86 87 |
# File 'lib/webmachine/request.rb', line 84 def @cookies ||= Webmachine::Cookie.parse(headers['Cookie']) @cookies end |
#delete? ⇒ Boolean
Is this a DELETE request?
133 134 135 |
# File 'lib/webmachine/request.rb', line 133 def delete? method == DELETE_METHOD end |
#get? ⇒ Boolean
Is this a GET request?
101 102 103 |
# File 'lib/webmachine/request.rb', line 101 def get? method == GET_METHOD end |
#has_body? ⇒ Boolean
@return[true, false] Whether the request body is present.
58 59 60 |
# File 'lib/webmachine/request.rb', line 58 def has_body? !(body.nil? || body.empty?) end |
#head? ⇒ Boolean
Is this a HEAD request?
109 110 111 |
# File 'lib/webmachine/request.rb', line 109 def head? method == HEAD_METHOD end |
#https? ⇒ Boolean
Is this an HTTPS request?
93 94 95 |
# File 'lib/webmachine/request.rb', line 93 def https? uri.scheme == 'https' end |
#options? ⇒ Boolean
Is this an OPTIONS request?
157 158 159 |
# File 'lib/webmachine/request.rb', line 157 def method == OPTIONS_METHOD end |
#post? ⇒ Boolean
Is this a POST request?
117 118 119 |
# File 'lib/webmachine/request.rb', line 117 def post? method == POST_METHOD end |
#put? ⇒ Boolean
Is this a PUT request?
125 126 127 |
# File 'lib/webmachine/request.rb', line 125 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.
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/webmachine/request.rb', line 66 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?
141 142 143 |
# File 'lib/webmachine/request.rb', line 141 def trace? method == TRACE_METHOD end |