faastruby 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c01308a98baef83f7584358342b5593f8017671243fccdb351bbca0501efb5e8
4
- data.tar.gz: 02573d5f8483a4a0d797caac9f97659550bfbab776fbf879dfb7a67503bfd63c
3
+ metadata.gz: 89c1ce111c17f6eb8b69d8d47cffc7dea7a6a48e373d43bd30cd7303c097d21f
4
+ data.tar.gz: e616406ad0c57004cc55e63b3c5db761164fb4ff4d3f40cab8474fcdafe7a3bb
5
5
  SHA512:
6
- metadata.gz: 34bbb6de97544ddefdb8f8c9d233da3469b22b0145631b8dec39e31d2bfb4e1a9b4f8be53bba86a019caeb2ec0e4d46af66967a6c4f91bd50fe2da59bd98bd07
7
- data.tar.gz: a74de808b41605fe03d80b1cc434c958b71b2373395e00c66ef7bd56b9ffc86a99555b28d9012130d0df6ef6382aa95e66e941cc3a526ad077fd7197346a2350
6
+ metadata.gz: 464be81d0ccc2a374a69111e61c29a7ee7f6797d3aeb4ce1c9e30bc76578fa07d313930a5034e566b5ce46a57356c8ab6897e49dea307e22530f2c0960626786
7
+ data.tar.gz: c2158f4ad8209e2028519c97edac9434e170158d801dc941160a5ea12ac25569aaa7b1efe01f056d5faf89238160eb31cee021261bc7ce73be05d6c326dad378
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.3 - Dec 31 2018
4
+ - Updated `faastruby-rpc` dependency to 0.2.0.
5
+ - Improved FaaStRuby Server.
6
+
3
7
  ## 0.4.2 - Dec 30 2018
4
8
  - Simplified Crystal Hello World example.
5
9
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- faastruby (0.4.1)
4
+ faastruby (0.4.2)
5
5
  colorize (~> 0.8)
6
6
  faastruby-rpc (~> 0.1.3)
7
7
  oj (~> 3.6)
@@ -6,57 +6,84 @@ require 'yaml'
6
6
  require 'oj'
7
7
  require 'faastruby-rpc'
8
8
 
9
- module FaaStRubyFunction
10
- def self.call(workspace_name, function_name, event, args)
11
- begin
12
- load "./#{workspace_name}/#{function_name}/handler.rb"
13
- response = handler(event, *args)
14
- return response if response.is_a?(FaaStRubyFunction::Response)
15
- body = {
16
- 'error' => "Please use the helpers 'render' or 'respond_with' as your function return value."
17
- }
18
- respond_with(Oj.dump(body), status: 500, headers: {'Content-Type' => 'application/json'})
19
- rescue Exception => e
20
- body = {
21
- 'error' => e.message,
22
- 'location' => e.backtrace&.first,
23
- }
24
- respond_with(Oj.dump(body), status: 500, headers: {'Content-Type' => 'application/json'})
9
+ module FaaStRuby
10
+ class DoubleRenderError < StandardError; end
11
+ end
12
+
13
+ module FaaStRuby
14
+ class Runner
15
+ def initialize
16
+ @rendered = false
25
17
  end
26
- end
27
- def self.respond_with(body, status: 200, headers: {})
28
- r = FaaStRubyFunction::Response.new(:body, :status, :headers)
29
- r.new(body, status, headers)
30
- end
31
18
 
