Class: Daneel::Adapters::Campfire

Inherits:
Daneel::Adapter show all
Defined in:
lib/daneel/adapters/campfire.rb

Instance Attribute Summary

Attributes inherited from Plugin

#robot

Instance Method Summary collapse

Methods inherited from Daneel::Adapter

named

Methods inherited from Plugin

#logger, requires_env

Constructor Details

#initialize(robot) ⇒ Campfire

Returns a new instance of Campfire.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/daneel/adapters/campfire.rb', line 9

def initialize(robot)
  super
  domain = ENV['CAMPFIRE_SUBDOMAIN']
  token  = ENV['CAMPFIRE_API_TOKEN']
  @fire  = Sparks.new(domain, token, :logger => logger)

  ENV['CAMPFIRE_ROOM_IDS'].split(",").map(&:to_i).map do |id|
    # Get info about the room state
    room = Room.new(id, self, @fire.room(id))
    robot.data.rooms[id] = room
    # Save the user info for all the users in the room
    room.data["users"].each do |data|
      user = User.new(data["id"], data["name"], data)
      robot.data.users[user.id] = user
    end
  end
end

Instance Method Details

#announce(*texts) ⇒ Object



43
44
45
46
47
# File 'lib/daneel/adapters/campfire.rb', line 43

def announce(*texts)
  robot.data.rooms.each do |id, room|
    say id, *texts
  end
end

#leaveObject



49
50
51
52
53
54
# File 'lib/daneel/adapters/campfire.rb', line 49

def leave
  # stop the listening threads
  @threads.each{|t| t.kill }
  # leave each room
  robot.data.rooms.each{|r| @fire.room(r.id).leave }
end

#meObject



56
57
58
59
60
61
62
# File 'lib/daneel/adapters/campfire.rb', line 56

def me
  @me ||= begin
    data = @fire.me
    me = User.new(data["id"], data["name"], data)
    robot.data.users[me.id] = me
  end
end

#runObject



27
28
29
30
31
32
33
34
35
# File 'lib/daneel/adapters/campfire.rb', line 27

def run
  @threads ||= []
  robot.data.rooms.each do |id, room|
    t = Thread.new { watch_room(room) } until t
    t.abort_on_exception = true
    @threads << t
  end
  @threads.each{|t| t.join }
end

#say(id, *texts) ⇒ Object



37
38
39
40
41
# File 'lib/daneel/adapters/campfire.rb', line 37

def say(id, *texts)
  texts.each do |text|
    text =~ /\n/ ? @fire.paste(id, text) : @fire.speak(id, text)
  end
end