class Kronos::Report
Constants
- STATUSES
Public Class Methods
failure_from(task_id, exception, timestamp = Time.now)
click to toggle source
# File lib/kronos/report.rb, line 17 def failure_from(task_id, exception, timestamp = Time.now) new( task_id: check_task_id(task_id), status: Kronos::Report::STATUSES[:failure], timestamp: check_timestamp(timestamp), exception: check_exception(exception) ) end
success_from(task_id, metadata, timestamp = Time.now)
click to toggle source
# File lib/kronos/report.rb, line 8 def success_from(task_id, metadata, timestamp = Time.now) new( task_id: check_task_id(task_id), status: Kronos::Report::STATUSES[:success], timestamp: check_timestamp(timestamp), metadata: check_metadata(metadata) ) end
Private Class Methods
check_exception(exception)
click to toggle source
# File lib/kronos/report.rb, line 44 def check_exception(exception) if exception.is_a?(Hash) return exception if valid_exception_format(exception) raise(ArgumentError, 'Invalid exception format') elsif exception.is_a?(::Exception) exception_hash_from(exception) else raise_invalid_argument('exception', exception, Hash) end end
check_metadata(metadata)
click to toggle source
# File lib/kronos/report.rb, line 38 def check_metadata(metadata) raise_invalid_argument('metadata', metadata, Hash) unless metadata.is_a?(Hash) return metadata if metadata.values.all? { |value| value.is_a?(String) } raise(ArgumentError, 'Expected all values in metadata to be strings') end
check_task_id(task)
click to toggle source
# File lib/kronos/report.rb, line 34 def check_task_id(task) task.is_a?(Symbol) ? task : raise_invalid_argument('Task ID', task, Symbol) end
check_timestamp(timestamp)
click to toggle source
# File lib/kronos/report.rb, line 30 def check_timestamp(timestamp) timestamp.is_a?(Time) ? timestamp : raise_invalid_argument('timestamp', timestamp, Time) end
exception_hash_from(exception)
click to toggle source
# File lib/kronos/report.rb, line 55 def exception_hash_from(exception) { type: exception.class.name, message: exception.message, stacktrace: exception.backtrace } end
raise_invalid_argument(name, received, expectation)
click to toggle source
# File lib/kronos/report.rb, line 76 def raise_invalid_argument(name, received, expectation) raise(ArgumentError, "Invalid #{name} given (#{received.class}). #{expectation} expected.") end
valid_exception_format(exception)
click to toggle source
# File lib/kronos/report.rb, line 63 def valid_exception_format(exception) type = exception[:type] message = exception[:message] stacktrace = exception[:stacktrace] type.is_a?(String) && !type.empty? && message.is_a?(String) && !message.empty? && stacktrace.is_a?(Array) && stacktrace.all? { |line| line.is_a?(String) } end
Public Instance Methods
failure?()
click to toggle source
# File lib/kronos/report.rb, line 85 def failure? status == Kronos::Report::STATUSES[:failure] end
success?()
click to toggle source
# File lib/kronos/report.rb, line 81 def success? status == Kronos::Report::STATUSES[:success] end