Class: Tzispa::Api::Handler

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

Instance Method Summary collapse

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

#contextObject (readonly)

Returns the value of attribute context.



33
34
35
# File 'lib/tzispa/api/handler.rb', line 33

def context
  @context
end

#dataObject (readonly)

Returns the value of attribute data.



33
34
35
# File 'lib/tzispa/api/handler.rb', line 33

def data
  @data
end

#errorObject

Returns the value of attribute error.



33
34
35
# File 'lib/tzispa/api/handler.rb', line 33

def error
  @error
end

#statusObject (readonly)

Returns the value of attribute status.



33
34
35
# File 'lib/tzispa/api/handler.rb', line 33

def status
  @status
end

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

Returns:

  • (Boolean)


48
49
50
# File 'lib/tzispa/api/handler.rb', line 48

def error?
  @error && @error != HANDLER_OK
end

#error_idObject



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

#messageObject



79
80
81
# File 'lib/tzispa/api/handler.rb', line 79

def message
  I18n.t(error_id, default: error.to_s) if error
end

#not_foundObject



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

Raises:



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