Skip to content

Commit 93754f6

Browse files
Add request compatibility conveniences
Assisted-By: devx/166ed168-1c4d-4c63-a5f6-8d0d9cbff13f
1 parent 0cabd25 commit 93754f6

2 files changed

Lines changed: 108 additions & 5 deletions

File tree

lib/utopia/request.rb

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,12 @@ def [] key
111111
else
112112
if key.is_a?(String) && key.start_with?("HTTP_")
113113
self.headers[key[5..].downcase.tr("_", "-")]
114-
else
114+
elsif @attributes.key?(key)
115115
@attributes[key]
116+
elsif key.is_a?(Symbol) && @attributes.key?(key.to_s)
117+
@attributes[key.to_s]
118+
else
119+
self.arguments[key.to_s]
116120
end
117121
end
118122
end
@@ -166,6 +170,42 @@ def with(method: self.method, path: self.path, path_info: nil, attributes: {})
166170
def method
167171
@http.method
168172
end
173+
alias request_method method
174+
175+
# @returns [Boolean] Whether the HTTP request method is GET.
176+
def get?
177+
@http.method == "GET"
178+
end
179+
180+
# @returns [Boolean] Whether the HTTP request method is HEAD.
181+
def head?
182+
@http.method == "HEAD"
183+
end
184+
185+
# @returns [Boolean] Whether the HTTP request method is POST.
186+
def post?
187+
@http.method == "POST"
188+
end
189+
190+
# @returns [Boolean] Whether the HTTP request method is PUT.
191+
def put?
192+
@http.method == "PUT"
193+
end
194+
195+
# @returns [Boolean] Whether the HTTP request method is PATCH.
196+
def patch?
197+
@http.method == "PATCH"
198+
end
199+
200+
# @returns [Boolean] Whether the HTTP request method is DELETE.
201+
def delete?
202+
@http.method == "DELETE"
203+
end
204+
205+
# @returns [Boolean] Whether the HTTP request method is OPTIONS.
206+
def options?
207+
@http.method == "OPTIONS"
208+
end
169209

170210
# @returns [String] The full request path, including query string.
171211
def path
@@ -213,6 +253,29 @@ def cookies
213253
def host
214254
@http.authority || @http.headers["host"]
215255
end
256+
alias host_with_port host
257+
258+
# @returns [String | Nil] The request URL scheme.
259+
def scheme
260+
@http.scheme
261+
end
262+
263+
# @returns [Boolean] Whether the request URL scheme is HTTPS.
264+
def ssl?
265+
self.scheme == "https"
266+
end
267+
268+
# @returns [String] The request base URL if scheme and host are available.
269+
def base_url
270+
scheme = self.scheme
271+
host = self.host
272+
273+
if scheme && host
274+
"#{scheme}://#{host}"
275+
else
276+
""
277+
end
278+
end
216279

217280
# @returns [String | Nil] The request user agent.
218281
def user_agent
@@ -223,6 +286,12 @@ def user_agent
223286
def referrer
224287
@http.headers["referer"]
225288
end
289+
alias referer referrer
290+
291+
# @returns [Hash | Nil] The request session, if installed by Utopia::Session.
292+
def session
293+
@attributes["utopia.session"] || @attributes["rack.session"]
294+
end
226295

227296
# @returns [String | Nil] The remote peer address, if available.
228297
def ip
@@ -231,11 +300,10 @@ def ip
231300

232301
# @returns [String] The full request URL if scheme and host are available.
233302
def url
234-
scheme = @http.scheme
235-
host = self.host
303+
base_url = self.base_url
236304

237-
if scheme && host
238-
"#{scheme}://#{host}#{self.path}"
305+
if !base_url.empty?
306+
"#{base_url}#{self.path}"
239307
else
240308
self.path
241309
end

test/utopia/request.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,39 @@
2929

3030
expect(request.attributes[:locale]).to be == "en"
3131
end
32+
33+
it "provides HTTP method predicates" do
34+
expect(request.request_method).to be == "POST"
35+
expect(request.post?).to be == true
36+
expect(request.get?).to be == false
37+
expect(request.options?).to be == false
38+
end
39+
40+
it "looks up arguments by string or symbol keys" do
41+
expect(request["q"]).to be == "utopia"
42+
expect(request[:q]).to be == "utopia"
43+
end
44+
45+
it "prefers request-local attributes over arguments" do
46+
request[:q] = "local"
47+
48+
expect(request[:q]).to be == "local"
49+
end
50+
51+
it "provides common request conveniences" do
52+
http_request.scheme = "https"
53+
http_request.authority = "example.com"
54+
http_request.headers["referer"] = "/from"
55+
56+
request["utopia.session"] = {"user_id" => 10}
57+
58+
expect(request.scheme).to be == "https"
59+
expect(request.ssl?).to be == true
60+
expect(request.host_with_port).to be == "example.com"
61+
expect(request.base_url).to be == "https://example.com"
62+
expect(request.url).to be == "https://example.com/search?q=utopia"
63+
expect(request.referer).to be == "/from"
64+
expect(request.referrer).to be == "/from"
65+
expect(request.session).to be == {"user_id" => 10}
66+
end
3267
end

0 commit comments

Comments
 (0)