faastruby-rpc 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0399784305d0de9f8ee8db0f602cc2062a1c1d8d631bdfd0514c1d5b846e7c0
4
- data.tar.gz: f437fe5dce46945b840117d3cfa6a0b68699046b7f0a64ba7822fd2278dcc4cc
3
+ metadata.gz: a7ca42876deb3b89a2ce6f08e5076e920622e7e0ff90d02f97e2c27d80000191
4
+ data.tar.gz: 4d3ca69fd16b80edf8d2a34d8832491b512e358a4a43f9dbaeb9d482d52c685d
5
5
  SHA512:
6
- metadata.gz: ab2e9df498673377e78ba3d94103a4cc07daca5ce7bcd601aede51ded5e60a74560821e5cc51089a988fbfd45a448c14a88dcb5825ea79d5a2facf1ad2d70643
7
- data.tar.gz: a946ba11384ce3d9ec7a090f9be8405b9a69e69400fe153cff602a2cc2fd35e815c7474296f9dde19c8128f4c7e5db68396cf62e490d974aaf00dedc4fe8b222
6
+ metadata.gz: 7cc828cf2f83f4662261948ad038ca5a547e2a88400a5dfbe0deac4e51e62d7e983e4476a05abe01576847ce6e598368c34c3dd0f75869abfeb3c3ffd5a090ab
7
+ data.tar.gz: 6f46911239ee2a27399cdceac025ba726ee461888dc0dab2cb704fb33abb9474048b17de1220c5b59e73ad55b86847677d428b648711894bb22681bcf330f034
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.1 - Jan 3rd 2019
4
+ - Pass a block when calling a function and the code will be executed when the function responds, and the block's return value will replace the function's response body
5
+
3
6
  ## 0.2.0 - Dec 31 2018
4
7
  - Redesigned UX for calling of external functions
5
8
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- faastruby-rpc (0.1.4)
4
+ faastruby-rpc (0.2.0)
5
5
  oj (~> 3.6)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -59,6 +59,23 @@ You can use positional or keyword arguments when calling external functions, as
59
59
 
60
60
  This gem is already required when you run your functions in FaaStRuby, or using `faastruby server`.
61
61
 
62
+ ## Running code when the invoked function responds
63
+ If you pass a block when you call another function, the block will execute as soon as the response arrives. For example:
64
+
65
+ ```ruby
66
+ require_function 'my-workspace/echo', as: 'Echo'
67
+ def handler(event)
68
+ name = Echo.call(name: 'john doe') do |response|
69
+ # response.body #=> "john doe"
70
+ # response.code #=> 200
71
+ # response.headers #=> {"content-type"=>"text/plain",...}
72
+ # What you return from the block will be the value of `name` or `name.body`
73
+ response.body.capitalize
74
+ end
75
+ render text: "Hello, #{name}!" # Will render 'John Doe'
76
+ end
77
+ ```
78
+
62
79
  ## Handling errors
63
80
 
64
81
  By default, an exception is raised if the invoked function HTTP status code is greater or equal to 400. This is important to make your functions easier to debug, and you will always know what to expect from that function call.
@@ -11,6 +11,9 @@ module FaaStRuby
11
11
  @headers = headers
12
12
  @klass = klass
13
13
  end
14
+ def body=(value)
15
+ @body = value
16
+ end
14
17
  end
15
18
  class Function
16
19
  def initialize(path, raise_errors: true)
@@ -25,46 +28,44 @@ module FaaStRuby
25
28
  }
26
29
  @raise_errors = raise_errors
27
30
  end
28
- def call_with(*args)
29
- execute(req_body: Oj.dump(args), headers: {'Content-Type' => 'application/json', 'Faastruby-Rpc' => 'true'})
30
- end
31
31
 
32
32
  def call(*args)
33
- return call_with(*args) if args.any?
34
- execute(method: 'get')
33
+ @thread = Thread.new do
34
+ output = args.any? ? call_with(*args) : execute(method: 'get')
35
+ @response.body = yield(output) if block_given?
36
+ end
37
+ self
35
38
  end
