shark-on-lambda 0.0.0 → 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.gitlab-ci.yml +13 -0
  4. data/.rubocop.yml +13 -0
  5. data/.travis.yml +9 -2
  6. data/README.md +184 -18
  7. data/Rakefile +2 -0
  8. data/bin/console +4 -9
  9. data/changelog.md +17 -0
  10. data/gems.locked +92 -0
  11. data/{Gemfile → gems.rb} +2 -1
  12. data/lib/shark-on-lambda.rb +1 -5
  13. data/lib/shark_on_lambda.rb +104 -1
  14. data/lib/shark_on_lambda/api_gateway/base_controller.rb +76 -0
  15. data/lib/shark_on_lambda/api_gateway/base_handler.rb +82 -0
  16. data/lib/shark_on_lambda/api_gateway/concerns/http_response_validation.rb +61 -0
  17. data/lib/shark_on_lambda/api_gateway/errors.rb +49 -0
  18. data/lib/shark_on_lambda/api_gateway/headers.rb +37 -0
  19. data/lib/shark_on_lambda/api_gateway/jsonapi_controller.rb +77 -0
  20. data/lib/shark_on_lambda/api_gateway/jsonapi_parameters.rb +68 -0
  21. data/lib/shark_on_lambda/api_gateway/jsonapi_renderer.rb +105 -0
  22. data/lib/shark_on_lambda/api_gateway/parameters.rb +18 -0
  23. data/lib/shark_on_lambda/api_gateway/query.rb +69 -0
  24. data/lib/shark_on_lambda/api_gateway/request.rb +148 -0
  25. data/lib/shark_on_lambda/api_gateway/response.rb +82 -0
  26. data/lib/shark_on_lambda/api_gateway/serializers/base_error_serializer.rb +20 -0
  27. data/lib/shark_on_lambda/concerns/filter_actions.rb +82 -0
  28. data/lib/shark_on_lambda/concerns/resettable_singleton.rb +18 -0
  29. data/lib/shark_on_lambda/concerns/yaml_config_loader.rb +28 -0
  30. data/lib/shark_on_lambda/configuration.rb +71 -0
  31. data/lib/shark_on_lambda/inferrers/name_inferrer.rb +66 -0
  32. data/lib/shark_on_lambda/inferrers/serializer_inferrer.rb +45 -0
  33. data/lib/shark_on_lambda/secrets.rb +43 -0
  34. data/lib/shark_on_lambda/tasks.rb +3 -0
  35. data/lib/shark_on_lambda/tasks/build.rake +146 -0
  36. data/lib/{shark-on-lambda → shark_on_lambda}/version.rb +1 -1
  37. data/shark-on-lambda.gemspec +21 -6
  38. metadata +158 -20
  39. data/Gemfile.lock +0 -35
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 98f01b23b24cf2ba3b39241fbd06f8c007f06bd46cac36f7bed9c2e55a94b804
4
- data.tar.gz: a3311630cb31d1bffac58fe45353a19e49530558a2ccdcf8f1026d6241c35ce1
3
+ metadata.gz: 44a4ee1f3d1628311fba41801df9dee659ebae05ad000992310f345248c26a16
4
+ data.tar.gz: 5b807fc9fe412d64b23c2eee28dd8462f7375302f6a71b89c99e2b023ebbb90a
5
5
  SHA512:
