Class: Talker::Client
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- Talker::Client
- Defined in:
- lib/talker/client.rb
Defined Under Namespace
Classes: Error
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 ⇒ Client
constructor
A new instance of Client.
- #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 ⇒ Client
Returns a new instance of Client.
31 32 33 |
# File 'lib/talker/client.rb', line 31 def initialize @users = {} end |
Instance Attribute Details
#connect_options ⇒ Object
Returns the value of attribute connect_options.
10 11 12 |
# File 'lib/talker/client.rb', line 10 def @connect_options end |
#current_user ⇒ Object
Returns the value of attribute current_user.
10 11 12 |
# File 'lib/talker/client.rb', line 10 def current_user @current_user end |
#thread ⇒ Object
Returns the value of attribute thread.
10 11 12 |
# File 'lib/talker/client.rb', line 10 def thread @thread end |
Class Method Details
.connect(options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/talker/client.rb', line 12 def self.connect(={}) host = .delete(:host) || "talkerapp.com" port = (.delete(:port) || 8500).to_i ssl = [:ssl] != false thread = Thread.new { EM.run } unless EM.reactor_running? connection = EM.connect host, port, self do |c| c.start_tls if ssl c.thread = thread c. = yield c if block_given? end thread.join if thread connection end |
Instance Method Details
#close ⇒ Object
70 71 72 |
# File 'lib/talker/client.rb', line 70 def close close_connection_after_writing end |
#connection_completed ⇒ Object
EventMachine callbacks
77 78 79 80 |
# File 'lib/talker/client.rb', line 77 def connection_completed send @connect_options.merge(:type => "connect") EM.add_periodic_timer(20) { send :type => "ping" } end |
#find_user!(user_name) ⇒ Object
52 53 54 |
# File 'lib/talker/client.rb', line 52 def find_user!(user_name) @users.values.detect { |user| user["name"] == user_name } || raise(Error, "User #{user_name} not found") end |
#leave ⇒ Object
65 66 67 68 |
# File 'lib/talker/client.rb', line 65 def leave send :type => "close" close end |
#post_init ⇒ Object
82 83 84 85 |
# File 'lib/talker/client.rb', line 82 def post_init @parser = Yajl::Parser.new @parser.on_parse_complete = method(:event_parsed) end |
#receive_data(data) ⇒ Object
87 88 89 |
# File 'lib/talker/client.rb', line 87 def receive_data(data) @parser << data end |
#send_message(message, attributes = {}) ⇒ Object
48 49 50 |
# File 'lib/talker/client.rb', line 48 def (, attributes={}) send({ :type => "message", :content => }.merge(attributes)) end |
#send_private_message(to, message) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/talker/client.rb', line 56 def (to, ) if to.is_a?(String) user_id = find_user!(to)["id"] else user_id = to end , :to => user_id end |
#unbind ⇒ Object
91 92 93 94 |
# File 'lib/talker/client.rb', line 91 def unbind trigger :close @thread.kill if @thread end |
#users ⇒ Object
44 45 46 |
# File 'lib/talker/client.rb', line 44 def users @users.values end |