32
- def self.render(js: nil, body: nil, inline: nil, html: nil, json: nil, yaml: nil, text: nil, status: 200, headers: {}, content_type: nil)
33
- headers["Content-Type"] = content_type if content_type
34
- case
35
- when json
36
- headers["Content-Type"] ||= "application/json"
37
- resp_body = json.is_a?(String) ? json : Oj.dump(json)
38
- when html, inline
39
- headers["Content-Type"] ||= "text/html"
40
- resp_body = html
41
- when text
42
- headers["Content-Type"] ||= "text/plain"
43
- resp_body = text
44
- when yaml
45
- headers["Content-Type"] ||= "application/yaml"
46
- resp_body = yaml.to_yaml
47
- when body
48
- headers["Content-Type"] ||= "application/octet-stream"
49
- resp_body = raw
50
- when js
51
- headers["Content-Type"] ||= "text/javascript"
52
- resp_body = js
19
+ def call(workspace_name, function_name, event, args)
20
+ begin
21
+ load "./#{workspace_name}/#{function_name}/handler.rb"
22
+ response = handler(event, *args)
23
+ return response if response.is_a?(FaaStRuby::Response)
24
+ body = {
25
+ 'error' => "Please use the helpers 'render' or 'respond_with' as your function return value."
26
+ }
27
+ FaaStRuby::Response.new(body: Oj.dump(body), status: 500, headers: {'Content-Type' => 'application/json'})
28
+ rescue Exception => e
29
+ body = {
30
+ 'error' => e.message,
31
+ 'location' => e.backtrace&.first,
32
+ }
33
+ FaaStRuby::Response.new(body: Oj.dump(body), status: 500, headers: {'Content-Type' => 'application/json'})
34
+ end
35
+ end
36
+
37
+ def rendered!
38
+ @rendered = true
39
+ end
40
+ def rendered?
41
+ @rendered
42
+ end
43
+
44
+ def respond_with(body, status: 200, headers: {})
45
+ raise FaaStRuby::DoubleRenderError.new("You called 'render' or 'respond_with' twice in your handler method") if rendered?
46
+ response = FaaStRuby::Response.new(body: body, status: status, headers: headers)
47
+ rendered!
48
+ response
49
+ end
50
+
51
+ def render(js: nil, body: nil, inline: nil, html: nil, json: nil, yaml: nil, text: nil, status: 200, headers: {}, content_type: nil)
52
+ headers["Content-Type"] = content_type if content_type
53
+ case
54
+ when json
55
+ headers["Content-Type"] ||= "application/json"
56
+ resp_body = json.is_a?(String) ? json : Oj.dump(json)
57
+ when html, inline
58
+ headers["Content-Type"] ||= "text/html"
59
+ resp_body = html
60
+ when text
61
+ headers["Content-Type"] ||= "text/plain"
62
+ resp_body = text
63
+ when yaml
64
+ headers["Content-Type"] ||= "application/yaml"
65
+ resp_body = yaml.is_a?(String) ? yaml : YAML.load(yaml)
66
+ when body
67
+ headers["Content-Type"] ||= "application/octet-stream"
68
+ resp_body = raw
69
+ when js
70
+ headers["Content-Type"] ||= "text/javascript"
71
+ resp_body = js
72
+ end
73
+ respond_with(resp_body, status: status, headers: headers)
53
74
  end
54
- respond_with(resp_body, status: status, headers: headers)
55
75
  end
76
+
56
77
  class Event < Struct
57
78
  end
58
79
 
59
- class Response < Struct
80
+ class Response
81
+ attr_accessor :body, :status, :headers
82
+ def initialize(body:, status: 200, headers: {})
83
+ @body = body
84
+ @status = status
85
+ @headers = headers
86
+ end
60
87
  end
61
88
  end
62
89
 
@@ -71,11 +98,11 @@ class FaaStRubyServer < Sinatra::Application
71
98
  end
72
99
  set :server, %w[puma]
73
100
  set :run, false
74
- set :show_exceptions, false
101
+ set :show_exceptions, true
75
102
 
76
103
  register Sinatra::MultiRoute
77
104
  route :get, :post, :put, :patch, :delete, '/:workspace_name/:function_name' do
78
- e = FaaStRubyFunction::Event.new(:body, :query_params, :headers, :context)
105
+ e = FaaStRuby::Event.new(:body, :query_params, :headers, :context)
79
106
  headers = env.select { |key, value| key.include?('HTTP_') || ['CONTENT_TYPE', 'CONTENT_LENGTH', 'REMOTE_ADDR', 'REQUEST_METHOD', 'QUERY_STRING'].include?(key) }
80
107
  if headers.has_key?("HTTP_FAASTRUBY_RPC")
81
108
  body = nil
@@ -87,7 +114,7 @@ class FaaStRubyServer < Sinatra::Application
87
114
  query_params = parse_query(request.query_string)
88
115
  context = set_context(params[:workspace_name], params[:function_name])
89
116
  event = e.new(body, query_params, headers, context)
90
- response = FaaStRubyFunction.call(params[:workspace_name], params[:function_name], event, rpc_args)
117
+ response = FaaStRuby::Runner.new.call(params[:workspace_name], params[:function_name], event, rpc_args)
91
118
  status response.status
92
119
  headers response.headers
93
120
  body response.body
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.add_runtime_dependency 'sinatra', '~> 2.0'
21
21
  spec.add_runtime_dependency 'sinatra-contrib', '~> 2.0'
22
22
  spec.add_runtime_dependency 'puma', '~> 3.12'
23
- spec.add_runtime_dependency 'faastruby-rpc', '~> 0.1.3'
23
+ spec.add_runtime_dependency 'faastruby-rpc', '~> 0.2.0'
24
24
 
25
25
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
26
26
  # to allow pushing to a single host or delete this section to allow pushing to any host.
@@ -1,3 +1,3 @@
1
1
  module FaaStRuby
2
- VERSION = '0.4.2'
2
+ VERSION = '0.4.3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faastruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Arruda
@@ -142,14 +142,14 @@ dependencies:
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: 0.1.3
145
+ version: 0.2.0
146
146
  type: :runtime
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: 0.1.3
152
+ version: 0.2.0
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: bundler
155
155
  requirement: !ruby/object:Gem::Requirement