lib/captivus/payload.rb in captivus-0.0.3 vs lib/captivus/payload.rb in captivus-0.0.5

- old
+ new

@@ -1,22 +1,78 @@ -require 'captivus/backtrace' +require 'captivus/util' +require 'json-schema' module Captivus class Payload - def initialize(exception) - if exception.respond_to?(:class) && exception.class.respond_to?(:name) && exception.respond_to?(:message) - @as_json = { - 'event' => { - 'type' => exception.class.name, - 'message' => exception.message, - 'timestamp' => Time.now.utc.to_s - }, - 'backtrace' => Backtrace.new(exception).as_json + SCHEMA = { + 'type' => 'object', + 'properties' => { + 'notifier' => { + 'type' => 'object', + 'required' => true, + 'properties' => { + 'name' => {'type' => 'string', 'required' => true}, + 'version' => {'type' => 'string', 'required' => true}, + 'language' => {'type' => 'string', 'required' => true} + } + }, + 'event' => { + 'type' => 'object', + 'required' => true, + 'properties' => { + 'type' => {'type' => 'string', 'required' => true}, + 'message' => {'type' => 'string', 'required' => true}, + 'timestamp' => {'type' => 'string', 'required' => true, 'format' => 'date-time'} + } + }, + 'backtrace' => { + 'type' => 'array', + 'required' => true, + 'items' => { + 'type' => 'object', + 'properties' => { + 'file' => {'type' => 'string', 'required' => true}, + 'number' => { 'type' => 'integer', 'required' => true}, + 'method' => {'type' => 'string'} + } + } + }, + 'context' => { + 'type' => 'object', + 'properties' => { + 'controller' => {'type' => 'string'}, + 'action' => {'type' => 'string'}, + 'params' => {'type' => 'object'} + } + }, + 'environment' => { + 'type' => 'object', + 'required' => true, + 'properties' => { + 'name' => {'type' => 'string', 'required' => true} + } } - else - raise ArgumentError, "Unexpected exception: #{exception.inspect}" - end + } + } + + def initialize(json) + JSON::Validator.validate! SCHEMA, json + @json = json end - attr_reader :as_json + def as_json(*) + Captivus::Util.json_to_hash json + end + + def to_json(*) + json.dup + end + + def ==(other) + other.is_a?(Payload) && json == other.json + end + + protected + + attr_reader :json end end \ No newline at end of file