cloud_events 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "base64"
4
3
  require "json"
5
4
 
6
5
  module CloudEvents
@@ -38,21 +37,21 @@ module CloudEvents
38
37
  # @raise [CloudEvents::SpecVersionError] if an unsupported specversion is
39
38
  # found.
40
39
  #
41
- def decode_event content: nil, content_type: nil, data_decoder: nil, **_other_kwargs
40
+ def decode_event(content: nil, content_type: nil, data_decoder: nil, **_other_kwargs)
42
41
  return nil unless content && content_type&.media_type == "application" && content_type&.subtype_format == "json"
43
42
  case content_type.subtype_base
44
43
  when "cloudevents"
45
- event = decode_hash_structure ::JSON.parse(content), charset: charset_of(content), data_decoder: data_decoder
44
+ event = decode_hash_structure(::JSON.parse(content), charset: charset_of(content), data_decoder: data_decoder)
46
45
  { event: event }
47
46
  when "cloudevents-batch"
48
- charset = charset_of content
47
+ charset = charset_of(content)
49
48
  batch = Array(::JSON.parse(content)).map do |structure|
50
- decode_hash_structure structure, charset: charset, data_decoder: data_decoder
49
+ decode_hash_structure(structure, charset: charset, data_decoder: data_decoder)
51
50
  end
52
51
  { event_batch: batch }
53
52
  end
54
53
  rescue ::JSON::JSONError
55
- raise FormatSyntaxError, "JSON syntax error"
54
+ raise(FormatSyntaxError, "JSON syntax error")
56
55
  end
57
56
 
58
57
  ##
@@ -78,25 +77,25 @@ module CloudEvents
78
77
  # @return [nil] if declining the request.
79
78
  # @raise [CloudEvents::FormatSyntaxError] if the JSON could not be parsed
80
79
  #
81
- def encode_event event: nil, event_batch: nil, data_encoder: nil, sort: false, **_other_kwargs
80
+ def encode_event(event: nil, event_batch: nil, data_encoder: nil, sort: false, **_other_kwargs)
82
81
  if event && !event_batch
83
- structure = encode_hash_structure event, data_encoder: data_encoder
84
- structure = sort_keys structure if sort
82
+ structure = encode_hash_structure(event, data_encoder: data_encoder)
83
+ structure = sort_keys(structure) if sort
85
84
  subtype = "cloudevents"
86
85
  elsif event_batch && !event
87
86
  structure = event_batch.map do |elem|
88
- structure_elem = encode_hash_structure elem, data_encoder: data_encoder
87
+ structure_elem = encode_hash_structure(elem, data_encoder: data_encoder)
89
88
  sort ? sort_keys(structure_elem) : structure_elem
90
89
  end
91
90
  subtype = "cloudevents-batch"
92
91
  else
93
92
  return nil
94
93
  end
95
- content = ::JSON.dump structure
96
- content_type = ContentType.new "application/#{subtype}+json; charset=#{charset_of content}"
94
+ content = ::JSON.dump(structure)
95
+ content_type = ContentType.new("application/#{subtype}+json; charset=#{charset_of(content)}")
97
96
  { content: content, content_type: content_type }
98
97
  rescue ::JSON::JSONError
99
- raise FormatSyntaxError, "JSON syntax error"
98
+ raise(FormatSyntaxError, "JSON syntax error")
100
99
  end
101
100
 
102
101
  ##
@@ -120,17 +119,17 @@ module CloudEvents
120
119
  # @raise [CloudEvents::SpecVersionError] if an unsupported specversion is
121
120
  # found.
122
121
  #
123
- def decode_data spec_version: nil, content: nil, content_type: nil, **_other_kwargs
122
+ def decode_data(spec_version: nil, content: nil, content_type: nil, **_other_kwargs)
124
123
  return nil unless spec_version
125
124
  return nil unless content
126
- return nil unless json_content_type? content_type
125
+ return nil unless json_content_type?(content_type)
127
126
  unless spec_version =~ /^0\.3|1(\.|$)/
128
- raise SpecVersionError, "Unrecognized specversion: #{spec_version}"
127
+ raise(SpecVersionError, "Unrecognized specversion: #{spec_version}")
129
128
  end
130
- data = ::JSON.parse content
129
+ data = ::JSON.parse(content)
131
130
  { data: data, content_type: content_type }
132
131
  rescue ::JSON::JSONError
133
- raise FormatSyntaxError, "JSON syntax error"
132
+ raise(FormatSyntaxError, "JSON syntax error")
134
133
  end
