class DucksboardReporter::Widget

Attributes

id[R]
options[R]
reporter[R]
updater[R]

Public Class Methods

new(klass, id, reporter, options = {}) click to toggle source
# File lib/ducksboard_reporter/widget.rb, line 9
def initialize(klass, id, reporter, options = {})
  @klass = klass
  @id = id
  @reporter = reporter
  @options = options
  @updater = instanciate_updater
end

Public Instance Methods

interval() click to toggle source
# File lib/ducksboard_reporter/widget.rb, line 34
def interval
  options[:interval] || 10
end
start() click to toggle source
# File lib/ducksboard_reporter/widget.rb, line 17
def start
  debug log_format("Started using reporter #{reporter.name}")

  every(interval) do
    update
  end
end
update() click to toggle source
# File lib/ducksboard_reporter/widget.rb, line 25
def update
  value = map_value(value_method)
  debug log_format("Updating value #{value}")

  @updater.update(value)
rescue *timeout_errors, Errno::ECONNRESET => e
  error e
end

Private Instance Methods

instanciate_updater() click to toggle source
# File lib/ducksboard_reporter/widget.rb, line 49
def instanciate_updater
  klass = Class.new(Ducksboard::Widget)
  klass.default_timeout(interval - 1)
  klass.new(@id)
end
log_format(msg) click to toggle source
# File lib/ducksboard_reporter/widget.rb, line 40
def log_format(msg)
  @log_prefix ||= "Widget #{@klass}(#{id}): "
  @log_prefix + msg
end
map_value(object) click to toggle source
# File lib/ducksboard_reporter/widget.rb, line 55
def map_value(object)
  object = $1.to_sym if object =~ /\A:(.+)/

  case object
  when Symbol
    @reporter.public_send(object)
  when Hash
    object.inject({}) do |memo, (k, v)|
      memo[k] = map_value(v)
      memo
    end
  else
    object
  end
end
timeout_errors() click to toggle source
# File lib/ducksboard_reporter/widget.rb, line 71
def timeout_errors
  v = RUBY_VERSION.split(".")[0].to_i
  if v >= 2
    [Net::ReadTimeout, Net::OpenTimeout]
  else
    [Timeout::Error]
  end
end
value_method() click to toggle source
# File lib/ducksboard_reporter/widget.rb, line 45
def value_method
  options[:value] || :value
end