Class: Colossus::WriterClient

Inherits:
Object
  • Object
show all
Defined in:
lib/colossus/writer_client.rb

Defined Under Namespace

Classes: FayeExtension

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, writer_token, time_out = 2) ⇒ WriterClient

Returns a new instance of WriterClient.



5
6
7
8
9
# File 'lib/colossus/writer_client.rb', line 5

def initialize(url, writer_token, time_out = 2)
  @url          = url
  @writer_token = writer_token
  @time_out     = time_out
end

Instance Attribute Details

#time_outObject (readonly)

Returns the value of attribute time_out.



3
4
5
# File 'lib/colossus/writer_client.rb', line 3

def time_out
  @time_out
end

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/colossus/writer_client.rb', line 3

def url
  @url
end

#writer_tokenObject (readonly)

Returns the value of attribute writer_token.



3
4
5
# File 'lib/colossus/writer_client.rb', line 3

def writer_token
  @writer_token
end

Instance Method Details

#get_presences(optional_user_ids = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/colossus/writer_client.rb', line 11

def get_presences(optional_user_ids = nil)
  user_ids = Array(optional_user_ids) if optional_user_ids
  unique_token = generate_unique_token
  EM.synchrony do
    EM::Synchrony.add_timer(time_out) { raise "Presence request timed out" }
    EM::Synchrony.sync(faye_client.subscribe("/presences/response/#{unique_token}") { |message| return message['statuses'] })
    EM::Synchrony.sync(faye_client.publish("/presences/request/#{unique_token}", user_ids))
  end
end

#push_message(user_ids, message) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/colossus/writer_client.rb', line 21

def push_message(user_ids, message)
  user_ids = Array(user_ids)
  EM.synchrony do
    user_ids.each do |user_id|
      EM::Synchrony.sync(faye_client.publish("/users/#{user_id}", message))
    end
    EM.stop
  end
end