shark-on-lambda 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -0
  3. data/changelog.md +12 -0
  4. data/doc/upgrade-from-0.6.x-to-1.x.md +3 -3
  5. data/lib/shark-on-lambda.rb +9 -0
  6. data/lib/shark_on_lambda.rb +14 -54
  7. data/lib/shark_on_lambda/api_gateway_handler.rb +3 -55
  8. data/lib/shark_on_lambda/application.rb +74 -2
  9. data/lib/shark_on_lambda/base_controller.rb +29 -9
  10. data/lib/shark_on_lambda/cacheable.rb +21 -0
  11. data/lib/shark_on_lambda/configuration.rb +1 -65
  12. data/lib/shark_on_lambda/inferrers/serializer_inferrer.rb +10 -7
  13. data/lib/shark_on_lambda/jsonapi_renderer.rb +1 -2
  14. data/lib/shark_on_lambda/middleware/honeybadger.rb +5 -3
  15. data/lib/shark_on_lambda/middleware/jsonapi_rescuer.rb +21 -1
  16. data/lib/shark_on_lambda/middleware/lambda_logger.rb +5 -13
  17. data/lib/shark_on_lambda/rake_tasks.rb +16 -0
  18. data/lib/shark_on_lambda/request.rb +0 -3
  19. data/lib/shark_on_lambda/rspec/env_builder.rb +71 -37
  20. data/lib/shark_on_lambda/rspec/helpers.rb +5 -88
  21. data/lib/shark_on_lambda/rspec/request_helpers.rb +63 -0
  22. data/lib/shark_on_lambda/rspec/{jsonapi_helpers.rb → response_helpers.rb} +4 -10
  23. data/lib/shark_on_lambda/version.rb +1 -1
  24. data/shark-on-lambda.gemspec +7 -5
  25. metadata +37 -37
  26. data/lib/shark_on_lambda/concerns/resettable_singleton.rb +0 -18
  27. data/lib/shark_on_lambda/concerns/yaml_config_loader.rb +0 -28
  28. data/lib/shark_on_lambda/dispatcher.rb +0 -26
  29. data/lib/shark_on_lambda/inferrers/name_inferrer.rb +0 -66
  30. data/lib/shark_on_lambda/jsonapi_controller.rb +0 -32
  31. data/lib/shark_on_lambda/middleware/rescuer.rb +0 -37
  32. data/lib/shark_on_lambda/query.rb +0 -67
  33. data/lib/shark_on_lambda/rack_adapters/api_gateway.rb +0 -128
  34. data/lib/shark_on_lambda/secrets.rb +0 -43
@@ -2,27 +2,21 @@
2
2
 
3
3
  module SharkOnLambda
4
4
  module RSpec
5
- module JsonapiHelpers
6
- include Helpers
7
-
5
+ module ResponseHelpers
8
6
  def jsonapi_attributes
9
- jsonapi_data[:attributes] || {}
7
+ jsonapi_data.fetch(:attributes, {})
10
8
  end
11
9
 
12
10
  def jsonapi_data
13
- parsed_body[:data] || {}
11
+ parsed_body.fetch(:data, {})
14
12
  end
15
13
 
16
14
  def jsonapi_errors
17
- parsed_body[:errors] || []
15
+ parsed_body.fetch(:errors, [])
18
16
  end
19
17
 
20
18
  private
21
19
 
22
- def default_content_type
23
- 'application/vnd.api+json'
24
- end
25
-
26
20
  def parsed_body
27
21
  @parsed_body ||= JSON.parse(response.body).with_indifferent_access
28
22
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SharkOnLambda
4
- VERSION = '1.0.1'
4
+ VERSION = '2.0.0'
5
5
  end
@@ -30,18 +30,20 @@ Gem::Specification.new do |spec|
30
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
31
  spec.require_paths = ['lib']
32
32
 
