yake 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6bd208d64f868cf763f2cb19ff4b766a771a3011b6edda1918cf4e3f9436b1c0
4
- data.tar.gz: 439c99ae77a312ad882f8d5b58061dd9841e7f163a6e01476a54845ad38efab5
3
+ metadata.gz: 6688bf834975490165ca48ff259e22402ec5dc417099c8c2291b4796eb76b5b2
4
+ data.tar.gz: d92d5faec4b6bd8b60a65962af51493176a4184a87a52de188f84367bce4002b
5
5
  SHA512:
6
- metadata.gz: 2052948f82b026240655aaf07d6a5a15dc687aa432d97fdcbec0b9c4cb88aca4c64f50d7051336f1a7b99d005b2f9d6234f50a750221897d86a6d033012200e6
7
- data.tar.gz: 8800efb2e6428321aadc94c84788f8918290ab620476f51e20d1e0cfd66170bd4a3002ff86c5d4554de5c3aceaa81ecefb14fcb393fe0f5ac368d70233a83429
6
+ metadata.gz: 2cf319bba7cd22f2c5f58a66c401dd77dfc2d050214d39d755748d9dc78baed88c603dbfdbbe3d233385e35270c73ec19ab9d3edae394ef9e6dbc2c89c9c1a34
7
+ data.tar.gz: 2221a9a8108091b59c9e0eeeb7959c675faee09385847ed5fbf9a75ebbc788be6a734c13eec4a948af883ce1210d9d7fe45401c1e0f3b0cba2efceab03a88df5
data/README.md CHANGED
@@ -9,7 +9,7 @@ Write your AWS Lambda function handlers using a Rake-like declarative syntax:
9
9
 
10
10
  ```ruby
11
11
  # ./lambda_function.rb
12
- require "yake"
12
+ require 'yake'
13
13
 
14
14
  handler :lambda_handler do |event|
15
15
  # Your code here
@@ -22,11 +22,11 @@ You can even declare Sinatra-like API Gateway routes for a main entrypoint:
22
22
 
23
23
  ```ruby
24
24
  # ./lambda_function.rb
25
- require "yake/api"
25
+ require 'yake/api'
26
26
 
27
- header "content-type" => "application/json"
27
+ header 'content-type' => 'application/json'
28
28
 
29
- get "/fizz" do
29
+ get '/fizz' do
30
30
  respond 200, { ok: true }.to_json
31
31
  end
32
32
 
@@ -44,7 +44,7 @@ end
44
44
  Add this line to your application's Gemfile:
45
45
 
46
46
  ```ruby
47
- gem "yake"
47
+ gem 'yake'
48
48
  ```
49
49
 
50
50
  And then execute:
@@ -82,10 +82,12 @@ REPORT RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf Duration: 43.97 ms Billed
82
82
 
83
83
  Logging the request ID in this way makes gathering logs lines for a particular execution in CloudWatch much easier.
84
84
 
85
- This feature can be disabled by adding a declaration in your handler file:
85
+ You can customize or disable the logger:
86
86
 
87
87
  ```ruby
88
- logging :off
88
+ logging :off # disables logging entirely
89
+ logging pretty: false # Logs event/result in compact JSON
90
+ logging :on, MyLogger.new # Use a custom logger
89
91
  ```
90
92
 
91
93
  Include `Yake::Logger` on a class to access this logger:
@@ -108,35 +110,35 @@ Requiring the `yake/api` module will add the API-specific DSL into your handler.
108
110
  Define API routes using Sinatra-like syntax
109
111
 
