Module: Pushify::Juggernaut

Defined in:
lib/pushify/juggernaut.rb

Constant Summary collapse

CONFIG_FILE =
"#{File.dirname(__FILE__)}/../../install/juggernaut_hosts.yml"
CONFIG =
YAML::load(::ERB.new(IO.read(CONFIG_FILE)).result).freeze
CR =
"\0"

Class Method Summary collapse

Class Method Details

.send_data(hash, response = false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pushify/juggernaut.rb', line 27

def send_data(hash, response = false)
  hash[:channels]   = Array(hash[:channels])   if hash[:channels]
  hash[:client_ids] = Array(hash[:client_ids]) if hash[:client_ids]

  res = []
  hosts.each do |address|
    begin
      hash[:secret_key] = address[:secret_key] if address[:secret_key]

      @socket = TCPSocket.new(address[:host], address[:port])
      # the \0 is to mirror flash
      @socket.print(hash.to_json + CR)
      @socket.flush
      res << @socket.readline(CR) if response
    ensure
      @socket.close if @socket and !@socket.closed?
    end
  end
  res.collect {|r| ActiveSupport::JSON.decode(r.chomp!(CR)) } if response
end

.send_to_all(data) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/pushify/juggernaut.rb', line 17

def send_to_all(data)
  fc = {
    :command   => :broadcast,
    :body      => data, 
    :type      => :to_channels,
    :channels  => []
  }
  send_data(fc)
end