Module: Juggernaut

Extended by:
Juggernaut
Included in:
Juggernaut
Defined in:
lib/juggernaut.rb,
lib/juggernaut/rails/engine.rb

Constant Summary collapse

EVENTS =
[
  "juggernaut:subscribe", 
  "juggernaut:unsubscribe", 
  "juggernaut:custom"
]

Instance Method Summary collapse

Instance Method Details

#optionsObject



14
15
16
# File 'lib/juggernaut.rb', line 14

def options
  @options ||= {}
end

#options=(val) ⇒ Object



18
19
20
# File 'lib/juggernaut.rb', line 18

def options=(val)
  @options = val
end

#publish(channels, data, options = {}) ⇒ Object



26
27
28
29
# File 'lib/juggernaut.rb', line 26

def publish(channels, data, options = {})
  message = ({:channels => Array(channels).uniq, :data => data}).merge(options)
  redis.publish(key, message.to_json) 
end

#subscribeObject



31
32
33
34
35
36
37
# File 'lib/juggernaut.rb', line 31

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

#url=(url) ⇒ Object



22
23
24
# File 'lib/juggernaut.rb', line 22

def url=(url)
  options[:url] = url
end