Module: Waxx::App

Extended by:
App
Included in:
App
Defined in:
waxx/app.rb

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

Root =
Waxx::Root + '/app'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#runsObject (readonly)

`@runs` holds a hash of the entire app (routes) with the methods for each



17
18
19
# File '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 '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 '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

Returns:

  • (Boolean)


122
123
124
125
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
# File 'waxx/app.rb', line 122

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, 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/*



114
115
116
117
# File 'waxx/app.rb', line 114

def alert(x, status:200, type:"request", title:"Alert", message:"", args: [])
  x.res.status = status
  App[:app_error][type.to_sym][:get][x, title, message, *args]
end

#csrf_failure(x) ⇒ Object



53
54
55
# File '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



176
177
178
# File 'waxx/app.rb', line 176

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
# File 'waxx/app.rb', line 101

def error(x, status:200, type:"request", title:"An error occurred", message:"", args: [])
  x.res.status = status
  if App[:app_error][type.to_sym]
    App[:app_error][type.to_sym][:get][x, title, message, *args]
  else
    x << "ERROR: #{title} - #{message}"
  end
end

#initObject



19
20
21
22
# File 'waxx/app.rb', line 19

def init
  @runs = {}
  Waxx::Server.require_apps
end

#log(x, cat, name, value = nil, id = nil) ⇒ Object



154
155
156
# File 'waxx/app.rb', line 154

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



163
164
165
166
167
168
169
170
171
172
173
174
# File 'waxx/app.rb', line 163

def (x)
  if x.ext == "json"
    x.res.status = 400
    x << {ok: false, msg: 'Login needed: Session did not pass ACL'}
  else
    App::Html.render(x,
      title: "Please Login",
      #message: {type:"info", message: "Please login"},
      content: App::Usr::Html.(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 'waxx/app.rb', line 28

def not_found(x, title:"Not Found", message:nil)
  begin
    if message.nil? and not @runs[:website].nil?
      return @runs[:website][:page][:get].call(x, *(x.args))
    end
  rescue => e
    message = e.to_s
  end
  error(x, status: 404, type: "request", title: title, message: message)
end

#random_password(size = 10) ⇒ Object

Return a random string with no confusing chars (0Oo1iIl etc)



159
160
161
# File 'waxx/app.rb', line 159

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”.

  1. x

  2. app: The name of the app (Symbol)

  3. act: The act to run (String or Symbol - type must match definition)

4, meth: The request method (Symbol)

  1. 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 '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