Class: Talker
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- Talker
- Defined in:
- lib/talker.rb,
lib/talker/cli.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#room ⇒ Object
Returns the value of attribute room.
-
#thread ⇒ Object
Returns the value of attribute thread.
-
#token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#connection_completed ⇒ Object
EventMachine callbacks.
-
#initialize ⇒ Talker
constructor
A new instance of Talker.
- #leave ⇒ Object
- #post_init ⇒ Object
- #receive_data(data) ⇒ Object
- #send_message(message, attributes = {}) ⇒ Object
- #send_private_message(to, message) ⇒ Object
- #unbind ⇒ Object
- #users ⇒ Object
Constructor Details
#initialize ⇒ Talker
Returns a new instance of Talker.
27 28 29 |
# File 'lib/talker.rb', line 27 def initialize @users = {} end |
Instance Attribute Details
#room ⇒ Object
Returns the value of attribute room.
7 8 9 |
# File 'lib/talker.rb', line 7 def room @room end |
#thread ⇒ Object
Returns the value of attribute thread.
7 8 9 |
# File 'lib/talker.rb', line 7 def thread @thread end |
#token ⇒ Object
Returns the value of attribute token.
7 8 9 |
# File 'lib/talker.rb', line 7 def token @token end |
Class Method Details
.connect(options = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/talker.rb', line 9 def self.connect(={}) host = [:host] || "talkerapp.com" port = ([:port] || 8500).to_i room = [:room].to_i token = [:token] thread = Thread.new { EM.run } unless EM.reactor_running? EM.connect host, port, self do |c| c.thread = thread c.room = room c.token = token yield c if block_given? end thread.join unless thread.nil? end |
Instance Method Details
#close ⇒ Object
72 73 74 |
# File 'lib/talker.rb', line 72 def close close_connection_after_writing end |
#connection_completed ⇒ Object
EventMachine callbacks
62 63 64 65 |
# File 'lib/talker.rb', line 62 def connection_completed send :type => "connect", :room => @room, :token => @token EM.add_periodic_timer(20) { send :type => "ping" } end |
#leave ⇒ Object
67 68 69 70 |
# File 'lib/talker.rb', line 67 def leave send :type => "close" close end |
#post_init ⇒ Object
76 77 78 79 |
# File 'lib/talker.rb', line 76 def post_init @parser = Yajl::Parser.new @parser.on_parse_complete = method(:event_parsed) end |
#receive_data(data) ⇒ Object
81 82 83 |
# File 'lib/talker.rb', line 81 def receive_data(data) @parser << data end |
#send_message(message, attributes = {}) ⇒ Object
44 45 46 |
# File 'lib/talker.rb', line 44 def (, attributes={}) send({ :type => "message", :content => }.merge(attributes)) end |
#send_private_message(to, message) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/talker.rb', line 48 def (to, ) if to.is_a?(String) user = @users.values.detect { |user| user["name"] == to } raise Error, "User #{to} not found" unless user user_id = user["id"] else user_id = to end , :to => user_id end |
#unbind ⇒ Object
85 86 87 88 |
# File 'lib/talker.rb', line 85 def unbind trigger :close @thread.kill if @thread end |
#users ⇒ Object
40 41 42 |
# File 'lib/talker.rb', line 40 def users @users.values end |