jets 2.0.1 → 2.0.3

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: 595189c68ebf3b306738227bb516e6ab44f93c9be22a67c4ae9028129384c553
4
- data.tar.gz: b1ff731c9dff57744f63de0de36d62a177267b21236814eab97ce516bacac19c
3
+ metadata.gz: 1296b877cb1281059d7410eb87b22b2343017ce69de87689535cdabd1f1b76be
4
+ data.tar.gz: 0cd13123cf8fb894154cde0123631e1af4a1bae4a709a8e5c486306efedd87af
5
5
  SHA512:
6
- metadata.gz: 931fce86443f6cd48e7b481572bc739ae354cb7015f26229c8b80eb3bb8fabd65d6a12b5488fc57739ccaa7bf62426c7619cd2d60c2a42cbfaba2690ca2506de
7
- data.tar.gz: a75e02410737e0c5aa77a22721462c2dcb784c048873f91f5d51dc3db3b3883d2af716bd79ebe8918f31717b5b46d606f54128bacee1fa5c259686e2cf21ba5b
6
+ metadata.gz: 1c9f8d26180f8a82b7600c8e1bdfbe3c23e1d96ee80dc20e60e1c387d04a7c8b98896fe28a653bdd065c68847c2688ea6592c109a4b22210fa7786baaa8c2e17
7
+ data.tar.gz: 7b7edddbd10a35c12a434a9edef4f449db2057a116dcc6aec9f3d03ed36bba2071089a438187ee84307588b26bbea5412aab2219d843b0388d2248a09dd28cd2
@@ -3,6 +3,13 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/).
5
5
 
6
+ ## [2.0.3]
7
+ - #331 spec_helpers fixes: response.headers support, fix session, run spec controller through middleware
8
+
9
+ ## [2.0.2]
10
+ - #322, #325, #326, #329 update docs
11
+ - #330 fix remove Rails const
12
+
6
13
  ## [2.0.1]
7
14
  - #319 improve mega mode or afterburner, start back up rack process if it dies for any reason
8
15
 
