Module: FunctionsFramework::CloudEvents::JsonStructure

Defined in:
lib/functions_framework/cloud_events/json_structure.rb

Overview

A content handler for the JSON structure and JSON batch format. See https://github.com/cloudevents/spec/blob/master/json-format.md

Class Method Summary collapse

Class Method Details

.decode_batched_content(input, content_type) ⇒ Array<FunctionsFramework::CloudEvents::Event>

Decode a batch of events from the given input string

Parameters:

Returns:



50
51
52
53
54
55
56
# File 'lib/functions_framework/cloud_events/json_structure.rb', line 50

def decode_batched_content input, content_type
  input = input.read if input.respond_to? :read
  charset = content_type.charset
  input = input.encode charset if charset
  structure_array = Array(::JSON.parse(input))
  structure_array.map { |structure| decode_hash_structure structure }
end

.decode_hash_structure(structure) ⇒ FunctionsFramework::CloudEvents::Event

Decode a single event from a hash data structure with keys and types conforming to the JSON event format

Parameters:

  • structure (Hash)

    Input hash

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/functions_framework/cloud_events/json_structure.rb', line 65

def decode_hash_structure structure
  data =
    if structure.key? "data_base64"
      ::Base64.decode64 structure["data_base64"]
    else
      structure["data"]
    end
  spec_version = structure["specversion"]
  raise "Unrecognized specversion: #{spec_version}" unless spec_version == "1.0"
  Event.new \
    id:                structure["id"],
    source:            structure["source"],
    type:              structure["type"],
    spec_version:      spec_version,
    data:              data,
    data_content_type: structure["datacontenttype"],
    data_schema:       structure["dataschema"],
    subject:           structure["subject"],
    time:              structure["time"]
end

.decode_structured_content(input, content_type) ⇒ FunctionsFramework::CloudEvents::Event

Decode an event from the given input string

Parameters:

Returns:



34
35
36
37
38
39
40
# File 'lib/functions_framework/cloud_events/json_structure.rb', line 34

def decode_structured_content input, content_type
  input = input.read if input.respond_to? :read
  charset = content_type.charset
  input = input.encode charset if charset
  structure = ::JSON.parse input
  decode_hash_structure structure
end