Class: ActiveHistory::Event

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Event

Returns a new instance of Event.



5
6
7
8
9
10
# File 'lib/activehistory/event.rb', line 5

def initialize(attrs={})
  @attrs = attrs
  @attrs[:timestamp] ||= Time.now
  @actions = []
  @regards = []
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



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

def actions
  @actions
end

#regardsObject

Returns the value of attribute regards.



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

def regards
  @regards
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

Instance Method Details

#action!(action) ⇒ Object



12
13
14
15
16
# File 'lib/activehistory/event.rb', line 12

def action!(action)
  action = ActiveHistory::Action.new(action)
  @actions << action
  action
end

#action_for(id) ⇒ Object



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

def action_for(id)
  @actions.find { |a| a.subject == id }
end

#as_jsonObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/activehistory/event.rb', line 32

def as_json
  {
    ip:         @attrs[:ip],
    user_agent: @attrs[:user_agent],
    session_id: @attrs[:session_id],
    account:    @attrs[:account],
    api_key:    @attrs[:api_key],
    metadata:   @attrs[:metadata],
    timestamp:  @attrs[:timestamp].utc.iso8601(3),
    actions:    @actions.map(&:as_json),
    regards:    @regards.map(&:as_json)
  }
end

#encapsulateObject



46
47
48
# File 'lib/activehistory/event.rb', line 46

def encapsulate
ensure
end

#regard!(regard) ⇒ Object



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

def regard!(regard)
  @regards << regard
end

#save!Object



26
27
28
29
30
# File 'lib/activehistory/event.rb', line 26

def save!
  return if @actions.empty?
  
  ActiveHistory.connection.post('/events', self.as_json)
end