Module: FunctionsFramework::CloudEvents
- Defined in:
- lib/functions_framework/cloud_events.rb,
lib/functions_framework/cloud_events/event.rb,
lib/functions_framework/cloud_events/content_type.rb,
lib/functions_framework/cloud_events/binary_content.rb,
lib/functions_framework/cloud_events/json_structure.rb
Overview
CloudEvents implementation.
This is a Ruby implementation of the CloudEvents 1.0 specification. It provides for unmarshaling of events from Rack environment data from binary (i.e. header-based) format, as well as structured (body-based) and batch formats. A standard JSON structure parser is included. It is also possible to register handlers for other formats.
TODO: Unmarshaling of events is implemented, but marshaling is not.
Defined Under Namespace
Modules: BinaryContent, JsonStructure Classes: ContentType, Event
Class Method Summary collapse
-
.decode_batched_content(input, content_type) ⇒ Array<FunctionsFramework::CloudEvents::Event>
Decode a batch of events from the given content data.
-
.decode_rack_env(env) ⇒ FunctionsFramework::CloudEvents::Event+
Decode an event from the given Rack environment hash.
-
.decode_structured_content(input, content_type) ⇒ FunctionsFramework::CloudEvents::Event
Decode a single event from the given content data.
-
.register_batched_format(format, handler) ⇒ self
Register a handler for the given batched format.
-
.register_structured_format(format, handler) ⇒ self
Register a handler for the given structured format.
Class Method Details
.decode_batched_content(input, content_type) ⇒ Array<FunctionsFramework::CloudEvents::Event>
Decode a batch of events from the given content data. This should be
passed the request body, if the Content-Type is of the form
application/cloudevents-batch+format.
126 127 128 129 130 131 132 133 |
# File 'lib/functions_framework/cloud_events.rb', line 126 def decode_batched_content input, content_type handlers = @batched_formats[content_type.subtype_format] || [] handlers.reverse_each do |handler| events = handler.decode_batched_content input, content_type return events if events end raise "Unknown cloudevents batch format: #{content_type.subtype_format.inspect}" end |
.decode_rack_env(env) ⇒ FunctionsFramework::CloudEvents::Event+
Decode an event from the given Rack environment hash. Following the CloudEvents spec, this chooses a handler based on the Content-Type of the request.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/functions_framework/cloud_events.rb', line 82 def decode_rack_env env content_type_header = env["CONTENT_TYPE"] raise "Missing content-type header" unless content_type_header content_type = ContentType.new content_type_header if content_type.media_type == "application" case content_type.subtype_prefix when "cloudevents" return decode_structured_content env["rack.input"], content_type when "cloudevents-batch" return decode_batched_content env["rack.input"], content_type end end BinaryContent.decode_rack_env env, content_type end |
.decode_structured_content(input, content_type) ⇒ FunctionsFramework::CloudEvents::Event
Decode a single event from the given content data. This should be
passed the request body, if the Content-Type is of the form
application/cloudevents+format.
107 108 109 110 111 112 113 114 |
# File 'lib/functions_framework/cloud_events.rb', line 107 def decode_structured_content input, content_type handlers = @structured_formats[content_type.subtype_format] || [] handlers.reverse_each do |handler| event = handler.decode_structured_content input, content_type return event if event end raise "Unknown cloudevents format: #{content_type.subtype_format.inspect}" end |
.register_batched_format(format, handler) ⇒ self
Register a handler for the given batched format.
The handler object must respond to the method
#decode_batched_content. See
JsonStructure for an example.
65 66 67 68 69 |
# File 'lib/functions_framework/cloud_events.rb', line 65 def register_batched_format format, handler handlers = @batched_formats[format.to_s.strip.downcase] ||= [] handlers << handler unless handlers.include? handler self end |
.register_structured_format(format, handler) ⇒ self
Register a handler for the given structured format.
The handler object must respond to the method
#decode_structured_content. See
JsonStructure for an example.
48 49 50 51 52 |
# File 'lib/functions_framework/cloud_events.rb', line 48 def register_structured_format format, handler handlers = @structured_formats[format.to_s.strip.downcase] ||= [] handlers << handler unless handlers.include? handler self end |