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
- 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 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
#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?
151 152 153 |
# File 'lib/webmachine/request.rb', line 151 def connect? method == CONNECT_METHOD end |
#cookies ⇒ Hash
The cookies sent in the request.
84 85 86 87 88 89 |
# File 'lib/webmachine/request.rb', line 84 def unless @cookies @cookies = Webmachine::Cookie.parse(headers['Cookie']) end @cookies end |
#delete? ⇒ Boolean
Is this a DELETE request?
135 136 137 |
# File 'lib/webmachine/request.rb', line 135 def delete? method == DELETE_METHOD end |
#get? ⇒ Boolean
Is this a GET request?
103 104 105 |
# File 'lib/webmachine/request.rb', line 103 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?
111 112 113 |
# File 'lib/webmachine/request.rb', line 111 def head? method == HEAD_METHOD end |
#https? ⇒ Boolean
Is this an HTTPS request?
95 96 97 |
# File 'lib/webmachine/request.rb', line 95 def https? uri.scheme == "https" end |
#options? ⇒ Boolean
Is this an OPTIONS request?
159 160 161 |
# File 'lib/webmachine/request.rb', line 159 def method == OPTIONS_METHOD end |
#post? ⇒ Boolean
Is this a POST request?
119 120 121 |
# File 'lib/webmachine/request.rb', line 119 def post? method == POST_METHOD end |
#put? ⇒ Boolean
Is this a PUT request?
127 128 129 |
# File 'lib/webmachine/request.rb', line 127 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?
143 144 145 |
# File 'lib/webmachine/request.rb', line 143 def trace? method == TRACE_METHOD end |