Class: AptControl::Bot

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
Actors::Proxied, ArgHelpers
Defined in:
lib/apt_control/bot.rb

Defined Under Namespace

Modules: ArgHelpers, ClassMethods

Instance Method Summary collapse

Methods included from ClassMethods

handlers, method_added

Methods included from Actors::Proxied

#actor, included

Methods included from ArgHelpers

#split_args

Constructor Details

#initialize(dependencies) ⇒ Bot

Returns a new instance of Bot.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/apt_control/bot.rb', line 34

def initialize(dependencies)
  @jabber         = dependencies.fetch(:jabber)
  @package_states = dependencies.fetch(:package_states)
  @logger         = dependencies.fetch(:logger)
  @command_start  = dependencies.fetch(:command_start)
  @include_cmd    = dependencies.fetch(:include_cmd)
  @control_file   = dependencies.fetch(:control_file)

  @bot_pattern = /#{Regexp.escape(@command_start)}\: ([^ ]+)(?: (.+))?/
  @logger.info("looking for messages starting with #{@command_start}")
  @logger.debug("  match pattern: #{@bot_pattern}")
end

Instance Method Details

#handle_include(args) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/apt_control/bot.rb', line 90

def handle_include(args)
  performed = @include_cmd.run(@package_states) do |state, version|
    send_message("#{state.dist.name} #{state.package_name} #{state.included} => #{version}")
    true
  end

  send_message("no packages were included") if performed.empty?
end

#handle_promote(args) ⇒ Object



108
109
110
# File 'lib/apt_control/bot.rb', line 108

def handle_promote(args)
  promote_command.run(*args)
end

#handle_reload(args) ⇒ Object



99
100
101
102
# File 'lib/apt_control/bot.rb', line 99

def handle_reload(args)
  @control_file.reload!
  send_message("control file reloaded")
end

#handle_set(args) ⇒ Object



104
105
106
# File 'lib/apt_control/bot.rb', line 104

def handle_set(args)
  set_command.run(*args)
end

#handle_status(args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/apt_control/bot.rb', line 75

def handle_status(args)
  dist = args[0]
  package_name = args[1]

  found = @package_states.map do |package_state|
    next if dist && dist != package_state.dist.name
    next if package_name && package_name != package_state.package_name

    send_message(package_state.status_line)
    true
  end.compact

  send_message("no packages found: distribution => #{dist.inspect}, package_name => #{package_name.inspect} ") if found.empty?
end

#on_message(text) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/apt_control/bot.rb', line 47

def on_message(text)
  return unless match = @bot_pattern.match(text)

  command, args = [match[1], match[2]]

  handler = self.class.handlers.include?(command) or
    return print_help("unknown command '#{command}'")

  args = split_args(args || '')
  begin
    self.send("handle_#{command}", args)
  rescue => e
    begin ; send_message("error: #{e}") ; rescue => e ; end
    @logger.error("error handling #{command}")
    @logger.error(e)
  end
end


69
70
71
72
73
# File 'lib/apt_control/bot.rb', line 69

def print_help(message)
  send_message(message)
  send_message("Send commands with '#{@command_start} COMMAND [ARGS...]'")
  send_message("Available commands: #{self.class.handlers.join(' ')}")
end

#promote_commandObject



116
117
118
119
# File 'lib/apt_control/bot.rb', line 116

def promote_command
  AptControl::Commands::Promote.new(control_file: @control_file,
    package_states: @package_states)
end

#send_message(msg) ⇒ Object



65
66
67
# File 'lib/apt_control/bot.rb', line 65

def send_message(msg)
  @jabber.async.send_message(msg)
end

#set_commandObject



112
113
114
# File 'lib/apt_control/bot.rb', line 112

def set_command
  AptControl::Commands::Set.new(control_file: @control_file)
end