Class: Tzispa::Api::Handler
- Inherits:
-
Object
- Object
- Tzispa::Api::Handler
- Extended by:
- Forwardable
- Includes:
- Helpers::Hooks::After, Helpers::Hooks::Before, Helpers::Provider, Helpers::SignRequirer
- Defined in:
- lib/tzispa/api/handler.rb
Constant Summary collapse
- HANDLER_OK =
:ok- HANDLER_MISSING_PARAMETER =
:missing_parameter
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#error ⇒ Object
Returns the value of attribute error.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #error? ⇒ Boolean
- #error_id ⇒ Object
- #error_status(error, status = nil) ⇒ Object
-
#initialize(context) ⇒ Handler
constructor
A new instance of Handler.
- #message ⇒ Object
- #not_found ⇒ Object
- #redirect_url(url) ⇒ Object
- #result(type:, data: nil, error: nil) ⇒ Object
- #result_download(data) ⇒ Object
- #result_json(data) ⇒ Object
- #result_redirect(data) ⇒ Object
- #run!(verb, predicate = nil) ⇒ Object
Constructor Details
#initialize(context) ⇒ Handler
Returns a new instance of Handler.
42 43 44 45 46 |
# File 'lib/tzispa/api/handler.rb', line 42 def initialize(context) @context = context @error = nil @status = nil end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
33 34 35 |
# File 'lib/tzispa/api/handler.rb', line 33 def context @context end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
33 34 35 |
# File 'lib/tzispa/api/handler.rb', line 33 def data @data end |
#error ⇒ Object
Returns the value of attribute error.
33 34 35 |
# File 'lib/tzispa/api/handler.rb', line 33 def error @error end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
33 34 35 |
# File 'lib/tzispa/api/handler.rb', line 33 def status @status end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
33 34 35 |
# File 'lib/tzispa/api/handler.rb', line 33 def type @type end |
Instance Method Details
#error? ⇒ Boolean
48 49 50 |
# File 'lib/tzispa/api/handler.rb', line 48 def error? @error && @error != HANDLER_OK end |
#error_id ⇒ Object
83 84 85 |
# File 'lib/tzispa/api/handler.rb', line 83 def error_id "#{self.class.name.dottize}.#{error}" if error end |
#error_status(error, status = nil) ⇒ Object
52 53 54 55 |
# File 'lib/tzispa/api/handler.rb', line 52 def error_status(error, status = nil) @error = error @status = status end |
#message ⇒ Object
79 80 81 |
# File 'lib/tzispa/api/handler.rb', line 79 def I18n.t(error_id, default: error.to_s) if error end |
#not_found ⇒ Object
75 76 77 |
# File 'lib/tzispa/api/handler.rb', line 75 def not_found result type: :not_found end |
#redirect_url(url) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/tzispa/api/handler.rb', line 97 def redirect_url(url) if url && !url.strip.empty? url.start_with?('#') ? "#{request.referer}#{url}" : url else request.referer end end |
#result(type:, data: nil, error: nil) ⇒ Object
57 58 59 60 61 |
# File 'lib/tzispa/api/handler.rb', line 57 def result(type:, data: nil, error: nil) @type = type @data = data @error = error if error end |
#result_download(data) ⇒ Object
67 68 69 |
# File 'lib/tzispa/api/handler.rb', line 67 def result_download(data) result type: :download, data: data end |
#result_json(data) ⇒ Object
63 64 65 |
# File 'lib/tzispa/api/handler.rb', line 63 def result_json(data) result type: :json, data: data end |
#result_redirect(data) ⇒ Object
71 72 73 |
# File 'lib/tzispa/api/handler.rb', line 71 def result_redirect(data) result type: :redirect, data: data end |
#run!(verb, predicate = nil) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/tzispa/api/handler.rb', line 87 def run!(verb, predicate = nil) raise UnknownHandlerVerb.new(verb, self.class.name) unless provides? verb raise InvalidSign if sign_required? && !sign_valid? do_before # process compound predicates args = predicate ? predicate.split(',') : nil send verb, *args do_after end |