Class: Ava::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/controller/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_connectionsObject (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

#connectionsObject (readonly)

Returns the value of attribute connections.



4
5
6
# File 'lib/controller/controller.rb', line 4

def connections
  @connections
end

#encryptObject

Returns the value of attribute encrypt.



4
5
6
# File 'lib/controller/controller.rb', line 4

def encrypt
  @encrypt
end

#keyObject

Returns the value of attribute key.



4
5
6
# File 'lib/controller/controller.rb', line 4

def key
  @key
end

#objectsObject (readonly)

Returns the value of attribute objects.



4
5
6
# File 'lib/controller/controller.rb', line 4

def objects
  @objects
end

#portObject

Returns the value of attribute port.



4
5
6
# File 'lib/controller/controller.rb', line 4

def port
  @port
end

#threadObject (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.

Raises:

  • (ArgumentError)


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_objectsObject



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_gemsObject



98
99
100
# File 'lib/controller/controller.rb', line 98

def required_gems
  Gem.loaded_specs.keys
end

#restartObject



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

Returns:

  • (Boolean)


44
45
46
# File 'lib/controller/controller.rb', line 44

def running?
  defined?(@thread) && @thread.alive?
end

#startObject



30
31
32
33
# File 'lib/controller/controller.rb', line 30

def start
  listen unless defined?(@thread) && @thread.alive?
  running?
end

#stopObject



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