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

#envHash (readonly)

The environment variables of the request

Returns:

  • (Hash)


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)

Returns:

  • (Object)


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)

Returns:

  • (Object)

    the value



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

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

#acceptString?

Get the accept header

Returns:

  • (String, nil)


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

Returns:

  • (Boolean)


185
186
187
# File 'lib/lennarb/request.rb', line 185

def ajax?
  xhr?
end

#bodyString

Read the body of the request

Returns:

  • (String)


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

def body
  @body ||= super.read
end

#content_lengthString?

Get the content length header

Returns:

  • (String, nil)


140
141
142
# File 'lib/lennarb/request.rb', line 140

def content_length
  env["HTTP_CONTENT_LENGTH"]
end

#content_typeString?

Get the content type header

Returns:

  • (String, nil)


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

Returns:

  • (Boolean)


231
232
233
# File 'lib/lennarb/request.rb', line 231

def delete?
  request_method == "DELETE"
end

#formatSymbol?

Get the requested format (.html, .json, etc)

Returns:

  • (Symbol, nil)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


239
240
241
# File 'lib/lennarb/request.rb', line 239

def head?
  request_method == "HEAD"
end

#headersHash

Get the headers of the request

Returns:

  • (Hash)


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

#hostString?

Get the host header

Returns:

  • (String, nil)


132
133
134
# File 'lib/lennarb/request.rb', line 132

def host
  env["HTTP_HOST"]
end

#ipString

Get the client IP address

Returns:

  • (String)


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

Returns:

  • (Boolean)


164
165
166
# File 'lib/lennarb/request.rb', line 164

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

#json_bodyHash?

Parse JSON body if content type is application/json

Returns:

  • (Hash, nil)


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

#paramsHash

Get the request parameters merged with route parameters

Returns:

  • (Hash)


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)


247
248
249
# File 'lib/lennarb/request.rb', line 247

def patch?
  request_method == "PATCH"
end

#pathString

Get the request path without query string

Returns:

  • (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)


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

Returns:

  • (Boolean)


223
224
225
# File 'lib/lennarb/request.rb', line 223

def put?
  request_method == "PUT"
end

#query_paramsHash

Get the query parameters

Returns:

  • (Hash)


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

#refererString?

Get the referer header

Returns:

  • (String, nil)


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)

Returns:

  • (Boolean)


98
99
100
# File 'lib/lennarb/request.rb', line 98

def secure?
  scheme == "https"
end

#user_agentString?

Get the user agent

Returns:

  • (String, nil)


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

Returns:

  • (Boolean)


156
157
158
# File 'lib/lennarb/request.rb', line 156

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