Class: Tzispa::Controller::Api

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

Direct Known Subclasses

SignedApi

Instance Attribute Summary

Attributes inherited from Base

#application, #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

.generate_handler(domain, name) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tzispa/controller/api.rb', line 91

def generate_handler(domain, name)
  raise "The handler '#{name}' already exist" if File.exist?(handler_class_file)
  File.open(handler_class_file(domain, name), "w") { |f|
    handler_code = String.new
    f.puts handler_code.indenter("require 'tzispa/api/handler'\n\n")
    level = 0
    handler_namespace.split('::').each { |ns|
      f.puts handler_code.indenter("module #{ns}\n", level > 0 ? 2 : 0).to_s
      level += 1
    }
    f.puts handler_code.indenter("\nclass #{handler_class_name} < Tzispa::Api::Handler\n\n", 2)
    f.puts handler_code.indenter("end\n\n")
    handler_namespace.split('::').each { |ns|
      f.puts handler_code.unindenter("end\n", 2)
    }
  }
end

.handler_class(domain, handler_name) ⇒ Object



86
87
88
89
# File 'lib/tzispa/controller/api.rb', line 86

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

.handler_class_file(domain, handler_name) ⇒ Object



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

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

.handler_class_name(handler_name) ⇒ Object



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

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

.handler_namespace(domain) ⇒ Object



82
83
84
# File 'lib/tzispa/controller/api.rb', line 82

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

Instance Method Details

#dispatch!Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/tzispa/controller/api.rb', line 22

def dispatch!
  handler_name, domain_name = context.router_params[:handler].split('.').reverse
  domain = domain_name.nil? ? context.app.domain : Tzispa::Domain.new(name: domain_name)
  verb = context.router_params[:verb]
  predicate = context.router_params[:predicate]
  handler = self.class.handler_class(domain, handler_name).new(context)
  handler.run! verb, predicate
  send(handler.response_verb, handler) if handler.response_verb
  response.finish
end

#download(handler) ⇒ Object



68
69
70
# File 'lib/tzispa/controller/api.rb', line 68

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

#html(handler) ⇒ Object



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

def html(handler)
  content_type :htm
  context.flash << handler.message if config.sessions&.enabled && handler.error?
  response.body << handler.data
  set_api_headers handler.status
end

#json(handler) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/tzispa/controller/api.rb', line 50

def json(handler)
  content_type :json
  data = ::String === handler.data ? JSON.parse(handler.data) : handler.data.to_json
  unless handler.error?
    response.body << data
  else
    response.body << Hash[:__error, true, :__error_msg, handler.message, :__error_code, handler.status].to_json
  end
  set_api_headers handler.status
end

#redirect(handler) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/tzispa/controller/api.rb', line 33

def redirect(handler)
  url = if handler.data && !handler.data.strip.empty?
    handler.data.start_with?('#') ? "#{request.referer}#{handler.data}" : handler.data
  else
    request.referer
  end        
  context.flash << handler.message if config.sessions&.enabled && handler.error?
  context.redirect url, config.absolute_redirects, response
end

#text(handler) ⇒ Object



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

def text(handler)
  content_type :text
  context.flash << handler.message if config.sessions&.enabled && handler.error?
  response.body << handler.data
  set_api_headers handler.status
end