Class: WarCorrespondent::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/warcorrespondent/reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, &block) ⇒ Reporter

Returns a new instance of Reporter.



7
8
9
10
11
12
13
14
15
16
# File 'lib/warcorrespondent/reporter.rb', line 7

def initialize(args, &block)
  @timeout = 300
  [:timeout, :identifier, :options].each do |key|
    if args[key] && self.respond_to?("#{key}=")
      self.send("#{key}=",args[key])
    end
  end
  @block = block if block
  WarCorrespondent::register_reporter(self)
end

Instance Attribute Details

#identifierObject

Returns the value of attribute identifier.



4
5
6
# File 'lib/warcorrespondent/reporter.rb', line 4

def identifier
  @identifier
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/warcorrespondent/reporter.rb', line 5

def options
  @options
end

#timeoutObject

Returns the value of attribute timeout.



3
4
5
# File 'lib/warcorrespondent/reporter.rb', line 3

def timeout
  @timeout
end

Instance Method Details

#runObject



34
35
36
37
38
39
# File 'lib/warcorrespondent/reporter.rb', line 34

def run
  loop do
    update
    sleep timeout
  end
end

#updateObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/warcorrespondent/reporter.rb', line 18

def update
  Logging::logger.debug{"Updating #{@identifier}"}
  data = instance_eval(&@block)
  return if data == nil
  data = [data] if data.class == Hash
  data.map! do |e|
    e = {:timestamp => Time.now.to_i}.merge(e)
    e[:identifier] = identifier + (e[:identifier]  ? (':' + e[:identifier]) : '')
    e[:identifier].gsub!(/:+/, ':')
    e
  end
  WarCorrespondent::update(data)
rescue Exception => e
  Logging::logger.warn{e}
end