Method: Tinder::Room#listen

Defined in:
lib/vendor/tinder/lib/tinder/room.rb

#listenObject

Listen for new messages in the room, yielding them to the provided block as they arrive. Each message is a hash with:

  • :body: the body of the message

  • :user: Campfire user, which is itself a hash, of:

    • :id: User id

    • :name: User name

    • :email_address: Email address

    • :admin: Boolean admin flag

    • :created_at: User creation timestamp

    • :type: User type (e.g. Member)

  • :id: Campfire message id

  • :type: Campfire message type

  • :room_id: Campfire room id

  • :created_at: Message creation timestamp

    room.listen do |m|

    room.speak "Go away!" if m[:body] =~ /Java/i
    

    end



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 124

def listen
  raise "no block provided" unless block_given?
  
  require 'twitter/json_stream'
  
  join # you have to be in the room to listen
  auth = connection.default_options[:basic_auth]
  options = {
    :host => "streaming.#{Connection::HOST}",
    :path => room_url_for(:live),
    :auth => "#{auth[:username]}:#{auth[:password]}",
    :timeout => 2
  }
  EventMachine::run do
    stream = Twitter::JSONStream.connect(options)
    stream.each_item do |message|
      message = HashWithIndifferentAccess.new(JSON.parse(message))
      message[:user] = user(message.delete(:user_id))
      message[:created_at] = Time.parse(message[:created_at])
      yield(message)
    end
  end
end