Class: FunctionsFramework::CloudEvents::JsonFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/functions_framework/cloud_events/json_format.rb

Overview

An implementation of JSON format and JSON batch format.

Supports the CloudEvents 0.3 and CloudEvents 1.0 variants of this format. See https://github.com/cloudevents/spec/blob/v0.3/json-format.md and https://github.com/cloudevents/spec/blob/v1.0/json-format.md.

Instance Method Summary collapse

Instance Method Details

#decode(json, **_other_kwargs) ⇒ FunctionsFramework::CloudEvents::Event

Decode an event from the given input JSON string.

Parameters:

  • json (String)

    A JSON-formatted string

Returns:



34
35
36
37
# File 'lib/functions_framework/cloud_events/json_format.rb', line 34

def decode json, **_other_kwargs
  structure = ::JSON.parse json
  decode_hash_structure structure
end

#decode_batch(json, **_other_kwargs) ⇒ Array<FunctionsFramework::CloudEvents::Event>

Decode a batch of events from the given input string.

Parameters:

  • json (String)

    A JSON-formatted string

Returns:



58
59
60
61
62
63
# File 'lib/functions_framework/cloud_events/json_format.rb', line 58

def decode_batch json, **_other_kwargs
  structure_array = Array(::JSON.parse(json))
  structure_array.map do |structure|
    decode_hash_structure structure
  end
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 envelope.

Parameters:

  • structure (Hash)

    An input hash.

Returns:



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/functions_framework/cloud_events/json_format.rb', line 88

def decode_hash_structure structure
  spec_version = structure["specversion"].to_s
  case spec_version
  when "0.3"
    decode_hash_structure_v0 structure
  when /^1(\.|$)/
    decode_hash_structure_v1 structure
  else
    raise SpecVersionError, "Unrecognized specversion: #{spec_version}"
  end
end

#encode(event, sort: false, **_other_kwargs) ⇒ String

Encode an event to a JSON string.

Parameters:

Returns:

  • (String)

    The JSON representation.



46
47
48
49
50
# File 'lib/functions_framework/cloud_events/json_format.rb', line 46

def encode event, sort: false, **_other_kwargs
  structure = encode_hash_structure event
  structure = sort_keys structure if sort
  ::JSON.dump structure
end

#encode_batch(events, sort: false, **_other_kwargs) ⇒ String

Encode a batch of event to a JSON string.

Parameters:

Returns:

  • (String)

    The JSON representation.



73
74
75
76
77
78
79
# File 'lib/functions_framework/cloud_events/json_format.rb', line 73

def encode_batch events, sort: false, **_other_kwargs
  structure_array = Array(events).map do |event|
    structure = encode_hash_structure event
    sort ? sort_keys(structure) : structure
  end
  ::JSON.dump structure_array
end

#encode_hash_structure(event) ⇒ String

Encode a single event to a hash data structure with keys and types conforming to the JSON envelope.

Parameters:

Returns:

  • (String)

    The hash structure.



107
108
109
110
111
112
113
114
115
116
# File 'lib/functions_framework/cloud_events/json_format.rb', line 107

def encode_hash_structure event
  case event
  when Event::V0
    encode_hash_structure_v0 event
  when Event::V1
    encode_hash_structure_v1 event
  else
    raise SpecVersionError, "Unrecognized specversion: #{event.spec_version}"
  end
end