Sha256: 10978331972ab6631025dc880f7209eb05120de9ae96f28186c988d62a922d8d

Contents?: true

Size: 904 Bytes

Versions: 2

Compression:

Stored size: 904 Bytes

Contents

module MiniCheck
  class Check
    attr_accessor :name
    attr_accessor :healthy
    attr_accessor :action
    attr_accessor :exception

    def initialize args = {}, &block
      args = {name: args} if !args.is_a?(Hash)
      args[:action] = block if block_given?

      set_attributes args
    end

    def healthy?
      !!healthy
    end

    def run
      begin
        self.healthy = action.call
        self.exception = nil
      rescue Exception => e
        self.healthy = false
        self.exception = e
      end
    end

    def to_hash
      {}.tap do |h|
        h[:healthy] = healthy?
        h[:error] = error_hash if exception
      end
    end

    private

    def error_hash
      {
        message: exception.message,
        stack: exception.backtrace
      }
    end

    def set_attributes args = {}
      args.each do |k,v|
        send("#{k}=", v)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mini_check-0.3.0 lib/mini_check/check.rb
mini_check-0.2.0 lib/mini_check/check.rb