data/README.md CHANGED
@@ -8,6 +8,7 @@ Ruby and Lambda splat out a baby and that child's name is [Jets](http://rubyonje
8
8
  [![CircleCI](https://circleci.com/gh/tongueroo/jets.svg?style=svg)](https://circleci.com/gh/tongueroo/jets)
9
9
  [![Gem Version](https://badge.fury.io/rb/jets.svg)](https://badge.fury.io/rb/jets)
10
10
  [![Support](https://img.shields.io/badge/Support-Help-blue.svg)](http://rubyonjets.com/support/)
11
+ [![Gitter Chat](https://badges.gitter.im/tongueroo/jets.png)](https://gitter.im/tongueroo/jets)
11
12
 
12
13
  **Upgrading**: If you are upgrading Jets, please check on the [Upgrading Notes](http://rubyonjets.com/docs/extras/upgrading/).
13
14
 
@@ -39,7 +40,7 @@ end
39
40
 
40
41
  Here's the function in the Lambda console:
41
42
 
42
- ![](https://raw.githubusercontent.com/tongueroo/jets/master/docs/img/docs/jets-simple-lambda-function-console.png)
43
+ ![Code Example in AWS Lambda console](https://raw.githubusercontent.com/tongueroo/jets/master/docs/img/docs/jets-simple-lambda-function-console.png)
43
44
 
44
45
 
45
46
  Though simple functions are supported by Jets, they do not add much value as other ways to write Ruby code with Jets. Classes like [Controllers](http://rubyonjets.com/docs/controllers/) and [Jobs](http://rubyonjets.com/docs/jobs/) add many conveniences and are more powerful to use. We’ll cover them next.
@@ -70,7 +71,7 @@ Helper methods like `params` provide the parameters from the API Gateway event.
70
71
 
71
72
  Jets creates Lambda functions for each public method in your controller. Here they are in the Lambda console:
72
73
 
73
- ![](https://raw.githubusercontent.com/tongueroo/jets/master/docs/img/docs/demo-lambda-functions-controller.png)
74
+ ![Lambda Functions for each public method in AWS Console](https://raw.githubusercontent.com/tongueroo/jets/master/docs/img/docs/demo-lambda-functions-controller.png)
74
75
 
75
76
  ### Jets Routing
76
77
 
@@ -96,7 +97,7 @@ end
96
97
 
97
98
  The `routes.rb` gets translated to API Gateway resources:
98
99
 
99
- ![](https://raw.githubusercontent.com/tongueroo/jets/master/docs/img/quick-start/demo-api-gateway.png)
100
+ ![API Gateway Resources generated from routes in AWS console](https://raw.githubusercontent.com/tongueroo/jets/master/docs/img/quick-start/demo-api-gateway.png)
100
101
 
101
102
  Test your API Gateway endpoints with curl or postman. Note, replace the URL endpoint with the one that is created:
102
103
 
@@ -128,7 +129,7 @@ end
128
129
 
129
130
  `HardJob#dig` runs every 10 hours and `HardJob#lift` runs every 12 hours. The `rate` and `cron` methods created CloudWatch Event Rules. Example:
130
131
 
131
- ![](https://raw.githubusercontent.com/tongueroo/jets/master/docs/img/docs/demo-job-cloudwatch-rule.png)
132
+ ![CloudWatch Event Rules in AWS Console](https://raw.githubusercontent.com/tongueroo/jets/master/docs/img/docs/demo-job-cloudwatch-rule.png)
132
133
 
133
134
  ### Jets Deployment
134
135
 
@@ -89,7 +89,7 @@ module Jets
89
89
  end
90
90
 
91
91
  def test?
92
- ENV['TEST']
92
+ ENV['TEST'] || Jets.env.test?
93
93
  end
94
94
 
95
95
  private
@@ -80,7 +80,8 @@ class Jets::CLI
80
80
  # `jets generate` is called without additional args. We'll take it over early and fix it here.
81
81
  autocomplete_command = Jets::Commands::Base.autocomplete(args[0])
82
82
  generate = autocomplete_command == "generate"
83
- if generate && (args.size == 1 || help_flags.include?(args.last))
83
+
84
+ if generate && ((args.size == 1 || help_flags.include?(args.last)) || args.size == 2)
84
85
  puts Jets::Generator.help
85
86
  exit
86
87
  end
@@ -97,13 +97,13 @@ module Jets::Commands
97
97
  end
98
98
 
99
99
  desc "generate [type] [args]", "Generates things like scaffolds"
100
- long_desc Jets::Generator.help
100
+ long_desc Help.text(:generate) # do use Jets::Generator.help as it'll load Rails const
101
101
  def generate(generator, *args)
102
102
  Jets::Generator.invoke(generator, *args)
103
103
  end
104
104
 
105
105
  desc "degenerate [type] [args]", "Destroys things like scaffolds"
106
- long_desc Jets::Generator.help
106
+ long_desc Help.text(:generate) # do use Jets::Generator.help as it'll load Rails const
107
107
  def degenerate(generator, *args)
108
108
  Jets::Generator.revoke(generator, *args)
109
109
  end
@@ -23,6 +23,13 @@ class Jets::Controller
23
23
  @response = Response.new
24
24
  end
25
25
 
26
+ # One key difference between process! vs dispatch!
27
+ #
28
+ # process! - takes the request through the middleware stack
29
+ # dispatch! - does not
30
+ #
31
+ # Most of the time, you want process! instead of dispatch!
32
+ #
26
33
  def process!
27
34
  adapter = Jets::Controller::Rack::Adapter.new(event, context, meth)
28
35
  adapter.rack_vars(
@@ -10,8 +10,6 @@ class Jets::Generator
10
10
  end
11
11
 
12
12
  def help(args=ARGV)
13
- require_generators
14
-
15
13
  # `jets generate -h` results in:
16
14
  #
17
15
  # args = ["generate", "-h"]
@@ -91,8 +91,7 @@ class Jets::Router::MethodCreator
91
91
 
92
92
  private
93
93
  def singularize(s)
94
- return unless s # nil
95
- s.singularize
94
+ s&.singularize
96
95
  end
97
96
  end
98
97
  end
@@ -4,51 +4,12 @@ require 'base64'
4
4
 
5
5
  module Jets
6
6
  module SpecHelpers
7
- attr_reader :request, :response
8
- def initialize(*)
9
- super
10
- @request = Request.new(:get, '/', {}, Params.new)
11
- @response = nil # will be set after http_call
12
- end
13
-
14
- rest_methods = %w[get post put patch delete]
15
- rest_methods.each do |meth|
16
- define_method(meth) do |path, **params|
17
- http_call(method: meth, path: path, **params)
18
- end
19
- # Example:
20
- # def get(path, **params)
21
- # http_call(method: :get, path: path, **params)
22
- # end
23
- end
24
-
25
- def http_call(method:, path:, **params)
26
- request.method = method.to_sym
27
- request.path = path
28
- request.headers.deep_merge!(params.delete(:headers) || {})
29
-
30
- request.params.body_params = params.delete(:params) || params || {}
31
-
32
- request.params.query_params = params.delete(:query)
33
- request.params.query_params ||= params if request.method == :get
34
- request.params.query_params ||= {}
35
-
36
- request.params.path_params = params
37
-
38
- @response = request.dispatch!
39
- end
40
-
41
- def fixture_path(filename)
42
- "#{Jets.root}/spec/fixtures/#{filename}"
43
- end
44
-
45
- def fixture_file(filename)
46
- File.new(fixture_path(filename))
47
- end
7
+ include Fixtures
8
+ include Controllers
48
9
  end
49
10
  end
50
11
 
51
12
  require "rspec"
52
13
  RSpec.configure do |c|
53
14
  c.include Jets::SpecHelpers
54
- end
15
+ end
@@ -0,0 +1,38 @@
1
+ module Jets::SpecHelpers
2
+ module Controllers
3
+ attr_reader :request, :response
4
+
5
+ def initialize(*)
6
+ super
7
+ @request = Request.new(:get, '/', {}, Params.new)
8
+ @response = nil # will be set after http_call
9
+ end
10
+
11
+ rest_methods = %w[get post put patch delete]
12
+ rest_methods.each do |meth|
13
+ define_method(meth) do |path, **params|
14
+ http_call(method: meth, path: path, **params)
15
+ end
16
+ # Example:
17
+ # def get(path, **params)
18
+ # http_call(method: :get, path: path, **params)
19
+ # end
20
+ end
21
+
22
+ def http_call(method:, path:, **params)
23
+ request.method = method.to_sym
24
+ request.path = path
25
+ request.headers.deep_merge!(params.delete(:headers) || {})
26
+
27
+ request.params.body_params = params.delete(:params) || params || {}
28
+
29
+ request.params.query_params = params.delete(:query)
30
+ request.params.query_params ||= params if request.method == :get
31
+ request.params.query_params ||= {}
32
+
33
+ request.params.path_params = params
34
+
35
+ @response = request.dispatch!
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,8 @@
1
+ module Jets::SpecHelpers::Controllers
2
+ class Params
3
+ attr_accessor :path_params, :body_params, :query_params
4
+ def initialize(path_params={}, body_params={}, query_params={})
5
+ @path_params, @body_params, @query_params = path_params, body_params, query_params
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,77 @@
1
+ module Jets::SpecHelpers::Controllers
2
+ class Request
3
+ attr_accessor :method, :path, :headers, :params
4
+ def initialize(method, path, headers={}, params={})
5
+ @method, @path, @headers, @params = method, path, headers, params
6
+ end
7
+
8
+ def event
9
+ json = {}
10
+ id_params = path.scan(%r{:([^/]+)}).flatten
11
+ expanded_path = path.dup
12
+ path_parameters = {}
13
+
14
+ id_params.each do |id_param|
15
+ raise "missing param: :#{id_param}" unless params.path_params.include? id_param.to_sym
16
+
17
+ path_param_value = params.path_params[id_param.to_sym]
18
+ raise "Path param :#{id_param} value cannot be blank" if path_param_value.blank?
19
+
20
+ expanded_path.gsub!(":#{id_param}", path_param_value.to_s)
21
+ path_parameters.deep_merge!(id_param => path_param_value.to_s)
22
+ end
23
+
24
+ json['resource'] = path
25
+ json['path'] = expanded_path
26
+ json['httpMethod'] = method.to_s.upcase
27
+ json['pathParameters'] = path_parameters
28
+ json['headers'] = (headers || {}).stringify_keys
29
+
30
+ if method != :get
31
+ json['headers']['Content-Type'] = 'application/x-www-form-urlencoded'
32
+ body = Rack::Multipart.build_multipart(params.body_params)
33
+
34
+ if body
35
+ json['headers']['Content-Length'] ||= body.length.to_s
36
+ json['headers']['Content-Type'] = "multipart/form-data; boundary=#{Rack::Multipart::MULTIPART_BOUNDARY}"
37
+ else
38
+ body = Rack::Utils.build_nested_query(params.body_params)
39
+ end
40
+
41
+ json['body'] = Base64.encode64(body)
42
+ json['isBase64Encoded'] = true
43
+ end
44
+
45
+ params.query_params.each do |key, value|
46
+ json['queryStringParameters'] ||= {}
47
+ json['queryStringParameters'][key.to_s] = value.to_s
48
+ end
49
+
50
+ json
51
+ end
52
+
53
+ def find_route!
54
+ path = self.path
55
+ path = path[0..-2] if path.end_with? '/'
56
+ path = path[1..-1] if path.start_with? '/'
57
+
58
+ route = Jets::Router.routes.find { |r| r.path == path && r.method == method.to_s.upcase }
59
+ raise "Route not found: #{method.to_s.upcase} #{path}" if route.blank?
60
+
61
+ route
62
+ end
63
+
64
+ def dispatch!
65
+ route = find_route!
66
+ klass = Object.const_get(route.controller_name)
67
+ controller = klass.new(event, {}, route.action_name)
68
+ response = controller.process! # response is API Gateway Hash
69
+
70
+ unless response['statusCode'] && response['body']
71
+ raise "Expected response to an API Gateway Hash Structure. Are you rendering correctly?"
72
+ end
73
+
74
+ Response.new(response) # converts APIGW hash to prettier object
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,10 @@
1
+ module Jets::SpecHelpers::Controllers
2
+ class Response
3
+ attr_reader :status, :headers, :body
4
+ def initialize(response)
5
+ @status = response['statusCode'].to_i
6
+ @headers = response['headers']
7
+ @body = response['body']
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module Jets::SpecHelpers
2
+ module Fixtures
3
+ def fixture_path(filename)
4
+ "#{Jets.root}/spec/fixtures/#{filename}"
5
+ end
6
+
7
+ def fixture_file(filename)
8
+ File.new(fixture_path(filename))
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Jets
2
- VERSION = "2.0.1"
2
+ VERSION = "2.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jets
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-29 00:00:00.000000000 Z
11
+ date: 2019-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionmailer
@@ -914,9 +914,11 @@ files:
914
914
  - lib/jets/rule/base.rb
915
915
  - lib/jets/rule/dsl.rb
916
916
  - lib/jets/spec_helpers.rb
917
- - lib/jets/spec_helpers/params.rb
918
- - lib/jets/spec_helpers/request.rb
919
- - lib/jets/spec_helpers/response.rb
917
+ - lib/jets/spec_helpers/controllers.rb
918
+ - lib/jets/spec_helpers/controllers/params.rb
919
+ - lib/jets/spec_helpers/controllers/request.rb
920
+ - lib/jets/spec_helpers/controllers/response.rb
921
+ - lib/jets/spec_helpers/fixtures.rb
920
922
  - lib/jets/stack.rb
921
923
  - lib/jets/stack/builder.rb
922
924
  - lib/jets/stack/definition.rb
@@ -1,10 +0,0 @@
1
- module Jets
2
- module SpecHelpers
3
- class Params
4
- attr_accessor :path_params, :body_params, :query_params
5
- def initialize(path_params={}, body_params={}, query_params={})
6
- @path_params, @body_params, @query_params = path_params, body_params, query_params
7
- end
8
- end
9
- end
10
- end
@@ -1,78 +0,0 @@
1
- module Jets
2
- module SpecHelpers
3
- class Request
4
- attr_accessor :method, :path, :headers, :params
5
- def initialize(method, path, headers={}, params={})
6
- @method, @path, @headers, @params = method, path, headers, params
7
- end
8
-
9
- def event
10
- json = {}
11
- id_params = path.scan(%r{:([^/]+)}).flatten
12
- expanded_path = path.dup
13
- path_parameters = {}
14
-
15
- id_params.each do |id_param|
16
- raise "missing param: :#{id_param}" unless params.path_params.include? id_param.to_sym
17
-
18
- path_param_value = params.path_params[id_param.to_sym]
19
- raise "Path param :#{id_param} value cannot be blank" if path_param_value.blank?
20
-
21
- expanded_path.gsub!(":#{id_param}", path_param_value.to_s)
22
- path_parameters.deep_merge!(id_param => path_param_value.to_s)
23
- end
24
-
25
- json['resource'] = path
26
- json['path'] = expanded_path
27
- json['httpMethod'] = method.to_s.upcase
28
- json['pathParameters'] = path_parameters
29
- json['headers'] = (headers || {}).stringify_keys
30
-
31
- if method != :get
32
- json['headers']['Content-Type'] = 'application/x-www-form-urlencoded'
33
- body = Rack::Multipart.build_multipart(params.body_params)
34
-
35
- if body
36
- json['headers']['Content-Length'] ||= body.length.to_s
37
- json['headers']['Content-Type'] = "multipart/form-data; boundary=#{Rack::Multipart::MULTIPART_BOUNDARY}"
38
- else
39
- body = Rack::Utils.build_nested_query(params.body_params)
40
- end
41
-
42
- json['body'] = Base64.encode64(body)
43
- json['isBase64Encoded'] = true
44
- end
45
-
46
- params.query_params.each do |key, value|
47
- json['queryStringParameters'] ||= {}
48
- json['queryStringParameters'][key.to_s] = value.to_s
49
- end
50
-
51
- json
52
- end
53
-
54
- def find_route!
55
- path = self.path
56
- path = path[0..-2] if path.end_with? '/'
57
- path = path[1..-1] if path.start_with? '/'
58
-
59
- route = Jets::Router.routes.find { |r| r.path == path && r.method == method.to_s.upcase }
60
- raise "Route not found: #{method.to_s.upcase} #{path}" if route.blank?
61
-
62
- route
63
- end
64
-
65
- def dispatch!
66
- route = find_route!
67
- controller = Object.const_get(route.controller_name).new(event, {}, route.action_name)
68
- response = controller.dispatch!
69
-
70
- if !response.is_a?(Array) || response.size != 3
71
- raise "Expected response to be an array of size 3. Are you rendering correctly?"
72
- end
73
-
74
- Response.new(response[0].to_i, response[2].read)
75
- end
76
- end
77
- end
78
- end
@@ -1,5 +0,0 @@
1
- module Jets
2
- module SpecHelpers
3
- Response = Struct.new(:status, :body)
4
- end
5
- end