135
134
 
136
135
  ##
@@ -155,15 +154,15 @@ module CloudEvents
155
154
  # @return [Hash] if accepting the request.
156
155
  # @return [nil] if declining the request.
157
156
  #
158
- def encode_data spec_version: nil, data: UNSPECIFIED, content_type: nil, sort: false, **_other_kwargs
157
+ def encode_data(spec_version: nil, data: UNSPECIFIED, content_type: nil, sort: false, **_other_kwargs)
159
158
  return nil unless spec_version
160
159
  return nil if data == UNSPECIFIED
161
- return nil unless json_content_type? content_type
160
+ return nil unless json_content_type?(content_type)
162
161
  unless spec_version =~ /^0\.3|1(\.|$)/
163
- raise SpecVersionError, "Unrecognized specversion: #{spec_version}"
162
+ raise(SpecVersionError, "Unrecognized specversion: #{spec_version}")
164
163
  end
165
- data = sort_keys data if sort
166
- content = ::JSON.dump data
164
+ data = sort_keys(data) if sort
165
+ content = ::JSON.dump(data)
167
166
  { content: content, content_type: content_type }
168
167
  end
169
168
 
@@ -178,15 +177,15 @@ module CloudEvents
178
177
  # non-JSON content types.
179
178
  # @return [CloudEvents::Event]
180
179
  #
181
- def decode_hash_structure structure, charset: nil, data_decoder: nil
180
+ def decode_hash_structure(structure, charset: nil, data_decoder: nil)
182
181
  spec_version = structure["specversion"].to_s
183
182
  case spec_version
184
183
  when "0.3"
185
- decode_hash_structure_v0 structure, charset
184
+ decode_hash_structure_v0(structure, charset)
186
185
  when /^1(\.|$)/
187
- decode_hash_structure_v1 structure, charset, spec_version, data_decoder
186
+ decode_hash_structure_v1(structure, charset, spec_version, data_decoder)
188
187
  else
189
- raise SpecVersionError, "Unrecognized specversion: #{spec_version}"
188
+ raise(SpecVersionError, "Unrecognized specversion: #{spec_version}")
190
189
  end
191
190
  end
192
191
 
@@ -199,33 +198,33 @@ module CloudEvents
199
198
  # non-JSON content types.
200
199
  # @return [String] The hash structure.
201
200
  #
202
- def encode_hash_structure event, data_encoder: nil
201
+ def encode_hash_structure(event, data_encoder: nil)
203
202
  case event
204
203
  when Event::V0
205
- encode_hash_structure_v0 event
204
+ encode_hash_structure_v0(event)
206
205
  when Event::V1
207
- encode_hash_structure_v1 event, data_encoder
206
+ encode_hash_structure_v1(event, data_encoder)
208
207
  else
209
- raise SpecVersionError, "Unrecognized event type: #{event.class}"
208
+ raise(SpecVersionError, "Unrecognized event type: #{event.class}")
210
209
  end
211
210
  end
212
211
 
213
212
  private
214
213
 
215
- def json_content_type? content_type
214
+ def json_content_type?(content_type)
216
215
  content_type&.subtype_base == "json" || content_type&.subtype_format == "json"
217
216
  end
218
217
 
219
- def sort_keys obj
220
- return obj unless obj.is_a? ::Hash
218
+ def sort_keys(obj)
219
+ return obj unless obj.is_a?(::Hash)
221
220
  result = {}
222
221
  obj.keys.sort.each do |key|
223
- result[key] = sort_keys obj[key]
222
+ result[key] = sort_keys(obj[key])
224
223
  end
225
224
  result
226
225
  end
227
226
 
228
- def charset_of str
227
+ def charset_of(str)
229
228
  encoding = str.encoding
230
229
  if encoding == ::Encoding::ASCII_8BIT
231
230
  "binary"
@@ -234,29 +233,29 @@ module CloudEvents
234
233
  end
235
234
  end
236
235
 
237
- def decode_hash_structure_v0 structure, charset
238
- unless structure.key? "datacontenttype"
236
+ def decode_hash_structure_v0(structure, charset)
237
+ unless structure.key?("datacontenttype")
239
238
  structure = structure.dup
240
239
  content_type = "application/json"
241
240
  content_type = "#{content_type}; charset=#{charset}" if charset
242
241
  structure["datacontenttype"] = content_type
243
242
  end
