Class: Pepito::Robot
- Inherits:
-
Object
- Object
- Pepito::Robot
- Defined in:
- lib/pepito/robot.rb
Overview
The robot component of the bot.
Instance Attribute Summary collapse
-
#adapters ⇒ Hash
readonly
List of running adapters.
-
#database ⇒ Pepito::Database
readonly
Redis connection.
-
#default_adapters ⇒ Array<String>
readonly
Default adapters that can’t be turned off.
-
#default_handlers ⇒ Array<String>
readonly
Default handlers that can’t be turned off.
-
#handlers ⇒ Hash
readonly
List of running handlers.
-
#http_api ⇒ Pepito::HTTPApi::RackApp
readonly
The running http_api.
-
#http_api_server ⇒ Puma::Server
readonly
The Puma::Server running the HTTP API.
-
#http_api_thread ⇒ Thread
readonly
The thread running the HTTP API.
-
#logger ⇒ Logger
readonly
Logger.
-
#name ⇒ String
readonly
Name of the bot.
-
#web_app ⇒ Pepito::WebApp::App
readonly
The web app.
-
#web_app_server ⇒ Puma::Server
readonly
The Puma::Server running the Web App.
-
#web_app_thread ⇒ Thread
readonly
The thread running the web app.
Instance Method Summary collapse
-
#adapter?(adapter) ⇒ Boolean
Checks if an adapter is running or not.
-
#adapters_list ⇒ Array<String>
Retrieve the list of possible adapters to install from server.
-
#handler?(handler) ⇒ Boolean
Checks if an handler is running or not.
-
#handlers_list ⇒ Array<String>
Retrieve the list of possible handlers to install from server.
- #initialize ⇒ void constructor
-
#receive_message(message) ⇒ void
Helper to receive a message.
-
#run ⇒ void
Runs the robot.
-
#start_adapter(adapter, gem_name, config) ⇒ Integer
Starts an adapter.
-
#start_handler(handler) ⇒ Integer
Starts an handler.
-
#stop_adapter(adapter) ⇒ Integer
Stops an adapter.
-
#stop_handler(handler) ⇒ Integer
Stops an handler.
Constructor Details
#initialize ⇒ void
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pepito/robot.rb', line 73 def initialize @logger = Logger.new(ENV['LOG_FILE'] || STDOUT) @logger.info('Connecting to the database') @database = Database.new @http_api = HTTPApi::RackApp.new @web_app = WebApp::App @default_handlers = %w(Info Help Room) @handlers = {} @default_adapters = [] @adapters = {} @name = @database.get('name') || 'pepito' end |
Instance Attribute Details
#adapters ⇒ Hash (readonly)
List of running adapters.
50 51 52 |
# File 'lib/pepito/robot.rb', line 50 def adapters @adapters end |
#database ⇒ Pepito::Database (readonly)
Redis connection
58 59 60 |
# File 'lib/pepito/robot.rb', line 58 def database @database end |
#default_adapters ⇒ Array<String> (readonly)
Default adapters that can’t be turned off
70 71 72 |
# File 'lib/pepito/robot.rb', line 70 def default_adapters @default_adapters end |
#default_handlers ⇒ Array<String> (readonly)
Default handlers that can’t be turned off
66 67 68 |
# File 'lib/pepito/robot.rb', line 66 def default_handlers @default_handlers end |
#handlers ⇒ Hash (readonly)
List of running handlers.
46 47 48 |
# File 'lib/pepito/robot.rb', line 46 def handlers @handlers end |
#http_api ⇒ Pepito::HTTPApi::RackApp (readonly)
The running http_api.
22 23 24 |
# File 'lib/pepito/robot.rb', line 22 def http_api @http_api end |
#http_api_server ⇒ Puma::Server (readonly)
The Puma::Server running the HTTP API
30 31 32 |
# File 'lib/pepito/robot.rb', line 30 def http_api_server @http_api_server end |
#http_api_thread ⇒ Thread (readonly)
The thread running the HTTP API.
26 27 28 |
# File 'lib/pepito/robot.rb', line 26 def http_api_thread @http_api_thread end |
#logger ⇒ Logger (readonly)
Logger
62 63 64 |
# File 'lib/pepito/robot.rb', line 62 def logger @logger end |
#name ⇒ String (readonly)
Name of the bot.
54 55 56 |
# File 'lib/pepito/robot.rb', line 54 def name @name end |
#web_app ⇒ Pepito::WebApp::App (readonly)
The web app.
34 35 36 |
# File 'lib/pepito/robot.rb', line 34 def web_app @web_app end |
#web_app_server ⇒ Puma::Server (readonly)
The Puma::Server running the Web App
42 43 44 |
# File 'lib/pepito/robot.rb', line 42 def web_app_server @web_app_server end |
#web_app_thread ⇒ Thread (readonly)
The thread running the web app.
38 39 40 |
# File 'lib/pepito/robot.rb', line 38 def web_app_thread @web_app_thread end |
Instance Method Details
#adapter?(adapter) ⇒ Boolean
Checks if an adapter is running or not.
306 307 308 |
# File 'lib/pepito/robot.rb', line 306 def adapter?(adapter) @adapters.key?(adapter) end |
#adapters_list ⇒ Array<String>
Retrieve the list of possible adapters to install from server
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/pepito/robot.rb', line 130 def adapters_list [ { name: 'Slack', gem: 'pepito-slack', configs: [ { name: 'SLACK_API_TOKEN', required: true } ] } ] end |
#handler?(handler) ⇒ Boolean
Checks if an handler is running or not.
206 207 208 |
# File 'lib/pepito/robot.rb', line 206 def handler?(handler) @handlers.key?(handler) end |
#handlers_list ⇒ Array<String>
Retrieve the list of possible handlers to install from server
124 125 126 |
# File 'lib/pepito/robot.rb', line 124 def handlers_list %w(Info Help ExtensionsCatalog Room) end |
#receive_message(message) ⇒ void
This method returns an undefined value.
Helper to receive a message.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/pepito/robot.rb', line 109 def () match = @handlers.dup.map do |_, handler| handler.chat_routes.map do |route| next unless .match_route?(route) strings = handler.public_send(route.func, .source, route.pattern.match(.command || .body)) .source.adapter.(.source, strings) unless strings.nil? true end.any? end.any? .source.adapter.(.source, ['No match for command "' + .command + '"']) if !match && .command end |
#run ⇒ void
This method returns an undefined value.
Runs the robot.
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/pepito/robot.rb', line 94 def run run_http_api run_web_app run_handlers run_adapters begin sleep rescue Interrupt @logger.info('Exiting') end end |
#start_adapter(adapter, gem_name, config) ⇒ Integer
Starts an adapter.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/pepito/robot.rb', line 221 def start_adapter(adapter, gem_name, config) @logger.info('"' + adapter.to_s + '" adapter starting') if @adapters.key?(adapter) @logger.warn('"' + adapter.to_s + '" adapter already running') return 1 else begin require gem_name rescue LoadError begin inst = Gem::DependencyInstaller.new inst.install gem_name require gem_name rescue => e @logger.error(e) return 2 end end begin adp = Pepito::Adapters.const_get(adapter).new(self, config) rescue NameError => e @logger.error('"' + adapter.to_s + '" adapter could not be found') @logger.error(e) return 2 rescue Pepito::Errors::RequiredMethodsError => e @logger.error('"' + adapter.to_s + '" adapter failed to start. ' + e. + ': ' + e.missing_methods.to_s) return 3 rescue Pepito::Errors::MissingConfigurationValueError => e @logger.error('"' + adapter.to_s + '" adapter failed to start. ' + e. + ': ' + e.configuration_values.to_s) return 4 rescue => e @logger.error('"' + adapter.to_s + '" adapter failed to start. Error: ' + e..to_s) @logger.error(e) return 5 end # If it didnt fail, spawn adapter thread @adapters[adapter] = adp @database.sadd('adapters', adapter) # Clear old config in database @database.del(adapter) # Set gem name in database @database.set(adapter + ':gem', gem_name) # Setup new config in database config.each do |key, value| @database.hset(adapter + ':config', key, value) end Thread.new do adp.run end @logger.info('"' + adapter.to_s + '" adapter started successfully') return 0 end end |
#start_handler(handler) ⇒ Integer
Starts an handler.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/pepito/robot.rb', line 152 def start_handler(handler) @logger.info('"' + handler.to_s + '" handler starting') if @handlers.key?(handler) @logger.warn('"' + handler.to_s + '" handler is already running') return 1 else begin @handlers[handler] = Handlers.const_get(handler).new(self) rescue NameError @logger.error('"' + handler.to_s + '" handler does not exist') return 2 rescue => e @logger.error('"' + handler.to_s + '" handler failed to start. Error: ' + e..to_s) @logger.error(e) return 3 end @http_api.router.uncompile @handlers[handler].run @handlers[handler].start @database.sadd('handlers', handler) @logger.info('"' + handler.to_s + '" handler started successfully') return 0 end end |
#stop_adapter(adapter) ⇒ Integer
Stops an adapter.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/pepito/robot.rb', line 285 def stop_adapter(adapter) if @default_adapters.include?(adapter) @logger.error('Can\'t stop "' + adapter.to_s + '" adapter because it is a default adapter') return 1 elsif !@adapters.key?(adapter) @logger.warn('"' + adapter.to_s + '" adapter is not running') return 2 else @adapters[adapter].stop @adapters.delete(adapter) @database.del(adapter + ':config') @database.del(adapter + ':gem') @database.srem('adapters', adapter) @logger.info('"' + adapter.to_s + '" adapter stopped successfully') return 0 end end |
#stop_handler(handler) ⇒ Integer
Stops an handler.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/pepito/robot.rb', line 183 def stop_handler(handler) @logger.info('Stopping "' + handler.to_s + '" handler') if @default_handlers.include?(handler) @logger.error('Can\'t stop "' + handler.to_s + '" because it is a default handler') return 1 elsif !@handlers.key?(handler) @logger.warn('"' + handler.to_s + '" handler is not running') return 2 else @handlers.delete(handler) @database.srem('handlers', handler) @http_api.router.reset! @handlers.each do |_, h| h.start end @logger.info('"' + handler.to_s + '" handler stopped successfully') return 0 end end |