Class: Lennarb::Request

Inherits:
Rack::Request
  • Object
show all
Defined in:
lib/lennarb/request.rb

Overview

Request object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, route_params = {}) ⇒ Request

Initialize the request object

Parameters:

  • env (Hash)
  • route_params (Hash) (defaults to: {})


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

#envObject (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

Parameters:

  • key (String)


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

def [](key)
  env[key]
end

#[]=(key, value) ⇒ Object

Set a value in the environment

Parameters:

  • key (String)
  • value (Object)


60
61
62
# File 'lib/lennarb/request.rb', line 60

def []=(key, value)
  env[key] = value
end

#acceptObject

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

Returns:

  • (Boolean)


181
182
183
# File 'lib/lennarb/request.rb', line 181

def ajax?
  xhr?
end

#bodyObject

Read the body of the request



42
43
44
# File 'lib/lennarb/request.rb', line 42

def body
  @body ||= super.read
end

#content_lengthObject

Get the content length header



135
136
137
# File 'lib/lennarb/request.rb', line 135

def content_length
  env["HTTP_CONTENT_LENGTH"]
end

#content_typeObject

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

Returns:

  • (Boolean)


225
226
227
# File 'lib/lennarb/request.rb', line 225

def delete?
  request_method == "DELETE"
end

#formatObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


233
234
235
# File 'lib/lennarb/request.rb', line 233

def head?
  request_method == "HEAD"
end

#headersObject

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

#hostObject

Get the host header



127
128
129
# File 'lib/lennarb/request.rb', line 127

def host
  env["HTTP_HOST"]
end

#ipObject

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

Returns:

  • (Boolean)


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

def json?
  content_type&.include?("application/json")
end

#json_bodyObject

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

#paramsObject

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

Returns:

  • (Boolean)


241
242
243
# File 'lib/lennarb/request.rb', line 241

def patch?
  request_method == "PATCH"
end

#pathObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


217
218
219
# File 'lib/lennarb/request.rb', line 217

def put?
  request_method == "PUT"
end

#query_paramsObject

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

#refererObject

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)

Returns:

  • (Boolean)


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

def secure?
  scheme == "https"
end

#user_agentObject

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

Returns:

  • (Boolean)


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

def xhr?
  env["HTTP_X_REQUESTED_WITH"]&.casecmp("XMLHttpRequest")&.zero? || false
end