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 ⇒ Object
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 ⇒ Object
Get the accept header.
-
#ajax? ⇒ Boolean
Check if the request is an AJAX request (alias for xhr?).
-
#body ⇒ Object
Read the body of the request.
-
#content_length ⇒ Object
Get the content length header.
-
#content_type ⇒ Object
Get the content type header.
-
#delete? ⇒ Boolean
Check if the request is a DELETE request.
-
#format ⇒ Object
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 ⇒ Object
Get the headers of the request.
-
#host ⇒ Object
Get the host header.
-
#initialize(env, route_params = {}) ⇒ Request
constructor
Initialize the request object.
-
#ip ⇒ Object
Get the client IP address.
-
#json? ⇒ Boolean
Check if the request is a JSON request.
-
#json_body ⇒ Object
Parse JSON body if content type is application/json.
-
#params ⇒ Object
Get the request parameters merged with route parameters.
-
#patch? ⇒ Boolean
Check if the request is a PATCH request.
-
#path ⇒ Object
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 ⇒ Object
Get the query parameters.
-
#referer ⇒ Object
Get the referer header.
-
#secure? ⇒ Boolean
Check if the request is secure (HTTPS).
-
#user_agent ⇒ Object
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 ⇒ Object (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 ⇒ Object
Get the accept header
111 112 113 |
# File 'lib/lennarb/request.rb', line 111 def accept env["HTTP_ACCEPT"] end |
#ajax? ⇒ Boolean
Check if the request is an AJAX request (alias for xhr?)
181 182 183 |
# File 'lib/lennarb/request.rb', line 181 def ajax? xhr? end |
#body ⇒ Object
Read the body of the request
42 43 44 |
# File 'lib/lennarb/request.rb', line 42 def body @body ||= super.read end |
#content_length ⇒ Object
Get the content length header
135 136 137 |
# File 'lib/lennarb/request.rb', line 135 def content_length env["HTTP_CONTENT_LENGTH"] end |
#content_type ⇒ Object
Get the content type header
143 144 145 |
# File 'lib/lennarb/request.rb', line 143 def content_type env["HTTP_CONTENT_TYPE"] end |
#delete? ⇒ Boolean
Check if the request is a DELETE request
225 226 227 |
# File 'lib/lennarb/request.rb', line 225 def delete? request_method == "DELETE" end |
#format ⇒ Object
Get the requested format (.html, .json, etc)
189 190 191 192 193 194 195 |
# File 'lib/lennarb/request.rb', line 189 def format path_info = env["PATH_INFO"] return nil unless path_info.include?(".") extension = File.extname(path_info).delete(".") extension.empty? ? nil : extension.to_sym end |
#get? ⇒ Boolean
Check if the request is a GET request
201 202 203 |
# File 'lib/lennarb/request.rb', line 201 def get? request_method == "GET" end |
#head? ⇒ Boolean
Check if the request is a HEAD request
233 234 235 |
# File 'lib/lennarb/request.rb', line 233 def head? request_method == "HEAD" end |
#headers ⇒ Object
Get the headers of the request
77 78 79 |
# File 'lib/lennarb/request.rb', line 77 def headers @headers ||= env.select { |key, _| key.start_with?("HTTP_") } end |
#host ⇒ Object
Get the host header
127 128 129 |
# File 'lib/lennarb/request.rb', line 127 def host env["HTTP_HOST"] end |
#ip ⇒ Object
Get the client IP address
85 86 87 |
# File 'lib/lennarb/request.rb', line 85 def ip ip_address end |
#json? ⇒ Boolean
Check if the request is a JSON request
159 160 161 |
# File 'lib/lennarb/request.rb', line 159 def json? content_type&.include?("application/json") end |
#json_body ⇒ Object
Parse JSON body if content type is application/json
167 168 169 170 171 172 173 174 175 |
# File 'lib/lennarb/request.rb', line 167 def json_body return nil unless json? @json_body ||= begin require "json" JSON.parse(body, symbolize_names: true) rescue JSON::ParserError nil end end |
#params ⇒ Object
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
241 242 243 |
# File 'lib/lennarb/request.rb', line 241 def patch? request_method == "PATCH" end |
#path ⇒ Object
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
209 210 211 |
# File 'lib/lennarb/request.rb', line 209 def post? request_method == "POST" end |
#put? ⇒ Boolean
Check if the request is a PUT request
217 218 219 |
# File 'lib/lennarb/request.rb', line 217 def put? request_method == "PUT" end |
#query_params ⇒ Object
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 ⇒ Object
Get the referer header
119 120 121 |
# File 'lib/lennarb/request.rb', line 119 def referer env["HTTP_REFERER"] end |
#secure? ⇒ Boolean
Check if the request is secure (HTTPS)
93 94 95 |
# File 'lib/lennarb/request.rb', line 93 def secure? scheme == "https" end |
#user_agent ⇒ Object
Get the user agent
103 104 105 |
# File 'lib/lennarb/request.rb', line 103 def user_agent env["HTTP_USER_AGENT"] end |
#xhr? ⇒ Boolean
Check if the request is an XHR request
151 152 153 |
# File 'lib/lennarb/request.rb', line 151 def xhr? env["HTTP_X_REQUESTED_WITH"]&.casecmp("XMLHttpRequest")&.zero? || false end |