-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47d5490
commit a767ce5
Showing
6 changed files
with
98 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rack" | ||
|
||
module Hanami | ||
module Lambda | ||
class Env | ||
def initialize(event:, headers:, context:) | ||
@event = event | ||
@headers = standardize_headers(headers) | ||
@context = context | ||
end | ||
|
||
def to_h | ||
{ | ||
::Rack::REQUEST_METHOD => @event["httpMethod"], | ||
::Rack::PATH_INFO => @event["path"] || "", | ||
::Rack::RACK_VERSION => ::Rack.release, | ||
::Rack::RACK_INPUT => StringIO.new(@event["body"] || ""), | ||
::Hanami::Lambda::LAMBDA_EVENT => @event, | ||
::Hanami::Lambda::LAMBDA_CONTEXT => @context | ||
}.merge(@headers) | ||
end | ||
|
||
private | ||
|
||
def standardize_headers(headers) | ||
headers.transform_keys do |key| | ||
if ::Rack::Headers::KNOWN_HEADERS[key.downcase.tr("_", "-")] | ||
key.upcase.tr("-", "_") | ||
else | ||
"HTTP_#{key.upcase.tr('-', '_')}" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Hanami | ||
module Lambda | ||
class Env | ||
@event: Hash[String, untyped] | ||
|
||
@headers: Hash[String, String] | ||
|
||
@context: Object | ||
|
||
def initialize: (event: Hash[String, untyped], headers: Hash[String, String], context: Object) -> void | ||
|
||
# Build a hash of the Rack environment | ||
# | ||
# @return [Hash] the hash of Rack environment | ||
# | ||
def to_h: () -> ::Hash[String, untyped] | ||
|
||
private | ||
|
||
def standardize_headers: (Hash[String, String]) -> Hash[String, String] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Rack | ||
class Headers | ||
KNOWN_HEADERS: Hash[String, String] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Hanami::Lambda::Env do | ||
subject(:env) { described_class.new(event: event, headers: headers, context: context) } | ||
|
||
let(:event) do | ||
{ | ||
"httpMethod" => "GET", | ||
"path" => "/", | ||
"body" => "" | ||
} | ||
end | ||
|
||
let(:headers) do | ||
{ | ||
"Content-Type" => "text/plain", | ||
"X-Custom-Header" => "Custom Value" | ||
} | ||
end | ||
|
||
let(:context) { double(:context) } | ||
|
||
describe "#to_h" do | ||
subject(:hash) { env.to_h } | ||
|
||
it { is_expected.to include(::Hanami::Lambda::LAMBDA_EVENT) } | ||
it { is_expected.to include(::Hanami::Lambda::LAMBDA_CONTEXT) } | ||
it { is_expected.to include("CONTENT_TYPE" => "text/plain") } | ||
it { is_expected.to include("HTTP_X_CUSTOM_HEADER" => "Custom Value") } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters