Sha256: aedb161a2609524c06f1ee7fe556c7b226763093b9c77b935c9dc577454d39cd

Contents?: true

Size: 834 Bytes

Versions: 1

Compression:

Stored size: 834 Bytes

Contents

require "redis"
require "json"

module Juggernaut
  EVENTS = [
    "juggernaut:subscribe", 
    "juggernaut:unsubscribe", 
    "juggernaut:custom"
  ]
  
  def options
    @options ||= {}
  end
  
  def options=(val)
    @options = val
  end
  
  def url=(url)
    options[:url] = url
  end
  
  def publish(channels, data, options = {})
    message = ({:channels => Array(channels).uniq, :data => data}).merge(options)
    redis.publish(key, message.to_json) 
  end
  
  def subscribe
    Redis.connect(options).subscribe(*EVENTS) do |on|
      on.message do |type, msg|
        yield(type.gsub(/^juggernaut:/, "").to_sym, JSON.parse(msg))
      end
    end
  end
  
  protected
    def redis
      @redis ||= Redis.connect(options)
    end
  
    def key(*args)
      args.unshift(:juggernaut).join(":")
    end

    extend self
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
juggernaut-2.0.3 lib/juggernaut.rb