class BubBot::Slack::Command

Public Class Methods

aliases() click to toggle source
# File lib/bub_bot/slack/command.rb, line 10
def self.aliases
  # Guess the command name from the class name
  [self.name.demodulize.downcase]
end
can_handle?(command) click to toggle source
# File lib/bub_bot/slack/command.rb, line 6
def self.can_handle?(command)
  aliases.include?(command)
end
new(options) click to toggle source
# File lib/bub_bot/slack/command.rb, line 15
def initialize(options)
  @options = options
  puts "initialized a command"
end

Public Instance Methods

run() click to toggle source
# File lib/bub_bot/slack/command.rb, line 20
def run
  name = self.class.name
  raise "Your command #{name} needs to implement 'run'"
end

Private Instance Methods

bot_name() click to toggle source
# File lib/bub_bot/slack/command.rb, line 57
def bot_name
  BubBot.configuration.bot_name
end
client() click to toggle source
# File lib/bub_bot/slack/command.rb, line 35
def client
  BubBot::Slack::Client.instance
end
create_token_iterator() click to toggle source

Returns an iterator over the token list that returns nil when out of tokens.

Eg if the tokens are `aaa bbb ccc`:

iterator = create_token_iterator iterator.next # aaa iterator.next # bbb iterator.next # ccc iterator.next # nil

A good way to use this is for parsing order-agnostic commands:

iterator = create_token_iterator while token = iterator.next

if token == 'bake'
  recipe = iterator.next
  raise "bad recipe" unless %w(bread cookies).include?(recipe)
  bake(iterator.next)
elsif token == 'order'
  raise 'missing type' unless food_type = iterator.next
  raise 'missing when' unless when = iterator.next
  order(food_type, when)
end

end

Warning: don't use to_a on this iterator - it'll result in an infinite loop. In retrospect, this was a bad pattern. TODO: refactor this.

# File lib/bub_bot/slack/command.rb, line 88
def create_token_iterator
  unsafe_iterator = tokens.each

  return Enumerator.new do |yielder|
    loop do
      yielder.yield unsafe_iterator.next
    end
    loop do
      yielder.yield nil
    end
  end
end
deployer() click to toggle source
# File lib/bub_bot/slack/command.rb, line 31
def deployer
  @@deployer ||= BubBot::DeployManager.new
end
respond(options) click to toggle source

Takes either a string or some options

# File lib/bub_bot/slack/command.rb, line 53
def respond(options)
  BubBot::Slack::Response.new(options, client)
end
servers() click to toggle source
# File lib/bub_bot/slack/command.rb, line 27
def servers
  @@servers ||= BubBot::ServerManager.new
end
source_user_id() click to toggle source
# File lib/bub_bot/slack/command.rb, line 39
def source_user_id
  @options['user']
end
source_user_name() click to toggle source
# File lib/bub_bot/slack/command.rb, line 43
def source_user_name
  # TODO: cache these, since it's probably the same few people most of the time.
  client.users_info(user: source_user_id)&.dig('user', 'name')
end
tokens() click to toggle source
# File lib/bub_bot/slack/command.rb, line 48
def tokens
  @tokens ||= @options['text'].split(' ')
end