Class: Ava::Controller
- Inherits:
-
Object
- Object
- Ava::Controller
- Defined in:
- lib/controller/controller.rb
Instance Attribute Summary collapse
-
#allowed_connections ⇒ Object
readonly
Returns the value of attribute allowed_connections.
-
#blacklist(name, *methods) ⇒ Object
readonly
Returns the value of attribute blacklist.
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#encrypt ⇒ Object
Returns the value of attribute encrypt.
-
#key ⇒ Object
Returns the value of attribute key.
-
#objects ⇒ Object
readonly
Returns the value of attribute objects.
-
#port ⇒ Object
Returns the value of attribute port.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
-
#whitelist(name, *methods) ⇒ Object
readonly
Returns the value of attribute whitelist.
Instance Method Summary collapse
- #allow_connections(*connections) ⇒ Object
- #blacklist_all(object) ⇒ Object
- #blacklist_global(*methods) ⇒ Object
-
#initialize(*args, **named) ⇒ Controller
constructor
A new instance of Controller.
- #parse_command(cmd) ⇒ Object
- #register(**objects) ⇒ Object
- #registered_objects ⇒ Object
- #remove(name) ⇒ Object
- #required_gems ⇒ Object
- #restart ⇒ Object
- #run_method(object, method, *args, **named) ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
- #whitelist_method(*methods) ⇒ Object
Constructor Details
#initialize(*args, **named) ⇒ Controller
Returns a new instance of Controller.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/controller/controller.rb', line 7 def initialize *args, **named @objects, @connections = {}, {} @whitelist, @blacklist = {methods:[]}, {methods:[:eval]} @allowed_connections = nil setup_defaults self.key = named.include?(:key) ? named[:key] : SecureRandom.hex(20) self.port = named.include?(:port) ? named[:port] : 2016 self.encrypt = named.include?(:encrypt) ? named[:encrypt] : true listen if named.include?(:start) && named[:start] end |
Instance Attribute Details
#allowed_connections ⇒ Object (readonly)
Returns the value of attribute allowed_connections.
4 5 6 |
# File 'lib/controller/controller.rb', line 4 def allowed_connections @allowed_connections end |
#blacklist(name, *methods) ⇒ Object (readonly)
Returns the value of attribute blacklist.
4 5 6 |
# File 'lib/controller/controller.rb', line 4 def blacklist @blacklist end |
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
4 5 6 |
# File 'lib/controller/controller.rb', line 4 def connections @connections end |
#encrypt ⇒ Object
Returns the value of attribute encrypt.
4 5 6 |
# File 'lib/controller/controller.rb', line 4 def encrypt @encrypt end |
#key ⇒ Object
Returns the value of attribute key.
4 5 6 |
# File 'lib/controller/controller.rb', line 4 def key @key end |
#objects ⇒ Object (readonly)
Returns the value of attribute objects.
4 5 6 |
# File 'lib/controller/controller.rb', line 4 def objects @objects end |
#port ⇒ Object
Returns the value of attribute port.
4 5 6 |
# File 'lib/controller/controller.rb', line 4 def port @port end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
4 5 6 |
# File 'lib/controller/controller.rb', line 4 def thread @thread end |
#whitelist(name, *methods) ⇒ Object (readonly)
Returns the value of attribute whitelist.
4 5 6 |
# File 'lib/controller/controller.rb', line 4 def whitelist @whitelist end |
Instance Method Details
#allow_connections(*connections) ⇒ Object
56 57 58 |
# File 'lib/controller/controller.rb', line 56 def allow_connections *connections @allowed_connections = connections.first.nil? ? nil : connections end |
#blacklist_all(object) ⇒ Object
86 87 88 |
# File 'lib/controller/controller.rb', line 86 def blacklist_all object blacklist object, :_all end |
#blacklist_global(*methods) ⇒ Object
90 91 92 |
# File 'lib/controller/controller.rb', line 90 def blacklist_global *methods @blacklist[:methods]+=methods end |
#parse_command(cmd) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/controller/controller.rb', line 102 def parse_command cmd begin object = cmd.keys.first method = cmd[object].keys.first args = cmd[object][method].delete :args named = cmd[object][method] run_method(object, method, *args, **named) rescue StandardError, Exception => e {status: 500, error: "#{e}\n#{e.backtrace.join("\n")}"} end end |
#register(**objects) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/controller/controller.rb', line 48 def register **objects objects.each do |name, object| name = name.to_sym raise "Cannot name an object :controller, it is reserved." if name == :controller @objects[name] = object end end |
#registered_objects ⇒ Object
94 95 96 |
# File 'lib/controller/controller.rb', line 94 def registered_objects @objects.keys end |
#remove(name) ⇒ Object
60 61 62 |
# File 'lib/controller/controller.rb', line 60 def remove name @objects.delete name unless name == :controller end |
#required_gems ⇒ Object
98 99 100 |
# File 'lib/controller/controller.rb', line 98 def required_gems Gem.loaded_specs.keys end |
#restart ⇒ Object
40 41 42 |
# File 'lib/controller/controller.rb', line 40 def restart stop && start end |
#run_method(object, method, *args, **named) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/controller/controller.rb', line 114 def run_method object, method, *args, **named if @objects.include?(object) return {status: 401, error: "You are not authorized to run '#{method}' on '#{object}'."} unless validate_method(object, method) obj = @objects[object] a = !args.nil? && (!args.is_a?(Array) || !args.empty?) n = !named.nil? && !named.empty? begin if a && n res = obj.send(method, *args, **named) elsif !a && n res = obj.send(method, **named) elsif a && !n res = obj.send(method, *args) else res = obj.send(method) end {status: 200, response: res} rescue StandardError, Exception => e {status: 501, error: "#{e}\n#{e.backtrace.join("\n")}"} end else {status: 404, error: "Object '#{object}' does not exist."} end end |
#running? ⇒ Boolean
44 45 46 |
# File 'lib/controller/controller.rb', line 44 def running? defined?(@thread) && @thread.alive? end |
#start ⇒ Object
30 31 32 33 |
# File 'lib/controller/controller.rb', line 30 def start listen unless defined?(@thread) && @thread.alive? running? end |
#stop ⇒ Object
35 36 37 38 |
# File 'lib/controller/controller.rb', line 35 def stop @thread.kill if defined?(@thread) && @thread.alive? !running? end |
#whitelist_method(*methods) ⇒ Object
74 75 76 |
# File 'lib/controller/controller.rb', line 74 def whitelist_method *methods @whitelist[:methods]+=methods end |