Class: Tzispa::Api::Handler
- Inherits:
-
Object
- Object
- Tzispa::Api::Handler
- Extended by:
- Forwardable
- Includes:
- Helpers::Provider, Helpers::SignRequirer
- Defined in:
- lib/tzispa/api/handler.rb
Constant Summary collapse
- HANDLER_STATUS_UNDEFINED =
nil
- HANDLER_STATUS_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.
-
#response_verb ⇒ Object
readonly
Returns the value of attribute response_verb.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #error? ⇒ Boolean
-
#initialize(context) ⇒ Handler
constructor
A new instance of Handler.
- #message ⇒ Object
- #not_found ⇒ Object
- #result(response_verb:, data: nil, status: HANDLER_STATUS_UNDEFINED) ⇒ Object
- #result_download(data, status: nil) ⇒ Object
- #result_json(data, status: nil) ⇒ Object
- #run!(verb, predicate = nil) ⇒ Object
- #set_status(value) ⇒ Object
Constructor Details
#initialize(context) ⇒ Handler
Returns a new instance of Handler.
36 37 38 |
# File 'lib/tzispa/api/handler.rb', line 36 def initialize(context) @context = context end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
28 29 30 |
# File 'lib/tzispa/api/handler.rb', line 28 def context @context end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
28 29 30 |
# File 'lib/tzispa/api/handler.rb', line 28 def data @data end |
#response_verb ⇒ Object (readonly)
Returns the value of attribute response_verb.
28 29 30 |
# File 'lib/tzispa/api/handler.rb', line 28 def response_verb @response_verb end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
28 29 30 |
# File 'lib/tzispa/api/handler.rb', line 28 def status @status end |
Instance Method Details
#error? ⇒ Boolean
46 47 48 |
# File 'lib/tzispa/api/handler.rb', line 46 def error? status && status != HANDLER_STATUS_OK end |
#message ⇒ Object
62 63 64 |
# File 'lib/tzispa/api/handler.rb', line 62 def I18n.t("#{self.class.name.dottize}.#{status}", default: "#{status}") if status end |
#not_found ⇒ Object
58 59 60 |
# File 'lib/tzispa/api/handler.rb', line 58 def not_found result response_verb: :not_found end |
#result(response_verb:, data: nil, status: HANDLER_STATUS_UNDEFINED) ⇒ Object
40 41 42 43 44 |
# File 'lib/tzispa/api/handler.rb', line 40 def result(response_verb:, data: nil, status: HANDLER_STATUS_UNDEFINED) @response_verb = response_verb @status = status if status @data = data end |
#result_download(data, status: nil) ⇒ Object
54 55 56 |
# File 'lib/tzispa/api/handler.rb', line 54 def result_download(data, status: nil) result response_verb: :download, data: data, status: status end |
#result_json(data, status: nil) ⇒ Object
50 51 52 |
# File 'lib/tzispa/api/handler.rb', line 50 def result_json(data, status: nil) result response_verb: :json, data: data, status: status end |
#run!(verb, predicate = nil) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/tzispa/api/handler.rb', line 66 def run!(verb, predicate=nil) raise UnknownHandlerVerb.new(verb, self.class.name) unless provides? verb raise InvalidSign.new if sign_required? && !sign_valid? # process compound predicates args = predicate ? predicate.split(',') : nil send verb, *args end |
#set_status(value) ⇒ Object
74 75 76 |
# File 'lib/tzispa/api/handler.rb', line 74 def set_status(value) @status = value end |