33
- spec.add_dependency 'actionpack'
34
- spec.add_dependency 'activesupport'
35
- spec.add_dependency 'jsonapi-rb'
33
+ spec.required_ruby_version = '>= 2.5'
34
+
35
+ spec.add_dependency 'actionpack', '~> 6.0.0'
36
+ spec.add_dependency 'activesupport', '~> 6.0.0'
37
+ spec.add_dependency 'jsonapi-rb', '~> 0.5.0'
36
38
  spec.add_dependency 'rack', '>= 2.0.8', '< 3'
37
- spec.add_dependency 'zeitwerk', '~> 2.2'
39
+ spec.add_dependency 'rack-on-lambda', '~> 1.0', '>= 1.0.1'
40
+ spec.add_dependency 'zeitwerk', '~> 2.3'
38
41
 
39
42
  # TODO: Do we really need `activemodel`?
40
43
  # Or can we get away with mocking out ActiveModel::Errors?
41
44
  spec.add_development_dependency 'activemodel'
42
45
  spec.add_development_dependency 'bundler'
43
46
  spec.add_development_dependency 'factory_bot'
44
- spec.add_development_dependency 'fasterer'
45
47
  spec.add_development_dependency 'pry'
46
48
  spec.add_development_dependency 'pry-byebug'
47
49
  spec.add_development_dependency 'rake'
metadata CHANGED
@@ -1,57 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shark-on-lambda
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Huy Dinh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-16 00:00:00.000000000 Z
11
+ date: 2020-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 6.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 6.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 6.0.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 6.0.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: jsonapi-rb
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 0.5.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 0.5.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rack
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -73,35 +73,41 @@ dependencies:
73
73
  - !ruby/object:Gem::Version
74
74
  version: '3'
75
75
  - !ruby/object:Gem::Dependency
76
- name: zeitwerk
76
+ name: rack-on-lambda
77
77
  requirement: !ruby/object:Gem::Requirement
78
78
  requirements:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
- version: '2.2'
81
+ version: '1.0'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 1.0.1
82
85
  type: :runtime
83
86
  prerelease: false
84
87
  version_requirements: !ruby/object:Gem::Requirement
85
88
  requirements:
86
89
  - - "~>"
87
90
  - !ruby/object:Gem::Version
88
- version: '2.2'
91
+ version: '1.0'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 1.0.1
89
95
  - !ruby/object:Gem::Dependency
90
- name: activemodel
96
+ name: zeitwerk
91
97
  requirement: !ruby/object:Gem::Requirement
92
98
  requirements:
93
- - - ">="
99
+ - - "~>"
94
100
  - !ruby/object:Gem::Version
95
- version: '0'
96
- type: :development
101
+ version: '2.3'
102
+ type: :runtime
97
103
  prerelease: false
98
104
  version_requirements: !ruby/object:Gem::Requirement
99
105
  requirements:
100
- - - ">="
106
+ - - "~>"
101
107
  - !ruby/object:Gem::Version
102
- version: '0'
108
+ version: '2.3'
103
109
  - !ruby/object:Gem::Dependency
104
- name: bundler
110
+ name: activemodel
105
111
  requirement: !ruby/object:Gem::Requirement
106
112
  requirements:
107
113
  - - ">="
@@ -115,7 +121,7 @@ dependencies:
115
121
  - !ruby/object:Gem::Version
116
122
  version: '0'
117
123
  - !ruby/object:Gem::Dependency
118
- name: factory_bot
124
+ name: bundler
119
125
  requirement: !ruby/object:Gem::Requirement
120
126
  requirements:
121
127
  - - ">="
@@ -129,7 +135,7 @@ dependencies:
129
135
  - !ruby/object:Gem::Version
130
136
  version: '0'
131
137
  - !ruby/object:Gem::Dependency
132
- name: fasterer
138
+ name: factory_bot
133
139
  requirement: !ruby/object:Gem::Requirement
134
140
  requirements:
135
141
  - - ">="
