Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

See this http://keepachangelog.com link for information on how we want this documented formatted.

## v2.7.0

#### Added

- Support EventBridge events in handler with default proc to log.

## v2.6.3

#### Added
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
lamby (2.6.3)
lamby (2.7.0)
rack

GEM
Expand Down
5 changes: 5 additions & 0 deletions lib/lamby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'rack'
require 'base64'
require 'lamby/version'
require 'lamby/config'
require 'lamby/sam_helpers'
require 'lamby/rack'
require 'lamby/rack_alb'
Expand All @@ -23,6 +24,10 @@ def handler(app, event, context, options = {})
Handler.call(app, event, context, options)
end

def config
Lamby::Config.config
end

autoload :SsmParameterStore, 'lamby/ssm_parameter_store'

end
47 changes: 47 additions & 0 deletions lib/lamby/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Lamby
module Config

def configure
yield(config)
config
end

def reconfigure
config.reconfigure { |c| yield(c) if block_given? }
end

def config
@config ||= Configuration.new
end

extend self

end

class Configuration

def initialize
initialize_defaults
end

def reconfigure
instance_variables.each { |var| instance_variable_set var, nil }
initialize_defaults
yield(self) if block_given?
self
end

def initialize_defaults
@event_bridge_handler = lambda { |event, context| puts(event) }
end

def event_bridge_handler
@event_bridge_handler
end

def event_bridge_handler=(func)
@event_bridge_handler = func
end

end
end
20 changes: 17 additions & 3 deletions lib/lamby/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def body
def call
return self if @called
@status, @headers, @body = call_app
set_cookies
set_cookies if rack?
@called = true
self
end
Expand Down Expand Up @@ -78,20 +78,34 @@ def rack
end

def rack_response
rack.response(self)
rack? ? rack.response(self) : {}
end

def call_app
if Debug.on?(@event)
Debug.call @event, @context, rack.env
else
elsif rack?
@app.call rack.env
elsif event_bridge?
Lamby.config.event_bridge_handler.call @event, @context
[200, {}, StringIO.new('')]
else
[404, {}, StringIO.new('')]
end
end

def content_encoding_compressed?(hdrs)
content_encoding_header = hdrs['Content-Encoding'] || ''
content_encoding_header.split(', ').any? { |h| ['br', 'gzip'].include?(h) }
end

def rack?
@event.key?('httpMethod') || @event.dig('requestContext', 'http')
end

def event_bridge?
Lamby.config.event_bridge_handler &&
@event.key?('source') && @event.key?('detail') && @event.key?('detail-type')
end
end
end
2 changes: 0 additions & 2 deletions lib/lamby/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module Lamby
class Railtie < ::Rails::Railtie

config.lamby = ActiveSupport::OrderedOptions.new

rake_tasks do
load 'lamby/tasks.rake'
load 'lamby/templates.rake'
Expand Down
2 changes: 1 addition & 1 deletion lib/lamby/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Lamby
VERSION = '2.6.3'
VERSION = '2.7.0'
end
30 changes: 30 additions & 0 deletions test/handler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,36 @@ class HandlerTest < LambySpec

end

describe 'event_bridge' do

let(:event) do
{
"version" => "0",
"id"=>"0874bcac-1dac-2393-637f-201025f217b0",
"detail-type"=>"orderCreated",
"source"=>"com.myorg.stores",
"account"=>"123456789012",
"time"=>"2021-04-29T13:51:41Z",
"region"=>"us-east-1",
"resources"=>[],
"detail"=>{"id" => "123"}
}
end

it 'has a configurable proc' do
expect(Lamby.config.event_bridge_handler).must_be_instance_of Proc
Lamby.config.event_bridge_handler = lambda { |e,c| "#{e}#{c}" }
r = Lamby.config.event_bridge_handler.call(1,2)
expect(r).must_equal '12'
end

it 'basic event puts to log' do
out = capture(:stdout) { @result = Lamby.handler app, event, context }
expect(out).must_match %r{0874bcac-1dac-2393-637f-201025f217b0}
end

end

private

def session_cookie(result)
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class LambySpec < Minitest::Spec
TestHelpers::StreamHelpers

before do
Lamby.config.reconfigure
delete_dummy_files
end

Expand Down