Class: Jabot::Base

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/jabot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL

#clients, #command, #password, #standalone_mode, #username

Constructor Details

#initializeBase

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

#commandsObject (readonly)

Returns the value of attribute commands.



12
13
14
# File 'lib/jabot.rb', line 12

def commands
  @commands
end

#configObject

Returns the value of attribute config.



13
14
15
# File 'lib/jabot.rb', line 13

def config
  @config
end

#jabberObject (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_commandsObject

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_listenObject



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 |message, sender_id|
    begin
      result = @commands.run(message)
      @jabber.send sender_id, result if result.is_a?(String) && !result.empty?
    rescue Commands::CommandNotExists => e
      @jabber.send sender_id, e.message
    end
  end

  if @standalone_mode
    puts 'Start listening...'
    loop do
      break unless @jabber.is_listen
    end
  end
end