lambda_open_api 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +181 -12
- data/lib/lambda_open_api/builder.rb +34 -5
- data/lib/lambda_open_api/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 215da615c51f2cc4828de0551f0f3c8ff55e1139913125654f336f5b596a9641
|
4
|
+
data.tar.gz: 55aa015422479a62519535d4ac452b03fe1e994fcb6aac8cf919054007a9ab9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f288e0522addb39986627173c569a6ecead21a7ab7dd45e7bc9b72ec2e0dad79dacad4f1f5d038e7ac9aeebd6ba46f78ee76272ebc492f0c52a3dabfbeea6ac0
|
7
|
+
data.tar.gz: 1228582d556426675f94e02a0fb83be97494e0e6ed084e1f4d442101023847b03d2122418fc467cd30adacb7a29bc9ff3ebbf01d10b144f8eaa77a05393aa8ab
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,22 +1,191 @@
|
|
1
1
|
# LambdaOpenApi
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
This gem is a light weight DSL that works with rspec to allow developers to generate an OpenAPI (swagger) file based an AWS Lambda invoked by API Gateway. It works by writing a simple unit test for your lambda's code. When the test is executed, the input event and returned response are captured and used to build an OpenAPI file.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
7
|
+
Add to your gem file:
|
8
|
+
```ruby
|
9
|
+
group :test do
|
10
|
+
gem 'rspec'
|
11
|
+
gem 'lambda_open_api'
|
12
|
+
end
|
13
|
+
```
|
14
|
+
|
15
|
+
Create an initializer file to configure the gem.
|
16
|
+
```ruby
|
17
|
+
LambdaOpenApi.configure do |config|
|
18
|
+
config.file_name = "open_api.json"
|
19
|
+
config.title = "My Example Api"
|
20
|
+
config.description = "About this api"
|
21
|
+
config.version = "1"
|
22
|
+
config.host = "https://my_example_api.com"
|
23
|
+
config.schemes = ["https"]
|
24
|
+
config.consumes = ["application/json"]
|
25
|
+
config.produces = ["application/json"]
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
Include the gem in your spec hepler file `spec/spec_helper.rb` or any file that gets loaded before rspec is ran.
|
30
|
+
```ruby
|
31
|
+
require "lambda_open_api"
|
32
|
+
|
33
|
+
RSpec.configure do |config|
|
34
|
+
# ...
|
35
|
+
# the rest of your normal config
|
36
|
+
#...
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
That's it!
|
16
41
|
|
17
42
|
## Usage
|
18
43
|
|
19
|
-
|
44
|
+
Let's say we have a lmabda that looks something like this.
|
45
|
+
```ruby
|
46
|
+
require_relative '../spec_helper'
|
47
|
+
|
48
|
+
class MyLambda
|
49
|
+
def self.process(event:, context: {})
|
50
|
+
body = JSON.parse(event["body"])
|
51
|
+
|
52
|
+
# do something useful
|
53
|
+
|
54
|
+
{
|
55
|
+
statusCode: 200,
|
56
|
+
body: { message: "processed", name: body["name"], email: body["email"] }
|
57
|
+
}
|
58
|
+
end
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
We can write a spec file like this:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
RSpec.describe MyLambda do
|
66
|
+
|
67
|
+
resource "Users" do
|
68
|
+
|
69
|
+
get "users/{id}" do
|
70
|
+
path_summery "Some very high level details"
|
71
|
+
path_description "Some more details about this path"
|
72
|
+
|
73
|
+
example_case "200" do
|
74
|
+
parameter({id: 1})
|
75
|
+
|
76
|
+
event_body({
|
77
|
+
name: "Timbo Baggins",
|
78
|
+
email: "tbaggings@hotmail.com"
|
79
|
+
}.to_json)
|
80
|
+
|
81
|
+
event_headers({
|
82
|
+
"Api-Key" => "the_api_key"
|
83
|
+
})
|
84
|
+
|
85
|
+
run_example do
|
86
|
+
expect(lambda_response[:body]).to eq({:message => "processed", :email=>"tbaggings@hotmail.com", :name=>"Timbo Baggins"})
|
87
|
+
expect(lambda_response[:statusCode]).to eq(200)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
Run rspec
|
97
|
+
```bash
|
98
|
+
rspec
|
99
|
+
```
|
100
|
+
|
101
|
+
An Open Api file will be generated and saved that looks like this:
|
102
|
+
```json
|
103
|
+
{
|
104
|
+
"swagger": "2.0",
|
105
|
+
"info": {
|
106
|
+
"title": "My Example Api",
|
107
|
+
"description": "About this api",
|
108
|
+
"version": "1"
|
109
|
+
},
|
110
|
+
"host": "https://my_example.com",
|
111
|
+
"schemes": [
|
112
|
+
"https"
|
113
|
+
],
|
114
|
+
"consumes": [
|
115
|
+
"application/json"
|
116
|
+
],
|
117
|
+
"produces": [
|
118
|
+
"application/json"
|
119
|
+
],
|
120
|
+
"paths": {
|
121
|
+
"users/{id}": {
|
122
|
+
"get": {
|
123
|
+
"tags": [
|
124
|
+
"Users"
|
125
|
+
],
|
126
|
+
"summary": "Some very high level details",
|
127
|
+
"description": "Some more details about this path",
|
128
|
+
"consumes": [
|
129
|
+
"application/json"
|
130
|
+
],
|
131
|
+
"produces": [
|
132
|
+
"application/json"
|
133
|
+
],
|
134
|
+
"parameters": [
|
135
|
+
{
|
136
|
+
"name": "id",
|
137
|
+
"in": "path",
|
138
|
+
"description": "",
|
139
|
+
"required": true,
|
140
|
+
"type": "integer"
|
141
|
+
},
|
142
|
+
{
|
143
|
+
"name": "body",
|
144
|
+
"in": "body",
|
145
|
+
"description": "",
|
146
|
+
"required": false,
|
147
|
+
"schema": {
|
148
|
+
"description": "",
|
149
|
+
"type": "object",
|
150
|
+
"properties": {
|
151
|
+
"name": {
|
152
|
+
"type": "string"
|
153
|
+
},
|
154
|
+
"email": {
|
155
|
+
"type": "string"
|
156
|
+
}
|
157
|
+
},
|
158
|
+
"required": [
|
159
|
+
|
160
|
+
]
|
161
|
+
}
|
162
|
+
}
|
163
|
+
],
|
164
|
+
"responses": {
|
165
|
+
"200": {
|
166
|
+
"examples": {
|
167
|
+
"application/json": {
|
168
|
+
"statusCode": 200,
|
169
|
+
"body": {
|
170
|
+
"message": "processed",
|
171
|
+
"name": "Timbo Baggins",
|
172
|
+
"email": "tbaggings@hotmail.com"
|
173
|
+
}
|
174
|
+
}
|
175
|
+
}
|
176
|
+
}
|
177
|
+
}
|
178
|
+
}
|
179
|
+
}
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
```
|
184
|
+
|
185
|
+
From https://editor-next.swagger.io/
|
186
|
+
<img width="853" alt="Screen Shot 2023-01-30 at 9 42 18 AM" src="https://user-images.githubusercontent.com/9610694/215508235-c0edfadb-6df1-4cc5-83ef-43fdecc9dc34.png">
|
187
|
+
|
188
|
+
<img width="851" alt="Screen Shot 2023-01-30 at 9 42 04 AM" src="https://user-images.githubusercontent.com/9610694/215508248-b852cff0-2bf3-40ae-a601-43c0e6173831.png">
|
20
189
|
|
21
190
|
## Development
|
22
191
|
|
@@ -26,7 +195,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
26
195
|
|
27
196
|
## Contributing
|
28
197
|
|
29
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
198
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Timothyjb/lambda_open_api.
|
30
199
|
|
31
200
|
## License
|
32
201
|
|
@@ -65,14 +65,12 @@ module LambdaOpenApi
|
|
65
65
|
end
|
66
66
|
|
67
67
|
def run_example(test_name=nil, &block)
|
68
|
-
|
69
|
-
@action.set_response(lambda_response_value)
|
68
|
+
klass = create_class
|
70
69
|
|
71
70
|
it "#{test_name || @action.path_name}" do
|
72
|
-
@
|
73
|
-
|
71
|
+
@klass = klass
|
74
72
|
def lambda_response
|
75
|
-
@
|
73
|
+
@lambda_response ||= @klass.invoke
|
76
74
|
end
|
77
75
|
|
78
76
|
instance_eval &block
|
@@ -99,6 +97,37 @@ module LambdaOpenApi
|
|
99
97
|
OpenStruct.new(aws_request_id: "123")
|
100
98
|
end
|
101
99
|
|
100
|
+
def create_class
|
101
|
+
dynamic_name = "ClassName#{rand(10000)}"
|
102
|
+
klass = Object.const_set(dynamic_name, Class.new do
|
103
|
+
def self.set_event(event)
|
104
|
+
@event = event
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.event
|
108
|
+
@event
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.set_described_class(klass)
|
112
|
+
@klass = klass
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.invoke
|
116
|
+
invokcation = LambdaOpenApi::Invoker.new(klass: @klass, method: "process", event: @event.json, context: OpenStruct.new(aws_request_id: "123"))
|
117
|
+
@action.set_response(invokcation.response_body)
|
118
|
+
invokcation.response_body
|
119
|
+
end
|
120
|
+
def self.set_action(action)
|
121
|
+
@action = action
|
122
|
+
end
|
123
|
+
end)
|
124
|
+
|
125
|
+
klass.set_event(@event)
|
126
|
+
klass.set_action(@action)
|
127
|
+
klass.set_described_class(described_class)
|
128
|
+
klass
|
129
|
+
end
|
130
|
+
|
102
131
|
end
|
103
132
|
end
|
104
133
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lambda_open_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Timothyjb
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-01-
|
11
|
+
date: 2023-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|