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



12
13
14
# File 'lib/engine2/handler.rb', line 12

def halt_forbidden cause = '', message = LOCS[:access_forbidden]
    halt_json 403, cause, message
end

#halt_json(code, cause, message) ⇒ Object



8
9
10
# File 'lib/engine2/handler.rb', line 8

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



24
25
26
# File 'lib/engine2/handler.rb', line 24

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



20
21
22
# File 'lib/engine2/handler.rb', line 20

def halt_not_found cause = '', message = LOCS[:access_not_found]
    halt_json 404, cause, message
end

#halt_server_error(cause, message) ⇒ Object



28
29
30
# File 'lib/engine2/handler.rb', line 28

def halt_server_error cause, message
    halt_json 500, cause, message
end

#halt_unauthorized(cause = '', message = LOCS[:access_unauthorized]) ⇒ Object



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

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

#initial?Boolean



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

def initial?
    params[:initial]
end

#logged_in?Boolean



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

def logged_in?
    !user.nil?
end

#no_cacheObject



48
49
50
51
52
53
54
55
# File 'lib/engine2/handler.rb', line 48

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



61
62
63
64
# File 'lib/engine2/handler.rb', line 61

def param_to_json name
    permit param = params[name]
    JSON.parse(param, symbolize_names: true) # rescue halt_server_error
end

#permit(access) ⇒ Object



32
33
34
# File 'lib/engine2/handler.rb', line 32

def permit access
    settings.development? ? raise(E2Error.new("Permission denied")) : halt_forbidden('Permission denied') unless access
end

#post_to_jsonObject



57
58
59
# File 'lib/engine2/handler.rb', line 57

def post_to_json
    JSON.parse(request.body.read, symbolize_names: true) # rescue halt_server_error
end

#serve_api_error(error) ⇒ Object



105
106
107
# File 'lib/engine2/handler.rb', line 105

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



66
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
# File 'lib/engine2/handler.rb', line 66

def serve_api_resource verb, path
    path = path.split('/') # -1 ?
    is_meta = path.pop if path.last == 'meta'
    action = ROOT
    path.each do |pat|
        action = action[pat.to_sym]
        halt_not_found unless action
        halt_unauthorized unless action.check_access!(self)
    end

    meta = action.*
    response = if is_meta
        params[:access] ? action.access_info(self) : {meta: meta.get, actions: action.actions_info(self)}
    else
        if meta.http_method == verb && meta.invokable
            begin
                meta.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



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

def user
    session[:user]
end