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
12 13 14 |
# File 'lib/engine2/handler.rb', line 12 def halt_forbidden cause = '', = LOCS[:access_forbidden] halt_json 403, cause, end |
#halt_json(code, cause, message) ⇒ Object
8 9 10 |
# File 'lib/engine2/handler.rb', line 8 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
24 25 26 |
# File 'lib/engine2/handler.rb', line 24 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
20 21 22 |
# File 'lib/engine2/handler.rb', line 20 def halt_not_found cause = '', = LOCS[:access_not_found] halt_json 404, cause, end |
#halt_server_error(cause, message) ⇒ Object
28 29 30 |
# File 'lib/engine2/handler.rb', line 28 def halt_server_error cause, halt_json 500, cause, end |
#halt_unauthorized(cause = '', message = LOCS[:access_unauthorized]) ⇒ Object
16 17 18 |
# File 'lib/engine2/handler.rb', line 16 def cause = '', = LOCS[:access_unauthorized] halt_json 401, cause, 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_cache ⇒ Object
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_json ⇒ Object
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 ? = path.pop if path.last == 'meta' action = ROOT path.each do |pat| action = action[pat.to_sym] halt_not_found unless action unless action.check_access!(self) end = action.* response = if params[:access] ? action.access_info(self) : {meta: .get, actions: action.actions_info(self)} else if .http_method == verb && .invokable begin .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
44 45 46 |
# File 'lib/engine2/handler.rb', line 44 def user session[:user] end |