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.



105
106
107
# File 'lib/rage/request.rb', line 105

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"


186
187
188
# File 'lib/rage/request.rb', line 186

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

#envHash

Get the Rack environment hash.



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

def env
  @env
end

#formatString?

Get the content type of the request.



147
148
149
# File 'lib/rage/request.rb', line 147

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")


167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rage/request.rb', line 167

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

#fullpathString

Get the request path including the query string.

Examples:

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


133
134
135
# File 'lib/rage/request.rb', line 133

def fullpath
  rack_request.fullpath
end

#get?Boolean

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



81
82
83
# File 'lib/rage/request.rb', line 81

def get?
  rack_request.get?
end

#head?Boolean

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



111
112
113
# File 'lib/rage/request.rb', line 111

def head?
  rack_request.head?
end

#headersObject

Get the request headers.

Examples:

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


155
156
157
# File 'lib/rage/request.rb', line 155

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

#hostString

Get the hostname from the request.



57
58
59
# File 'lib/rage/request.rb', line 57

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.



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

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.



93
94
95
# File 'lib/rage/request.rb', line 93

def patch?
  rack_request.patch?
end

#pathString

Get the request path.

Examples:

request.path # => "/users"


125
126
127
# File 'lib/rage/request.rb', line 125

def path
  rack_request.path
end

#portInteger

Get the port number from the request.



63
64
65
# File 'lib/rage/request.rb', line 63

def port
  rack_request.port
end

#post?Boolean

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



87
88
89
# File 'lib/rage/request.rb', line 87

def post?
  rack_request.post?
end

#protocolString

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



51
52
53
# File 'lib/rage/request.rb', line 51

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

#put?Boolean

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



99
100
101
# File 'lib/rage/request.rb', line 99

def put?
  rack_request.put?
end

#query_stringString

Get the query string from the request.



69
70
71
# File 'lib/rage/request.rb', line 69

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.



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

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

#ssl?Boolean

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



45
46
47
# File 'lib/rage/request.rb', line 45

def ssl?
  rack_request.ssl?
end

#urlString

Get the full request URL.



117
118
119
# File 'lib/rage/request.rb', line 117

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"


141
142
143
# File 'lib/rage/request.rb', line 141

def user_agent
  rack_request.user_agent
end