Class: SlackGame::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_game/controller.rb

Defined Under Namespace

Classes: InputParser

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



14
15
16
17
# File 'lib/slack_game/controller.rb', line 14

def initialize
  init
  listen
end

Class Method Details

.inherited(subclass) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/slack_game/controller.rb', line 4

def self.inherited(subclass)
  def subclass.command(command, pattern)
    parsers << InputParser.new(command, pattern)
  end

  def subclass.parsers
    @@parsers ||= []
  end
end

Instance Method Details

#initObject



19
20
21
22
23
24
25
# File 'lib/slack_game/controller.rb', line 19

def init
  slack = Slack::Client.new(token: ENV['SLACK_TOKEN'])
  @rtm = slack.realtime
  @rtm.on(:message) do |m|
    input(m['text'])
  end
end

#input(command) ⇒ Object



34
35
36
37
# File 'lib/slack_game/controller.rb', line 34

def input(command)
  parser = self.class.parsers.find { |p| p.match?(command) }
  @command = parser.command if parser
end

#listenObject



27
28
29
30
31
32
# File 'lib/slack_game/controller.rb', line 27

def listen
  @thread = Thread.new do
    @rtm.start
  end
  @thread.run
end

#takeObject



39
40
41
42
43
# File 'lib/slack_game/controller.rb', line 39

def take
  command = @command
  @command = nil
  command
end