functions_framework 0.1.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +6 -2
- data/CHANGELOG.md +44 -0
- data/README.md +56 -136
- data/bin/functions-framework-ruby +19 -0
- data/docs/deploying-functions.md +182 -0
- data/docs/overview.md +142 -0
- data/docs/running-a-functions-server.md +122 -0
- data/docs/testing-functions.md +169 -0
- data/docs/writing-functions.md +275 -0
- data/lib/functions_framework.rb +16 -50
- data/lib/functions_framework/cli.rb +71 -13
- data/lib/functions_framework/cloud_events.rb +12 -110
- data/lib/functions_framework/cloud_events/content_type.rb +107 -30
- data/lib/functions_framework/cloud_events/errors.rb +42 -0
- data/lib/functions_framework/cloud_events/event.rb +56 -249
- data/lib/functions_framework/cloud_events/event/field_interpreter.rb +150 -0
- data/lib/functions_framework/cloud_events/event/v0.rb +236 -0
- data/lib/functions_framework/cloud_events/event/v1.rb +223 -0
- data/lib/functions_framework/cloud_events/http_binding.rb +310 -0
- data/lib/functions_framework/cloud_events/json_format.rb +173 -0
- data/lib/functions_framework/function.rb +80 -26
- data/lib/functions_framework/legacy_event_converter.rb +145 -0
- data/lib/functions_framework/registry.rb +0 -39
- data/lib/functions_framework/server.rb +61 -51
- data/lib/functions_framework/testing.rb +64 -24
- data/lib/functions_framework/version.rb +1 -1
- metadata +16 -4
- data/lib/functions_framework/cloud_events/binary_content.rb +0 -59
- data/lib/functions_framework/cloud_events/json_structure.rb +0 -88
@@ -0,0 +1,236 @@
|
|
1
|
+
# Copyright 2020 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require "date"
|
16
|
+
require "uri"
|
17
|
+
|
18
|
+
module FunctionsFramework
|
19
|
+
module CloudEvents
|
20
|
+
module Event
|
21
|
+
##
|
22
|
+
# A CloudEvents V0 data type.
|
23
|
+
#
|
24
|
+
# This object represents a complete CloudEvent, including the event data
|
25
|
+
# and context attributes. It supports the standard required and optional
|
26
|
+
# attributes defined in CloudEvents V0.3, and arbitrary extension
|
27
|
+
# attributes. All attribute values can be obtained (in their string form)
|
28
|
+
# via the {Event::V0#[]} method. Additionally, standard attributes have
|
29
|
+
# their own accessor methods that may return typed objects (such as
|
30
|
+
# `DateTime` for the `time` attribute).
|
31
|
+
#
|
32
|
+
# This object is immutable. The data and attribute values can be
|
33
|
+
# retrieved but not modified. To obtain an event with modifications, use
|
34
|
+
# the {#with} method to create a copy with the desired changes.
|
35
|
+
#
|
36
|
+
# See https://github.com/cloudevents/spec/blob/v0.3/spec.md for
|
37
|
+
# descriptions of the standard attributes.
|
38
|
+
#
|
39
|
+
class V0
|
40
|
+
include Event
|
41
|
+
|
42
|
+
##
|
43
|
+
# Create a new cloud event object with the given data and attributes.
|
44
|
+
#
|
45
|
+
# Event attributes may be presented as keyword arguments, or as a Hash
|
46
|
+
# passed in via the `attributes` argument (but not both).
|
47
|
+
#
|
48
|
+
# The following standard attributes are supported and exposed as
|
49
|
+
# attribute methods on the object.
|
50
|
+
#
|
51
|
+
# * **:spec_version** (or **:specversion**) [`String`] - _required_ -
|
52
|
+
# The CloudEvents spec version (i.e. the `specversion` field.)
|
53
|
+
# * **:id** [`String`] - _required_ - The event `id` field.
|
54
|
+
# * **:source** [`String`, `URI`] - _required_ - The event `source`
|
55
|
+
# field.
|
56
|
+
# * **:type** [`String`] - _required_ - The event `type` field.
|
57
|
+
# * **:data** [`Object`] - _optional_ - The data associated with the
|
58
|
+
# event (i.e. the `data` field.)
|
59
|
+
# * **:data_content_encoding** (or **:datacontentencoding**)
|
60
|
+
# [`String`] - _optional_ - The content-encoding for the data (i.e.
|
61
|
+
# the `datacontentencoding` field.)
|
62
|
+
# * **:data_content_type** (or **:datacontenttype**) [`String`,
|
63
|
+
# {ContentType}] - _optional_ - The content-type for the data, if
|
64
|
+
# the data is a string (i.e. the event `datacontenttype` field.)
|
65
|
+
# * **:schema_url** (or **:schemaurl**) [`String`, `URI`] -
|
66
|
+
# _optional_ - The event `schemaurl` field.
|
67
|
+
# * **:subject** [`String`] - _optional_ - The event `subject` field.
|
68
|
+
# * **:time** [`String`, `DateTime`, `Time`] - _optional_ - The
|
69
|
+
# event `time` field.
|
70
|
+
#
|
71
|
+
# Any additional attributes are assumed to be extension attributes.
|
72
|
+
# They are not available as separate methods, but can be accessed via
|
73
|
+
# the {Event::V1#[]} operator.
|
74
|
+
#
|
75
|
+
# @param attributes [Hash] The data and attributes, as a hash.
|
76
|
+
# @param args [keywords] The data and attributes, as keyword arguments.
|
77
|
+
#
|
78
|
+
def initialize attributes: nil, **args
|
79
|
+
interpreter = FieldInterpreter.new attributes || args
|
80
|
+
@spec_version = interpreter.spec_version ["specversion", "spec_version"], accept: /^0\.3$/
|
81
|
+
@id = interpreter.string ["id"], required: true
|
82
|
+
@source = interpreter.uri ["source"], required: true
|
83
|
+
@type = interpreter.string ["type"], required: true
|
84
|
+
@data = interpreter.object ["data"], allow_nil: true
|
85
|
+
@data_content_encoding = interpreter.string ["datacontentencoding", "data_content_encoding"]
|
86
|
+
@data_content_type = interpreter.content_type ["datacontenttype", "data_content_type"]
|
87
|
+
@schema_url = interpreter.uri ["schemaurl", "schema_url"]
|
88
|
+
@subject = interpreter.string ["subject"]
|
89
|
+
@time = interpreter.rfc3339_date_time ["time"]
|
90
|
+
@attributes = interpreter.finish_attributes
|
91
|
+
end
|
92
|
+
|
93
|
+
##
|
94
|
+
# Create and return a copy of this event with the given changes. See
|
95
|
+
# the constructor for the parameters that can be passed. In general,
|
96
|
+
# you can pass a new value for any attribute, or pass `nil` to remove
|
97
|
+
# an optional attribute.
|
98
|
+
#
|
99
|
+
# @param changes [keywords] See {#initialize} for a list of arguments.
|
100
|
+
# @return [FunctionFramework::CloudEvents::Event]
|
101
|
+
#
|
102
|
+
def with **changes
|
103
|
+
attributes = @attributes.merge changes
|
104
|
+
V0.new attributes: attributes
|
105
|
+
end
|
106
|
+
|
107
|
+
##
|
108
|
+
# Return the value of the given named attribute. Both standard and
|
109
|
+
# extension attributes are supported.
|
110
|
+
#
|
111
|
+
# Attribute names must be given as defined in the standard CloudEvents
|
112
|
+
# specification. For example `specversion` rather than `spec_version`.
|
113
|
+
#
|
114
|
+
# Results are given in their "raw" form, generally a string. This may
|
115
|
+
# be different from the Ruby object returned from corresponding
|
116
|
+
# attribute methods. For example:
|
117
|
+
#
|
118
|
+
# event["time"] # => String rfc3339 representation
|
119
|
+
# event.time # => DateTime object
|
120
|
+
#
|
121
|
+
# @param key [String,Symbol] The attribute name.
|
122
|
+
# @return [String,nil]
|
123
|
+
#
|
124
|
+
def [] key
|
125
|
+
@attributes[key.to_s]
|
126
|
+
end
|
127
|
+
|
128
|
+
##
|
129
|
+
# Return a hash representation of this event.
|
130
|
+
#
|
131
|
+
# @return [Hash]
|
132
|
+
#
|
133
|
+
def to_h
|
134
|
+
@attributes.dup
|
135
|
+
end
|
136
|
+
|
137
|
+
##
|
138
|
+
# The `id` field. Required.
|
139
|
+
#
|
140
|
+
# @return [String]
|
141
|
+
#
|
142
|
+
attr_reader :id
|
143
|
+
|
144
|
+
##
|
145
|
+
# The `source` field as a `URI` object. Required.
|
146
|
+
#
|
147
|
+
# @return [URI]
|
148
|
+
#
|
149
|
+
attr_reader :source
|
150
|
+
|
151
|
+
##
|
152
|
+
# The `type` field. Required.
|
153
|
+
#
|
154
|
+
# @return [String]
|
155
|
+
#
|
156
|
+
attr_reader :type
|
157
|
+
|
158
|
+
##
|
159
|
+
# The `specversion` field. Required.
|
160
|
+
#
|
161
|
+
# @return [String]
|
162
|
+
#
|
163
|
+
attr_reader :spec_version
|
164
|
+
alias specversion spec_version
|
165
|
+
|
166
|
+
##
|
167
|
+
# The event-specific data, or `nil` if there is no data.
|
168
|
+
#
|
169
|
+
# Data may be one of the following types:
|
170
|
+
# * Binary data, represented by a `String` using the `ASCII-8BIT`
|
171
|
+
# encoding.
|
172
|
+
# * A string in some other encoding such as `UTF-8` or `US-ASCII`.
|
173
|
+
# * Any JSON data type, such as a Boolean, Integer, Array, Hash, or
|
174
|
+
# `nil`.
|
175
|
+
#
|
176
|
+
# @return [Object]
|
177
|
+
#
|
178
|
+
attr_reader :data
|
179
|
+
|
180
|
+
##
|
181
|
+
# The optional `datacontentencoding` field as a `String` object, or
|
182
|
+
# `nil` if the field is absent.
|
183
|
+
#
|
184
|
+
# @return [String,nil]
|
185
|
+
#
|
186
|
+
attr_reader :data_content_encoding
|
187
|
+
alias datacontentencoding data_content_encoding
|
188
|
+
|
189
|
+
##
|
190
|
+
# The optional `datacontenttype` field as a
|
191
|
+
# {FunctionsFramework::CloudEvents::ContentType} object, or `nil` if
|
192
|
+
# the field is absent.
|
193
|
+
#
|
194
|
+
# @return [FunctionsFramework::CloudEvents::ContentType,nil]
|
195
|
+
#
|
196
|
+
attr_reader :data_content_type
|
197
|
+
alias datacontenttype data_content_type
|
198
|
+
|
199
|
+
##
|
200
|
+
# The optional `schemaurl` field as a `URI` object, or `nil` if the
|
201
|
+
# field is absent.
|
202
|
+
#
|
203
|
+
# @return [URI,nil]
|
204
|
+
#
|
205
|
+
attr_reader :schema_url
|
206
|
+
alias schemaurl schema_url
|
207
|
+
|
208
|
+
##
|
209
|
+
# The optional `subject` field, or `nil` if the field is absent.
|
210
|
+
#
|
211
|
+
# @return [String,nil]
|
212
|
+
#
|
213
|
+
attr_reader :subject
|
214
|
+
|
215
|
+
##
|
216
|
+
# The optional `time` field as a `DateTime` object, or `nil` if the
|
217
|
+
# field is absent.
|
218
|
+
#
|
219
|
+
# @return [DateTime,nil]
|
220
|
+
#
|
221
|
+
attr_reader :time
|
222
|
+
|
223
|
+
## @private
|
224
|
+
def == other
|
225
|
+
other.is_a?(V1) && @attributes == other.instance_variable_get(:@attributes)
|
226
|
+
end
|
227
|
+
alias eql? ==
|
228
|
+
|
229
|
+
## @private
|
230
|
+
def hash
|
231
|
+
@hash ||= @attributes.hash
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
@@ -0,0 +1,223 @@
|
|
1
|
+
# Copyright 2020 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require "date"
|
16
|
+
require "uri"
|
17
|
+
|
18
|
+
module FunctionsFramework
|
19
|
+
module CloudEvents
|
20
|
+
module Event
|
21
|
+
##
|
22
|
+
# A CloudEvents V1 data type.
|
23
|
+
#
|
24
|
+
# This object represents a complete CloudEvent, including the event data
|
25
|
+
# and context attributes. It supports the standard required and optional
|
26
|
+
# attributes defined in CloudEvents V1.0, and arbitrary extension
|
27
|
+
# attributes. All attribute values can be obtained (in their string form)
|
28
|
+
# via the {Event::V1#[]} method. Additionally, standard attributes have
|
29
|
+
# their own accessor methods that may return typed objects (such as
|
30
|
+
# `DateTime` for the `time` attribute).
|
31
|
+
#
|
32
|
+
# This object is immutable. The data and attribute values can be
|
33
|
+
# retrieved but not modified. To obtain an event with modifications, use
|
34
|
+
# the {#with} method to create a copy with the desired changes.
|
35
|
+
#
|
36
|
+
# See https://github.com/cloudevents/spec/blob/v1.0/spec.md for
|
37
|
+
# descriptions of the standard attributes.
|
38
|
+
#
|
39
|
+
class V1
|
40
|
+
include Event
|
41
|
+
|
42
|
+
##
|
43
|
+
# Create a new cloud event object with the given data and attributes.
|
44
|
+
#
|
45
|
+
# Event attributes may be presented as keyword arguments, or as a Hash
|
46
|
+
# passed in via the `attributes` argument (but not both).
|
47
|
+
#
|
48
|
+
# The following standard attributes are supported and exposed as
|
49
|
+
# attribute methods on the object.
|
50
|
+
#
|
51
|
+
# * **:spec_version** (or **:specversion**) [`String`] - _required_ -
|
52
|
+
# The CloudEvents spec version (i.e. the `specversion` field.)
|
53
|
+
# * **:id** [`String`] - _required_ - The event `id` field.
|
54
|
+
# * **:source** [`String`, `URI`] - _required_ - The event `source`
|
55
|
+
# field.
|
56
|
+
# * **:type** [`String`] - _required_ - The event `type` field.
|
57
|
+
# * **:data** [`Object`] - _optional_ - The data associated with the
|
58
|
+
# event (i.e. the `data` field.)
|
59
|
+
# * **:data_content_type** (or **:datacontenttype**) [`String`,
|
60
|
+
# {ContentType}] - _optional_ - The content-type for the data, if
|
61
|
+
# the data is a string (i.e. the event `datacontenttype` field.)
|
62
|
+
# * **:data_schema** (or **:dataschema**) [`String`, `URI`] -
|
63
|
+
# _optional_ - The event `dataschema` field.
|
64
|
+
# * **:subject** [`String`] - _optional_ - The event `subject` field.
|
65
|
+
# * **:time** [`String`, `DateTime`, `Time`] - _optional_ - The
|
66
|
+
# event `time` field.
|
67
|
+
#
|
68
|
+
# Any additional attributes are assumed to be extension attributes.
|
69
|
+
# They are not available as separate methods, but can be accessed via
|
70
|
+
# the {Event::V1#[]} operator.
|
71
|
+
#
|
72
|
+
# @param attributes [Hash] The data and attributes, as a hash.
|
73
|
+
# @param args [keywords] The data and attributes, as keyword arguments.
|
74
|
+
#
|
75
|
+
def initialize attributes: nil, **args
|
76
|
+
interpreter = FieldInterpreter.new attributes || args
|
77
|
+
@spec_version = interpreter.spec_version ["specversion", "spec_version"], accept: /^1(\.|$)/
|
78
|
+
@id = interpreter.string ["id"], required: true
|
79
|
+
@source = interpreter.uri ["source"], required: true
|
80
|
+
@type = interpreter.string ["type"], required: true
|
81
|
+
@data = interpreter.object ["data"], allow_nil: true
|
82
|
+
@data_content_type = interpreter.content_type ["datacontenttype", "data_content_type"]
|
83
|
+
@data_schema = interpreter.uri ["dataschema", "data_schema"]
|
84
|
+
@subject = interpreter.string ["subject"]
|
85
|
+
@time = interpreter.rfc3339_date_time ["time"]
|
86
|
+
@attributes = interpreter.finish_attributes
|
87
|
+
end
|
88
|
+
|
89
|
+
##
|
90
|
+
# Create and return a copy of this event with the given changes. See
|
91
|
+
# the constructor for the parameters that can be passed. In general,
|
92
|
+
# you can pass a new value for any attribute, or pass `nil` to remove
|
93
|
+
# an optional attribute.
|
94
|
+
#
|
95
|
+
# @param changes [keywords] See {#initialize} for a list of arguments.
|
96
|
+
# @return [FunctionFramework::CloudEvents::Event]
|
97
|
+
#
|
98
|
+
def with **changes
|
99
|
+
attributes = @attributes.merge changes
|
100
|
+
V1.new attributes: attributes
|
101
|
+
end
|
102
|
+
|
103
|
+
##
|
104
|
+
# Return the value of the given named attribute. Both standard and
|
105
|
+
# extension attributes are supported.
|
106
|
+
#
|
107
|
+
# Attribute names must be given as defined in the standard CloudEvents
|
108
|
+
# specification. For example `specversion` rather than `spec_version`.
|
109
|
+
#
|
110
|
+
# Results are given in their "raw" form, generally a string. This may
|
111
|
+
# be different from the Ruby object returned from corresponding
|
112
|
+
# attribute methods. For example:
|
113
|
+
#
|
114
|
+
# event["time"] # => String rfc3339 representation
|
115
|
+
# event.time # => DateTime object
|
116
|
+
#
|
117
|
+
# @param key [String,Symbol] The attribute name.
|
118
|
+
# @return [String,nil]
|
119
|
+
#
|
120
|
+
def [] key
|
121
|
+
@attributes[key.to_s]
|
122
|
+
end
|
123
|
+
|
124
|
+
##
|
125
|
+
# Return a hash representation of this event.
|
126
|
+
#
|
127
|
+
# @return [Hash]
|
128
|
+
#
|
129
|
+
def to_h
|
130
|
+
@attributes.dup
|
131
|
+
end
|
132
|
+
|
133
|
+
##
|
134
|
+
# The `id` field. Required.
|
135
|
+
#
|
136
|
+
# @return [String]
|
137
|
+
#
|
138
|
+
attr_reader :id
|
139
|
+
|
140
|
+
##
|
141
|
+
# The `source` field as a `URI` object. Required.
|
142
|
+
#
|
143
|
+
# @return [URI]
|
144
|
+
#
|
145
|
+
attr_reader :source
|
146
|
+
|
147
|
+
##
|
148
|
+
# The `type` field. Required.
|
149
|
+
#
|
150
|
+
# @return [String]
|
151
|
+
#
|
152
|
+
attr_reader :type
|
153
|
+
|
154
|
+
##
|
155
|
+
# The `specversion` field. Required.
|
156
|
+
#
|
157
|
+
# @return [String]
|
158
|
+
#
|
159
|
+
attr_reader :spec_version
|
160
|
+
alias specversion spec_version
|
161
|
+
|
162
|
+
##
|
163
|
+
# The event-specific data, or `nil` if there is no data.
|
164
|
+
#
|
165
|
+
# Data may be one of the following types:
|
166
|
+
# * Binary data, represented by a `String` using the `ASCII-8BIT`
|
167
|
+
# encoding.
|
168
|
+
# * A string in some other encoding such as `UTF-8` or `US-ASCII`.
|
169
|
+
# * Any JSON data type, such as a Boolean, Integer, Array, Hash, or
|
170
|
+
# `nil`.
|
171
|
+
#
|
172
|
+
# @return [Object]
|
173
|
+
#
|
174
|
+
attr_reader :data
|
175
|
+
|
176
|
+
##
|
177
|
+
# The optional `datacontenttype` field as a
|
178
|
+
# {FunctionsFramework::CloudEvents::ContentType} object, or `nil` if
|
179
|
+
# the field is absent.
|
180
|
+
#
|
181
|
+
# @return [FunctionsFramework::CloudEvents::ContentType,nil]
|
182
|
+
#
|
183
|
+
attr_reader :data_content_type
|
184
|
+
alias datacontenttype data_content_type
|
185
|
+
|
186
|
+
##
|
187
|
+
# The optional `dataschema` field as a `URI` object, or `nil` if the
|
188
|
+
# field is absent.
|
189
|
+
#
|
190
|
+
# @return [URI,nil]
|
191
|
+
#
|
192
|
+
attr_reader :data_schema
|
193
|
+
alias dataschema data_schema
|
194
|
+
|
195
|
+
##
|
196
|
+
# The optional `subject` field, or `nil` if the field is absent.
|
197
|
+
#
|
198
|
+
# @return [String,nil]
|
199
|
+
#
|
200
|
+
attr_reader :subject
|
201
|
+
|
202
|
+
##
|
203
|
+
# The optional `time` field as a `DateTime` object, or `nil` if the
|
204
|
+
# field is absent.
|
205
|
+
#
|
206
|
+
# @return [DateTime,nil]
|
207
|
+
#
|
208
|
+
attr_reader :time
|
209
|
+
|
210
|
+
## @private
|
211
|
+
def == other
|
212
|
+
other.is_a?(V1) && @attributes == other.instance_variable_get(:@attributes)
|
213
|
+
end
|
214
|
+
alias eql? ==
|
215
|
+
|
216
|
+
## @private
|
217
|
+
def hash
|
218
|
+
@hash ||= @attributes.hash
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|