6
- metadata.gz: ca6c0dd4f9be066778409ca6e89e216906e1e924bd39b403fdb7fce990ba898145bd0ac1f9f9e225840657f5094aa973a9f0a79f53fabb4593eebaf018ea9ad3
7
- data.tar.gz: 6216f2cf31dde529ac2b9cae27f931089a54497486f69836bb76dd600cdcf7b5b1c15bb79a0ab7a3e5a97e5e5b5c68c94d29a2df494c17b756eb1904c49a2720
6
+ metadata.gz: 6e0d9987dce6c080ef81c9802df11c29c7e020a23a04e60c70b8c024add57ff8facead26ff82a8e6ead6b7029b3b491162aea23ac31d8ec1cbfbbbad45c186e6
7
+ data.tar.gz: 2d95aee2c8d0a82a8c53adeea24a77756f1e63f731653265983b56956105b00f9096e480747778daed50dd7ef1066a276d702d7de0e9662842e9a3a609129592
data/.gitignore CHANGED
@@ -9,3 +9,8 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ # Add gems to `vendor/cache`
14
+ vendor/cache/*
15
+ !vendor/cache/bima*.gem
16
+ !vendor/cache/shark*.gem
@@ -0,0 +1,13 @@
1
+ rspec:
2
+ image: ruby:2.5
3
+ script:
4
+ - gem install bundler
5
+ - bundle install
6
+ - bundle exec rake spec
7
+
8
+ rubocop:
9
+ image: ruby:2.5
10
+ script:
11
+ - gem install bundler
12
+ - bundle install
13
+ - bundle exec rubocop
@@ -1,3 +1,16 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Metrics/BlockLength:
5
+ Exclude:
6
+ - 'spec/**/*_spec.rb'
7
+ - 'spec/factories/*.rb'
8
+
1
9
  Naming/FileName:
2
10
  Exclude:
3
11
  - lib/shark-on-lambda.rb
12
+ - spec/shark-on-lambda_spec.rb
13
+
14
+ # TODO: Add documentation and remove the Style/Documentation exception.
15
+ Style/Documentation:
16
+ Enabled: false
@@ -3,5 +3,12 @@ sudo: false
3
3
  language: ruby
4
4
  cache: bundler
5
5
  rvm:
6
- - 2.6.0
7
- before_install: gem install bundler -v 1.17.2
6
+ - 2.5.3
7
+
8
+ install:
9
+ - gem install bundler
10
+ - bundle install
11
+
12
+ script:
13
+ - bundle exec rake spec
14
+ - bundle exec rubocop
data/README.md CHANGED
@@ -1,43 +1,209 @@
1
- # Shark::On::Lambda
1
+ # shark-on-lambda
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/shark/on/lambda`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Travis CI Build Status](https://travis-ci.org/Skudo/shark-on-lambda.svg?branch=develop)](https://travis-ci.org/Skudo/shark-on-lambda)
4
+ [![CodeClimate Maintainability Score](https://api.codeclimate.com/v1/badges/fb0c16b3c6212f97b753/maintainability)](https://codeclimate.com/github/Skudo/shark-on-lambda/maintainability)
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ `shark-on-lambda` provides a lightweight framework to write Ruby on AWS Lambda
7
+ whilst using a familiar approach to web services known from e. g. Ruby on Rails.
8
+
9
+ ## History
10
+
11
+ For a long time, "going serverless" on AWS Lambda was a world Ruby developers
12
+ only could explore using methods such as packing their own Ruby binaries,
13
+ going the JRuby way, running on the Java VM, or using Ruby On Jets.
14
+
15
+ When AWS went public with the Ruby 2.5 runtime for AWS Lambda at the end of
16
+ 2018, that changed for Ruby developers and suddenly, a whole new world opened.
17
+
18
+ Since it is possible to run any Rack-based application on AWS Lambda, you can
19
+ even run your own Rails application there, if you want to.
20
+
21
+ However, if you prefer a more lightweight solution, `shark-on-lambda` may be
22
+ of interest to you, as it offers you a similar approach to things whilst
23
+ maintaining a smaller memory footprint.
24
+
25
+ ## Changelog
26
+
27
+ ### Pre-1.0
28
+ * Supports events from the API Gateway.
6
29
 
7
30
  ## Installation
8
31
 
9
- Add this line to your application's Gemfile:
32
+ Add this line to your application's `gems.rb`:
10
33
 
11
34
  ```ruby
