yake 0.4.1 → 0.4.2

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: 224ba56816d47b87e5691c7d95904807b9754b6df437fa85a23e8c3190534c8c
4
+ data.tar.gz: de423580d089fd5879788cd6491daeb30012adcdc9ae7ff6d666a090e8bf73a8
5
5
  SHA512:
6
- metadata.gz: 2052948f82b026240655aaf07d6a5a15dc687aa432d97fdcbec0b9c4cb88aca4c64f50d7051336f1a7b99d005b2f9d6234f50a750221897d86a6d033012200e6
7
- data.tar.gz: 8800efb2e6428321aadc94c84788f8918290ab620476f51e20d1e0cfd66170bd4a3002ff86c5d4554de5c3aceaa81ecefb14fcb393fe0f5ac368d70233a83429
6
+ metadata.gz: d270d5b7eb530917d3d7f2e0ddd2e847fd40300215366f22aa4dd67808f84c028172be6e3f8c9a2894a192a527cbbb361c2ab346b17e4b90ec02ec72d8865fa1
7
+ data.tar.gz: 23120b48ee291d9698b83cad68f4d54ebc5abbb99ba72ed3f01af1d3f36a4a7ac72f20e5b468e290c9ed7995d3a9d541e9f07b2d52e254afd716317f3b9794d4
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 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,35 @@ 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
+ ## Datadog Integration
183
+
184
+ As of `~> 0.4`, `yake` comes with a helper for writing Lambdas that integrate with Datadog's `datadog-ruby` gem.
181
185
 
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.
186
+ Creating a Lambda handler that wraps the Datadog tooling is easy:
187
+
188
+ ```ruby
189
+ require 'aws-sdk-someservice'
190
+ require 'yake/datadog'
191
+
192
+ # Configure Datadog to use AWS tracing
193
+ Datadog::Lambda.configure_apm { |config| config.use :aws }
194
+
195
+ datadog :handler do |event|
196
+ # …
197
+ end
198
+ ```
199
+
200
+ ## Deployment
201
+
202
+ After writing your Lambda handler code you can deploy it to AWS using any number of tools. I recommend the following tools:
203
+
204
+ - [Terraform](https://www.terraform.io) — my personal favorite Infrastructure-as-Code tool
205
+ - [AWS SAM](https://aws.amazon.com/serverless/sam/) — a great alternative with less configuration than Terraform
206
+ - [Serverless](https://www.serverless.com) — Supposedly the most popular option, though I have not used it
207
+
208
+ ## Development
183
209
 
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).
210
+ After checking out the repo, run `bundle` to install dependencies. Then, run `rake spec` to run the tests.
185
211
 
186
212
  ## Contributing
187
213
 
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
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.4.2'
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.4.2
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: 2021-11-04 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
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.