Module: FDK

Defined in:
lib/fdk/runner.rb,
lib/fdk/context.rb,
lib/fdk/version.rb

Defined Under Namespace

Classes: Config, Context

Constant Summary collapse

VERSION =
"0.0.7"

Class Method Summary collapse

Class Method Details

.handle(func) ⇒ Object



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)
    # STDERR.puts `env`
    format = ENV['FN_FORMAT']
    if format == "json"
        obs = ""
        endbracket=false
        STDIN.each do |line|
            # STDERR.puts "LINE: --#{line}--"
            # STDERR.flush
            ls = line.strip
            # STDERR.puts "ls: #{ls}"
            if ls == "}"
                # STDERR.puts "endbracket true"
                endbracket=true
            elsif ls == "" && endbracket
                # TODO: this isn't very robust, probably needs to be better :/
                # STDERR.puts "OBJECT: #{obs}"
                payload = JSON.parse(obs)
                # STDERR.puts "payload: #{payload.inspect}"
                c = Context.new(payload)
                # STDERR.puts "context: " + c.inspect
                # STDERR.flush
                body = payload['body']
                if c.content_type == 'application/json'
                    body = JSON.parse(body)
                end
                # TODO: begin/rescue so we can respond with proper error response and code
                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"
        # TODO: check if content type json, and if so, parse it before passing it in
        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']
        }
        # STDERR.puts "payload: #{payload}"
        c = Context.new(payload)
        if c.content_type == "application/json"
            # STDERR.puts "parsing 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

.single_event(func, c, i) ⇒ Object



76
77
78
79
# File 'lib/fdk/runner.rb', line 76

def self.single_event(func, c, i)
    s = send func, c, i
    return s
end