Module: Juggernaut

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#optionsObject



11
12
13
# File 'lib/juggernaut.rb', line 11

def options
  @options ||= {}
end

#options=(val) ⇒ Object



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

def options=(val)
  @options = val
end

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



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

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

#subscribeObject



28
29
30
31
32
33
34
# File 'lib/juggernaut.rb', line 28

def subscribe
  Redis.new(redis_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



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

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