Class: Bosh::Monitor::Events::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/monitor/events/base.rb

Direct Known Subclasses

Alert, Heartbeat

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bosh/monitor/events/base.rb', line 38

def initialize(attributes = {})
  @attributes = {}
  @kind = :unknown

  attributes.each_pair do |k, v|
    @attributes[k.to_s] = v
  end

  @logger = Bhm.logger
  @errors = Set.new
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/bosh/monitor/events/base.rb', line 8

def attributes
  @attributes
end

#errorsObject (readonly)

Returns the value of attribute errors.



9
10
11
# File 'lib/bosh/monitor/events/base.rb', line 9

def errors
  @errors
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/bosh/monitor/events/base.rb', line 4

def id
  @id
end

#kindObject (readonly)

Returns the value of attribute kind.



7
8
9
# File 'lib/bosh/monitor/events/base.rb', line 7

def kind
  @kind
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/bosh/monitor/events/base.rb', line 6

def logger
  @logger
end

Class Method Details

.create(kind, attributes = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bosh/monitor/events/base.rb', line 19

def self.create(kind, attributes = {})
  if !attributes.kind_of?(Hash)
    raise InvalidEvent, "Cannot create event from #{attributes.class}"
  end

  case kind.to_s
  when "heartbeat"
    klass = Bhm::Events::Heartbeat
  when "alert"
    klass = Bhm::Events::Alert
  else
    raise InvalidEvent, "Cannot find '#{kind}' event handler"
  end

  event = klass.new(attributes)
  event.id = SecureRandom.uuid if event.id.nil?
  event
end

.create!(kind, attributes = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/bosh/monitor/events/base.rb', line 11

def self.create!(kind, attributes = {})
  event = create(kind, attributes)
  if !event.valid?
    raise InvalidEvent, event.error_message
  end
  event
end

Instance Method Details

#add_error(error) ⇒ Object



50
51
52
# File 'lib/bosh/monitor/events/base.rb', line 50

def add_error(error)
  @errors << error
end

#error_messageObject



59
60
61
# File 'lib/bosh/monitor/events/base.rb', line 59

def error_message
  @errors.to_a.join(", ")
end

#valid?Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/bosh/monitor/events/base.rb', line 54

def valid?
  validate
  @errors.empty?
end