12
- gem 'shark-on-lambda'
35
+ gem 'shark_on_lambda'
13
36
  ```
14
37
 
15
38
  And then execute:
16
39
 
17
40
  $ bundle
18
41
 
19
- Or install it yourself as:
42
+ Or install it yourself:
20
43
 
21
- $ gem install shark-on-lambda
44
+ $ gem install shark_on_lambda
22
45
 
23
- ## Usage
46
+ ## Handlers
24
47
 
25
- TODO: Write usage instructions here
48
+ Handlers are the entry points for Lambda function invocation, e. g. when they
49
+ are triggered by HTTP requests on the API Gateway. They also are responsible for
50
+ responding with a well-formed response to the caller.
26
51
 
27
- ## Development
52
+ ```ruby
53
+ class MyHandler < SharkOnLambda::ApiGateway::BaseHandler
54
+ end
55
+ ```
28
56
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+ By inheriting from `SharkOnLambda::ApiGateway::BaseHandler`, your own handler
58
+ class is indirectly tied to your controller class by convention: It assumes
59
+ the controller name is `MyController` and it will dispatch events to that
60
+ controller.
30
61
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
+ If you however bring your own class with a different name, you can configure
63
+ your handler to use that controller instead:
32
64
 
33
- ## Contributing
65
+ ```ruby
66
+ class MyHandler < SharkOnLambda::ApiGateway::BaseHandler
67
+ self.controller_class = AnotherController
68
+ end
69
+ ```
34
70
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/shark-on-lambda. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
71
+ All controller methods are going to be automagically mapped to class methods
72
+ on the handler class.
73
+
74
+ ```ruby
75
+ class MyController < SharkOnLambda::ApiGateway::BaseController
76
+ def index
77
+ end
78
+
79
+ def show
80
+ end
81
+ end
82
+
83
+ class MyHandler < SharkOnLambda::ApiGateway::BaseHandler
84
+ end
85
+ ```
86
+
87
+ `MyHandler` will respond to `.index` and `.show` class methods that accept the
88
+ `event` and `context` objects from the API Gateway. Those are passed to
89
+ `MyController` and eventually, the controller method that corresponds to the
90
+ handler class method is called.
91
+
92
+ ## Controllers
93
+
94
+ Controllers are similar to Rails controllers: In addition to having access to
95
+ the AWS Lambda `event` and `context` objects, you also have access to `params`,
96
+ `request`, and `response` objects that are derived from the `event` object.
97
+
98
+ ### "Basic" controllers
99
+
100
+ You also have access to the `render` and `redirect_to` methods __that are not as
101
+ powerful as the Rails equivalent by far__. There are none of the `render_*`
102
+ functions you may know from Rails and `render` does not really support multiple
103
+ renderers (yet).
104
+
105
+ ```ruby
106
+ class MyController < SharkOnLambda::ApiGateway::BaseController
107
+ def index
108
+ # Make the API Gateway respond with a 201 response saying "Hello, World!"
109
+ # in the response body.
110
+ #
111
+ # The default status code for `render` is 200.
112
+ render 'Hello, World!', status: 201
113
+ end
114
+
115
+ def show
116
+ # Does what you think it does.
117
+ #
118
+ # The default status code for `redirect_to` is 307.
119
+ redirect_to 'https://github.com', status: 302
120
+ end
121
+ end
122
+ ```
36
123
 
37
- ## License
124
+ ### _JSON API_-compliant controllers
38
125
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
126
+ If you inherit your controller from
127
+ `SharkOnLambda::ApiGateway::JsonapiController`, `render` and `redirect_to` will
128
+ create _JSON API_-compliant responses.
129
+
130
+ You however __must__ have a serialiser for the objects you want to render.
131
+ Otherwise, rendering will fail and you will receive an _Internal Server Error_
132
+ instead.
133
+
134
+ _JSON API_ `fields` and `include` query string parameters are automagically
135
+ being parsed and used for rendering automagically by the _JSON API_ renderer.
136
+
137
+ ## _JSON API_ serialisers
138
+
139
+ We use `jsonapi-serializable` (and `jsonapi-rb` in general) for our
140
+ _JSON API_ compatibility. Therefore, we expect the serialisers to be inherited
141
+ from `::JSONAPI::Serializable::Resource` (or `::JSONAPI::Serializable::Error`).
142
+
143
+ ```ruby
144
+ class SomethingSerializer < ::JSONAPI::Serializable::Resource
145
+ type :somethings
146
+
147
+ attributes :foo, :bar, :baz
148
+ end
149
+ ```
150
+
151
+ ### Serialiser lookup
152
+
153
+ Each object that is to be serialised requires a serialiser class that knows
154
+ how to serialise it. We also implemented a convention over configuration
155
+ approach here to determine which serialiser class to use:
156
+
157
+ 1) If the object is an instance of `YourClass` and `YourClassSerializer` is
158
+ defined, `YourClassSerializer` is used as the serialiser class.
159
+
160
+ 2) If the object is an instance of `YourClass` and `YourClassSerializer` is
161
+ __not__ defined, check whether 1) applies for any of the ancestors of
162
+ `YourClass`.
163
+
164
+ #### Example
165
+
166
+ If `YourClass` has `YourBaseClass`, `Object`, and `BasicObject` as ancestor
167
+ classes, the first existing one of `YourClassSerializer`,
168
+ `YourBaseClassSerializer`, `ObjectSerializer`, and `BasicObjectSerializer`
169
+ (in that order) is used. If none of those exist, serialisation will fail with
170
+ an _Internal Server Error_.
171
+
172
+ ## Configuration
173
+
174
+ You can "initialise" `SharkOnLambda` using its `.initialize!` class method.
175
+
176
+ `SharkOnLambda.initialize!` yields to a block with the `config` and `secrets`
177
+ objects where you can access and add to those two objects.
178
+
179
+ ```ruby
180
+ SharkOnLambda.initialize! do |config, secrets|
181
+ # Do things here.
182
+ end
183
+ ```
184
+
185
+ Calling `SharkOnLambda.initialize!` does these things (in order):
186
+
187
+ 1. Process the block passed to `.initialize!`.
188
+ 2. Load `config/settings.yml` and `config/settings.local.yml` into the
189
+ `config` object.
190
+ 3. Load `config/database.yml` and `config/database.local.yml` into
191
+ `config.database`.
192
+ 4. Load `config/secrets.yml` and `secrets/secrets.local.yml` into the
193
+ `secrets` object.
194
+ 5. Load all `config/initializers/*.rb` files.
195
+
196
+ If `SharkOnLambda.config.stage` was set inside the block passed to
197
+ `.initialize!`, configurations and secrets for that stage will be merged into
198
+ the default set (the `default` node in the YAML files) of configuration and
199
+ secrets, overwriting values where applicable.
200
+
201
+ ## Development
40
202
 
41
- ## Code of Conduct
203
+ Clone this repository and change away. Then, once you are done, please submit
204
+ a pull request at https://github.com/Skudo/shark-on-lambda/pulls.
42
205
 
43
- Everyone interacting in the Shark::On::Lambda project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/shark-on-lambda/blob/master/CODE_OF_CONDUCT.md).
206
+ However, please make sure the tests (`bundle exec rake spec`) and `rubocop`
207
+ (`bundle exec rubocop`) pass before submitting a pull request. Pull requests
208
+ that do not pass both on the CI system will not be merged. On the same note,
209
+ untested code will not be merged, either.
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
 
@@ -1,14 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
+ #
3
+ # frozen_string_literal: true
2
4
 
3
5
  require 'bundler/setup'
4
- require 'shark-on-lambda'
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
6
  require 'irb'
7
+ require 'shark_on_lambda'
8
+
14
9
  IRB.start(__FILE__)
@@ -0,0 +1,17 @@
1
+ ## Changelog
2
+
3
+ #### 0.6.7
4
+
5
+ - [Fix] Build 0.6.6 using the wrong branch.
6
+
7
+ #### 0.6.6
8
+
9
+ - [Fix] Handle the quirkiness of API Gateway query strings properly.
10
+
11
+ #### 0.6.5
12
+
13
+ - [Fix] Parse nested query string parameters correctly.
14
+
15
+ #### 0.6.4
16
+
17
+ - Initial public release.
@@ -0,0 +1,92 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ shark-on-lambda (0.6.7)
5
+ activesupport
6
+ jsonapi-rb
7
+ rack
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (6.0.0)
13
+ activesupport (= 6.0.0)
14
+ activesupport (6.0.0)
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ i18n (>= 0.7, < 2)
17
+ minitest (~> 5.1)
18
+ tzinfo (~> 1.1)
19
+ zeitwerk (~> 2.1, >= 2.1.8)
20
+ ast (2.4.0)
21
+ concurrent-ruby (1.1.5)
22
+ diff-lcs (1.3)
23
+ docile (1.3.2)
24
+ factory_bot (5.1.0)
25
+ activesupport (>= 4.2.0)
26
+ i18n (1.6.0)
27
+ concurrent-ruby (~> 1.0)
28
+ jaro_winkler (1.5.3)
29
+ json (2.2.0)
30
+ jsonapi-deserializable (0.2.0)
31
+ jsonapi-rb (0.5.0)
32
+ jsonapi-deserializable (~> 0.2.0)
33
+ jsonapi-serializable (~> 0.3.0)
34
+ jsonapi-renderer (0.2.2)
35
+ jsonapi-serializable (0.3.1)
36
+ jsonapi-renderer (~> 0.2.0)
37
+ minitest (5.12.0)
38
+ parallel (1.17.0)
39
+ parser (2.6.4.1)
40
+ ast (~> 2.4.0)
41
+ rack (2.0.7)
42
+ rainbow (3.0.0)
43
+ rake (13.0.0)
44
+ rspec (3.8.0)
45
+ rspec-core (~> 3.8.0)
46
+ rspec-expectations (~> 3.8.0)
47
+ rspec-mocks (~> 3.8.0)
48
+ rspec-core (3.8.2)
49
+ rspec-support (~> 3.8.0)
50
+ rspec-expectations (3.8.4)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.8.0)
53
+ rspec-mocks (3.8.1)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.8.0)
56
+ rspec-support (3.8.2)
57
+ rubocop (0.74.0)
58
+ jaro_winkler (~> 1.5.1)
59
+ parallel (~> 1.10)
60
+ parser (>= 2.6)
61
+ rainbow (>= 2.2.2, < 4.0)
62
+ ruby-progressbar (~> 1.7)
63
+ unicode-display_width (>= 1.4.0, < 1.7)
64
+ ruby-progressbar (1.10.1)
65
+ simplecov (0.17.1)
66
+ docile (~> 1.1)
67
+ json (>= 1.8, < 3)
68
+ simplecov-html (~> 0.10.0)
69
+ simplecov-html (0.10.2)
70
+ thread_safe (0.3.6)
71
+ tzinfo (1.2.5)
72
+ thread_safe (~> 0.1)
73
+ unicode-display_width (1.6.0)
74
+ yard (0.9.20)
75
+ zeitwerk (2.1.10)
76
+
77
+ PLATFORMS
78
+ ruby
79
+
80
+ DEPENDENCIES
81
+ activemodel
82
+ bundler
83
+ factory_bot
84
+ rake
85
+ rspec
86
+ rubocop
87
+ shark-on-lambda!
88
+ simplecov
89
+ yard
90
+
91
+ BUNDLED WITH
92
+ 2.0.2
@@ -1,6 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
6
 
5
- # Specify your gem's dependencies in shark-on-lambda.gemspec
6
7
  gemspec
@@ -1,7 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'shark-on-lambda/version'
4
-
5
- # Top-level module for this gem.
6
- module SharkOnLambda
7
- end
3
+ require 'shark_on_lambda'