244
- Event::V0.new attributes: structure
243
+ Event::V0.new(attributes: structure)
245
244
  end
246
245
 
247
- def decode_hash_structure_v1 structure, charset, spec_version, data_decoder
246
+ def decode_hash_structure_v1(structure, charset, spec_version, data_decoder)
248
247
  unless structure.key?("data") || structure.key?("data_base64")
249
- return Event::V1.new set_attributes: structure
248
+ return Event::V1.new(set_attributes: structure)
250
249
  end
251
250
  structure = structure.dup
252
- content, content_type = retrieve_content_from_data_fields structure, charset
253
- populate_data_fields_from_content structure, content, content_type, spec_version, data_decoder
254
- Event::V1.new set_attributes: structure
251
+ content, content_type = retrieve_content_from_data_fields(structure, charset)
252
+ populate_data_fields_from_content(structure, content, content_type, spec_version, data_decoder)
253
+ Event::V1.new(set_attributes: structure)
255
254
  end
256
255
 
257
- def retrieve_content_from_data_fields structure, charset
258
- if structure.key? "data_base64"
259
- content = ::Base64.decode64 structure.delete "data_base64"
256
+ def retrieve_content_from_data_fields(structure, charset)
257
+ if structure.key?("data_base64")
258
+ content = structure.delete("data_base64").unpack1("m")
260
259
  content_type = structure["datacontenttype"] || "application/octet-stream"
261
260
  else
262
261
  content = structure["data"]
@@ -266,60 +265,60 @@ module CloudEvents
266
265
  [content, ContentType.new(content_type)]
267
266
  end
268
267
 
269
- def populate_data_fields_from_content structure, content, content_type, spec_version, data_decoder
270
- if json_content_type? content_type
271
- structure["data_encoded"] = ::JSON.dump content
268
+ def populate_data_fields_from_content(structure, content, content_type, spec_version, data_decoder)
269
+ if json_content_type?(content_type)
270
+ structure["data_encoded"] = ::JSON.dump(content)
272
271
  structure["data"] = content
273
272
  else
274
273
  structure["data_encoded"] = content = content.to_s
275
- result = data_decoder&.decode_data spec_version: spec_version, content: content, content_type: content_type
274
+ result = data_decoder&.decode_data(spec_version: spec_version, content: content, content_type: content_type)
276
275
  if result
277
276
  structure["data"] = result[:data]
278
277
  content_type = result[:content_type]
279
278
  else
280
- structure.delete "data"
279
+ structure.delete("data")
281
280
  end
282
281
  end
283
282
  structure["datacontenttype"] = content_type
284
283
  end
285
284
 
286
- def encode_hash_structure_v0 event
285
+ def encode_hash_structure_v0(event)
287
286
  structure = event.to_h
288
287
  structure["datacontenttype"] ||= "application/json"
289
288
  structure
290
289
  end
291
290
 
292
- def encode_hash_structure_v1 event, data_encoder
291
+ def encode_hash_structure_v1(event, data_encoder)
293
292
  structure = event.to_h
294
293
  return structure unless structure.key?("data") || structure.key?("data_encoded")
295
294
  content_type = event.data_content_type
296
295
  if content_type.nil? || json_content_type?(content_type)
297
- encode_data_fields_for_json_content structure, event
296
+ encode_data_fields_for_json_content(structure, event)
298
297
  else
299
- encode_data_fields_for_other_content structure, event, data_encoder
298
+ encode_data_fields_for_other_content(structure, event, data_encoder)
300
299
  end
301
300
  structure
302
301
  end
303
302
 
304
- def encode_data_fields_for_json_content structure, event
305
- structure["data"] = ::JSON.parse event.data unless event.data_decoded?
306
- structure.delete "data_encoded"
303
+ def encode_data_fields_for_json_content(structure, event)
304
+ structure["data"] = ::JSON.parse(event.data) unless event.data_decoded?
305
+ structure.delete("data_encoded")
307
306
  structure["datacontenttype"] ||= "application/json"
308
307
  end
309
308
 
310
- def encode_data_fields_for_other_content structure, event, data_encoder
311
- data_encoded = structure.delete "data_encoded"
309
+ def encode_data_fields_for_other_content(structure, event, data_encoder)
310
+ data_encoded = structure.delete("data_encoded")
312
311
  unless data_encoded
