Class: Tzispa::Engine::Rig::Api

Inherits:
Controller::Http show all
Includes:
Helpers::Response
Defined in:
lib/tzispa/engine/rig/api.rb

Instance Attribute Summary

Attributes inherited from Controller::Http

#custom_errors

Attributes inherited from Controller::Base

#application, #callmethod, #context, #context_class

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Controller::Http

#call

Methods inherited from Controller::Base

#call

Constructor Details

#initialize(app) ⇒ Api

Returns a new instance of Api.



22
23
24
# File 'lib/tzispa/engine/rig/api.rb', line 22

def initialize(app)
  super app, :dispatch!, Tzispa::Engine::Rig::Context, false
end

Class Method Details

.handler_class(request_method, domain, handler_name) ⇒ Object



110
111
112
113
114
# File 'lib/tzispa/engine/rig/api.rb', line 110

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



102
103
104
# File 'lib/tzispa/engine/rig/api.rb', line 102

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

.handler_class_name(handler_name) ⇒ Object



98
99
100
# File 'lib/tzispa/engine/rig/api.rb', line 98

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

.handler_namespace(domain, request_method) ⇒ Object



106
107
108
# File 'lib/tzispa/engine/rig/api.rb', line 106

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

Instance Method Details

#api_flash(message) ⇒ Object



83
84
85
# File 'lib/tzispa/engine/rig/api.rb', line 83

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

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



87
88
89
90
91
# File 'lib/tzispa/engine/rig/api.rb', line 87

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

#dispatch!Object



26
27
28
29
30
31
32
# File 'lib/tzispa/engine/rig/api.rb', line 26

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

#domainObject



38
39
40
41
# File 'lib/tzispa/engine/rig/api.rb', line 38

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



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

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

#empty(handler) ⇒ Object



55
56
57
# File 'lib/tzispa/engine/rig/api.rb', line 55

def empty(handler)
  api_response handler.status
end

#handler_nameObject



43
44
45
# File 'lib/tzispa/engine/rig/api.rb', line 43

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

#handler_redirect_url(url) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/tzispa/engine/rig/api.rb', line 47

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



64
65
66
67
# File 'lib/tzispa/engine/rig/api.rb', line 64

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

#json(handler) ⇒ Object



69
70
71
72
# File 'lib/tzispa/engine/rig/api.rb', line 69

def json(handler)
  content = handler.data.is_a?(::String) ? JSON.parse(handler.data) : handler.data.to_json
  api_response handler.status, content, :json
end

#prepare_handlerObject



34
35
36
# File 'lib/tzispa/engine/rig/api.rb', line 34

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

#redirect(handler) ⇒ Object



59
60
61
62
# File 'lib/tzispa/engine/rig/api.rb', line 59

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

#request_methodObject



93
94
95
# File 'lib/tzispa/engine/rig/api.rb', line 93

def request_method
  context.request_method.downcase
end

#text(handler) ⇒ Object



74
75
76
77
# File 'lib/tzispa/engine/rig/api.rb', line 74

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