From 986f1b3639f2e478d88f05ddb8f12efce8ce5ad1 Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Sat, 22 Dec 2018 00:49:32 -0500 Subject: [PATCH 1/2] Allow passing a block normally In addition to passing a `Proc` as a value, this commit allows us to pass a first-class block to `Crambda.run_handler`: Crambda.run_handler do |event, context| { foo: "bar", omg: "lol", wtf: 42 } end --- src/crambda.cr | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/crambda.cr b/src/crambda.cr index 551235f..419dd7c 100644 --- a/src/crambda.cr +++ b/src/crambda.cr @@ -65,4 +65,8 @@ module Crambda end end end + + def self.run_handler(&block : JSON::Any, Context -> T) forall T + run_handler block + end end From dfdf2de2c60e4a18435aab0aa260f5a2888ffdc8 Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Sat, 22 Dec 2018 00:57:48 -0500 Subject: [PATCH 2/2] Update README with new block API --- README.md | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index b6dd854..3dc5c9a 100644 --- a/README.md +++ b/README.md @@ -24,24 +24,13 @@ targets: require "json" require "crambda" -def handler(event : JSON::Any, context : Crambda::Context) +Crambda.run_handler do |event, context| pp context - JSON.parse("[1, 2]") -end - -Crambda.run_handler(->handler(JSON::Any, Crambda::Context)) -``` - -Where `Crambda.run_handler` expects a handler that takes a `JSON::Any` event -and a `Context`, and returns a `JSON::Any` response: - -```crystal -def self.run_handler(handler : Proc(JSON::Any, Context, JSON::Any)) - # ... + [1, 2] end ``` -And `Context` is a class that looks like this: +`Crambda.run_handler` expects a block that takes a `JSON::Any` event and a `Crambda::Context` and returns an object that implements `to_json`. The `Crambda::Context` is a class that looks like this: ```crystal class Context