110
112
  ```ruby
111
- any "/…" do |event|
113
+ any '/…' do |event|
112
114
  # Handle 'ANY /…' route key events
113
115
  end
114
116
 
115
- delete "/…" do |event|
117
+ delete '/…' do |event|
116
118
  # Handle 'DELETE /…' route key events
117
119
  end
118
120
 
119
- get "/…" do |event|
121
+ get '/…' do |event|
120
122
  # Handle 'GET /…' route key events
121
123
  end
122
124
 
123
- head "/…" do |event|
125
+ head '/…' do |event|
124
126
  # Handle 'HEAD /…' route key events
125
127
  end
126
128
 
127
- options "/…" do |event|
129
+ options '/…' do |event|
128
130
  # Handle 'OPTIONS /…' route key events
129
131
  end
130
132
 
131
- patch "/…" do |event|
133
+ patch '/…' do |event|
132
134
  # Handle 'PATCH /…' route key events
133
135
  end
134
136
 
135
- post "/…" do |event|
137
+ post '/…' do |event|
136
138
  # Handle 'POST /…' route key events
137
139
  end
138
140
 
139
- put "/…" do |event|
141
+ put '/…' do |event|
140
142
  # Handle 'PUT /…' route key events
141
143
  end
142
144
  ```
@@ -146,14 +148,14 @@ Helper methods are also made available to help produce a response for API Gatewa
146
148
  Set a default header for ALL responses:
147
149
 
148
150
  ```ruby
149
- header "content-type" => "application/json; charset=utf-8"
150
- header "x-custom-header" => "fizz"
151
+ header 'content-type' => 'application/json; charset=utf-8'
152
+ header 'x-custom-header' => 'fizz'
151
153
  ```
152
154
 
153
155
  Produce an API Gateway-style response object:
154
156
 
155
157
  ```ruby
156
- respond 200, { ok: true }.to_json, "x-extra-header" => "buzz"
158
+ respond 200, { ok: true }.to_json, 'x-extra-header' => 'buzz'
157
159
  # {
158
160
  # "statusCode" => 200,
159
161
  # "body" => '{"ok":true}',
@@ -177,11 +179,121 @@ end
177
179
 
178
180
  Finally, `yake` does not depend on any other gems, using the Ruby stdlib only. This helps keep your Lambda packages slim & speedy.
179
181
 
180
- ## Development
182
+ ## Support Helpers
183
+
184
+ As of `v0.5`, `yake` comes with a support module for common transformations.
185
+
186
+ `Hash` transformations:
187
+
188
+ ```ruby
189
+ require 'yake/support'
190
+
191
+ { fizz: 'buzz' }.encode64
192
+ # => "eyJmaXp6IjoiYnV6eiJ9\n"
193
+
194
+ { fizz: 'buzz' }.strict_encode64
195
+ # => "eyJmaXp6IjoiYnV6eiJ9"
196
+
197
+ { 'fizz' => { 'buzz' => %w[jazz fuzz] } }.symbolize_names
198
+ # => { :fizz => { :buzz => ["jazz", "fuzz"] } }
199
+
200
+ { fizz: 'buzz' }.to_form
201
+ # => "fizz=buzz"
202
+ ```
203
+
204
+ `Integer` transformations:
205
+
206
+ ```ruby
207
+ 7.days
208
+ # => 604800
209
+
210
+ 7.hours
211
+ # => 25200
212
+
213
+ 7.minutes
214
+ # => 420
215
+
216
+ 1234567890.utc
217
+ # => 2009-02-13 23:31:30 UTC
218
+ ```
219
+
220
+ `String` transformations:
221
+
222
+ ```ruby
223
+ 'snake_case_string'.camel_case
224
+ # => SnakeCaseString
225
+
226
+ "Zml6eg==\n".decode64
227
+ # => "fizz"
228
+
229
+ 'fizz'.encode64
230
+ # => "Zml6eg==\n"
231
+
232
+ 'CamelCaseString'.snake_case
233
+ # => 'camel_case_string'
234
+
235
+ 'Zml6eg=='.strict_decode64
236
+ # => "fizz"
237
+
238
+ 'fizz'.strict_encode64
239
+ # => "Zml6eg=="
240
+
241
+ '{"fizz":"buzz"}'.to_h_from_json
242
+ # => { "fizz" => "buzz" }
181
243
 
