Class: Lennarb::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

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

#acceptObject



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

def accept         = headers['HTTP_ACCEPT']

#bodyObject

Read the body of the request



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

def body = @body ||= super.read

#content_lengthObject



64
# File 'lib/lennarb/request.rb', line 64

def content_length = headers['HTTP_CONTENT_LENGTH']

#content_typeObject



65
# File 'lib/lennarb/request.rb', line 65

def content_type   = headers['HTTP_CONTENT_TYPE']

#headersObject

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

#hostObject



63
# File 'lib/lennarb/request.rb', line 63

def host           = headers['HTTP_HOST']

#ipObject



58
# File 'lib/lennarb/request.rb', line 58

def ip             = ip_address

#paramsObject

Get the request body



30
# File 'lib/lennarb/request.rb', line 30

def params = @params ||= super.merge(@route_params)&.transform_keys(&:to_sym)

#pathObject

Get the request path



36
# File 'lib/lennarb/request.rb', line 36

def path = @path ||= super.split('?').first

#query_paramsObject

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

#refererObject



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

def referer        = headers['HTTP_REFERER']

#secure?Boolean

Returns:

  • (Boolean)


59
# File 'lib/lennarb/request.rb', line 59

def secure?        = scheme == 'https'

#user_agentObject



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

def user_agent     = headers['HTTP_USER_AGENT']

#xhr?Boolean

Returns:

  • (Boolean)


66
# File 'lib/lennarb/request.rb', line 66

def xhr?           = headers['HTTP_X_REQUESTED_WITH']&.casecmp('XMLHttpRequest')&.zero?