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.

See https://github.com/cloudevents/spec/blob/master/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:



32
33
34
35
# File 'lib/functions_framework/cloud_events/json_format.rb', line 32

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:



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

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 event format.

Parameters:

  • structure (Hash)

    An input hash.

Returns:



86
87
88
89
90
91
92
# File 'lib/functions_framework/cloud_events/json_format.rb', line 86

def decode_hash_structure structure
  if structure.key? "data_base64"
    structure = structure.dup
    structure["data"] = ::Base64.decode64 structure.delete "data_base64"
  end
  Event.create spec_version: structure["specversion"], attributes: structure
end

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

Encode an event to a JSON string.

Parameters:

Returns:

  • (String)

    The JSON representation.



44
45
46
47
48
# File 'lib/functions_framework/cloud_events/json_format.rb', line 44

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.



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

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 event format.

Parameters:

Returns:

  • (String)

    The hash structure.



101
102
103
104
105
106
107
108
109
# File 'lib/functions_framework/cloud_events/json_format.rb', line 101

def encode_hash_structure event
  structure = event.to_h
  data = structure["data"]
  if data.is_a?(::String) && data.encoding == ::Encoding::ASCII_8BIT
    structure.delete "data"
    structure["data_base64"] = ::Base64.encode64 data
  end
  structure
end