Class: Tina4::Request
- Inherits:
-
Object
- Object
- Tina4::Request
- Defined in:
- lib/tina4/request.rb
Instance Attribute Summary collapse
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#path_params ⇒ Object
readonly
Returns the value of attribute path_params.
-
#query_string ⇒ Object
readonly
Returns the value of attribute query_string.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #bearer_token ⇒ Object
- #body ⇒ Object
- #cookies ⇒ Object
- #files ⇒ Object
- #header(name) ⇒ Object
-
#headers ⇒ Object
Lazy accessors — only compute when needed.
-
#initialize(env, path_params = {}) ⇒ Request
constructor
A new instance of Request.
- #json_body ⇒ Object
- #params ⇒ Object
- #session ⇒ Object
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_type ⇒ Object (readonly)
Returns the value of attribute content_type.
7 8 9 |
# File 'lib/tina4/request.rb', line 7 def content_type @content_type end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
7 8 9 |
# File 'lib/tina4/request.rb', line 7 def env @env end |
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
7 8 9 |
# File 'lib/tina4/request.rb', line 7 def ip @ip end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
7 8 9 |
# File 'lib/tina4/request.rb', line 7 def method @method end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/tina4/request.rb', line 7 def path @path end |
#path_params ⇒ Object (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_string ⇒ Object (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_token ⇒ Object
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 |
#body ⇒ Object
42 43 44 |
# File 'lib/tina4/request.rb', line 42 def body @body ||= read_body end |
#cookies ⇒ Object
34 35 36 |
# File 'lib/tina4/request.rb', line 34 def @cookies ||= end |
#files ⇒ Object
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 |
#headers ⇒ Object
Lazy accessors — only compute when needed
30 31 32 |
# File 'lib/tina4/request.rb', line 30 def headers @headers ||= extract_headers end |
#json_body ⇒ Object
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 |
#params ⇒ Object
50 51 52 |
# File 'lib/tina4/request.rb', line 50 def params @params ||= build_params end |
#session ⇒ Object
38 39 40 |
# File 'lib/tina4/request.rb', line 38 def session @session ||= @env["tina4.session"] || {} end |