Class: CeilingCat::Campfire::Room

Inherits:
Room
  • Object
show all
Defined in:
lib/ceiling_cat/services/campfire/room.rb

Instance Attribute Summary collapse

Attributes inherited from Room

#connection, #room

Instance Method Summary collapse

Methods inherited from Room

#available_commands, #config, #list_of_users_in_room, #plugin, #plugin_descriptions, #plugin_installed?, #plugins, #store

Constructor Details

#initialize(opts = {}) ⇒ Room

Returns a new instance of Room.



6
7
8
9
# File 'lib/ceiling_cat/services/campfire/room.rb', line 6

def initialize(opts={})
  super
  @campfire_room = @connection.campfire.find_room_by_name(opts[:room_name])
end

Instance Attribute Details

#campfire_roomObject

Returns the value of attribute campfire_room.



4
5
6
# File 'lib/ceiling_cat/services/campfire/room.rb', line 4

def campfire_room
  @campfire_room
end

Instance Method Details

#is_me?(user) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/ceiling_cat/services/campfire/room.rb', line 61

def is_me?(user)
  user.id == me.id
end

#meObject



65
66
67
# File 'lib/ceiling_cat/services/campfire/room.rb', line 65

def me
  @me ||= CeilingCat::User.new(@connection.campfire.me[:name], :id => @connection.campfire.me[:id], :role => @connection.campfire.me[:type])
end

#say(something_to_say) ⇒ Object



89
90
91
92
93
# File 'lib/ceiling_cat/services/campfire/room.rb', line 89

def say(something_to_say)
  Array(something_to_say).each do |line|
    @campfire_room.speak(line)
  end
end

#setup_interruptsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ceiling_cat/services/campfire/room.rb', line 95

def setup_interrupts
  trap('INT') do
    puts "Leaving room...."
    @campfire_room.leave if @campfire_room
    exit 1
  end

  trap('USR1') do
    puts "Leaving room"
    @campfire_room.leave if @campfire_room
    # logger.info "Reloading config"
    # config(true)
    # raise ReloadException.new("Rejoin room please, ceiling cat")
  end
end

#users_in_room(opts = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ceiling_cat/services/campfire/room.rb', line 69

def users_in_room(opts={})
  if opts[:reload] || @users_in_room.nil?
    puts "Requesting user list"
    @users_in_room = @campfire_room.users
  end

  @users_in_room.collect{ |user_in_room|
    user = CeilingCat::User.new(user_in_room[:name], :id => user_in_room[:id], :role => user_in_room[:type])
    unless is_me?(user)
      if opts[:type].present?
        if user.role.to_s.downcase == opts[:type].downcase
          user
        end
      else
        user
      end
    end
  }.compact
end

#watchObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ceiling_cat/services/campfire/room.rb', line 11

def watch
  puts "Watching room..."
  setup_interrupts
  begin
    loop do
      begin
        Timeout::timeout(300) do
          @campfire_room.listen do |event|
            begin
              if event[:type] != "TimestampMessage"
                user = CeilingCat::User.new(event[:user][:name], :id => event[:user][:id], :role => event[:user][:type])

                unless is_me?(user) # Otherwise CC will talk to itself
                  event = CeilingCat::Campfire::Event.new(self,event[:body], user, :type => event[:type])
                  event.handle
                  users_in_room(:reload => true) if event.type != :chat # If someone comes or goes, reload the list.
                end
              end
            rescue => e
              raise e
            end
          end
        end
      rescue Timeout::Error
        retry # Reconnect regularly to keep CC talking with Campfire
      end
    end
  rescue Faraday::Error::ParsingError
    puts "Error parsing response. Campfire may be down. Trying again."
    retry
  rescue HTTP::Parser::Error
    puts "Trouble parsing the HTTP response."
    retry
  rescue ReloadException => e
    retry
  rescue NoMethodError => e
    puts "No Method Error"
    e.backtrace.each do |line|
      puts "Backtrace: #{line}"
    end
    retry
  rescue StandardError => e
    puts e.class
    e.backtrace.each do |line|
      puts "Backtrace: #{line}"
    end
    retry
  end
end