Class: Engine2::Handler

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/engine2/handler.rb

Instance Method Summary collapse

Instance Method Details

#halt_forbidden(cause = '', message = LOCS[:access_forbidden]) ⇒ Object



13
14
15
# 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) ⇒ Object



9
10
11
# 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]) ⇒ Object



25
26
27
# 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]) ⇒ Object



21
22
23
# 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) ⇒ Object



29
30
31
# 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]) ⇒ Object



17
18
19
# File 'lib/engine2/handler.rb', line 17

def halt_unauthorized cause = '', message = LOCS[:access_unauthorized]
    halt_json 401, cause, message
end

#initial?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/engine2/handler.rb', line 37

def initial?
    params[:initial]
end

#logged_in?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/engine2/handler.rb', line 41

def logged_in?
    !user.nil?
end

#no_cacheObject



49
50
51
52
53
54
55
56
# 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) ⇒ Object



62
63
64
65
# 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) ⇒ Object



33
34
35
# 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_jsonObject



58
59
60
# 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) ⇒ Object



106
107
108
# 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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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

#userObject



45
46
47
# File 'lib/engine2/handler.rb', line 45

def user
    session[:user]
end