Sha256: 8fea72116ec3b26e4f8cfe60b9197460dd57c5212ad656776642a57d783940ef

Contents?: true

Size: 1.51 KB

Versions: 2

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

2 entries across 2 versions & 1 rubygems

Version Path
pushify-1.2.2 lib/pushify/juggernaut.rb
pushify-1.2.1 lib/pushify/juggernaut.rb