Class: Engine2::Handler
- Defined in:
- lib/engine2/handler.rb
Instance Method Summary collapse
- #halt_forbidden(cause = '', message = LOCS[:access_forbidden]) ⇒ Object
- #halt_json(code, cause, message) ⇒ Object
- #halt_method_not_allowed(cause = '', message = LOCS[:access_method_not_allowed]) ⇒ Object
- #halt_not_found(cause = '', message = LOCS[:access_not_found]) ⇒ Object
- #halt_server_error(cause, message) ⇒ Object
- #halt_unauthorized(cause = '', message = LOCS[:access_unauthorized]) ⇒ Object
- #initial? ⇒ Boolean
- #logged_in? ⇒ Boolean
- #no_cache ⇒ Object
- #param_to_json(name) ⇒ Object
- #permit(access) ⇒ Object
- #post_to_json ⇒ Object
- #serve_api_error(error) ⇒ Object
- #serve_api_resource(verb, path) ⇒ Object
- #user ⇒ Object
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 = '', = LOCS[:access_forbidden] halt_json 403, cause, end |
#halt_json(code, cause, message) ⇒ Object
9 10 11 |
# File 'lib/engine2/handler.rb', line 9 def halt_json code, cause, halt code, {'Content-Type' => 'application/json'}, {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 = '', = LOCS[:access_method_not_allowed] halt_json 405, cause, 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 = '', = LOCS[:access_not_found] halt_json 404, cause, end |
#halt_server_error(cause, message) ⇒ Object
29 30 31 |
# File 'lib/engine2/handler.rb', line 29 def halt_server_error cause, halt_json 500, cause, end |
#halt_unauthorized(cause = '', message = LOCS[:access_unauthorized]) ⇒ Object
17 18 19 |
# File 'lib/engine2/handler.rb', line 17 def cause = '', = LOCS[:access_unauthorized] halt_json 401, cause, end |
#initial? ⇒ Boolean
37 38 39 |
# File 'lib/engine2/handler.rb', line 37 def initial? params[:initial] end |
#logged_in? ⇒ Boolean
41 42 43 |
# File 'lib/engine2/handler.rb', line 41 def logged_in? !user.nil? end |
#no_cache ⇒ Object
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_json ⇒ Object
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 ? = path.pop if path.last == 'meta' node = ROOT path.each do |pat| node = node[pat.to_sym] halt_not_found unless node unless node.check_access!(self) end action = node.* response = if params[:access] ? node.access_info(self) : {meta: action., actions: node.nodes_info(self)} else if action.http_method == verb && action.invokable begin action.invoke!(self) rescue => error 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 ⇒ Object
45 46 47 |
# File 'lib/engine2/handler.rb', line 45 def user session[:user] end |