Class: Ristretta::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/ristretta/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_attrs, timestamp) ⇒ Event

Returns a new instance of Event.



5
6
7
8
# File 'lib/ristretta/event.rb', line 5

def initialize(event_attrs, timestamp)
  @event_attrs = JSON.parse(event_attrs)
  @timestamp = timestamp.to_i
end

Instance Attribute Details

#event_attrsObject (readonly)

Returns the value of attribute event_attrs.



3
4
5
# File 'lib/ristretta/event.rb', line 3

def event_attrs
  @event_attrs
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



3
4
5
# File 'lib/ristretta/event.rb', line 3

def timestamp
  @timestamp
end

Class Method Details

.create(event_subject, event_type, event_attrs, timestamp = Time.now.to_i) ⇒ Object



10
11
12
13
14
15
# File 'lib/ristretta/event.rb', line 10

def Event.create(event_subject, event_type, event_attrs, timestamp = Time.now.to_i)
  Ristretta.client.zadd(Ristretta.event_key({
    event_type: event_type,
    event_subject: event_subject
  }), timestamp, (event_attrs.merge(timestamp: timestamp.to_i)).to_json, nx: true)
end

.find(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ristretta/event.rb', line 17

def Event.find(options = {})
  raise(Exceptions::SubjectNotSpecified, "event_subject must be specified") if options[:event_subject].nil?
  raise(Exceptions::TypeNotSpecified, "event_type must be specified") if options[:event_type].nil?

  start_timestamp = options[:since].to_i
  end_timestamp = options[:until] || Time.now.to_i
  
  Ristretta.client.zrangebyscore(Ristretta.event_key(options), start_timestamp, end_timestamp, with_scores: true).collect do |event_data|
    self.new(event_data.first, event_data.last.to_i)
  end
end