yake 0.1.1 → 0.2.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: 117ea613cf7d5a703aba3702eb6ab076fa77490c7e7a901b1e00c22db5c55675
4
- data.tar.gz: 8a9a8efba6b480218c2adf79990f567ed2d5a01756e98f4aa4e1d2ee7a6734fa
3
+ metadata.gz: 2be2a80d8b7737cf954f33faef88eb7054ce7b0bbb516f8a216f3b6ec0b5578c
4
+ data.tar.gz: 69e7b3323594c0f2d794674dc938104ecf1f2d37ccbd4818272aefb6ab4345f7
5
5
  SHA512:
6
- metadata.gz: 9c543d7f07274ba4b6fb707fcd2326dfc33a10e027723d39a78eaf0bf5937209968759b1caa497d1a024911d70d76221da449346da67defd8d0c2ee1a9a8dfad
7
- data.tar.gz: 64f6319d4ad3578aa7920caba1315995b05df6790862bf0256a4b5019cf3e64c64b7861b4eec25acf4a4e729bfb44bc0e68b06863bcf7918a0980c2921a52fc5
6
+ metadata.gz: 206fdff33a178be14611721811bf5d3177b7e5b3bc7bf09237844aeac2a93e74b7b88281675a169fe41333dfaa9d40eb8bfb4aab3cac20ee762c3fa4d419c5d0
7
+ data.tar.gz: cc3a7a75a4f467028acc9bc892681067fbe86f4be6ed7cfe120deadfd34100f2da648f013d6ac81021fade9bcf0342ba576b6fa61a520cbc0724022aadae0aba
data/README.md CHANGED
@@ -26,7 +26,7 @@ require "yake/api"
26
26
 
27
27
  header "content-type" => "application/json"
28
28
 
29
- get "/fizz" do |handler|
29
+ get "/fizz" do
30
30
  respond 200, { ok: true }.to_json
31
31
  end
32
32
 
@@ -59,19 +59,17 @@ Or install it yourself as:
59
59
  gem install yake
60
60
  ```
61
61
 
62
- ## Why Use It?
62
+ ## Why Is It Called "yake"?
63
63
 
64
- So why use `yake` for your Lambda functions?
64
+ "λ" + Rake, but "λ" is hard to type and I think "y" looks like a funny little upside-down-and-backwards Lambda symbol.
65
65
 
66
- #### Zero Dependencies
66
+ ## Why Use It?
67
67
 
68
- `yake` does not depend on any other gems, using the Ruby stdlib only. This helps keep your Lambda packages slim & speedy.
68
+ So why use `yake` for your Lambda functions?
69
69
 
70
70
  #### Event Logging
71
71
 
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 logs the input event and the returned value prefixed with the ID of the request.
73
-
74
- 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:
75
73
 
76
74
  ```
77
75
  START RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf Version: $LATEST
@@ -82,14 +80,25 @@ END RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf
82
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
83
81
  ```
84
82
 
85
- This makes gathering logs lines for a particular execution in CloudWatch much easier.
83
+ Logging the request ID in this way makes gathering logs lines for a particular execution in CloudWatch much easier.
86
84
 
87
- 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:
88
86
 
89
87
  ```ruby
90
88
  logging :off
91
89
  ```
92
90
 
91
+ Include `Yake::Logger` 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
+
93
102
  #### API Routes
94
103
 
95
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.
@@ -144,7 +153,7 @@ respond 200, { ok: true }.to_json, "x-extra-header" => "buzz"
144
153
  # {
145
154
  # "statusCode" => 200,
146
155
  # "body" => '{"ok":true}',
147
- # "headers" => { "x-extra-header" => "fizz" }
156
+ # "headers" => { "x-extra-header" => "buzz" }
148
157
  # }
149
158
  ```
150
159
 
@@ -156,10 +165,14 @@ handler :lambda_handler do |event|
156
165
  rescue Yake::UndeclaredRoute => err
157
166
  respond 404, { message: err.message }.to_json
158
167
  rescue => err
159
- respond 500 { message: err.message }.to_json
168
+ respond 500, { message: err.message }.to_json
160
169
  end
161
170
  ```
162
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
+
163
176
  ## Development
164
177
 
165
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.
data/lib/yake/api.rb CHANGED
@@ -55,6 +55,12 @@ module Yake
55
55
  (@headers ||= {}).update(headers)
56
56
  end
57
57
 
58
+ ##
59
+ # Define ANY route
60
+ def any(path, &block)
61
+ define_singleton_method("ANY #{ path }") { |*args| instance_exec(*args, &block) }
62
+ end
63
+
58
64
  ##
59
65
  # Define DELETE route
60
66
  def delete(path, &block)
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.logger.nil? ? yield(event, context) : Yake.logger.wrap(event, context, &block)
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, **options)
19
+ def logging(switch, logger = nil)
20
20
  if switch == :on
21
- Yake.logger = Yake::Logger.new
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
- class Logger < ::Logger
8
- def initialize(logdev = $stdout, *)
9
- super
10
- @progname = "-"
11
- @formatter = LambdaFormatter.new
7
+ module Logger
8
+ attr_accessor :logger
9
+
10
+ def logger
11
+ @logger ||= Yake.logger
12
12
  end
13
13
 
14
- def wrap(event = nil, context = nil, &block)
15
- @progname = "RequestId: #{ context.aws_request_id }" if context.respond_to?(:aws_request_id)
16
- info("EVENT #{ event.to_json }")
17
- yield(event, context).tap { |res| info("RETURN #{ res.to_json }") }
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
- class LambdaFormatter < ::Logger::Formatter
24
- Format = "%s %s %s\n"
20
+ class Formatter < ::Logger::Formatter
21
+ Format = "%s %s %s\n"
25
22
 
26
- def call(severity, time, progname, msg)
27
- Format % [ severity, progname, msg2str(msg).strip ]
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
- module Loggable
29
+ class << self
32
30
  attr_accessor :logger
33
- end
34
31
 
35
- extend Loggable
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yake
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.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.1.1
4
+ version: 0.2.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-04-27 00:00:00.000000000 Z
11
+ date: 2021-05-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: