Class: Tina4::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Request.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tina4/request.rb', line 10

def initialize(env, path_params = {})
  @env = env
  @method = env["REQUEST_METHOD"]
  @path = env["PATH_INFO"] || "/"
  @query_string = env["QUERY_STRING"] || ""
  @content_type = env["CONTENT_TYPE"] || ""
  @ip = env["REMOTE_ADDR"] || "127.0.0.1"
  @path_params = path_params

  # Lazy-initialized fields (nil = not yet computed)
  @headers = nil
  @cookies = nil
  @session = nil
  @body = nil
  @params = nil
  @files = nil
  @json_body = nil
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



7
8
9
# File 'lib/tina4/request.rb', line 7

def content_type
  @content_type
end

#envObject (readonly)

Returns the value of attribute env.



7
8
9
# File 'lib/tina4/request.rb', line 7

def env
  @env
end

#ipObject (readonly)

Returns the value of attribute ip.



7
8
9
# File 'lib/tina4/request.rb', line 7

def ip
  @ip
end

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/tina4/request.rb', line 7

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/tina4/request.rb', line 7

def path
  @path
end

#path_paramsObject (readonly)

Returns the value of attribute path_params.



7
8
9
# File 'lib/tina4/request.rb', line 7

def path_params
  @path_params
end

#query_stringObject (readonly)

Returns the value of attribute query_string.



7
8
9
# File 'lib/tina4/request.rb', line 7

def query_string
  @query_string
end

Instance Method Details

#[](key) ⇒ Object



54
55
56
# File 'lib/tina4/request.rb', line 54

def [](key)
  params[key.to_s] || params[key.to_sym] || @path_params[key.to_sym]
end

#bearer_tokenObject



70
71
72
73
# File 'lib/tina4/request.rb', line 70

def bearer_token
  auth = header("authorization") || ""
  auth.sub(/\ABearer\s+/i, "") if auth =~ /\ABearer\s+/i
end

#bodyObject



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

def body
  @body ||= read_body
end

#cookiesObject



34
35
36
# File 'lib/tina4/request.rb', line 34

def cookies
  @cookies ||= parse_cookies
end

#filesObject



46
47
48
# File 'lib/tina4/request.rb', line 46

def files
  @files ||= extract_files
end

#header(name) ⇒ Object



58
59
60
# File 'lib/tina4/request.rb', line 58

def header(name)
  headers[name.to_s.downcase.gsub("-", "_")]
end

#headersObject

Lazy accessors — only compute when needed



30
31
32
# File 'lib/tina4/request.rb', line 30

def headers
  @headers ||= extract_headers
end

#json_bodyObject



62
63
64
65
66
67
68
# File 'lib/tina4/request.rb', line 62

def json_body
  @json_body ||= begin
    JSON.parse(body)
  rescue JSON::ParserError, TypeError
    {}
  end
end

#paramsObject



50
51
52
# File 'lib/tina4/request.rb', line 50

def params
  @params ||= build_params
end

#sessionObject



38
39
40
# File 'lib/tina4/request.rb', line 38

def session
  @session ||= @env["tina4.session"] || {}
end