Class: Rage::Request

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

Instance Method Summary collapse

Instance Method Details

#delete?Boolean

Check the HTTP request method to see if it was of type DELETE.

Returns:

  • (Boolean)

    true if it was a DELETE request



109
110
111
# File 'lib/rage/request.rb', line 109

def delete?
  rack_request.delete?
end

#domain(tld_length = 1) ⇒ Object

Get the domain part of the request.

Examples:

Consider a URL like: example.foo.gov

request.domain => "foo.gov"
request.domain(0) => "gov"
request.domain(2) => "example.foo.gov"

Parameters:

  • tld_length (Integer) (defaults to: 1)

    top-level domain length



192
193
194
# File 'lib/rage/request.rb', line 192

def domain(tld_length = 1)
  extract_domain(host, tld_length)
end

#envHash

Get the Rack environment hash.

Returns:

  • (Hash)

    the Rack env



79
80
81
# File 'lib/rage/request.rb', line 79

def env
  @env
end

#formatString?

Get the content type of the request.

Returns:

  • (String, nil)

    the MIME type of the request body



151
152
153
# File 'lib/rage/request.rb', line 151

def format
  rack_request.content_type
end

#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.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rage/request.rb', line 171

def fresh?(etag:, last_modified:)
  request_if_none_match = if_none_match
  request_not_modified_since = if_not_modified_since

  # Always render response when no freshness information
  # is provided in the request.
  return false unless request_if_none_match || request_not_modified_since

  if request_if_none_match
    etag_matches?(requested_etags: request_if_none_match, response_etag: etag)
  else
    not_modified?(request_not_modified_since: request_not_modified_since, response_last_modified: last_modified)
  end
end

#fullpathString

Get the request path including the query string.

Examples:

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

Returns:

  • (String)

    path with query string (if present)



137
138
139
# File 'lib/rage/request.rb', line 137

def fullpath
  rack_request.fullpath
end

#get?Boolean

Check the HTTP request method to see if it was of type GET.

Returns:

  • (Boolean)

    true if it was a GET request



85
86
87
# File 'lib/rage/request.rb', line 85

def get?
  rack_request.get?
end

#head?Boolean

Check the HTTP request method to see if it was of type HEAD.

Returns:

  • (Boolean)

    true if it was a HEAD request



115
116
117
# File 'lib/rage/request.rb', line 115

def head?
  rack_request.head?
end

#headersObject

Get the request headers.

Examples:

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


159
160
161
# File 'lib/rage/request.rb', line 159

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

#hostString

Get the hostname from the request.

Returns:

  • (String)

    the hostname



61
62
63
# File 'lib/rage/request.rb', line 61

def host
  rack_request.host
end

#methodString

Get the HTTP method of the request. If the client is using the Rack::MethodOverride middleware, then the X-HTTP-Method-Override header is checked first.

Returns:

  • (String)

    The HTTP Method override header or the request method header



199
200
201
# File 'lib/rage/request.rb', line 199

def method
  check_method(@env["rack.methodoverride.original_method"] || @env["REQUEST_METHOD"])
end

#patch?Boolean

Check the HTTP request method to see if it was of type PATCH.

Returns:

  • (Boolean)

    true if it was a PATCH request



97
98
99
# File 'lib/rage/request.rb', line 97

def patch?
  rack_request.patch?
end

#pathString

Get the request path.

Examples:

request.path # => "/users"

Returns:

  • (String)

    the path component of the URL



129
130
131
# File 'lib/rage/request.rb', line 129

def path
  rack_request.path
end

#portInteger

Get the port number from the request.

Returns:

  • (Integer)

    the port number



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

def port
  rack_request.port
end

#post?Boolean

Check the HTTP request method to see if it was of type POST.

Returns:

  • (Boolean)

    true if it was a POST request



91
92
93
# File 'lib/rage/request.rb', line 91

def post?
  rack_request.post?
end

#protocolString

Returns https:// if this was an HTTPS request and http:// otherwise.

Returns:

  • (String)

    https:// if this was an HTTP request over SSL/TLS or http:// if it was an HTTP request



55
56
57
# File 'lib/rage/request.rb', line 55

def protocol
  ssl? ? "https://" : "http://"
end

#put?Boolean

Check the HTTP request method to see if it was of type PUT.

Returns:

  • (Boolean)

    true if it was a PUT request



103
104
105
# File 'lib/rage/request.rb', line 103

def put?
  rack_request.put?
end

#query_stringString

Get the query string from the request.

Returns:

  • (String)

    the query string (empty string if no query)



73
74
75
# File 'lib/rage/request.rb', line 73

def query_string
  rack_request.query_string
end

#request_idString Also known as: uuid

Get the unique request ID. By default, this ID is internally generated, and all log entries created during the request are tagged with it. Alternatively, you can use the Rage::RequestId middleware to derive the ID from the X-Request-Id header.

Returns:

  • (String)

    the request ID



206
207
208
# File 'lib/rage/request.rb', line 206

def request_id
  @env["rage.request_id"]
end

#route_uri_patternString

Get the route URI pattern matched for this request.

Examples:

# For a route defined as:
#   get "/users/:id", to: "users#show"
request.route_uri_pattern # => "/users/:id"

Returns:

  • (String)

    the route URI pattern



218
219
220
221
222
223
224
# File 'lib/rage/request.rb', line 218

def route_uri_pattern
  if @controller
    Rage::Router::Util.route_uri_pattern(@controller.class, @controller.action_name)
  else
    path
  end
end

#ssl?Boolean

Check if the request was made using TLS/SSL which is if http or https protocol is used inside the URL.

Returns:

  • (Boolean)

    true if the request is TLS/SSL, false otherwise



49
50
51
# File 'lib/rage/request.rb', line 49

def ssl?
  rack_request.ssl?
end

#urlString

Get the full request URL.

Returns:

  • (String)

    complete URL including protocol, host, path, and query



121
122
123
# File 'lib/rage/request.rb', line 121

def url
  rack_request.url
end

#user_agentString?

Get the User-Agent header value.

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"

Returns:

  • (String, nil)

    the user agent string or nil



145
146
147
# File 'lib/rage/request.rb', line 145

def user_agent
  rack_request.user_agent
end