Class: Tzispa::Http::Context

Inherits:
Context
  • Object
show all
Extended by:
Forwardable
Includes:
Tzispa::Helpers::Response, Tzispa::Helpers::Session
Defined in:
lib/tzispa/http/context.rb

Direct Known Subclasses

Engine::Rig::Context

Instance Attribute Summary collapse

Attributes inherited from Context

#app, #cache, #env

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Context

Returns a new instance of Context.



22
23
24
25
26
27
# File 'lib/tzispa/http/context.rb', line 22

def initialize(app, env)
  super(app, env)
  @request = Request.new(env)
  @response = Response.new
  init_session
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



19
20
21
# File 'lib/tzispa/http/context.rb', line 19

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



19
20
21
# File 'lib/tzispa/http/context.rb', line 19

def response
  @response
end

Instance Method Details

#app_canonical_url(app_name, path_id, params = {}) ⇒ Object



63
64
65
# File 'lib/tzispa/http/context.rb', line 63

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



46
47
48
# File 'lib/tzispa/http/context.rb', line 46

def app_path(app_name, path_id, params = {})
  app[app_name].routes.path path_id, params
end

#canonical_rootObject



50
51
52
53
54
55
56
57
# File 'lib/tzispa/http/context.rb', line 50

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



59
60
61
# File 'lib/tzispa/http/context.rb', line 59

def canonical_url(path_id, params = {})
  "#{canonical_root}#{path(path_id, params)}"
end

#error_log(ex, status = nil) ⇒ Object



67
68
69
# File 'lib/tzispa/http/context.rb', line 67

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



71
72
73
# File 'lib/tzispa/http/context.rb', line 71

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



42
43
44
# File 'lib/tzispa/http/context.rb', line 42

def path(path_id, params = {})
  app.routes.path path_id, params
end

#request_methodObject



29
30
31
32
33
34
35
36
# File 'lib/tzispa/http/context.rb', line 29

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_paramsObject



38
39
40
# File 'lib/tzispa/http/context.rb', line 38

def router_params
  env['router.params'] || {}
end