Class: Polyn::Serializers::Json
- Inherits:
-
Object
- Object
- Polyn::Serializers::Json
- Defined in:
- lib/polyn/serializers/json.rb
Overview
Handles serializing and deserializing data to and from JSON.
Instance Method Summary collapse
- #combined_error_message(event, errors) ⇒ Object
- #decode(json) ⇒ Object
- #deserialize(json) ⇒ Object
- #deserialize!(json) ⇒ Object
- #format_schema_errors(errors) ⇒ Object
- #get_event_type(event) ⇒ Object
- #get_schema(type) ⇒ Object
-
#initialize(schema_store) ⇒ Json
constructor
A new instance of Json.
- #serialize!(event) ⇒ Object
- #validate(event) ⇒ Object
- #validate!(event) ⇒ Object
- #validate_cloud_event(event) ⇒ Object
- #validate_data(event) ⇒ Object
- #validate_event_instance!(event) ⇒ Object
- #validate_schema(schema, event) ⇒ Object
Constructor Details
#initialize(schema_store) ⇒ Json
Returns a new instance of Json.
25 26 27 |
# File 'lib/polyn/serializers/json.rb', line 25 def initialize(schema_store) @schema_store = schema_store end |
Instance Method Details
#combined_error_message(event, errors) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/polyn/serializers/json.rb', line 128 def (event, errors) [ "Polyn event #{event['id']} from #{event['source']} is not valid", "Event data: #{event.inspect}", ].concat(errors).join("\n") end |
#decode(json) ⇒ Object
54 55 56 57 58 |
# File 'lib/polyn/serializers/json.rb', line 54 def decode(json) JSON.parse(json) rescue JSON::ParserError Polyn::Errors::ValidationError.new("Polyn was unable to decode the following message: \n#{json}") end |
#deserialize(json) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/polyn/serializers/json.rb', line 43 def deserialize(json) data = decode(json) return data if data.is_a?(Polyn::Errors::Error) error = validate(data) return error if error.is_a?(Polyn::Errors::Error) data = Polyn::Utils::Hash.deep_symbolize_keys(data) Event.new(data) end |
#deserialize!(json) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/polyn/serializers/json.rb', line 36 def deserialize!(json) data = deserialize(json) raise data if data.is_a?(Polyn::Errors::Error) data end |
#format_schema_errors(errors) ⇒ Object
122 123 124 125 126 |
# File 'lib/polyn/serializers/json.rb', line 122 def format_schema_errors(errors) errors.map do |error| "Property: `#{error['data_pointer']}` - #{error['type']} - #{error['details']}" end end |
#get_event_type(event) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/polyn/serializers/json.rb', line 108 def get_event_type(event) if event["type"] Polyn::Naming.trim_domain_prefix(event["type"]) else Polyn::Errors::ValidationError.new( "Could not find a `type` in message #{event.inspect} \nEvery event must have a `type`", ) end end |
#get_schema(type) ⇒ Object
118 119 120 |
# File 'lib/polyn/serializers/json.rb', line 118 def get_schema(type) @schema_store.get(type) end |
#serialize!(event) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/polyn/serializers/json.rb', line 29 def serialize!(event) validate_event_instance!(event) event = event.to_h validate!(event) JSON.generate(event) end |
#validate(event) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/polyn/serializers/json.rb', line 65 def validate(event) error = validate_cloud_event(event) return error if error.is_a?(Polyn::Errors::Error) validate_data(event) end |
#validate!(event) ⇒ Object
60 61 62 63 |
# File 'lib/polyn/serializers/json.rb', line 60 def validate!(event) result = validate(event) raise result if result.is_a?(Polyn::Errors::Error) end |
#validate_cloud_event(event) ⇒ Object
81 82 83 84 |
# File 'lib/polyn/serializers/json.rb', line 81 def validate_cloud_event(event) cloud_event_schema = Polyn::CloudEvent.to_h validate_schema(cloud_event_schema, event) end |
#validate_data(event) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/polyn/serializers/json.rb', line 86 def validate_data(event) type = get_event_type(event) return type if type.is_a?(Polyn::Errors::Error) schema = get_schema(type) return schema if schema.is_a?(Polyn::Errors::Error) validate_schema(schema, event["data"]) end |
#validate_event_instance!(event) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/polyn/serializers/json.rb', line 72 def validate_event_instance!(event) if event.instance_of?(Polyn::Event) event else raise Polyn::Errors::ValidationError, "Can only serialize `Polyn::Event` instances. got #{event}" end end |
#validate_schema(schema, event) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/polyn/serializers/json.rb', line 96 def validate_schema(schema, event) schema = JSONSchemer.schema(schema) errors = schema.validate(event).to_a errors = format_schema_errors(errors) unless errors.empty? return Polyn::Errors::ValidationError.new((event, errors)) end errors end |