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
71
72
73
74
|
# File 'lib/fdk/runner.rb', line 9
def self.handle(func)
format = ENV['FN_FORMAT']
if format == "json"
obs = ""
endbracket=false
STDIN.each do |line|
ls = line.strip
if ls == "}"
endbracket=true
elsif ls == "" && endbracket
payload = JSON.parse(obs)
c = Context.new(payload)
body = payload['body']
if c.content_type == 'application/json'
body = JSON.parse(body)
end
s = FDK.single_event(func, c, body)
response = {
headers: {
'Content-Type' => 'application/json'
},
'status_code' => 200,
body: s.to_json,
}
STDOUT.puts response.to_json
STDOUT.puts
STDOUT.flush
obs = ""
next
else
endbracket = false
end
obs += line
end
elsif format == "default"
body = STDIN.read
payload = {}
payload['call_id'] = ENV['FN_CALL_ID']
payload['content_type'] = ENV['FN_HEADER_Content_Type']
payload['protocol'] = {
'type' => 'http',
'request_url' => ENV['FN_REQUEST_URL']
}
c = Context.new(payload)
if c.content_type == "application/json"
body = JSON.parse(body)
end
puts FDK.single_event(func, c, body).to_json
else
raise "Format #{format} not supported in Ruby FDK."
end
end
|