lambda_open_api 0.1.0 → 0.1.2

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: 7c565a700e0bceffbac7749e3dda568cc957a12134d132ee24ec5c525851f394
4
- data.tar.gz: 8a478edc71b50c63a18654c66ad577f1454565d2f5699eeb1b0337e108c3f73e
3
+ metadata.gz: 829f0a399f63ee1437bc2610fe834e5dde1d7748f5cba35a453015af14320b61
4
+ data.tar.gz: d0f6ee7fa8f5bf0248074e0d1437bb628c80951f16e4b190fe5630039f033058
5
5
  SHA512:
6
- metadata.gz: 88788828d826014db9639e1e0525185e3a626ce8c645195a24211d03974bcd4dbccb85f4faa3d3347fe2c864b40ef46f6e92dfbcfffce64b7e8e90f9630fc5d1
7
- data.tar.gz: ca67c4eb57ea54bc37f0e90c1a5c38c2f8925600a999b33e618430733901dcfae221e33f06c580f813a49cd8f4a0f53dfd7cfe8037abe755e0945c0e099a8f7e
6
+ metadata.gz: '09b5dedbebbcef578ef0763ddf8d683a318f44e4695844719b853043dbc7302c1fb0a8ab532b993d6806fee8f51997d6eec4ed768304d9d87b1fdf331ad7fc53'
7
+ data.tar.gz: '0831a372977b701c2602f33b75796178e1c324b4ee761b96e4aef4eb30d4218045011a0af29efae70e016f0ae825787fd45b4a5bb2a23264ba108a23c11396ef'
data/README.md CHANGED
@@ -1,22 +1,191 @@
1
1
  # LambdaOpenApi
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/lambda_open_api`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- Install the gem and add to the application's Gemfile by executing:
10
-
11
- $ bundle add lambda_open_api
12
-
13
- If bundler is not being used to manage dependencies, install the gem by executing:
14
-
15
- $ gem install lambda_open_api
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
- TODO: Write usage instructions here
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/[USERNAME]/lambda_open_api.
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
 
@@ -50,7 +50,7 @@ module LambdaOpenApi
50
50
  def crud_verb(verb, path)
51
51
  @action = LambdaOpenApi::Action.new(name: @name, http_verb: verb, path_name: path)
52
52
  @event = @default_event.dup
53
- @event.request_context = {"httpMethod": verb.upcase}
53
+ @event.request_context = {"httpMethod" => verb.upcase}
54
54
 
55
55
  yield
56
56
 
@@ -65,14 +65,12 @@ module LambdaOpenApi
65
65
  end
66
66
 
67
67
  def run_example(test_name=nil, &block)
68
- lambda_response_value = invoke_lambda
69
- @action.set_response(lambda_response_value)
68
+ klass = create_class
70
69
 
71
70
  it "#{test_name || @action.path_name}" do
72
- @lambda_response_value = lambda_response_value
73
-
71
+ @klass = klass
74
72
  def lambda_response
75
- @lambda_response_value
73
+ @lambda_response ||= @klass.invoke
76
74
  end
77
75
 
78
76
  instance_eval &block
@@ -99,6 +97,33 @@ 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
+
118
+ invokcation.response_body
119
+ end
120
+ end)
121
+
122
+ klass.set_event(@event)
123
+ klass.set_described_class(described_class)
124
+ klass
125
+ end
126
+
102
127
  end
103
128
  end
104
129
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LambdaOpenApi
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
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.0
4
+ version: 0.1.2
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-30 00:00:00.000000000 Z
11
+ date: 2023-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest