Class: Tzispa::Http::Context
- Extended by:
- Forwardable
- Includes:
- Tzispa::Helpers::Response, Tzispa::Helpers::Session
- Defined in:
- lib/tzispa/http/context.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Attributes inherited from Context
Instance Method Summary collapse
- #app_canonical_url(app_name, path_id, params = {}) ⇒ Object
- #app_path(app_name, path_id, params = {}) ⇒ Object
- #canonical_root ⇒ Object
- #canonical_url(path_id, params = {}) ⇒ Object
- #error_log(ex, status = nil) ⇒ Object
- #info_log(ex, status = nil) ⇒ Object
-
#initialize(app, env) ⇒ Context
constructor
A new instance of Context.
- #path(path_id, params = {}) ⇒ Object
- #request_method ⇒ Object
- #router_params ⇒ Object
Constructor Details
#initialize(app, env) ⇒ Context
Returns a new instance of Context.
21 22 23 24 25 26 |
# File 'lib/tzispa/http/context.rb', line 21 def initialize(app, env) super(app, env) @request = Request.new(env) @response = Response.new init_session end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
18 19 20 |
# File 'lib/tzispa/http/context.rb', line 18 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
18 19 20 |
# File 'lib/tzispa/http/context.rb', line 18 def response @response end |
Instance Method Details
#app_canonical_url(app_name, path_id, params = {}) ⇒ Object
62 63 64 |
# File 'lib/tzispa/http/context.rb', line 62 def app_canonical_url(app_name, path_id, params = {}) "#{canonical_root}#{app_path(app_name, path_id, params)}" end |
#app_path(app_name, path_id, params = {}) ⇒ Object
45 46 47 |
# File 'lib/tzispa/http/context.rb', line 45 def app_path(app_name, path_id, params = {}) app[app_name].routes.path path_id, params end |
#canonical_root ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/tzispa/http/context.rb', line 49 def canonical_root @canonical_root ||= begin http_proto = Tzispa::Environment.instance.ssl? ? 'https://' : 'http://' http_host = Tzispa::Environment.instance.host http_port = Tzispa::Environment.instance.uri_port "#{http_proto}#{http_host}#{http_port}" end end |
#canonical_url(path_id, params = {}) ⇒ Object
58 59 60 |
# File 'lib/tzispa/http/context.rb', line 58 def canonical_url(path_id, params = {}) "#{canonical_root}#{path(path_id, params)}" end |
#error_log(ex, status = nil) ⇒ Object
66 67 68 |
# File 'lib/tzispa/http/context.rb', line 66 def error_log(ex, status = nil) logger.error "E [#{request.ip}] #{request.request_method} #{request.fullpath} #{status || response.status}\n#{ex.backtrace.first}: #{ex.message} (#{ex.class})\n#{ex.backtrace.drop(1).map { |s| "\t#{s}" }.join("\n") }" end |
#info_log(ex, status = nil) ⇒ Object
70 71 72 |
# File 'lib/tzispa/http/context.rb', line 70 def info_log(ex, status = nil) logger.info "I [#{request.ip}] #{request.request_method} #{request.fullpath} #{status || response.status}\n#{ex.backtrace.first}: #{ex.message} (#{ex.class})\n#{ex.backtrace.drop(1).map { |s| "\t#{s}" }.join("\n") }" end |
#path(path_id, params = {}) ⇒ Object
41 42 43 |
# File 'lib/tzispa/http/context.rb', line 41 def path(path_id, params = {}) app.routes.path path_id, params end |
#request_method ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/tzispa/http/context.rb', line 28 def request_method if request.request_method == 'POST' && request['_method'] env[Request::REQUEST_METHOD] = request['_method'] request['_method'] else request.request_method end end |
#router_params ⇒ Object
37 38 39 |
# File 'lib/tzispa/http/context.rb', line 37 def router_params env['router.params'] || {} end |