Module: Waxx::App
Overview
Defines the applications handlers and runs the handlers
‘@runs` holds a hash of the entire app (routes) with the methods for each
Defined Under Namespace
Classes: ParameterParseError
Constant Summary collapse
Instance Attribute Summary collapse
-
#runs ⇒ Object
readonly
‘@runs` holds a hash of the entire app (routes) with the methods for each.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Get an app runner You don’t normally call this directly (see Waxx::Object.runs).
-
#[]=(name, opts) ⇒ Object
Set an app runner You don’t normally call this directly (see Waxx::Object.runs).
-
#access?(x, acl: nil) ⇒ Boolean
Determine if the client or user has access to the handler method.
-
#alert(x, status: 200, type: "request", title: "Alert", message: "", args: []) ⇒ Object
Return an alert/error to the client Format is dependant on the request extention x.req.ext.
- #csrf_failure(x) ⇒ Object
- #debug(str, level = 3) ⇒ Object
-
#error(x, status: 200, type: "request", title: "An error occurred", message: "", args: []) ⇒ Object
Return an error to the client Format is dependant on the request extention x.req.ext.
- #init ⇒ Object
- #log(x, cat, name, value = nil, id = nil) ⇒ Object
- #login_needed(x) ⇒ Object
-
#not_found(x, title: "Not Found", message: nil) ⇒ Object
Return website page or an error message with status 404 ‘App.not_found(x, title: “Not Found”, message: “The record you requested was not found.”)` The layout of the error page (html) is defined in app/app/error/html.rb.
-
#random_password(size = 10) ⇒ Object
Return a random string with no confusing chars (0Oo1iIl etc).
-
#run(x, app, act, meth, args = []) ⇒ Object
Run an app.
Instance Attribute Details
#runs ⇒ Object (readonly)
‘@runs` holds a hash of the entire app (routes) with the methods for each
17 18 19 |
# File 'lib/waxx/app.rb', line 17 def runs @runs end |
Instance Method Details
#[](name) ⇒ Object
Get an app runner You don’t normally call this directly (see Waxx::Object.runs)
49 50 51 |
# File 'lib/waxx/app.rb', line 49 def [](name) @runs[name.to_sym] end |
#[]=(name, opts) ⇒ Object
Set an app runner You don’t normally call this directly (see Waxx::Object.runs)
42 43 44 |
# File 'lib/waxx/app.rb', line 42 def []=(name, opts) @runs[name.to_sym] = opts end |
#access?(x, acl: nil) ⇒ Boolean
Determine if the client or user has access to the handler method. See Waxx::Object.runs for details
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/waxx/app.rb', line 126 def access?(x, acl:nil) return true if acl.nil? return true if %w(* all any public).include? acl.to_s return true if acl.to_s == "user" and x.usr? case acl when String, Symbol return (x.usr["grp"] and x.usr["grp"].include? acl.to_s) when Array return (x.usr["grp"] and (x.usr["grp"] & acl).size > 0) when Hash g = nil if acl.keys.include? :any or acl.keys.include? :all g = acl[:any] || acl[:all] elsif acl.keys.include? :read and [:get, :head, :options].include? x.meth g = acl[:read] elsif acl.keys.include? :write and [:put, :post, :delete, :patch].include? x.meth g = acl[:write] else g = acl[x.meth] end return false if g.nil? return true if %w(* all any public).include? g.to_s return access?(x, acl: g) when Proc return acl.call(x) else Waxx.debug "No acl type recognized in App.access? for acl: #{acl.inspect}", 1 false end false end |
#alert(x, status: 200, type: "request", title: "Alert", message: "", args: []) ⇒ Object
Return an alert/error to the client Format is dependant on the request extention x.req.ext. Layouts in app/app/error/*
118 119 120 121 |
# File 'lib/waxx/app.rb', line 118 def alert(x, status:200, type:"request", title:"Alert", message:"", args: []) x.res.status = status App[:app_error][type.to_sym][:get][x, title, , *args] end |
#csrf_failure(x) ⇒ Object
53 54 55 |
# File 'lib/waxx/app.rb', line 53 def csrf_failure(x) error(x, status:400, type:"request", title:"Cross Site Request Forgery Error", message:"The request is missing the correct CSRF token.", args: []) end |
#debug(str, level = 3) ⇒ Object
180 181 182 |
# File 'lib/waxx/app.rb', line 180 def debug(str, level=3) Waxx.debug(str, level) end |
#error(x, status: 200, type: "request", title: "An error occurred", message: "", args: []) ⇒ Object
Return an error to the client Format is dependant on the request extention x.req.ext. Layouts in app/app/error/*
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/waxx/app.rb', line 101 def error(x, status:200, type:"request", title:"An error occurred", message:"", args: []) x.res.status = status begin if App[:app_error][type.to_sym] App[:app_error][type.to_sym][:get][x, title, , *args] else x << "ERROR: #{title} - #{}" end rescue x << "ERROR: #{title} - #{}" end end |
#init ⇒ Object
19 20 21 22 |
# File 'lib/waxx/app.rb', line 19 def init @runs = {} Waxx::Server.require_apps end |
#log(x, cat, name, value = nil, id = nil) ⇒ Object
158 159 160 |
# File 'lib/waxx/app.rb', line 158 def log(x, cat, name, value=nil, id=nil) AppLog.log(x, cat:cat, name:name, value:value, id:id) end |
#login_needed(x) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/waxx/app.rb', line 167 def login_needed(x) if x.ext == "json" x.res.status = 400 x << {ok: false, msg: 'Login needed: Session did not pass ACL'}.to_json else App::Html.render(x, title: "Please Login", #message: {type:"info", message: "Please login"}, content: App::Usr::Html.login(x, return_to: x.req.uri) ) end end |
#not_found(x, title: "Not Found", message: nil) ⇒ Object
Return website page or an error message with status 404 ‘App.not_found(x, title: “Not Found”, message: “The record you requested was not found.”)` The layout of the error page (html) is defined in app/app/error/html.rb
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/waxx/app.rb', line 28 def not_found(x, title:"Not Found", message:nil) begin if .nil? and @runs[:website] and @runs[:website][:page] return @runs[:website][:page][:get].call(x, *(x.args)) end rescue => e = e.to_s end error(x, status: 404, type: "request", title: title, message: ) end |
#random_password(size = 10) ⇒ Object
Return a random string with no confusing chars (0Oo1iIl etc)
163 164 165 |
# File 'lib/waxx/app.rb', line 163 def random_password(size=10) random_string(size, :chars, 'ABCDEFGHJKLMNPQRSTUVWXYabcdefghkmnpqrstuvwxyz23456789#%^&$*i-_+=') end |
#run(x, app, act, meth, args = []) ⇒ Object
Run an app
Can run the request method (get post put patch delete) or the generic “run”.
-
x
-
app: The name of the app (Symbol)
-
act: The act to run (String or Symbol - type must match definition)
4, meth: The request method (Symbol)
-
args: The args to pass to the method (after x) (Array)
Example: ‘App.run(x, :person, :record, :get, [1])` will call the get method with the parameter “1” of the record handler defined in App::Person
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 |
# File 'lib/waxx/app.rb', line 69 def run(x, app, act, meth, args=[]) if @runs[app.to_sym][act][meth.to_sym] begin @runs[app.to_sym][act][meth.to_sym][x, *args] rescue ArgumentError => e if Waxx['debug']['on_screen'] error(x, status: 405, type: "request", title: "Argument Error", message: "#{e.to_s}\n\n#{e.backtrace.join("\n")}") else Waxx.debug e App.not_found(x) end end elsif @runs[app.to_sym][act][:run] begin @runs[app.to_sym][act][:run][x, *args] rescue ArgumentError => e if Waxx['debug']['on_screen'] error(x, status: 405, type: "request", title: "Argument Error", message: "#{e.to_s}\n\n#{e.backtrace.join("\n")}") else Waxx.debug e App.not_found(x) end end else error(x, status: 405, type: "request", title: "Method Not Implemented", message: "The HTTP method requested is not implemented for this interface.") end end |