Class: Talker::Client

Inherits:
EM::Connection
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



31
32
33
# File 'lib/talker/client.rb', line 31

def initialize
  @users = {}
end

Instance Attribute Details

#connect_optionsObject

Returns the value of attribute connect_options.



10
11
12
# File 'lib/talker/client.rb', line 10

def connect_options
  @connect_options
end

#current_userObject

Returns the value of attribute current_user.



10
11
12
# File 'lib/talker/client.rb', line 10

def current_user
  @current_user
end

#threadObject

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(options={})
  host = options.delete(:host) || "talkerapp.com"
  port = (options.delete(:port) || 8500).to_i
  ssl = options[: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.connect_options = options
    yield c if block_given?
  end
  
  thread.join if thread
  
  connection
end

Instance Method Details

#closeObject



70
71
72
# File 'lib/talker/client.rb', line 70

def close
  close_connection_after_writing
end

#connection_completedObject

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

#leaveObject



65
66
67
68
# File 'lib/talker/client.rb', line 65

def leave
  send :type => "close"
  close
end

#post_initObject



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 send_message(message, attributes={})
  send({ :type => "message", :content => message }.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 send_private_message(to, message)
  if to.is_a?(String)
    user_id = find_user!(to)["id"]
  else
    user_id = to
  end
  send_message message, :to => user_id
end

#unbindObject



91
92
93
94
# File 'lib/talker/client.rb', line 91

def unbind
  trigger :close
  @thread.kill if @thread
end

#usersObject



44
45
46
# File 'lib/talker/client.rb', line 44

def users
  @users.values
end