Sha256: eabe7bebc1bcc4880161f52b0cda14fd84aa590fb028af191e312454ed4142af

Contents?: true

Size: 906 Bytes

Versions: 3

Compression:

Stored size: 906 Bytes

Contents

require "redis"
require "json"

# Attempt to provide Engine to Rails
require "juggernaut/rails/engine"

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

3 entries across 3 versions & 1 rubygems

Version Path
juggernaut-2.1.1 lib/juggernaut.rb
juggernaut-2.1.0 lib/juggernaut.rb
juggernaut-2.0.4 lib/juggernaut.rb