Class: Lennarb::Request
- Inherits:
-
Rack::Request
- Object
- Rack::Request
- Lennarb::Request
- Defined in:
- lib/lennarb/request.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
The environment variables of the request.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #accept ⇒ Object
-
#body ⇒ Object
Read the body of the request.
- #content_length ⇒ Object
- #content_type ⇒ Object
-
#headers ⇒ Object
Get the headers of the request.
- #host ⇒ Object
-
#initialize(env, route_params = {}) ⇒ Request
constructor
Initialize the request object.
- #ip ⇒ Object
-
#params ⇒ Object
Get the request body.
-
#path ⇒ Object
Get the request path.
-
#query_params ⇒ Object
Get the query parameters.
- #referer ⇒ Object
- #secure? ⇒ Boolean
- #user_agent ⇒ Object
- #xhr? ⇒ Boolean
Constructor Details
#initialize(env, route_params = {}) ⇒ Request
Initialize the request object
21 22 23 24 |
# File 'lib/lennarb/request.rb', line 21 def initialize(env, route_params = {}) super(env) @route_params = route_params end |
Instance Attribute Details
#env ⇒ Object (readonly)
The environment variables of the request
12 13 14 |
# File 'lib/lennarb/request.rb', line 12 def env @env end |
Instance Method Details
#[](key) ⇒ Object
72 73 74 |
# File 'lib/lennarb/request.rb', line 72 def [](key) env[key] end |
#[]=(key, value) ⇒ Object
68 69 70 |
# File 'lib/lennarb/request.rb', line 68 def []=(key, value) env[key] = value end |
#accept ⇒ Object
61 |
# File 'lib/lennarb/request.rb', line 61 def accept = headers['HTTP_ACCEPT'] |
#body ⇒ Object
Read the body of the request
42 |
# File 'lib/lennarb/request.rb', line 42 def body = @body ||= super.read |
#content_length ⇒ Object
64 |
# File 'lib/lennarb/request.rb', line 64 def content_length = headers['HTTP_CONTENT_LENGTH'] |
#content_type ⇒ Object
65 |
# File 'lib/lennarb/request.rb', line 65 def content_type = headers['HTTP_CONTENT_TYPE'] |
#headers ⇒ Object
Get the headers of the request
54 55 56 |
# File 'lib/lennarb/request.rb', line 54 def headers @headers ||= env.select { |key, _| key.start_with?('HTTP_') } end |
#host ⇒ Object
63 |
# File 'lib/lennarb/request.rb', line 63 def host = headers['HTTP_HOST'] |
#ip ⇒ Object
58 |
# File 'lib/lennarb/request.rb', line 58 def ip = ip_address |
#params ⇒ Object
Get the request body
30 |
# File 'lib/lennarb/request.rb', line 30 def params = @params ||= super.merge(@route_params)&.transform_keys(&:to_sym) |
#path ⇒ Object
Get the request path
36 |
# File 'lib/lennarb/request.rb', line 36 def path = @path ||= super.split('?').first |
#query_params ⇒ Object
Get the query parameters
48 49 50 |
# File 'lib/lennarb/request.rb', line 48 def query_params @query_params ||= Rack::Utils.parse_nested_query(query_string).transform_keys(&:to_sym) end |
#referer ⇒ Object
62 |
# File 'lib/lennarb/request.rb', line 62 def referer = headers['HTTP_REFERER'] |
#secure? ⇒ Boolean
59 |
# File 'lib/lennarb/request.rb', line 59 def secure? = scheme == 'https' |
#user_agent ⇒ Object
60 |
# File 'lib/lennarb/request.rb', line 60 def user_agent = headers['HTTP_USER_AGENT'] |
#xhr? ⇒ Boolean
66 |
# File 'lib/lennarb/request.rb', line 66 def xhr? = headers['HTTP_X_REQUESTED_WITH']&.casecmp('XMLHttpRequest')&.zero? |