Class: Ahoy::Broadcast

Inherits:
Object
  • Object
show all
Defined in:
lib/ahoy/broadcast.rb

Overview

Ahoy::Broadcast provides a simple interface to send a message to all online users. Example:

user = Ahoy::User.new("Dr. Nick")
cast = Ahoy::Broadcast.new(user)

cast.send("Hi, everybody!")
cast.close

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ Broadcast

:call-seq: Broadcast.new(user) -> broadcast

Create a new Ahoy::Broadcast.



19
20
21
22
23
# File 'lib/ahoy/broadcast.rb', line 19

def initialize(user)
  user.
  sleep 1
  @chats = user.contacts.map {|cont| user.chat(cont) rescue nil}.compact
end

Instance Method Details

#closeObject

:call-seq: broadcast.close -> close

End all chats.



43
44
45
# File 'lib/ahoy/broadcast.rb', line 43

def close
  @chats.each {|chat| chat.close}
end

#send(message) ⇒ Object

:call-seq: broadcast.send(string) -> array

Send string to all online contacts.



29
30
31
32
33
34
35
36
37
# File 'lib/ahoy/broadcast.rb', line 29

def send(message)
  @chats.each do |chat|
    begin
      chat.send(message)
    rescue
      @chats.delete(chat)
    end
  end
end