Class: Tzispa::Api::Handler

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#contextObject (readonly)

Returns the value of attribute context.



28
29
30
# File 'lib/tzispa/api/handler.rb', line 28

def context
  @context
end

#dataObject (readonly)

Returns the value of attribute data.



28
29
30
# File 'lib/tzispa/api/handler.rb', line 28

def data
  @data
end

#response_verbObject (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

#statusObject (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

Returns:

  • (Boolean)


46
47
48
# File 'lib/tzispa/api/handler.rb', line 46

def error?
  status && status != HANDLER_STATUS_OK
end

#messageObject



62
63
64
# File 'lib/tzispa/api/handler.rb', line 62

def message
  I18n.t("#{self.class.name.dottize}.#{status}", default: "#{status}") if status
end

#not_foundObject



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

Raises:



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