313
- result = data_encoder&.encode_data spec_version: event.spec_version,
312
+ result = data_encoder&.encode_data(spec_version: event.spec_version,
314
313
  data: event.data,
315
- content_type: event.data_content_type
316
- raise UnsupportedFormatError, "Unable to encode data of media type #{event.data_content_type}" unless result
314
+ content_type: event.data_content_type)
315
+ raise(UnsupportedFormatError, "Unable to encode data of media type #{event.data_content_type}") unless result
317
316
  data_encoded = result[:content]
318
317
  structure["datacontenttype"] = result[:content_type].to_s
319
318
  end
320
319
  if data_encoded.encoding == ::Encoding::ASCII_8BIT
321
- structure["data_base64"] = ::Base64.encode64 data_encoded
322
- structure.delete "data"
320
+ structure["data_base64"] = [data_encoded].pack("m0")
321
+ structure.delete("data")
323
322
  else
324
323
  structure["data"] = data_encoded
325
324
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "base64"
4
3
  require "json"
5
4
 
6
5
  module CloudEvents
@@ -31,9 +30,9 @@ module CloudEvents
31
30
  # @return [Hash] if accepting the request.
32
31
  # @return [nil] if declining the request.
33
32
  #
34
- def decode_data content: nil, content_type: nil, **_other_kwargs
33
+ def decode_data(content: nil, content_type: nil, **_other_kwargs)
35
34
  return nil unless content
36
- return nil unless text_content_type? content_type
35
+ return nil unless text_content_type?(content_type)
37
36
  { data: content.to_s, content_type: content_type }
38
37
  end
39
38
 
@@ -57,17 +56,17 @@ module CloudEvents
57
56
  # @return [Hash] if accepting the request.
58
57
  # @return [nil] if declining the request.
59
58
  #
60
- def encode_data data: UNSPECIFIED, content_type: nil, **_other_kwargs
59
+ def encode_data(data: UNSPECIFIED, content_type: nil, **_other_kwargs)
61
60
  return nil if data == UNSPECIFIED
62
- return nil unless text_content_type? content_type
61
+ return nil unless text_content_type?(content_type)
63
62
  { content: data.to_s, content_type: content_type }
64
63
  end
65
64
 
66
65
  private
67
66
 
68
- def text_content_type? content_type
67
+ def text_content_type?(content_type)
69
68
  content_type&.media_type == "text" ||
70
- content_type&.media_type == "application" && content_type&.subtype == "octet-stream"
69
+ (content_type&.media_type == "application" && content_type&.subtype == "octet-stream")
71
70
  end
72
71
  end
73
72
  end
@@ -5,5 +5,5 @@ module CloudEvents
5
5
  # Version of the Ruby CloudEvents SDK
6
6
  # @return [String]
7
7
  #
8
- VERSION = "0.7.1"
8
+ VERSION = "0.8.0"
9
9
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloud_events
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-10-04 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: The official Ruby implementation of the CloudEvents Specification. Provides
14
13
  data types for events, and HTTP/JSON bindings for marshalling and unmarshalling
@@ -43,11 +42,10 @@ homepage: https://github.com/cloudevents/sdk-ruby
43
42
  licenses:
44
43
  - Apache-2.0
45
44
  metadata:
46
- changelog_uri: https://cloudevents.github.io/sdk-ruby/v0.7.1/file.CHANGELOG.html
45
+ changelog_uri: https://cloudevents.github.io/sdk-ruby/v0.8.0/file.CHANGELOG.html
47
46
  source_code_uri: https://github.com/cloudevents/sdk-ruby
48
47
  bug_tracker_uri: https://github.com/cloudevents/sdk-ruby/issues
49
- documentation_uri: https://cloudevents.github.io/sdk-ruby/v0.7.1
50
- post_install_message:
48
+ documentation_uri: https://cloudevents.github.io/sdk-ruby/v0.8.0
51
49
  rdoc_options: []
52
50
  require_paths:
53
51
  - lib
@@ -55,15 +53,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
53
  requirements:
56
54
  - - ">="
57
55
  - !ruby/object:Gem::Version
58
- version: '2.5'
56
+ version: '2.7'
59
57
  required_rubygems_version: !ruby/object:Gem::Requirement
60
58
  requirements:
61
59
  - - ">="
62
60
  - !ruby/object:Gem::Version
63
61
  version: '0'
64
62
  requirements: []
65
- rubygems_version: 3.1.6
66
- signing_key:
63
+ rubygems_version: 3.6.9
67
64
  specification_version: 4
68
65
  summary: Ruby SDK for CloudEvents
69
66
  test_files: []