Class: Rage::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/request.rb

Instance Method Summary collapse

Instance Method Details

#fresh?(etag:, last_modified:) ⇒ Boolean

Check if the request is fresh.

Examples:

request.fresh?(etag: "123", last_modified: Time.utc(2023, 12, 15))
request.fresh?(last_modified: Time.utc(2023, 12, 15))
request.fresh?(etag: "123")

Parameters:

  • etag (String)

    The etag of the requested resource.

  • last_modified (Time)

    The last modified time of the requested resource.

Returns:

  • (Boolean)

    True if the request is fresh, false otherwise.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rage/request.rb', line 27

def fresh?(etag:, last_modified:)
  # Always render response when no freshness information
  # is provided in the request.
  return false unless if_none_match || if_not_modified_since

  etag_matches?(
    requested_etags: if_none_match, response_etag: etag
  ) && not_modified?(
    request_not_modified_since: if_not_modified_since,
    response_last_modified: last_modified
  )
end

#fullpathObject

Returns the full path including the query string.

Examples:

request.fullpath # => "/users?show_archived=true"


66
67
68
69
70
# File 'lib/rage/request.rb', line 66

def fullpath
  path = @env["PATH_INFO"]
  query_string = @env["QUERY_STRING"]
  query_string.empty? ? path : "#{path}?#{query_string}"
end

#headersObject

Get the request headers.

Examples:

request.headers["Content-Type"] # => "application/json"
request.headers["Connection"] # => "keep-alive"


15
16
17
# File 'lib/rage/request.rb', line 15

def headers
  @headers ||= Headers.new(@env)
end

#pathObject

Returns the path of the request.

Examples:

request.path # => "/users"


59
60
61
# File 'lib/rage/request.rb', line 59

def path
  @env["PATH_INFO"]
end

#urlObject

Returns the full URL of the request.

Examples:

request.url # => "https://example.com/users?show_archived=true"


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rage/request.rb', line 43

def url
  scheme = @env["rack.url_scheme"]
  host = @env["SERVER_NAME"]
  port = @env["SERVER_PORT"]
  path = @env["PATH_INFO"]
  query_string = @env["QUERY_STRING"]

  port_part = (scheme == "http" && port == "80") || (scheme == "https" && port == "443") ? "" : ":#{port}"
  query_part = query_string.empty? ? "" : "?#{query_string}"

  "#{scheme}://#{host}#{port_part}#{path}#{query_part}"
end

#user_agentObject

Returns the user agent of the request.

Examples:

request.user_agent # => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"


75
76
77
# File 'lib/rage/request.rb', line 75

def user_agent
  @env["HTTP_USER_AGENT"]
end