Skip to content

Commit 0979521

Browse files
#4, refactor: add Rack env builder
1 parent 8fab7c5 commit 0979521

File tree

5 files changed

+103
-22
lines changed

5 files changed

+103
-22
lines changed

lib/hanami/lambda/env.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
require "rack"
4+
5+
module Hanami
6+
module Lambda
7+
class Env
8+
def initialize(event:, headers:, context:)
9+
@event = event
10+
@headers = headers
11+
@content_type = extract_content_type
12+
@http_headers = transform_http_headers
13+
@context = context
14+
end
15+
16+
def to_h
17+
{
18+
::Rack::REQUEST_METHOD => @event["httpMethod"],
19+
::Rack::PATH_INFO => @event["path"] || "",
20+
::Rack::RACK_VERSION => ::Rack.release,
21+
::Rack::RACK_INPUT => StringIO.new(@event["body"] || ""),
22+
::Hanami::Lambda::LAMBDA_EVENT => @event,
23+
::Hanami::Lambda::LAMBDA_CONTEXT => @context
24+
}.tap do |env|
25+
env["CONTENT_TYPE"] = @content_type if @content_type
26+
end.merge(@http_headers)
27+
end
28+
29+
private
30+
31+
def extract_content_type
32+
@headers.delete("Content-Type") ||
33+
@headers.delete("content-type") ||
34+
@headers.delete("CONTENT_TYPE")
35+
end
36+
37+
def transform_http_headers
38+
@headers.transform_keys { |k| "HTTP_#{k.upcase.tr('-', '_')}" }
39+
end
40+
end
41+
end
42+
end

lib/hanami/lambda/rack.rb

+1-13
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,7 @@ def call(event:, context:)
4141
#
4242
# @since 0.1.0
4343
def build_env(event, headers, context)
44-
{
45-
::Rack::REQUEST_METHOD => event["httpMethod"],
46-
::Rack::PATH_INFO => event["path"] || "",
47-
::Rack::RACK_VERSION => ::Rack.release,
48-
::Rack::RACK_INPUT => StringIO.new(event["body"] || ""),
49-
::Hanami::Lambda::LAMBDA_EVENT => event,
50-
::Hanami::Lambda::LAMBDA_CONTEXT => context
51-
}.tap do |env|
52-
content_type = headers.delete("Content-Type") ||
53-
headers.delete("content-type") ||
54-
headers.delete("CONTENT_TYPE")
55-
env["CONTENT_TYPE"] = content_type if content_type
56-
end.merge(headers.transform_keys { |k| "HTTP_#{k.upcase.tr('-', '_')}" })
44+
Env.new(event: event, headers: headers, context: context).to_h
5745
end
5846
end
5947
end

sig/hanami/lambda/env.rbs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Hanami
2+
module Lambda
3+
class Env
4+
@event: untyped
5+
6+
@headers: untyped
7+
8+
@content_type: untyped
9+
10+
@http_headers: untyped
11+
12+
@context: untyped
13+
14+
def initialize: (event: untyped, headers: untyped, context: untyped) -> void
15+
16+
# Build a hash of the Rack environment
17+
#
18+
# @return [Hash] the hash of Rack environment
19+
#
20+
def to_h: () -> ::Hash[untyped, untyped]
21+
22+
private
23+
24+
def extract_content_type: () -> untyped
25+
26+
def transform_http_headers: () -> untyped
27+
end
28+
end
29+
end

spec/hanami/lambda/env_spec.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe Hanami::Lambda::Env do
4+
subject(:env) { described_class.new(event: event, headers: headers, context: context) }
5+
6+
let(:event) do
7+
{
8+
"httpMethod" => "GET",
9+
"path" => "/",
10+
"body" => ""
11+
}
12+
end
13+
14+
let(:headers) do
15+
{
16+
"Content-Type" => "text/plain",
17+
"X-Custom-Header" => "Custom Value"
18+
}
19+
end
20+
21+
let(:context) { double(:context) }
22+
23+
describe "#to_h" do
24+
subject(:hash) { env.to_h }
25+
26+
it { is_expected.to include(::Hanami::Lambda::LAMBDA_EVENT) }
27+
it { is_expected.to include(::Hanami::Lambda::LAMBDA_CONTEXT) }
28+
it { is_expected.to include("CONTENT_TYPE" => "text/plain") }
29+
it { is_expected.to include("HTTP_X_CUSTOM_HEADER" => "Custom Value") }
30+
end
31+
end

spec/hanami/lambda/rack_spec.rb

-9
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,4 @@
3333
it { is_expected.to include(body: "Hello from Rack") }
3434
it { is_expected.to include(headers: {}) }
3535
end
36-
37-
describe "#env" do
38-
subject(:env) { rack.build_env(event, headers, context) }
39-
40-
it { is_expected.to include(::Hanami::Lambda::LAMBDA_EVENT) }
41-
it { is_expected.to include(::Hanami::Lambda::LAMBDA_CONTEXT) }
42-
it { is_expected.to include("CONTENT_TYPE" => "text/plain") }
43-
it { is_expected.to include("HTTP_X_CUSTOM_HEADER" => "Custom Value") }
44-
end
4536
end

0 commit comments

Comments
 (0)