9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/fdk/runner.rb', line 9
def self.handle(function, input_stream: STDIN, output_stream: STDOUT)
format = ENV['FN_FORMAT']
if format == 'cloudevent'
parser = Yajl::Parser.new
parser.on_parse_complete = lambda do |event|
context = Context.new(event)
body = event['data']
se = FDK.single_event(function: function, context: context, input: body)
event['data'] = se
event['extensions']['protocol'] = {
headers: {
'Content-Type' => ['application/json']
},
'status_code' => 200
}
output_stream.puts event.to_json
output_stream.puts
output_stream.flush
end
input_stream.each_line { |line| parser.parse_chunk(line) }
elsif format == 'json'
parser = Yajl::Parser.new
parser.on_parse_complete = lambda do |event|
context = Context.new(event)
body = event['body']
if context.content_type == 'application/json' && body != ''
body = Yajl::Parser.parse(body)
end
se = FDK.single_event(function: function, context: context, input: body)
response = {
headers: {
'Content-Type' => ['application/json']
},
'status_code' => 200,
body: se.to_json
}
output_stream.puts response.to_json
output_stream.puts
output_stream.flush
end
input_stream.each_line { |line| parser.parse_chunk(line) }
elsif format == 'default'
event = {}
event['call_id'] = ENV['FN_CALL_ID']
event['protocol'] = {
'type' => 'http',
'request_url' => ENV['FN_REQUEST_URL']
}
output_stream.puts FDK.single_event(function: function, context: Context.new(event), input: input_stream.read.chomp).to_json
else
raise "Format '#{format}' not supported in Ruby FDK."
end
end
|