fdk 0.0.1 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -4
- data/lib/fdk/context.rb +31 -19
- data/lib/fdk/runner.rb +67 -12
- data/lib/fdk/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 889ac354ba75007c3c66504348de355f1e9df299
|
4
|
+
data.tar.gz: 57a526258372f03fb5277e6f0b8614879a729d5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e34f13c1f3296853605b3331d9154904dce1dbfc3c7c12fe8256246a9e7715d7b340cbb8ba1877a0c714544f03a3ddc0cc7998f109589760604d16610b4f59c
|
7
|
+
data.tar.gz: 37a51d0e2fc38dd1aa6974f445bd4aa85f8165827f52084d27930ab24a83bedb694e4e691e45382c0e5f0296897cea462fa4e80501db6f300db51c4d6a8191d3
|
data/README.md
CHANGED
@@ -36,16 +36,40 @@ FDK.call(myfunc)
|
|
36
36
|
```ruby
|
37
37
|
require 'fdk'
|
38
38
|
|
39
|
+
def myfunc(context, input)
|
40
|
+
return {message: "Hello World!"}
|
41
|
+
end
|
42
|
+
|
43
|
+
FDK.handle(:myfunc)
|
44
|
+
|
39
45
|
def call(context, input)
|
40
46
|
# Do some work here
|
41
|
-
|
42
|
-
return "I got: " + input + "!"
|
47
|
+
return "Hello " + input + "!"
|
43
48
|
end
|
44
49
|
```
|
45
50
|
|
46
|
-
##
|
51
|
+
## Running the example that is in the root directory of this repo
|
52
|
+
|
53
|
+
```sh
|
54
|
+
echo '{"name":"coolio"}' | fn run
|
55
|
+
```
|
47
56
|
|
48
57
|
```sh
|
49
|
-
echo '{"
|
58
|
+
fn deploy --app myapp --local && echo '{"name":"coolio"}' | fn call myapp /fdk-ruby
|
50
59
|
```
|
51
60
|
|
61
|
+
Change to hot:
|
62
|
+
|
63
|
+
Update func.yaml: `format: json`
|
64
|
+
|
65
|
+
```sh
|
66
|
+
fn deploy --app myapp --local && echo '{"name":"coolio"}' | fn call myapp /fdk-ruby
|
67
|
+
```
|
68
|
+
|
69
|
+
## Compare cold and hot
|
70
|
+
|
71
|
+
Run
|
72
|
+
|
73
|
+
```sh
|
74
|
+
ruby loop.rb
|
75
|
+
```
|
data/lib/fdk/context.rb
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
module FDK
|
2
|
+
|
3
|
+
# Config looks up values in the env vars
|
4
|
+
class Config
|
5
|
+
def [](key)
|
6
|
+
return ENV[key.upcase]
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
2
10
|
class Context
|
3
11
|
|
4
|
-
#
|
5
|
-
|
6
|
-
# per request
|
7
|
-
attr_accessor :request_url, :call_id, :headers
|
12
|
+
# TODO: Rethink FN_PATH, if it's a reference to the route, maybe it should be FN_ROUTE? eg: if it's a dynamic path, this env var would
|
13
|
+
# show the route's path (ie: route identifier), eg: /users/:name, not the actual path.
|
8
14
|
|
9
15
|
# FN_REQUEST_URL - the full URL for the request (parsing example)
|
10
16
|
# FN_APP_NAME - the name of the application that matched this route, eg: myapp
|
@@ -18,23 +24,29 @@ module FDK
|
|
18
24
|
# $X - any configuration values you've set for the Application or the Route. Replace X with the upper cased name of the config variable you set. Ex: minio_secret=secret will be exposed via MINIO_SECRET env var.
|
19
25
|
# FN_PARAM_$Y
|
20
26
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
if k.start_with? "FN_"
|
26
|
-
k2 = k[3..-1].downcase
|
27
|
-
self.instance_variable_set("@#{k2}".to_sym, v)
|
28
|
-
next
|
29
|
-
end
|
30
|
-
config[k] = v
|
31
|
-
end
|
32
|
-
headers = {}
|
27
|
+
attr_reader :payload, :config
|
28
|
+
|
29
|
+
def initialize(payload)
|
30
|
+
@payload = payload
|
33
31
|
end
|
34
32
|
|
35
|
-
|
36
|
-
|
37
|
-
|
33
|
+
def config
|
34
|
+
return @config if @config
|
35
|
+
@config = Config.new
|
36
|
+
return @config
|
38
37
|
end
|
38
|
+
|
39
|
+
def call_id
|
40
|
+
payload['call_id']
|
41
|
+
end
|
42
|
+
|
43
|
+
def content_type
|
44
|
+
payload['content_type']
|
45
|
+
end
|
46
|
+
|
47
|
+
def protocol
|
48
|
+
payload['protocol']
|
49
|
+
end
|
50
|
+
|
39
51
|
end
|
40
52
|
end
|
data/lib/fdk/runner.rb
CHANGED
@@ -2,24 +2,79 @@
|
|
2
2
|
# Executes it with input
|
3
3
|
# Responds with output
|
4
4
|
|
5
|
+
require 'json'
|
6
|
+
|
5
7
|
module FDK
|
6
|
-
def self.included(base)
|
7
|
-
puts "MODULE FDK INCLUDED"
|
8
|
-
p base
|
9
|
-
end
|
10
8
|
|
11
9
|
def self.handle(func)
|
12
|
-
puts
|
13
|
-
|
14
|
-
|
10
|
+
# STDERR.puts `env`
|
11
|
+
format = ENV['FN_FORMAT']
|
12
|
+
if format == "json"
|
13
|
+
obs = ""
|
14
|
+
endbracket=false
|
15
|
+
STDIN.each do |line|
|
16
|
+
# STDERR.puts "LINE: --#{line}--"
|
17
|
+
# STDERR.flush
|
18
|
+
ls = line.strip
|
19
|
+
STDERR.puts "ls: #{ls}"
|
20
|
+
if ls == "}"
|
21
|
+
STDERR.puts "endbracket true"
|
22
|
+
endbracket=true
|
23
|
+
elsif ls == "" && endbracket
|
24
|
+
# TODO: this isn't very robust, probably needs to be better :/
|
25
|
+
STDERR.puts "OBJECT: #{obs}"
|
26
|
+
payload = JSON.parse(obs)
|
27
|
+
# STDERR.puts "payload: #{payload.inspect}"
|
28
|
+
c = Context.new(payload)
|
29
|
+
# STDERR.puts "context: " + c.inspect
|
30
|
+
# STDERR.flush
|
31
|
+
body = payload['body']
|
32
|
+
if c.content_type == 'application/json'
|
33
|
+
body = JSON.parse(body)
|
34
|
+
end
|
35
|
+
# TODO: begin/rescue so we can respond with proper error response and code
|
36
|
+
s = FDK.single_event(func, c, body)
|
37
|
+
response = {
|
38
|
+
headers: {
|
39
|
+
'Content-Type' => 'application/json'
|
40
|
+
},
|
41
|
+
'status_code' => 200,
|
42
|
+
body: s.to_json,
|
43
|
+
}
|
44
|
+
STDOUT.puts response.to_json
|
45
|
+
STDOUT.puts
|
46
|
+
STDOUT.flush
|
47
|
+
obs = ""
|
48
|
+
next
|
49
|
+
else
|
50
|
+
endbracket = false
|
51
|
+
end
|
52
|
+
obs += line
|
53
|
+
end
|
54
|
+
elsif format == "default"
|
55
|
+
# TODO: check if content type json, and if so, parse it before passing it in
|
56
|
+
body = STDIN.read
|
57
|
+
payload = {}
|
58
|
+
payload['call_id'] = ENV['FN_CALL_ID']
|
59
|
+
payload['content_type'] = ENV['FN_HEADER_Content_Type']
|
60
|
+
payload['protocol'] = {
|
61
|
+
'type' => 'http',
|
62
|
+
'request_url' => ENV['FN_REQUEST_URL']
|
63
|
+
}
|
64
|
+
# STDERR.puts "payload: #{payload}"
|
65
|
+
c = Context.new(payload)
|
66
|
+
if c.content_type == "application/json"
|
67
|
+
# STDERR.puts "parsing json"
|
68
|
+
body = JSON.parse(body)
|
69
|
+
end
|
70
|
+
puts FDK.single_event(func, c, body).to_json
|
15
71
|
else
|
16
|
-
|
17
|
-
payload = STDIN.read
|
18
|
-
FDK.single_event(func, c, payload)
|
72
|
+
raise "Format #{format} not supported in Ruby FDK."
|
19
73
|
end
|
20
74
|
end
|
75
|
+
|
21
76
|
def self.single_event(func, c, i)
|
22
|
-
|
23
|
-
|
77
|
+
s = send func, c, i
|
78
|
+
return s
|
24
79
|
end
|
25
80
|
end
|
data/lib/fdk/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Travis Reeder
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
63
|
version: '0'
|
64
64
|
requirements: []
|
65
65
|
rubyforge_project:
|
66
|
-
rubygems_version: 2.6.
|
66
|
+
rubygems_version: 2.6.14
|
67
67
|
signing_key:
|
68
68
|
specification_version: 4
|
69
69
|
summary: Ruby FDK for Fn Project
|