class Engine2::Handler

Public Instance Methods

find_template(views, name, engine, &block) click to toggle source
Calls superclass method
# File lib/engine2/handler.rb, line 129
def find_template(views, name, engine, &block)
    views.each{|v| super(v, name, engine, &block)}
end
halt_forbidden(cause = '', message = LOCS[:access_forbidden]) click to toggle source
# File lib/engine2/handler.rb, line 13
def halt_forbidden cause = '', message = LOCS[:access_forbidden]
    halt_json 403, cause, message
end
halt_json(code, cause, message) click to toggle source
# File lib/engine2/handler.rb, line 9
def halt_json code, cause, message
    halt code, {'Content-Type' => 'application/json'}, {message: message, cause: cause}.to_json
end
halt_method_not_allowed(cause = '', message = LOCS[:access_method_not_allowed]) click to toggle source
# File lib/engine2/handler.rb, line 25
def halt_method_not_allowed cause = '', message = LOCS[:access_method_not_allowed]
    halt_json 405, cause, message
end
halt_not_found(cause = '', message = LOCS[:access_not_found]) click to toggle source
# File lib/engine2/handler.rb, line 21
def halt_not_found cause = '', message = LOCS[:access_not_found]
    halt_json 404, cause, message
end
halt_server_error(cause, message) click to toggle source
# File lib/engine2/handler.rb, line 29
def halt_server_error cause, message
    halt_json 500, cause, message
end
halt_unauthorized(cause = '', message = LOCS[:access_unauthorized]) click to toggle source
# File lib/engine2/handler.rb, line 17
def halt_unauthorized cause = '', message = LOCS[:access_unauthorized]
    halt_json 401, cause, message
end
initial?() click to toggle source
# File lib/engine2/handler.rb, line 37
def initial?
    params[:initial]
end
logged_in?() click to toggle source
# File lib/engine2/handler.rb, line 41
def logged_in?
    !user.nil?
end
no_cache() click to toggle source
# File lib/engine2/handler.rb, line 49
def no_cache
    # agent = request.user_agent
    # if agent && (agent["MSIE"] || agent["Trident"])
    #     headers["Pragma"] = "no-cache"
    #     headers["Expires"] = "0"
    #     headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    # end
end
param_to_json(name) click to toggle source
# File lib/engine2/handler.rb, line 62
def param_to_json name
    permit param = params[name]
    JSON.parse(param, symbolize_names: true) # rescue halt_server_error
end
permit(access) click to toggle source
# File lib/engine2/handler.rb, line 33
def permit access
    settings.development? ? raise(E2Error.new("Permission denied")) : halt_forbidden('Permission denied') unless access
end
post_to_json() click to toggle source
# File lib/engine2/handler.rb, line 58
def post_to_json
    JSON.parse(request.body.read, symbolize_names: true) # rescue halt_server_error
end
serve_api_error(error) click to toggle source
# File lib/engine2/handler.rb, line 106
def serve_api_error error
    halt_server_error Rack::Utils.escape_html(error.inspect) + "<hr>" + error.backtrace.take(30).map{|b| Rack::Utils.escape_html(b)}.join("<br>"), LOCS[:error]
end
serve_api_resource(verb, path) click to toggle source
# File lib/engine2/handler.rb, line 67
def serve_api_resource verb, path
    path = path.split('/') # -1 ?
    is_meta = path.pop if path.last == 'meta'
    node = ROOT
    path.each do |pat|
        node = node[pat.to_sym]
        halt_not_found unless node
        halt_unauthorized unless node.check_access!(self)
    end

    action = node.*
    response = if is_meta
        params[:access] ? node.access_info(self) : {meta: action.meta, actions: node.nodes_info(self)}
    else
        if action.http_method == verb && action.invokable
            begin
                action.invoke!(self)
            rescue => error
                attachment nil, nil
                # content_type :json
                serve_api_error(error)
            end
        else
            halt_method_not_allowed
        end
    end

    if response.is_a?(Hash)
        content_type :json
        response.to_json
    else
        response
    end
end
user() click to toggle source
# File lib/engine2/handler.rb, line 45
def user
    session[:user]
end