Class: Jabot::Base
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#config ⇒ Object
Returns the value of attribute config.
-
#jabber ⇒ Object
readonly
Returns the value of attribute jabber.
Instance Method Summary collapse
- #configure(&block) ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
-
#standard_commands ⇒ Object
add standard commands.
- #start_listen ⇒ Object
Methods included from DSL
#clients, #command, #password, #standalone_mode, #username
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
15 16 17 18 19 20 21 22 |
# File 'lib/jabot.rb', line 15 def initialize @config = { username: '', password: '', clients: [] } @standalone_mode = false end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
12 13 14 |
# File 'lib/jabot.rb', line 12 def commands @commands end |
#config ⇒ Object
Returns the value of attribute config.
13 14 15 |
# File 'lib/jabot.rb', line 13 def config @config end |
#jabber ⇒ Object (readonly)
Returns the value of attribute jabber.
12 13 14 |
# File 'lib/jabot.rb', line 12 def jabber @jabber end |
Instance Method Details
#configure(&block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jabot.rb', line 24 def configure(&block) @commands = Commands.new standard_commands instance_eval(&block) @jabber = Jabber.new client_id: @config[:username], client_password: @config[:password] @config[:clients].each do |item| @jabber.add_remote_client item end end |
#standard_commands ⇒ Object
add standard commands
54 55 56 57 58 59 60 61 |
# File 'lib/jabot.rb', line 54 def standard_commands command :stop do @jabber.disconnect end command :help do @commands.available_commands.map { |item| "#{item[:name]} #{item[:args]}" }.join("\n") end end |
#start_listen ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jabot.rb', line 35 def start_listen @jabber.listen do |, sender_id| begin result = @commands.run() @jabber.send sender_id, result if result.is_a?(String) && !result.empty? rescue Commands::CommandNotExists => e @jabber.send sender_id, e. end end if @standalone_mode puts 'Start listening...' loop do break unless @jabber.is_listen end end end |