Module: NoDevent::Emitter

Defined in:
lib/nodevent.rb

Class Method Summary collapse

Class Method Details

.configObject



81
82
83
84
85
86
87
# File 'lib/nodevent.rb', line 81

def config
  @@config ||= Hash.new({
                          "host" => "http://localhost:8080",
                          "namespace" => "/nodevent"
                        })
  @@config
end

.config=(obj) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/nodevent.rb', line 70

def config= obj
  @@config = nil
  @@publisher = nil
  @@config = config.merge(obj)
  if @@config.keys.include?("redis")
    r_config = @@config["redis"]

    @@publisher = Redis.new(:host => r_config["host"], :port => r_config["port"], :db => r_config["db"])
  end
end

.emit(room, name, message) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/nodevent.rb', line 93

def emit(room, name, message)
  room = NoDevent::Emitter.room(room)
  publisher.publish(config["namespace"],
                 { :room => room,
                   :event => name, 
                   :message => message}.to_json)
end

.publisherObject



89
90
91
# File 'lib/nodevent.rb', line 89

def publisher
  @@publisher || $redis
end

.room(obj) ⇒ Object



101
102
103
104
105
106
# File 'lib/nodevent.rb', line 101

def room(obj)
  obj = "#{obj.class}_#{obj.to_param}" if (defined?(ActiveRecord::Base) && 
                                     obj.is_a?(ActiveRecord::Base))
  obj = "#{obj.name}" if (obj.class == Class || obj.class == Module)
  obj
end

.room_key(obj, expires) ⇒ Object



108
109
110
111
112
113
# File 'lib/nodevent.rb', line 108

def room_key(obj, expires)
  r = room(obj)
  ts = (expires.to_f*1000).to_i
  hash = (Digest::SHA2.new << r << ts.to_s<< config["secret"]).to_s
  "#{hash}:#{ts}"
end