36
39
 
37
40
  def execute(req_body: nil, query_params: {}, headers: {}, method: 'post')
38
- @thread = Thread.new do
39
- url = "#{FAASTRUBY_HOST}/#{@path}#{convert_query_params(query_params)}"
40
- uri = URI.parse(url)
41
- use_ssl = uri.scheme == 'https' ? true : false
42
- response = fetch(use_ssl: use_ssl, uri: uri, headers: headers, method: @methods[method], req_body: req_body)
43
- resp_headers = {}
44
- response.each{|k,v| resp_headers[k] = v}
45
- case resp_headers['content-type']
46
- when 'application/json'
47
- begin
48
- resp_body = Oj.load(response.body)
49
- rescue Oj::ParseError => e
50
- if response.body.is_a?(String)
51
- resp_body = response.body
52
- else
53
- raise e if @raise_errors
54
- resp_body = {
55
- 'error' => e.message,
56
- 'location' => e.backtrace&.first
57
- }
58
- end
41
+ url = "#{FAASTRUBY_HOST}/#{@path}#{convert_query_params(query_params)}"
42
+ uri = URI.parse(url)
43
+ use_ssl = uri.scheme == 'https' ? true : false
44
+ response = fetch(use_ssl: use_ssl, uri: uri, headers: headers, method: @methods[method], req_body: req_body)
45
+ resp_headers = {}
46
+ response.each{|k,v| resp_headers[k] = v}
47
+ case resp_headers['content-type']
48
+ when 'application/json'
49
+ begin
50
+ resp_body = Oj.load(response.body)
51
+ rescue Oj::ParseError => e
52
+ if response.body.is_a?(String)
53
+ resp_body = response.body
54
+ else
55
+ raise e if @raise_errors
56
+ resp_body = {
57
+ 'error' => e.message,
58
+ 'location' => e.backtrace&.first
59
+ }
59
60
  end
60
- when 'application/yaml'
61
- resp_body = YAML.load(response.body)
62
- else
63
- resp_body = response.body
64
61
  end
65
- raise FaaStRuby::RPC::ExecutionError.new("Function #{@path} returned status code #{response.code} - #{resp_body['error']} - #{resp_body['location']}") if response.code.to_i >= 400 && @raise_errors
66
- @response = FaaStRuby::RPC::Response.new(resp_body, response.code.to_i, resp_headers)
62
+ when 'application/yaml'
63
+ resp_body = YAML.load(response.body)
64
+ else
65
+ resp_body = response.body
67
66
  end
67
+ raise FaaStRuby::RPC::ExecutionError.new("Function #{@path} returned status code #{response.code} - #{resp_body['error']} - #{resp_body['location']}") if response.code.to_i >= 400 && @raise_errors
68
+ @response = FaaStRuby::RPC::Response.new(resp_body, response.code.to_i, resp_headers)
68
69
  self
69
70
  end
70
71
 
@@ -78,7 +79,7 @@ module FaaStRuby
78
79
  end
79
80
 
80
81
  def to_s
81
- body || ""
82
+ body.to_s || ""
82
83
  end
83
84
 
84
85
  def body
@@ -103,6 +104,10 @@ module FaaStRuby
103
104
 
104
105
  private
105
106
 
107
+ def call_with(*args)
108
+ execute(req_body: Oj.dump(args), headers: {'Content-Type' => 'application/json', 'Faastruby-Rpc' => 'true'})
109
+ end
110
+
106
111
  def wait
107
112
  @thread.join
108
113
  end
@@ -1,5 +1,5 @@
1
1
  module FaaStRuby
2
2
  module RPC
3
- VERSION = '0.2.0'
3
+ VERSION = '0.2.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faastruby-rpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Arruda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-31 00:00:00.000000000 Z
11
+ date: 2019-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj