Class: Lennarb::Request
- Inherits:
-
Rack::Request
- Object
- Rack::Request
- Lennarb::Request
- Defined in:
- lib/lennarb/request.rb
Overview
Request object
Instance Attribute Summary collapse
-
#env ⇒ Hash
readonly
The environment variables of the request.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get a value from the environment.
-
#[]=(key, value) ⇒ Object
Set a value in the environment.
-
#accept ⇒ String?
Get the accept header.
-
#ajax? ⇒ Boolean
Check if the request is an AJAX request (alias for xhr?).
-
#body ⇒ String
Read the body of the request.
-
#content_length ⇒ String?
Get the content length header.
-
#content_type ⇒ String?
Get the content type header.
-
#delete? ⇒ Boolean
Check if the request is a DELETE request.
-
#format ⇒ Symbol?
Get the requested format (.html, .json, etc).
-
#get? ⇒ Boolean
Check if the request is a GET request.
-
#head? ⇒ Boolean
Check if the request is a HEAD request.
-
#headers ⇒ Hash
Get the headers of the request.
-
#host ⇒ String?
Get the host header.
-
#initialize(env, route_params = {}) ⇒ Request
constructor
Initialize the request object.
-
#ip ⇒ String
Get the client IP address.
-
#json? ⇒ Boolean
Check if the request is a JSON request.
-
#json_body ⇒ Hash?
Parse JSON body if content type is application/json.
-
#params ⇒ Hash
Get the request parameters merged with route parameters.
-
#patch? ⇒ Boolean
Check if the request is a PATCH request.
-
#path ⇒ String
Get the request path without query string.
-
#post? ⇒ Boolean
Check if the request is a POST request.
-
#put? ⇒ Boolean
Check if the request is a PUT request.
-
#query_params ⇒ Hash
Get the query parameters.
-
#referer ⇒ String?
Get the referer header.
-
#secure? ⇒ Boolean
Check if the request is secure (HTTPS).
-
#user_agent ⇒ String?
Get the user agent.
-
#xhr? ⇒ Boolean
Check if the request is an XHR request.
Constructor Details
#initialize(env, route_params = {}) ⇒ Request
Initialize the request object
17 18 19 20 |
# File 'lib/lennarb/request.rb', line 17 def initialize(env, route_params = {}) super(env) @route_params = route_params || {} end |
Instance Attribute Details
#env ⇒ Hash (readonly)
The environment variables of the request
8 9 10 |
# File 'lib/lennarb/request.rb', line 8 def env @env end |
Instance Method Details
#[](key) ⇒ Object
Get a value from the environment
69 70 71 |
# File 'lib/lennarb/request.rb', line 69 def [](key) env[key] end |
#[]=(key, value) ⇒ Object
Set a value in the environment
60 61 62 |
# File 'lib/lennarb/request.rb', line 60 def []=(key, value) env[key] = value end |
#accept ⇒ String?
Get the accept header
116 117 118 |
# File 'lib/lennarb/request.rb', line 116 def accept env["HTTP_ACCEPT"] end |
#ajax? ⇒ Boolean
Check if the request is an AJAX request (alias for xhr?)
185 186 187 |
# File 'lib/lennarb/request.rb', line 185 def ajax? xhr? end |
#body ⇒ String
Read the body of the request
42 43 44 |
# File 'lib/lennarb/request.rb', line 42 def body @body ||= super.read end |
#content_length ⇒ String?
Get the content length header
140 141 142 |
# File 'lib/lennarb/request.rb', line 140 def content_length env["HTTP_CONTENT_LENGTH"] end |
#content_type ⇒ String?
Get the content type header
148 149 150 |
# File 'lib/lennarb/request.rb', line 148 def content_type env["HTTP_CONTENT_TYPE"] end |
#delete? ⇒ Boolean
Check if the request is a DELETE request
231 232 233 |
# File 'lib/lennarb/request.rb', line 231 def delete? request_method == "DELETE" end |
#format ⇒ Symbol?
Get the requested format (.html, .json, etc)
193 194 195 196 197 198 199 200 201 |
# File 'lib/lennarb/request.rb', line 193 def format @format ||= begin path_info = env["PATH_INFO"] return nil unless path_info.include?(".") extension = File.extname(path_info).delete(".") extension.empty? ? nil : extension.to_sym end end |
#get? ⇒ Boolean
Check if the request is a GET request
207 208 209 |
# File 'lib/lennarb/request.rb', line 207 def get? request_method == "GET" end |
#head? ⇒ Boolean
Check if the request is a HEAD request
239 240 241 |
# File 'lib/lennarb/request.rb', line 239 def head? request_method == "HEAD" end |
#headers ⇒ Hash
Get the headers of the request
77 78 79 80 81 82 83 84 |
# File 'lib/lennarb/request.rb', line 77 def headers @headers ||= env.each_with_object({}) do |(key, value), result| if key.start_with?("HTTP_") header_name = key.sub("HTTP_", "").split("_").map(&:capitalize).join("-") result[header_name] = value end end end |
#host ⇒ String?
Get the host header
132 133 134 |
# File 'lib/lennarb/request.rb', line 132 def host env["HTTP_HOST"] end |
#ip ⇒ String
Get the client IP address
90 91 92 |
# File 'lib/lennarb/request.rb', line 90 def ip ip_address end |
#json? ⇒ Boolean
Check if the request is a JSON request
164 165 166 |
# File 'lib/lennarb/request.rb', line 164 def json? content_type&.include?("application/json") end |
#json_body ⇒ Hash?
Parse JSON body if content type is application/json
172 173 174 175 176 177 178 179 |
# File 'lib/lennarb/request.rb', line 172 def json_body return nil unless json? @json_body ||= begin JSON.parse(body, symbolize_names: true) rescue JSON::ParserError nil end end |
#params ⇒ Hash
Get the request parameters merged with route parameters
26 27 28 |
# File 'lib/lennarb/request.rb', line 26 def params @params ||= super.merge(@route_params)&.transform_keys(&:to_sym) end |
#patch? ⇒ Boolean
Check if the request is a PATCH request
247 248 249 |
# File 'lib/lennarb/request.rb', line 247 def patch? request_method == "PATCH" end |
#path ⇒ String
Get the request path without query string
34 35 36 |
# File 'lib/lennarb/request.rb', line 34 def path @path ||= super.split("?").first end |
#post? ⇒ Boolean
Check if the request is a POST request
215 216 217 |
# File 'lib/lennarb/request.rb', line 215 def post? request_method == "POST" end |
#put? ⇒ Boolean
Check if the request is a PUT request
223 224 225 |
# File 'lib/lennarb/request.rb', line 223 def put? request_method == "PUT" end |
#query_params ⇒ Hash
Get the query parameters
50 51 52 |
# File 'lib/lennarb/request.rb', line 50 def query_params @query_params ||= Rack::Utils.parse_nested_query(query_string || "").transform_keys(&:to_sym) end |
#referer ⇒ String?
Get the referer header
124 125 126 |
# File 'lib/lennarb/request.rb', line 124 def referer env["HTTP_REFERER"] end |
#secure? ⇒ Boolean
Check if the request is secure (HTTPS)
98 99 100 |
# File 'lib/lennarb/request.rb', line 98 def secure? scheme == "https" end |
#user_agent ⇒ String?
Get the user agent
108 109 110 |
# File 'lib/lennarb/request.rb', line 108 def user_agent env["HTTP_USER_AGENT"] end |
#xhr? ⇒ Boolean
Check if the request is an XHR request
156 157 158 |
# File 'lib/lennarb/request.rb', line 156 def xhr? env["HTTP_X_REQUESTED_WITH"]&.casecmp("XMLHttpRequest")&.zero? || false end |