Class: Talker

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/talker.rb,
lib/talker/cli.rb

Defined Under Namespace

Classes: CLI, 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

#initializeTalker

Returns a new instance of Talker.



28
29
30
# File 'lib/talker.rb', line 28

def initialize
  @users = {}
end

Instance Attribute Details

#connect_optionsObject

Returns the value of attribute connect_options.



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

def connect_options
  @connect_options
end

#current_userObject

Returns the value of attribute current_user.



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

def current_user
  @current_user
end

#threadObject

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

Instance Method Details

#closeObject



67
68
69
# File 'lib/talker.rb', line 67

def close
  close_connection_after_writing
end

#connection_completedObject

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

#leaveObject



62
63
64
65
# File 'lib/talker.rb', line 62

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

#post_initObject



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 send_message(message, attributes={})
  send({ :type => "message", :content => message }.merge(attributes))
end

#send_private_message(to, message) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/talker.rb', line 53

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



88
89
90
91
# File 'lib/talker.rb', line 88

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

#usersObject



41
42
43
# File 'lib/talker.rb', line 41

def users
  @users.values
end