Class: Tzispa::Http::Context
- Extended by:
- Forwardable
- Includes:
- Tzispa::Helpers::Response, Tzispa::Helpers::Security, Tzispa::Helpers::Session
- Defined in:
- lib/tzispa/http/context.rb
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
- #api(handler, verb, predicate, sufix, app_name = nil) ⇒ Object
- #app_canonical_url(app_name, path_id, params = {}) ⇒ Object
- #app_layout_canonical_url(app_name, layout, params = {}) ⇒ Object
- #app_layout_path(app_name, layout, params = {}) ⇒ Object
- #app_path(app_name, path_id, params = {}) ⇒ Object
- #canonical_root ⇒ Object
- #canonical_url(path_id, params = {}) ⇒ Object
-
#initialize(app, env) ⇒ Context
constructor
A new instance of Context.
- #layout ⇒ Object
- #layout_canonical_url(layout, params = {}) ⇒ Object
- #layout_path(layout, params = {}) ⇒ Object
- #login_redirect ⇒ Object
- #login_redirect? ⇒ Boolean
- #path(path_id, params = {}) ⇒ Object
- #path_sign?(sign, *args) ⇒ Boolean
- #request_method ⇒ Object
- #router_params ⇒ Object
- #sapi(handler, verb, predicate, sufix, app_name = nil) ⇒ Object
- #unauthorized_but_logged ⇒ Object
Constructor Details
#initialize(app, env) ⇒ Context
Returns a new instance of Context.
24 25 26 27 28 29 |
# File 'lib/tzispa/http/context.rb', line 24 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.
21 22 23 |
# File 'lib/tzispa/http/context.rb', line 21 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
21 22 23 |
# File 'lib/tzispa/http/context.rb', line 21 def response @response end |
Instance Method Details
#api(handler, verb, predicate, sufix, app_name = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/tzispa/http/context.rb', line 105 def api(handler, verb, predicate, sufix, app_name = nil) if app_name app_canonical_url app_name, :api, handler: handler, verb: verb, predicate: predicate, sufix: sufix else canonical_url :api, handler: handler, verb: verb, predicate: predicate, sufix: sufix end end |
#app_canonical_url(app_name, path_id, params = {}) ⇒ Object
81 82 83 |
# File 'lib/tzispa/http/context.rb', line 81 def app_canonical_url(app_name, path_id, params = {}) "#{canonical_root}#{app_path(app_name, path_id, params)}" end |
#app_layout_canonical_url(app_name, layout, params = {}) ⇒ Object
101 102 103 |
# File 'lib/tzispa/http/context.rb', line 101 def app_layout_canonical_url(app_name, layout, params = {}) "#{canonical_root}#{app_layout_path(app_name, layout, params)}" end |
#app_layout_path(app_name, layout, params = {}) ⇒ Object
91 92 93 94 95 |
# File 'lib/tzispa/http/context.rb', line 91 def app_layout_path(app_name, layout, params = {}) is_default = app[app_name].default_layout? == layout params = normalize_format(params.merge(layout: layout)) unless is_default app[app_name].routes.path layout, params end |
#app_path(app_name, path_id, params = {}) ⇒ Object
64 65 66 |
# File 'lib/tzispa/http/context.rb', line 64 def app_path(app_name, path_id, params = {}) app[app_name].routes.path path_id, params end |
#canonical_root ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/tzispa/http/context.rb', line 68 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
77 78 79 |
# File 'lib/tzispa/http/context.rb', line 77 def canonical_url(path_id, params = {}) "#{canonical_root}#{path(path_id, params)}" end |
#layout ⇒ Object
44 45 46 |
# File 'lib/tzispa/http/context.rb', line 44 def layout router_params&.fetch(:layout, nil) end |
#layout_canonical_url(layout, params = {}) ⇒ Object
97 98 99 |
# File 'lib/tzispa/http/context.rb', line 97 def layout_canonical_url(layout, params = {}) "#{canonical_root}#{layout_path(layout, params)}" end |
#layout_path(layout, params = {}) ⇒ Object
85 86 87 88 89 |
# File 'lib/tzispa/http/context.rb', line 85 def layout_path(layout, params = {}) is_default = app.default_layout? layout params = normalize_format(params.merge(layout: layout)) unless is_default app.routes.path layout, params end |
#login_redirect ⇒ Object
48 49 50 |
# File 'lib/tzispa/http/context.rb', line 48 def login_redirect redirect(layout_path(config.login_layout.to_sym), true, response) if login_redirect? end |
#login_redirect? ⇒ Boolean
52 53 54 |
# File 'lib/tzispa/http/context.rb', line 52 def login_redirect? !logged? && (layout != config.login_layout) end |
#path(path_id, params = {}) ⇒ Object
60 61 62 |
# File 'lib/tzispa/http/context.rb', line 60 def path(path_id, params = {}) app.routes.path path_id, params end |
#path_sign?(sign, *args) ⇒ Boolean
127 128 129 |
# File 'lib/tzispa/http/context.rb', line 127 def path_sign?(sign, *args) sign == sign_array(args, config.salt) end |
#request_method ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/tzispa/http/context.rb', line 31 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
40 41 42 |
# File 'lib/tzispa/http/context.rb', line 40 def router_params env['router.params'] || {} end |
#sapi(handler, verb, predicate, sufix, app_name = nil) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/tzispa/http/context.rb', line 115 def sapi(handler, verb, predicate, sufix, app_name = nil) if app_name 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 else sign = sign_array [handler, verb, predicate], app.config.salt canonical_url :sapi, sign: sign, handler: handler, verb: verb, predicate: predicate, sufix: sufix end end |
#unauthorized_but_logged ⇒ Object
56 57 58 |
# File 'lib/tzispa/http/context.rb', line 56 def unless logged? end |