Sha256: 088cfd0aa66970927b3700ae64d18c8e1d2a19865b2c1a852d18c0b47fb59e1e

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

ROOT = Dir.pwd unless defined? ROOT

module Pushify
  module Juggernaut
  
    if (File.exist?("#{ROOT}/config/juggernaut_hosts.yml"))
      CONFIG_FILE = "#{ROOT}/config/juggernaut_hosts.yml"
    else
      CONFIG_FILE = "#{File.dirname(__FILE__)}/../../install/juggernaut_hosts.yml"
    end

    CONFIG = YAML::load(ERB.new(IO.read(CONFIG_FILE)).result).freeze
    CR = "\0"

    class << self

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

      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

    private

      def hosts
        CONFIG[:hosts].select {|h| 
          !h[:environment] or h[:environment].to_s == ENV['RAILS_ENV']
        }
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pushify-1.2.0 lib/pushify/juggernaut.rb
pushify-1.1.0 lib/pushify/juggernaut.rb
pushify-1.0.0 lib/pushify/juggernaut.rb