Class: Utopia::Request
- Inherits:
-
Object
- Object
- Utopia::Request
- Defined in:
- lib/utopia/request.rb
Overview
Utopia's application-facing request wrapper.
Protocol request methods are delegated to the underlying request; parsing and application conveniences live here rather than on protocol-http itself.
Constant Summary collapse
- QUERY_PARSER =
Protocol::URL::FormData::Parser.new
Instance Attribute Summary collapse
-
#delegate ⇒ Object
readonly
The underlying protocol request.
-
#exception ⇒ Object
The exception associated with this request, if any.
-
#localization ⇒ Object
The immutable localization preferences associated with this request, if installed.
-
#session ⇒ Object
The session associated with this request, if installed.
-
#variables ⇒ Object
The controller variables associated with this request, if installed.
Class Method Summary collapse
-
.[](*arguments) ⇒ Object
Build a Utopia request from the given protocol request arguments.
Instance Method Summary collapse
-
#cookies ⇒ Object
Decoded request cookies.
-
#initialize(delegate) ⇒ Request
constructor
Initialize the request.
-
#ip ⇒ Object
The remote peer IP address, if available.
- #path ⇒ Object
-
#path=(value) ⇒ Object
Assign the decoded application path while preserving the current query string.
-
#post? ⇒ Boolean
Whether the request method is POST.
-
#query? ⇒ Boolean
Whether the request method is QUERY.
-
#query_parameters ⇒ Object
Decoded query arguments.
-
#referrer ⇒ Object
The request referrer.
-
#request_path ⇒ Object
The normalized original request path, before any internal request rewrites.
-
#url ⇒ Object
The structured request URL.
-
#url=(url) ⇒ Object
Assign the structured request URL.
-
#user_agent ⇒ Object
The request user agent.
-
#with(method: nil, path: self.path) ⇒ Object
Build a derived request with updated protocol fields.
Constructor Details
#initialize(delegate) ⇒ Request
Initialize the request.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/utopia/request.rb', line 26 def initialize(delegate) @delegate = delegate @url = nil @session = nil @variables = nil @localization = nil @exception = nil @query_parameters = nil @cookies = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object (private)
190 191 192 193 194 195 196 |
# File 'lib/utopia/request.rb', line 190 def method_missing(name, ...) if @delegate.respond_to?(name) @delegate.public_send(name, ...) else super end end |
Instance Attribute Details
#delegate ⇒ Object (readonly)
The underlying protocol request.
40 41 42 |
# File 'lib/utopia/request.rb', line 40 def delegate @delegate end |
#exception ⇒ Object
The exception associated with this request, if any.
52 53 54 |
# File 'lib/utopia/request.rb', line 52 def exception @exception end |
#localization ⇒ Object
The immutable localization preferences associated with this request, if installed.
49 50 51 |
# File 'lib/utopia/request.rb', line 49 def localization @localization end |
#session ⇒ Object
The session associated with this request, if installed.
43 44 45 |
# File 'lib/utopia/request.rb', line 43 def session @session end |
#variables ⇒ Object
The controller variables associated with this request, if installed.
46 47 48 |
# File 'lib/utopia/request.rb', line 46 def variables @variables end |
Class Method Details
.[](*arguments) ⇒ Object
Build a Utopia request from the given protocol request arguments.
20 21 22 |
# File 'lib/utopia/request.rb', line 20 def self.[](*arguments) self.new(Protocol::HTTP::Request[*arguments]) end |
Instance Method Details
#cookies ⇒ Object
Decoded request cookies.
140 141 142 |
# File 'lib/utopia/request.rb', line 140 def @cookies ||= (self.headers["cookie"]) end |
#ip ⇒ Object
The remote peer IP address, if available.
155 156 157 |
# File 'lib/utopia/request.rb', line 155 def ip @delegate.peer&.ip_address end |
#path ⇒ Object
71 72 73 |
# File 'lib/utopia/request.rb', line 71 def path Path.new(self.url.path.components(Protocol::URL::Encoding::System)) end |
#path=(value) ⇒ Object
Assign the decoded application path while preserving the current query string.
77 78 79 80 81 82 83 |
# File 'lib/utopia/request.rb', line 77 def path= value if value.is_a?(Protocol::URL::Path) self.url.path = value else self.url.path = Path[value].to_url_path end end |
#post? ⇒ Boolean
Whether the request method is POST.
92 93 94 |
# File 'lib/utopia/request.rb', line 92 def post? @delegate.method == "POST" end |
#query? ⇒ Boolean
Whether the request method is QUERY.
87 88 89 |
# File 'lib/utopia/request.rb', line 87 def query? @delegate.method == "QUERY" end |
#query_parameters ⇒ Object
Decoded query arguments.
116 117 118 |
# File 'lib/utopia/request.rb', line 116 def query_parameters @query_parameters ||= parse_query_parameters(self.url.query) end |
#referrer ⇒ Object
The request referrer.
150 151 152 |
# File 'lib/utopia/request.rb', line 150 def referrer @delegate.headers["referer"] end |
#request_path ⇒ Object
The normalized original request path, before any internal request rewrites.
98 99 100 101 102 103 |
# File 'lib/utopia/request.rb', line 98 def request_path path = @delegate.path.split("?", 2).first path = Protocol::URL::Path[path].normalize.simplify return Path.new(path.components(Protocol::URL::Encoding::System)) end |
#url ⇒ Object
The structured request URL.
66 67 68 |
# File 'lib/utopia/request.rb', line 66 def url @url ||= self.parse_url.normalize! end |
#url=(url) ⇒ Object
Assign the structured request URL.
161 162 163 164 |
# File 'lib/utopia/request.rb', line 161 def url= url @url = Protocol::URL[url] @query_parameters = nil end |
#user_agent ⇒ Object
The request user agent.
145 146 147 |
# File 'lib/utopia/request.rb', line 145 def user_agent @delegate.headers["user-agent"] end |
#with(method: nil, path: self.path) ⇒ Object
Build a derived request with updated protocol fields.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/utopia/request.rb', line 167 def with(method: nil, path: self.path) delegate = @delegate if method delegate = @delegate.dup delegate.method = method end request = self.class.new(delegate) request.session = @session request.variables = @variables request.localization = @localization request.exception = @exception request.path = path return request end |