Class: Tzispa::Http::Context

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

Constant Summary collapse

SESSION_LAST_ACCESS =
:__last_access
SESSION_ID =
:__session_id
SESSION_AUTH_USER =
:__auth__user
GLOBAL_MESSAGE_FLASH =
:__global_message_flash

Instance Attribute Summary collapse

Attributes inherited from Context

#app, #cache, #env

Instance Method Summary collapse

Constructor Details

#initialize(app, environment) ⇒ Context

Returns a new instance of Context.



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

def initialize(app, environment)
  super(app, environment)
  @request = Tzispa::Http::Request.new(environment)
  @response = Tzispa::Http::Response.new
  generate_session_id unless session[SESSION_ID]
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

#api(handler, verb, predicate, sufix, app_name = nil) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/tzispa/http/context.rb', line 115

def api(handler, verb, predicate, sufix, app_name = nil)
  unless app_name
    canonical_url :api, handler: handler, verb: verb, predicate: predicate, sufix: sufix
  else
    app_canonical_url app_name, :api, handler: handler, verb: verb, predicate: predicate, sufix: sufix
  end
end

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



93
94
95
# File 'lib/tzispa/http/context.rb', line 93

def app_canonical_url(app_name, path_id, params={})
  app[app_name].config.canonical_url + app_path(app_name, path_id, params)
end

#app_layout_canonical_url(app_name, layout, params = {}) ⇒ Object



111
112
113
# File 'lib/tzispa/http/context.rb', line 111

def app_layout_canonical_url(app_name, layout, params={})
  app[app_name].config.canonical_url + app_layout_path(app_name, layout, params)
end

#app_layout_path(app_name, layout, params = {}) ⇒ Object



102
103
104
105
# File 'lib/tzispa/http/context.rb', line 102

def app_layout_path(app_name, layout, params={})
  params = params.merge(layout: layout) unless app[app_name].config.default_layout&.to_sym == layout
  app[app_name].routes.path layout, normalize_format(params)
end

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



85
86
87
# File 'lib/tzispa/http/context.rb', line 85

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

#canonical_url(path_id, params = {}) ⇒ Object



89
90
91
# File 'lib/tzispa/http/context.rb', line 89

def canonical_url(path_id, params={})
  app.config.canonical_url + path(path_id, params)
end

#error_500(str) ⇒ Object



75
76
77
78
79
# File 'lib/tzispa/http/context.rb', line 75

def error_500(str)
  500.tap { |code|
    response.body = str if str
  }
end

#flashObject



51
52
53
# File 'lib/tzispa/http/context.rb', line 51

def flash
  @flash ||= SessionFlashBag.new(session, GLOBAL_MESSAGE_FLASH)
end

#last_accessObject



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

def last_access
  session[SESSION_LAST_ACCESS]
end

#layoutObject



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

def layout
  router_params&.fetch(:layout, nil)
end

#layout_canonical_url(layout, params = {}) ⇒ Object



107
108
109
# File 'lib/tzispa/http/context.rb', line 107

def layout_canonical_url(layout, params={})
  app.config.canonical_url + layout_path(layout, params)
end

#layout_path(layout, params = {}) ⇒ Object



97
98
99
100
# File 'lib/tzispa/http/context.rb', line 97

def layout_path(layout, params={})
  params = params.merge(layout: layout) unless app.config.default_layout&.to_sym == layout
  app.routes.path layout, normalize_format(params)
end

#logged?Boolean

Returns:

  • (Boolean)


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

def logged?
  session? and (not session[SESSION_AUTH_USER].nil?)
end

#loginObject



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

def 
  session[SESSION_AUTH_USER]
end

#login=(user) ⇒ Object



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

def login=(user)
  session[SESSION_AUTH_USER] = user unless user.nil?
end

#logoutObject



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

def logout
  session.delete(SESSION_AUTH_USER)
end

#path(path_id, params = {}) ⇒ Object



81
82
83
# File 'lib/tzispa/http/context.rb', line 81

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

#path_sign?(sign, *args) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/tzispa/http/context.rb', line 133

def path_sign?(sign, *args)
  sign == sign_array(args, config.salt)
end

#router_paramsObject



35
36
37
# File 'lib/tzispa/http/context.rb', line 35

def router_params
  env['router.params'] || Hash.new
end

#sapi(handler, verb, predicate, sufix, app_name = nil) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/tzispa/http/context.rb', line 123

def sapi(handler, verb, predicate, sufix, app_name = nil)
  unless app_name
    sign = sign_array [handler, verb, predicate], app.config.salt
    canonical_url :sapi, sign: sign, handler: handler, verb: verb, predicate: predicate, sufix: sufix
  else
    sign = sign_array [handler, verb, predicate], app[:app_name].config.salt
    app_canonical_url app_name, :sapi, sign: sign, handler: handler, verb: verb, predicate: predicate, sufix: sufix
  end
end

#session?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/tzispa/http/context.rb', line 55

def session?
  (not session[SESSION_ID].nil?) and (session[SESSION_ID] == session.id)
end

#set_last_accessObject



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

def set_last_access
  session[SESSION_LAST_ACCESS] = Time.now.utc.iso8601
end