cloud_events 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +11 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.md +203 -0
- data/README.md +250 -0
- data/lib/cloud_events.rb +31 -0
- data/lib/cloud_events/content_type.rb +208 -0
- data/lib/cloud_events/errors.rb +28 -0
- data/lib/cloud_events/event.rb +69 -0
- data/lib/cloud_events/event/field_interpreter.rb +136 -0
- data/lib/cloud_events/event/v0.rb +221 -0
- data/lib/cloud_events/event/v1.rb +208 -0
- data/lib/cloud_events/http_binding.rb +292 -0
- data/lib/cloud_events/json_format.rb +158 -0
- data/lib/cloud_events/version.rb +9 -0
- metadata +63 -0
@@ -0,0 +1,158 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "base64"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
module CloudEvents
|
7
|
+
##
|
8
|
+
# An implementation of JSON format and JSON batch format.
|
9
|
+
#
|
10
|
+
# Supports the CloudEvents 0.3 and CloudEvents 1.0 variants of this format.
|
11
|
+
# See https://github.com/cloudevents/spec/blob/v0.3/json-format.md and
|
12
|
+
# https://github.com/cloudevents/spec/blob/v1.0/json-format.md.
|
13
|
+
#
|
14
|
+
class JsonFormat
|
15
|
+
##
|
16
|
+
# Decode an event from the given input JSON string.
|
17
|
+
#
|
18
|
+
# @param json [String] A JSON-formatted string
|
19
|
+
# @return [CloudEvents::Event]
|
20
|
+
#
|
21
|
+
def decode json, **_other_kwargs
|
22
|
+
structure = ::JSON.parse json
|
23
|
+
decode_hash_structure structure
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Encode an event to a JSON string.
|
28
|
+
#
|
29
|
+
# @param event [CloudEvents::Event] An input event.
|
30
|
+
# @param sort [boolean] Whether to sort keys of the JSON output.
|
31
|
+
# @return [String] The JSON representation.
|
32
|
+
#
|
33
|
+
def encode event, sort: false, **_other_kwargs
|
34
|
+
structure = encode_hash_structure event
|
35
|
+
structure = sort_keys structure if sort
|
36
|
+
::JSON.dump structure
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# Decode a batch of events from the given input string.
|
41
|
+
#
|
42
|
+
# @param json [String] A JSON-formatted string
|
43
|
+
# @return [Array<CloudEvents::Event>]
|
44
|
+
#
|
45
|
+
def decode_batch json, **_other_kwargs
|
46
|
+
structure_array = Array(::JSON.parse(json))
|
47
|
+
structure_array.map do |structure|
|
48
|
+
decode_hash_structure structure
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
##
|
53
|
+
# Encode a batch of event to a JSON string.
|
54
|
+
#
|
55
|
+
# @param events [Array<CloudEvents::Event>] An array of input events.
|
56
|
+
# @param sort [boolean] Whether to sort keys of the JSON output.
|
57
|
+
# @return [String] The JSON representation.
|
58
|
+
#
|
59
|
+
def encode_batch events, sort: false, **_other_kwargs
|
60
|
+
structure_array = Array(events).map do |event|
|
61
|
+
structure = encode_hash_structure event
|
62
|
+
sort ? sort_keys(structure) : structure
|
63
|
+
end
|
64
|
+
::JSON.dump structure_array
|
65
|
+
end
|
66
|
+
|
67
|
+
##
|
68
|
+
# Decode a single event from a hash data structure with keys and types
|
69
|
+
# conforming to the JSON envelope.
|
70
|
+
#
|
71
|
+
# @param structure [Hash] An input hash.
|
72
|
+
# @return [CloudEvents::Event]
|
73
|
+
#
|
74
|
+
def decode_hash_structure structure
|
75
|
+
spec_version = structure["specversion"].to_s
|
76
|
+
case spec_version
|
77
|
+
when "0.3"
|
78
|
+
decode_hash_structure_v0 structure
|
79
|
+
when /^1(\.|$)/
|
80
|
+
decode_hash_structure_v1 structure
|
81
|
+
else
|
82
|
+
raise SpecVersionError, "Unrecognized specversion: #{spec_version}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
##
|
87
|
+
# Encode a single event to a hash data structure with keys and types
|
88
|
+
# conforming to the JSON envelope.
|
89
|
+
#
|
90
|
+
# @param event [CloudEvents::Event] An input event.
|
91
|
+
# @return [String] The hash structure.
|
92
|
+
#
|
93
|
+
def encode_hash_structure event
|
94
|
+
case event
|
95
|
+
when Event::V0
|
96
|
+
encode_hash_structure_v0 event
|
97
|
+
when Event::V1
|
98
|
+
encode_hash_structure_v1 event
|
99
|
+
else
|
100
|
+
raise SpecVersionError, "Unrecognized specversion: #{event.spec_version}"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
def sort_keys hash
|
107
|
+
result = {}
|
108
|
+
hash.keys.sort.each do |key|
|
109
|
+
result[key] = hash[key]
|
110
|
+
end
|
111
|
+
result
|
112
|
+
end
|
113
|
+
|
114
|
+
def decode_hash_structure_v0 structure
|
115
|
+
data = structure["data"]
|
116
|
+
content_type = structure["datacontenttype"]
|
117
|
+
if data.is_a?(::String) && content_type.is_a?(::String)
|
118
|
+
content_type = ContentType.new content_type
|
119
|
+
if content_type.subtype == "json" || content_type.subtype_format == "json"
|
120
|
+
structure = structure.dup
|
121
|
+
structure["data"] = ::JSON.parse data rescue data
|
122
|
+
structure["datacontenttype"] = content_type
|
123
|
+
end
|
124
|
+
end
|
125
|
+
Event::V0.new attributes: structure
|
126
|
+
end
|
127
|
+
|
128
|
+
def decode_hash_structure_v1 structure
|
129
|
+
if structure.key? "data_base64"
|
130
|
+
structure = structure.dup
|
131
|
+
structure["data"] = ::Base64.decode64 structure.delete "data_base64"
|
132
|
+
end
|
133
|
+
Event::V1.new attributes: structure
|
134
|
+
end
|
135
|
+
|
136
|
+
def encode_hash_structure_v0 event
|
137
|
+
structure = event.to_h
|
138
|
+
data = event.data
|
139
|
+
content_type = event.data_content_type
|
140
|
+
if data.is_a?(::String) && !content_type.nil?
|
141
|
+
if content_type.subtype == "json" || content_type.subtype_format == "json"
|
142
|
+
structure["data"] = ::JSON.parse data rescue data
|
143
|
+
end
|
144
|
+
end
|
145
|
+
structure
|
146
|
+
end
|
147
|
+
|
148
|
+
def encode_hash_structure_v1 event
|
149
|
+
structure = event.to_h
|
150
|
+
data = structure["data"]
|
151
|
+
if data.is_a?(::String) && data.encoding == ::Encoding::ASCII_8BIT
|
152
|
+
structure.delete "data"
|
153
|
+
structure["data_base64"] = ::Base64.encode64 data
|
154
|
+
end
|
155
|
+
structure
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cloud_events
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Azuma
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: The official Ruby implementation of the CloudEvents Specification. Provides
|
14
|
+
data types for events, and HTTP/JSON bindings for marshalling and unmarshalling
|
15
|
+
event data.
|
16
|
+
email:
|
17
|
+
- dazuma@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- ".yardopts"
|
23
|
+
- CHANGELOG.md
|
24
|
+
- LICENSE.md
|
25
|
+
- README.md
|
26
|
+
- lib/cloud_events.rb
|
27
|
+
- lib/cloud_events/content_type.rb
|
28
|
+
- lib/cloud_events/errors.rb
|
29
|
+
- lib/cloud_events/event.rb
|
30
|
+
- lib/cloud_events/event/field_interpreter.rb
|
31
|
+
- lib/cloud_events/event/v0.rb
|
32
|
+
- lib/cloud_events/event/v1.rb
|
33
|
+
- lib/cloud_events/http_binding.rb
|
34
|
+
- lib/cloud_events/json_format.rb
|
35
|
+
- lib/cloud_events/version.rb
|
36
|
+
homepage: https://github.com/cloudevents/sdk-ruby
|
37
|
+
licenses:
|
38
|
+
- Apache-2.0
|
39
|
+
metadata:
|
40
|
+
changelog_uri: https://github.com/cloudevents/sdk-ruby/blob/master/CHANGELOG.md
|
41
|
+
source_code_uri: https://github.com/cloudevents/sdk-ruby
|
42
|
+
bug_tracker_uri: https://github.com/cloudevents/sdk-ruby/issues
|
43
|
+
documentation_uri: https://rubydoc.info/gems/cloud_events/0.1.0
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 2.4.0
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubygems_version: 3.1.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Ruby SDK for CloudEvents
|
63
|
+
test_files: []
|