@@ -267,30 +273,24 @@ files:
267
273
  - lib/shark_on_lambda/api_gateway_handler.rb
268
274
  - lib/shark_on_lambda/application.rb
269
275
  - lib/shark_on_lambda/base_controller.rb
270
- - lib/shark_on_lambda/concerns/resettable_singleton.rb
271
- - lib/shark_on_lambda/concerns/yaml_config_loader.rb
276
+ - lib/shark_on_lambda/cacheable.rb
272
277
  - lib/shark_on_lambda/configuration.rb
273
- - lib/shark_on_lambda/dispatcher.rb
274
278
  - lib/shark_on_lambda/errors/base.rb
275
279
  - lib/shark_on_lambda/errors/base_serializer.rb
276
- - lib/shark_on_lambda/inferrers/name_inferrer.rb
277
280
  - lib/shark_on_lambda/inferrers/serializer_inferrer.rb
278
- - lib/shark_on_lambda/jsonapi_controller.rb
279
281
  - lib/shark_on_lambda/jsonapi_parameters.rb
280
282
  - lib/shark_on_lambda/jsonapi_renderer.rb
281
283
  - lib/shark_on_lambda/middleware/base.rb
282
284
  - lib/shark_on_lambda/middleware/honeybadger.rb
283
285
  - lib/shark_on_lambda/middleware/jsonapi_rescuer.rb
284
286
  - lib/shark_on_lambda/middleware/lambda_logger.rb
285
- - lib/shark_on_lambda/middleware/rescuer.rb
286
- - lib/shark_on_lambda/query.rb
287
- - lib/shark_on_lambda/rack_adapters/api_gateway.rb
287
+ - lib/shark_on_lambda/rake_tasks.rb
288
288
  - lib/shark_on_lambda/request.rb
289
289
  - lib/shark_on_lambda/response.rb
290
290
  - lib/shark_on_lambda/rspec/env_builder.rb
291
291
  - lib/shark_on_lambda/rspec/helpers.rb
292
- - lib/shark_on_lambda/rspec/jsonapi_helpers.rb
293
- - lib/shark_on_lambda/secrets.rb
292
+ - lib/shark_on_lambda/rspec/request_helpers.rb
293
+ - lib/shark_on_lambda/rspec/response_helpers.rb
294
294
  - lib/shark_on_lambda/version.rb
295
295
  - shark-on-lambda.gemspec
296
296
  homepage: https://github.com/Skudo/shark-on-lambda
@@ -305,7 +305,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
305
305
  requirements:
306
306
  - - ">="
307
307
  - !ruby/object:Gem::Version
308
- version: '0'
308
+ version: '2.5'
309
309
  required_rubygems_version: !ruby/object:Gem::Requirement
310
310
  requirements:
311
311
  - - ">="
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SharkOnLambda
4
- module Concerns
5
- module ResettableSingleton
6
- def self.included(base)
7
- base.include(Singleton)
8
- base.extend(ClassMethods)
9
- end
10
-
11
- module ClassMethods
12
- def reset
13
- @singleton__instance__ = nil
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SharkOnLambda
4
- module Concerns
5
- module YamlConfigLoader
6
- def load_yaml_files(stage:, fallback: :default, paths:)
7
- result = HashWithIndifferentAccess.new
8
- paths.each do |path|
9
- data = load_yaml_file(stage: stage, fallback: fallback, path: path)
10
- result.deep_merge!(data)
11
- end
12
- result
13
- end
14
-
15
- protected
16
-
17
- def load_yaml_file(stage:, fallback:, path:)
18
- return {} unless File.exist?(path)
19
-
20
- data = YAML.load_file(path)
21
- return {} unless data.is_a?(Hash)
22
-
23
- data = data.with_indifferent_access
24
- data[stage] || data[fallback] || {}
25
- end
26
- end
27
- end
28
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SharkOnLambda
4
- class Dispatcher
5
- def call(env)
6
- request = Request.new(env)
7
- response = Response.new
8
-
9
- controller = controller_class(env)
10
- action = controller_action(env)
11
- controller.dispatch(action, request, response)
12
-
13
- response.prepare!
14
- end
15
-
16
- private
17
-
18
- def controller_action(env)
19
- env['shark.action']
20
- end
21
-
22
- def controller_class(env)
23
- env['shark.controller'].camelize.constantize
24
- end
25
- end
26
- end
@@ -1,66 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SharkOnLambda
4
- module Inferrers
5
- class NameInferrer
6
- class << self
7
- def from_controller_name(class_name)
8
- from_name(:controller, class_name)
9
- end
10
-
11
- def from_deserializer_name(class_name)
12
- from_name(:deserializer, class_name)
13
- end
14
-
15
- def from_handler_name(class_name)
16
- from_name(:handler, class_name)
17
- end
18
-
19
- def from_model_name(class_name)
20
- from_name(:model, class_name)
21
- end
22
-
23
- def from_serializer_name(class_name)
24
- from_name(:serializer, class_name)
25
- end
26
-
27
- protected
28
-
29
- def from_name(type, class_name)
30
- base = class_name.underscore
31
- base = case type
32
- when :controller, :deserializer, :handler, :serializer
33
- base.sub(/_#{type}\z/, '')
34
- when :model
35
- base
36
- end
37
- new(base)
38
- end
39
- end
40
-
41
- def initialize(base)
42
- @base = base
43
- end
44
-
45
- def controller
46
- "#{@base}_controller".camelize
47
- end
48
-
49
- def deserializer
50
- "#{@base}_deserializer".camelize
51
- end
52
-
53
- def handler
54
- "#{@base}_handler".camelize
55
- end
56
-
57
- def model
58
- @base.camelize
59
- end
60
-
61
- def serializer
62
- "#{@base}_serializer".camelize
63
- end
64
- end
65
- end
66
- end
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SharkOnLambda
4
- class JsonapiController < BaseController
5
- ActionController::Renderers.add :jsonapi do |object, options|
6
- response.set_header('content-type', 'application/vnd.api+json')
7
- return { data: {} }.to_json if object.nil?
8
-
9
- jsonapi_params = JsonapiParameters.new(params)
10
- jsonapi_renderer = JsonapiRenderer.new(object)
11
- render_options = jsonapi_params.to_h.deep_merge(options)
12
-
13
- jsonapi_object = jsonapi_renderer.render(render_options)
14
- response.status = jsonapi_renderer.status
15
-
16
- jsonapi_object.to_json
17
- end
18
-
19
- def redirect_to(options = {}, response_status = {})
20
- super
21
- return if response_status[:status] == 304
22
-
23
- self.response_body = { data: {} }.to_json
24
- end
25
-
26
- def render(object, options = {})
27
- options.merge!(jsonapi: object, content_type: 'application/vnd.api+json')
28
-
29
- super(options)
30
- end
31
- end
32
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SharkOnLambda
4
- module Middleware
5
- class Rescuer < Base
6
- def call!(env)
7
- app.call(env)
8
- rescue Errors::Base => e
9
- rescue_shark_error(e)
10
- rescue StandardError => e
11
- rescue_standard_error(e)
12
- end
13
-
14
- private
15
-
16
- def error_response(status, headers, message)
17
- response_body = Rack::BodyProxy.new([message]) do
18
- message.close if message.respond_to?(:close)
19
- end
20
-
21
- [status, headers, response_body]
22
- end
23
-
24
- def rescue_shark_error(error)
25
- status = error.status || 500
26
- error_response(status, {}, error.message)
27
- end
28
-
29
- def rescue_standard_error(error)
30
- SharkOnLambda.logger.error(error.message)
31
- SharkOnLambda.logger.error(error.backtrace.join("\n"))
32
-
33
- error_response(500, {}, error.message)
34
- end
35
- end
36
- end
37
- end