Skip to content

Commit 1c7cae8

Browse files
authored
fix!: Properly handle data when the content-type indicates JSON (#57)
Signed-off-by: Daniel Azuma <[email protected]>
1 parent e518565 commit 1c7cae8

File tree

8 files changed

+604
-193
lines changed

8 files changed

+604
-193
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
inherit_gem:
22
google-style: google-style.yml
33

4+
Lint/UnusedMethodArgument:
5+
Exclude:
6+
- "lib/cloud_events/format.rb"
47
Metrics/BlockLength:
58
Exclude:
69
- "test/**/test_*.rb"

lib/cloud_events.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "cloud_events/content_type"
44
require "cloud_events/errors"
55
require "cloud_events/event"
6+
require "cloud_events/format"
67
require "cloud_events/http_binding"
78
require "cloud_events/json_format"
89

lib/cloud_events/errors.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ module CloudEvents
77
class CloudEventsError < ::StandardError
88
end
99

10+
##
11+
# An error signaling that a protocol handler does not believe that a piece of
12+
# data is intended to be a CloudEvent.
13+
#
14+
class NotCloudEventError < ::StandardError
15+
end
16+
1017
##
1118
# Errors indicating unsupported or incorrectly formatted HTTP content or
1219
# headers.

lib/cloud_events/format.rb

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# frozen_string_literal: true
2+
3+
require "base64"
4+
require "json"
5+
6+
module CloudEvents
7+
##
8+
# This module documents the method signatures that may be implemented by
9+
# formatters.
10+
#
11+
# Note that a formatter need not implement all methods. For example, an event
12+
# formatter should implement `decode_event` and `encode_event`, and may also
13+
# implement `decode_batch` and `encode_batch`, but might not implement
14+
# `decode_data` or `encode_data`. Additionally, this module itself is present
15+
# primarily for documentation, and need not be directly included by
16+
# implementations.
17+
#
18+
module Format
19+
##
20+
# Decode an event from the given serialized input.
21+
#
22+
# The arguments comprise an input string representing the encoded event
23+
# from a protocol source such as an HTTP request body, and a ContentType.
24+
# All additional arguments are optional, and may or may not be considered
25+
# by the formatter.
26+
#
27+
# The formatter must return either an event object, or `nil` to signal that
28+
# the formatter does not recognize the input and believes it should be
29+
# handled by a different formatter.
30+
# It can also raise an error to indicate that it believes it should handle
31+
# the input, but that the data is malformed.
32+
#
33+
# @param input [String] The input as a string.
34+
# @param content_type [CloudEvents::ContentType,nil] The input content
35+
# type, or `nil` if none is available.
36+
# @return [CloudEvents::Event] if decoding succeeded.
37+
# @return [nil] if a different formatter should be used.
38+
#
39+
def decode_event input, content_type, **_other_kwargs
40+
nil
41+
end
42+
43+
##
44+
# Encode an event to a string.
45+
#
46+
# The input must be a CloudEvent object. All additional arguments are
47+
# optional, and may or may not be considered by the formatter.
48+
#
49+
# The formatter must return either a tuple comprising the serialized form
50+
# of the event and an associated ContentType, or `nil` to signal that the
51+
# formatter is incapable of handling the given event and believes it should
52+
# be handled by a different formatter.
53+
# It can also raise an error to indicate that it believes it should handle
54+
# the input, but that the input is malformed.
55+
#
56+
# Implementations should make sure the encoding of the returned string is
57+
# correct. In particular, if the format uses binary data, the returned
58+
# string should have the appropriate `ASCII_8BIT` encoding, and the
59+
# returned ContentType should specify the appropriate charset.
60+
#
61+
# @param event [CloudEvents::Event] The input event.
62+
# @return [Array(String,CloudEvents::ContentType)] if encoding succeeded.
63+
# @return [nil] if a different formatter should be used.
64+
#
65+
def encode_event event, **_other_kwargs
66+
nil
67+
end
68+
69+
##
70+
# Decode a batch of events from the given serialized input.
71+
#
72+
# The arguments comprise an input string representing the encoded batch of
73+
# events from a protocol source such as an HTTP request body, and a
74+
# ContentType. All additional arguments are optional, and may or may not be
75+
# considered by the formatter.
76+
#
77+
# The formatter must return either an array (possibly empty) of event
78+
# objects, or `nil` to signal that the formatter does not recognize the
79+
# input and believes it should be handled by a different formatter.
80+
# It can also raise an error to indicate that it believes it should handle
81+
# the input, but that the data is malformed.
82+
#
83+
# @param input [String] The input as a string.
84+
# @param content_type [CloudEvents::ContentType,nil] The input content
85+
# type, or `nil` if none is available.
86+
# @return [Array<CloudEvents::Event>] if decoding succeeded.
87+
# @return [nil] if a different formatter should be used.
88+
#
89+
def decode_batch input, content_type, **_other_kwargs
90+
nil
91+
end
92+
93+
##
94+
# Encode a batch of events to a string.
95+
#
96+
# The input must be an array of CloudEvent objects (which could be empty).
97+
# All additional arguments are optional, and may or may not be considered
98+
# by the formatter.
99+
#
100+
# The formatter must return either a tuple comprising the serialized form
101+
# of the batch and an associated ContentType, or `nil` to signal that the
102+
# formatter is incapable of handling the given batch and believes it should
103+
# be handled by a different formatter.
104+
# It can also raise an error to indicate that it believes it should handle
105+
# the input, but that the input is malformed.
106+
#
107+
# Implementations should make sure the encoding of the returned string is
108+
# correct. In particular, if the format uses binary data, the returned
109+
# string should have the appropriate `ASCII_8BIT` encoding, and the
110+
# returned ContentType should specify the appropriate charset.
111+
#
112+
# @param events [Array<CloudEvents::Event>] An array of input events.
113+
# @return [Array(String,CloudEvents::ContentType)] if encoding succeeded.
114+
# @return [nil] if a different formatter should be used.
115+
#
116+
def encode_batch events, **_other_kwargs
117+
nil
118+
end
119+
120+
##
121+
# Decode an event data object from string format.
122+
#
123+
# The arguments comprise an input string representing the encoded data from
124+
# a protocol source such as an HTTP request body, and a ContentType. All
125+
# additional arguments are optional, and may or may not be considered by
126+
# the formatter.
127+
#
128+
# The formatter must return either a tuple comprising the event data object
129+
# and a final ContentType (which may be the same as the input ContentType),
130+
# or `nil` to signal that the formatter does not recognize the input and
131+
# believes it should be handled by a different formatter.
132+
# It can also raise an error to indicate that it believes it should handle
133+
# the input, but that the input is malformed.
134+
#
135+
# @param data [String] The input data string.
136+
# @param content_type [CloudEvents::ContentType,nil] The input content
137+
# type, or `nil` if none is available.
138+
# @return [Array(Object,CloudEvents::ContentType)] if decoding succeeded.
139+
# @return [nil] if a different formatter should be used.
140+
#
141+
def decode_data data, content_type, **_other_kwargs
142+
nil
143+
end
144+
145+
##
146+
# Encode an event data object to string format.
147+
#
148+
# The arguments comprise an object representing the event data, and a
149+
# suggested ContentType. All additional arguments are optional, and may or
150+
# may not be considered by the formatter.
151+
#
152+
# The formatter must return either a tuple comprising the encoded data
153+
# string and a final ContentType (which may be the same as the input
154+
# ContentType), or `nil` to signal that the formatter is incapable of
155+
# handling the given data object and believes it should be handled by a
156+
# different formatter.
157+
# It can also raise an error to indicate that it believes it should handle
158+
# the input, but that the input is malformed.
159+
#
160+
# @param data [Object] A data object to encode.
161+
# @param content_type [CloudEvents::ContentType,nil] The input content
162+
# type, or `nil` if none is available.
163+
# @return [Array(String,CloudEvents::ContentType)] if encoding succeeded.
164+
# @return [nil] if a different formatter should be used.
165+
#
166+
def encode_data data, content_type, **_other_kwargs
167+
nil
168+
end
169+
end
170+
end

0 commit comments

Comments
 (0)