182
- 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.
244
+ 'fizz=buzz'.to_h_from_form
245
+ # => { "fizz" => "buzz" }
246
+ ```
247
+
248
+ `Symbol` transformations
249
+
250
+ ```ruby
251
+ :snake_case_symbol.camel_case
252
+ # => :SnakeCaseSymbol
253
+
254
+ :CamelCaseSymbol.snake_case
255
+ # => 'camel_case_symbol'
256
+ ```
257
+
258
+ `UTC` Time helper
259
+
260
+ ```ruby
261
+ UTC.at 1234567890
262
+ # => 2009-02-13 23:31:30 UTC
263
+
264
+ UTC.now
265
+ # => 2022-02-26 13:57:07.860539 UTC
266
+ ```
267
+
268
+ ## Datadog Integration
269
+
270
+ As of `~> 0.4`, `yake` comes with a helper for writing Lambdas that integrate with Datadog's `datadog-ruby` gem.
271
+
272
+ Creating a Lambda handler that wraps the Datadog tooling is easy:
273
+
274
+ ```ruby
275
+ require 'aws-sdk-someservice'
276
+ require 'yake/datadog'
277
+
278
+ # Configure Datadog to use AWS tracing
279
+ Datadog::Lambda.configure_apm { |config| config.use :aws }
280
+
281
+ datadog :handler do |event|
282
+ # …
283
+ end
284
+ ```
285
+
286
+ ## Deployment
287
+
288
+ After writing your Lambda handler code you can deploy it to AWS using any number of tools. I recommend the following tools:
289
+
290
+ - [Terraform](https://www.terraform.io) — my personal favorite Infrastructure-as-Code tool
291
+ - [AWS SAM](https://aws.amazon.com/serverless/sam/) — a great alternative with less configuration than Terraform
292
+ - [Serverless](https://www.serverless.com) — Supposedly the most popular option, though I have not used it
293
+
294
+ ## Development
183
295
 
184
- 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
296
+ After checking out the repo, run `bundle` to install dependencies. Then, run `rake spec` to run the tests.
185
297
 
186
298
  ## Contributing
187
299
 
data/lib/yake/datadog.rb CHANGED
@@ -1,10 +1,25 @@
1
1
  # frozen_string_literal: true
2
+ require 'logger'
2
3
 
3
4
  require 'datadog/lambda'
4
5
  require 'yake'
5
6
 
6
7
  module Yake
7
8
  module Datadog
9
+ class Formatter < ::Logger::Formatter
10
+ Format = "[%s] %s %s %s %s\n"
11
+
12
+ def call(severity, time, progname, msg)
13
+ Format % [
14
+ severity,
15
+ time.utc.strftime('%Y-%m-%dT%H:%M:%S.%LZ'),
16
+ progname.nil? ? '-' : progname.split.last,
17
+ ::Datadog.tracer.active_correlation,
18
+ msg2str(msg).strip,
19
+ ]
20
+ end
21
+ end
22
+
8
23
  class MockContext < Struct.new(
9
24
  :clock_diff,
10
25
  :deadline_ms,
@@ -39,6 +54,8 @@ module Yake
39
54
  end
40
55
  end
41
56
  end
57
+
58
+ logger.formatter = Datadog::Formatter.new
42
59
  end
43
60
 
44
61
  extend Yake::Datadog::DSL
data/lib/yake/dsl.rb CHANGED
@@ -22,7 +22,8 @@ module Yake
22
22
 
23
23
  ##
24
24
  # Turn logging on/off
25
- def logging(switch, logger = nil)
25
+ def logging(switch = :on, logger = nil, pretty: true)
26
+ Yake.pretty = pretty
26
27
  if switch == :on
27
28
  Yake.logger = logger
28
29
  elsif switch == :off
data/lib/yake/logger.rb CHANGED
@@ -5,7 +5,7 @@ require 'logger'
5
5
 
6
6
  module Yake
7
7
  module Logger
8
- attr_accessor :logger
8
+ attr_writer :logger
9
9
 
10
10
  def logger
11
11
  @logger ||= Yake.logger
@@ -27,17 +27,24 @@ module Yake
27
27
  end
28
28
 
29
29
  class << self
30
- attr_accessor :logger
30
+ attr_writer :logger, :pretty
31
31
 
32
32
  def logger
33
33
  @logger ||= Logger.new
34
34
  end
35
35
 
36
+ def pretty?
37
+ @pretty != false
38
+ end
39
+
36
40
  def wrap(event = nil, context = nil, &block)
37
41
  original_progname = logger.progname
38
- logger.progname = context&.aws_request_id
39
- logger.info("EVENT #{ event.to_json }")
40
- yield(event, context).tap { |res| logger.info("RETURN #{ res.to_json }") }
42
+ logger.progname = context&.aws_request_id
43
+ jsonify = -> (obj) { pretty? ? JSON.pretty_generate(obj) : obj.to_json }
44
+ logger.info("EVENT #{ jsonify === event }")
45
+ yield(event, context).tap do |res|
46
+ logger.info("RETURN #{ jsonify === res }")
47
+ end
41
48
  ensure
42
49
  logger.progname = original_progname
43
50
  end
@@ -0,0 +1,39 @@
1
+ require 'base64'
2
+ require 'json'
3
+ require 'time'
4
+
5
+ class Hash
6
+ def encode64() to_json.encode64 end
7
+ def strict_encode64() to_json.strict_encode64 end
8
+ def symbolize_names() JSON.parse(to_json, symbolize_names: true) end
9
+ def to_form() URI.encode_www_form(self) end
10
+ end
11
+
12
+ class Integer
13
+ def days() hours * 24 end
14
+ def hours() minutes * 60 end
15
+ def minutes() seconds * 60 end
16
+ def seconds() self end
17
+ def utc() UTC.at(self) end
18
+ end
19
+
20
+ class String
21
+ def camel_case() split(/_/).map(&:capitalize).join end
22
+ def decode64() Base64.decode64(self) end
23
+ def encode64() Base64.encode64(self) end
24
+ def snake_case() gsub(/([a-z])([A-Z])/, '\1_\2').downcase end
25
+ def strict_decode64() Base64.strict_decode64(self) end
26
+ def strict_encode64() Base64.strict_encode64(self) end
27
+ def to_h_from_json(**params) JSON.parse(self, **params) end
28
+ def to_h_from_form() URI.decode_www_form(self).to_h end
29
+ end
30
+
31
+ class Symbol
32
+ def camel_case() to_s.camel_case.to_sym end
33
+ def snake_case() to_s.snake_case.to_sym end
34
+ end
35
+
36
+ class UTC < Time
37
+ def self.at(...) super.utc end
38
+ def self.now() super.utc end
39
+ end
data/lib/yake/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yake
4
- VERSION = '0.4.1'
4
+ VERSION = '0.5.0'
5
5
  end
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.4.1
4
+ version: 0.5.0
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-10-15 00:00:00.000000000 Z
11
+ date: 2022-02-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -18,7 +18,6 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - LICENSE
21
- - LICENSE.txt
22
21
  - README.md
23
22
  - lib/yake.rb
24
23
  - lib/yake/api.rb
@@ -26,6 +25,7 @@ files:
26
25
  - lib/yake/dsl.rb
27
26
  - lib/yake/errors.rb
28
27
  - lib/yake/logger.rb
28
+ - lib/yake/support.rb
29
29
  - lib/yake/version.rb
30
30
  homepage: https://github.com/amancevice/yake
31
31
  licenses:
@@ -39,14 +39,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
39
39
  requirements:
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
- version: 2.2.0
42
+ version: 2.7.5
43
43
  required_rubygems_version: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  requirements: []
49
- rubygems_version: 3.2.22
49
+ rubygems_version: 3.3.7
50
50
  signing_key:
51
51
  specification_version: 4
52
52
  summary: Rake-like DSL for declaring AWS Lambda function handlers
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2021 Alexander Mancevice
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.