Class: Talker
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- Talker
- Defined in:
- lib/talker.rb,
lib/talker/cli.rb
Defined Under Namespace
Constant Summary collapse
- CALLBACKS =
%w( connected message private_message join idle back leave presence error close event )
Instance Attribute Summary collapse
-
#connect_options ⇒ Object
Returns the value of attribute connect_options.
-
#current_user ⇒ Object
Returns the value of attribute current_user.
-
#thread ⇒ Object
Returns the value of attribute thread.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#connection_completed ⇒ Object
EventMachine callbacks.
- #find_user!(user_name) ⇒ Object
-
#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.
28 29 30 |
# File 'lib/talker.rb', line 28 def initialize @users = {} end |
Instance Attribute Details
#connect_options ⇒ Object
Returns the value of attribute connect_options.
9 10 11 |
# File 'lib/talker.rb', line 9 def @connect_options end |
#current_user ⇒ Object
Returns the value of attribute current_user.
9 10 11 |
# File 'lib/talker.rb', line 9 def current_user @current_user end |
#thread ⇒ Object
Returns the value of attribute thread.
9 10 11 |
# File 'lib/talker.rb', line 9 def thread @thread end |
Class Method Details
.connect(options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/talker.rb', line 11 def self.connect(={}) host = .delete(:host) || "talkerapp.com" port = (.delete(:port) || 8500).to_i thread = Thread.new { EM.run } unless EM.reactor_running? connection = EM.connect host, port, self do |c| c.thread = thread c. = yield c if block_given? end thread.join if thread connection end |
Instance Method Details
#close ⇒ Object
67 68 69 |
# File 'lib/talker.rb', line 67 def close close_connection_after_writing end |
#connection_completed ⇒ Object
EventMachine callbacks
74 75 76 77 |
# File 'lib/talker.rb', line 74 def connection_completed send @connect_options.merge(:type => "connect") EM.add_periodic_timer(20) { send :type => "ping" } end |
#find_user!(user_name) ⇒ Object
49 50 51 |
# File 'lib/talker.rb', line 49 def find_user!(user_name) @users.values.detect { |user| user["name"] == user_name } || raise(Error, "User #{user_name} not found") end |
#leave ⇒ Object
62 63 64 65 |
# File 'lib/talker.rb', line 62 def leave send :type => "close" close end |
#post_init ⇒ Object
79 80 81 82 |
# File 'lib/talker.rb', line 79 def post_init @parser = Yajl::Parser.new @parser.on_parse_complete = method(:event_parsed) end |
#receive_data(data) ⇒ Object
84 85 86 |
# File 'lib/talker.rb', line 84 def receive_data(data) @parser << data end |
#send_message(message, attributes = {}) ⇒ Object
45 46 47 |
# File 'lib/talker.rb', line 45 def (, attributes={}) send({ :type => "message", :content => }.merge(attributes)) end |
#send_private_message(to, message) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/talker.rb', line 53 def (to, ) if to.is_a?(String) user_id = find_user!(to)["id"] else user_id = to end , :to => user_id end |
#unbind ⇒ Object
88 89 90 91 |
# File 'lib/talker.rb', line 88 def unbind trigger :close @thread.kill if @thread end |
#users ⇒ Object
41 42 43 |
# File 'lib/talker.rb', line 41 def users @users.values end |