Class: Tzispa::Controller::Api

Inherits:
Base
  • Object
show all
Includes:
Helpers::Response
Defined in:
lib/tzispa/controller/api.rb

Instance Attribute Summary

Attributes inherited from Base

#application, #callmethod, #context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#call, #initialize

Constructor Details

This class inherits a constructor from Tzispa::Controller::Base

Class Method Details

.handler_class(request_method, domain, handler_name) ⇒ Object



107
108
109
110
111
# File 'lib/tzispa/controller/api.rb', line 107

def handler_class(request_method, domain, handler_name)
  domain.require "api/#{request_method}/#{handler_name}"
  "#{handler_namespace domain, request_method}::#{handler_class_name handler_name}"
    .constantize
end

.handler_class_file(domain, handler_name, request_method) ⇒ Object



99
100
101
# File 'lib/tzispa/controller/api.rb', line 99

def handler_class_file(domain, handler_name, request_method)
  "#{domain.path}/api/#{request_method}/#{handler_name}.rb"
end

.handler_class_name(handler_name) ⇒ Object



95
96
97
# File 'lib/tzispa/controller/api.rb', line 95

def handler_class_name(handler_name)
  "#{handler_name.camelize}Handler"
end

.handler_namespace(domain, request_method) ⇒ Object



103
104
105
# File 'lib/tzispa/controller/api.rb', line 103

def handler_namespace(domain, request_method)
  "#{domain.name.to_s.camelize}::Api::#{request_method.capitalize}"
end

Instance Method Details

#api_flash(message) ⇒ Object



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

def api_flash(message)
  context.flash << message if config.sessions&.enabled
end

#api_response(type, content, status = nil, error = nil) ⇒ Object



83
84
85
86
87
88
# File 'lib/tzispa/controller/api.rb', line 83

def api_response(type, content, status = nil, error = nil)
  content_type type
  response.body << content
  response.status = status if status
  api_headers error
end

#dispatch!Object



20
21
22
23
24
25
26
27
# File 'lib/tzispa/controller/api.rb', line 20

def dispatch!
  verb = context.router_params[:verb]
  predicate = context.router_params[:predicate]
  handler = prepare_handler
  handler.run! verb, predicate
  send(handler.type, handler) if handler.type
  response.finish
end

#domainObject



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

def domain
  _, domain_name = context.router_params[:handler].split('.').reverse
  domain_name ? Tzispa::Domain.new(name: domain_name) : context.app.domain
end

#download(handler) ⇒ Object



75
76
77
# File 'lib/tzispa/controller/api.rb', line 75

def download(handler)
  send_file handler.data[:path], handler.data
end

#handler_nameObject



38
39
40
# File 'lib/tzispa/controller/api.rb', line 38

def handler_name
  context.router_params[:handler].split('.').last
end

#handler_redirect_url(url) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/tzispa/controller/api.rb', line 42

def handler_redirect_url(url)
  if url && !url.strip.empty?
    url.start_with?('#') ? "#{request.referer}#{url}" : url
  else
    request.referer
  end
end

#html(handler) ⇒ Object



55
56
57
58
# File 'lib/tzispa/controller/api.rb', line 55

def html(handler)
  content = handler.error? ? handler.message : handler.data
  api_response :htm, content, handler.status, handler.error
end

#json(handler) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/tzispa/controller/api.rb', line 60

def json(handler)
  content = if handler.error?
              { error_message: handler.message,
                error_code: handler.error }.to_json
            else
              handler.data.is_a?(::String) ? JSON.parse(handler.data) : handler.data.to_json
            end
  api_response :json, content, handler.status, handler.error
end

#prepare_handlerObject



29
30
31
# File 'lib/tzispa/controller/api.rb', line 29

def prepare_handler
  self.class.handler_class(request_method, domain, handler_name).new(context)
end

#redirect(handler) ⇒ Object



50
51
52
53
# File 'lib/tzispa/controller/api.rb', line 50

def redirect(handler)
  api_flash(handler.message) if handler.error?
  context.redirect handler.redirect_url(handler.data), config.absolute_redirects, response
end

#request_methodObject



90
91
92
# File 'lib/tzispa/controller/api.rb', line 90

def request_method
  context.request_method.downcase
end

#text(handler) ⇒ Object



70
71
72
73
# File 'lib/tzispa/controller/api.rb', line 70

def text(handler)
  content = handler.error? ? handler.message : handler.data
  api_response :text, content, handler.status, handler.error
end