yake 0.1.0 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +40 -33
- data/lib/yake/api.rb +1 -0
- data/lib/yake/dsl.rb +4 -5
- data/lib/yake/logger.rb +27 -19
- data/lib/yake/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: 5a82e9c31a2af64f0db3fe44f694da9328239bb47c1c4a9fb4734ff66172d96e
|
4
|
+
data.tar.gz: a5175886ce9623095fa52a94a7993d9fefc8f53604f7188b7cbb42cf3c3be6c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7107fffe0cc8edde7184c0063456b24047daff692fd2a610d551f67a1815a6f6b59fcad4a3e10ba215cb25a53c278f8b73701eb5e676febba473cdce2d5a7e1f
|
7
|
+
data.tar.gz: f4242662c6b17d314d3ff2f536d0d4016860c18276d30e48b0d39fb07891a282d713a390760a5f862c3d3637263d5a111afe79e23c7b72cd54831487fc9322ac
|
data/README.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# λake
|
2
2
|
|
3
|
+

|
4
|
+
[](https://github.com/amancevice/yake/actions)
|
5
|
+
[](https://codeclimate.com/github/amancevice/yake/test_coverage)
|
6
|
+
[](https://codeclimate.com/github/amancevice/yake/maintainability)
|
7
|
+
|
3
8
|
Write your AWS Lambda function handlers using a Rake-like declarative syntax:
|
4
9
|
|
5
10
|
```ruby
|
@@ -21,7 +26,7 @@ require "yake/api"
|
|
21
26
|
|
22
27
|
header "content-type" => "application/json"
|
23
28
|
|
24
|
-
get "/fizz" do
|
29
|
+
get "/fizz" do
|
25
30
|
respond 200, { ok: true }.to_json
|
26
31
|
end
|
27
32
|
|
@@ -54,19 +59,17 @@ Or install it yourself as:
|
|
54
59
|
gem install yake
|
55
60
|
```
|
56
61
|
|
57
|
-
## Why
|
62
|
+
## Why Is It Called "yake"?
|
58
63
|
|
59
|
-
|
64
|
+
"λ" + Rake, but "λ" is hard to type and I think "y" looks like a funny little upside-down-and-backwards Lambda symbol.
|
60
65
|
|
61
|
-
|
66
|
+
## Why Use It?
|
62
67
|
|
63
|
-
`yake`
|
68
|
+
So why use `yake` for your Lambda functions?
|
64
69
|
|
65
70
|
#### Event Logging
|
66
71
|
|
67
|
-
By default, the `handler` function wraps its block in log lines formatted to match the style of Amazon's native Lambda logs sent to CloudWatch. Each invocation of the handler
|
68
|
-
|
69
|
-
Example log lines:
|
72
|
+
By default, the `handler` function wraps its block in log lines formatted to match the style of Amazon's native Lambda logs sent to CloudWatch. Each invocation of the handler will log both the _input event_ and the _returned value_, prefixed with the ID of the request:
|
70
73
|
|
71
74
|
```
|
72
75
|
START RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf Version: $LATEST
|
@@ -77,14 +80,25 @@ END RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf
|
|
77
80
|
REPORT RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf Duration: 43.97 ms Billed Duration: 44 ms Memory Size: 128 MB Max Memory Used: 77 MB
|
78
81
|
```
|
79
82
|
|
80
|
-
|
83
|
+
Logging the request ID in this way makes gathering logs lines for a particular execution in CloudWatch much easier.
|
81
84
|
|
82
|
-
This feature can be disabled by adding a declaration in your handler:
|
85
|
+
This feature can be disabled by adding a declaration in your handler file:
|
83
86
|
|
84
87
|
```ruby
|
85
88
|
logging :off
|
86
89
|
```
|
87
90
|
|
91
|
+
Include `Yake::Logging` on a class to access this logger:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
class Fizz
|
95
|
+
include Yake::Logger
|
96
|
+
end
|
97
|
+
|
98
|
+
Fizz.new.logger == Yake.logger
|
99
|
+
# => true
|
100
|
+
```
|
101
|
+
|
88
102
|
#### API Routes
|
89
103
|
|
90
104
|
A common use of Lambda functions is as a proxy for API Gateway. Oftentimes users will deploy a single Lambda function to handle all requests coming from API Gateway.
|
@@ -94,41 +108,33 @@ Requiring the `yake/api` module will add the API-specific DSL into your handler.
|
|
94
108
|
Define API routes using Sinatra-like syntax
|
95
109
|
|
96
110
|
```ruby
|
97
|
-
# Declare 'DELETE /…' route key
|
98
111
|
delete "/…" do |event|
|
99
|
-
#
|
112
|
+
# Handle 'DELETE /…' route key events
|
100
113
|
end
|
101
114
|
|
102
|
-
# Declare 'GET /…' route key
|
103
115
|
get "/…" do |event|
|
104
|
-
#
|
116
|
+
# Handle 'GET /…' route key events
|
105
117
|
end
|
106
118
|
|
107
|
-
# Declare 'HEAD /…' route key
|
108
119
|
head "/…" do |event|
|
109
|
-
#
|
120
|
+
# Handle 'HEAD /…' route key events
|
110
121
|
end
|
111
122
|
|
112
|
-
# Declare 'OPTIONS /…' route key
|
113
123
|
options "/…" do |event|
|
114
|
-
#
|
124
|
+
# Handle 'OPTIONS /…' route key events
|
115
125
|
end
|
116
126
|
|
117
|
-
# Declare 'PATCH /…' route key
|
118
127
|
patch "/…" do |event|
|
119
|
-
#
|
128
|
+
# Handle 'PATCH /…' route key events
|
120
129
|
end
|
121
130
|
|
122
|
-
# Declare 'POST /…' route key
|
123
131
|
post "/…" do |event|
|
124
|
-
#
|
132
|
+
# Handle 'POST /…' route key events
|
125
133
|
end
|
126
134
|
|
127
|
-
# Declare 'PUT /…' route key
|
128
135
|
put "/…" do |event|
|
129
|
-
#
|
136
|
+
# Handle 'PUT /…' route key events
|
130
137
|
end
|
131
|
-
|
132
138
|
```
|
133
139
|
|
134
140
|
Helper methods are also made available to help produce a response for API Gateway:
|
@@ -137,31 +143,36 @@ Set a default header for ALL responses:
|
|
137
143
|
|
138
144
|
```ruby
|
139
145
|
header "content-type" => "application/json; charset=utf-8"
|
146
|
+
header "x-custom-header" => "fizz"
|
140
147
|
```
|
141
148
|
|
142
149
|
Produce an API Gateway-style response object:
|
143
150
|
|
144
151
|
```ruby
|
145
|
-
respond 200, { ok: true }.to_json, "x-extra-header" => "
|
152
|
+
respond 200, { ok: true }.to_json, "x-extra-header" => "buzz"
|
146
153
|
# {
|
147
154
|
# "statusCode" => 200,
|
148
155
|
# "body" => '{"ok":true}',
|
149
|
-
# "headers" => { "x-extra-header" => "
|
156
|
+
# "headers" => { "x-extra-header" => "buzz" }
|
150
157
|
# }
|
151
158
|
```
|
152
159
|
|
153
160
|
Route an event to one of the declared routes:
|
154
161
|
|
155
162
|
```ruby
|
156
|
-
|
163
|
+
handler :lambda_handler do |event|
|
157
164
|
route event
|
158
165
|
rescue Yake::UndeclaredRoute => err
|
159
166
|
respond 404, { message: err.message }.to_json
|
160
167
|
rescue => err
|
161
|
-
respond 500 { message: err.message }.to_json
|
168
|
+
respond 500, { message: err.message }.to_json
|
162
169
|
end
|
163
170
|
```
|
164
171
|
|
172
|
+
#### Zero Dependencies
|
173
|
+
|
174
|
+
Finally, `yake` does not depend on any other gems, using the Ruby stdlib only. This helps keep your Lambda packages slim & speedy.
|
175
|
+
|
165
176
|
## Development
|
166
177
|
|
167
178
|
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.
|
@@ -175,7 +186,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/amance
|
|
175
186
|
## License
|
176
187
|
|
177
188
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
178
|
-
|
179
|
-
```
|
180
|
-
|
181
|
-
```
|
data/lib/yake/api.rb
CHANGED
data/lib/yake/dsl.rb
CHANGED
@@ -10,17 +10,17 @@ module Yake
|
|
10
10
|
# Lambda handler task wrapper
|
11
11
|
def handler(name, &block)
|
12
12
|
define_method(name) do |event:nil, context:nil|
|
13
|
-
Yake.
|
13
|
+
Yake.wrap(event, context, &block)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
##
|
18
18
|
# Turn logging on/off
|
19
|
-
def logging(switch,
|
19
|
+
def logging(switch, logger = nil)
|
20
20
|
if switch == :on
|
21
|
-
Yake.logger =
|
21
|
+
Yake.logger = logger
|
22
22
|
elsif switch == :off
|
23
|
-
Yake.logger = nil
|
23
|
+
Yake.logger = ::Logger.new(nil)
|
24
24
|
else
|
25
25
|
raise Errors::UnknownLoggingSetting, switch
|
26
26
|
end
|
@@ -29,4 +29,3 @@ module Yake
|
|
29
29
|
end
|
30
30
|
|
31
31
|
extend Yake::DSL
|
32
|
-
Yake.logger = Yake::Logger.new
|
data/lib/yake/logger.rb
CHANGED
@@ -4,33 +4,41 @@ require "json"
|
|
4
4
|
require "logger"
|
5
5
|
|
6
6
|
module Yake
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
@
|
7
|
+
module Logger
|
8
|
+
attr_accessor :logger
|
9
|
+
|
10
|
+
def logger
|
11
|
+
@logger ||= Yake.logger
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
ensure
|
19
|
-
@progname = "-"
|
14
|
+
class << self
|
15
|
+
def new(logdev = $stdout)
|
16
|
+
::Logger.new(logdev, progname: "-", formatter: Formatter.new)
|
17
|
+
end
|
20
18
|
end
|
21
|
-
end
|
22
19
|
|
23
|
-
|
24
|
-
|
20
|
+
class Formatter < ::Logger::Formatter
|
21
|
+
Format = "%s %s %s\n"
|
25
22
|
|
26
|
-
|
27
|
-
|
23
|
+
def call(severity, time, progname, msg)
|
24
|
+
Format % [ severity, progname, msg2str(msg).strip ]
|
25
|
+
end
|
28
26
|
end
|
29
27
|
end
|
30
28
|
|
31
|
-
|
29
|
+
class << self
|
32
30
|
attr_accessor :logger
|
33
|
-
end
|
34
31
|
|
35
|
-
|
32
|
+
def logger
|
33
|
+
@logger ||= Logger.new
|
34
|
+
end
|
35
|
+
|
36
|
+
def wrap(event = nil, context = nil, &block)
|
37
|
+
logger.progname = "RequestId: #{ context.aws_request_id }" if context.respond_to?(:aws_request_id)
|
38
|
+
logger.info("EVENT #{ event.to_json }")
|
39
|
+
yield(event, context).tap { |res| logger.info("RETURN #{ res.to_json }") }
|
40
|
+
ensure
|
41
|
+
logger.progname = "-"
|
42
|
+
end
|
43
|
+
end
|
36
44
|
end
|
data/lib/yake/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yake
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Mancevice
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|