Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4, refactor: add Rack env builder #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions lib/hanami/lambda/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require "rack"

module Hanami
module Lambda
class Env
def initialize(event:, headers:, context:)
@event = event
@headers = headers
@content_type = extract_content_type
@http_headers = transform_http_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
}.tap do |env|
env["CONTENT_TYPE"] = @content_type if @content_type
end.merge(@http_headers)
end

private

def extract_content_type
@headers.delete("Content-Type") ||
@headers.delete("content-type") ||
@headers.delete("CONTENT_TYPE")
end

def transform_http_headers
@headers.transform_keys { |k| "HTTP_#{k.upcase.tr('-', '_')}" }
end
end
end
end
14 changes: 1 addition & 13 deletions lib/hanami/lambda/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,7 @@ def call(event:, context:)
#
# @since 0.1.0
def build_env(event, headers, context)
{
::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
}.tap do |env|
content_type = headers.delete("Content-Type") ||
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get "content-type" with different keys because all values are possibly received from Lambda event. We should not change it.

headers.delete("content-type") ||
headers.delete("CONTENT_TYPE")
env["CONTENT_TYPE"] = content_type if content_type
end.merge(headers.transform_keys { |k| "HTTP_#{k.upcase.tr('-', '_')}" })
Env.new(event: event, headers: headers, context: context).to_h
end
end
end
Expand Down
29 changes: 29 additions & 0 deletions sig/hanami/lambda/env.rbs
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please specify the correct types.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Hanami
module Lambda
class Env
@event: Hash[::String, String? | Hash]

@headers: Hash[::String, ::String]

@content_type: ::String

@http_headers: Hash[::String, ::String]

@context: Object

def initialize: (event: Hash[::String, String? | Hash], 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?, (::String? | Hash[::String, String? | Hash] | Object)]

private

def extract_content_type: () -> ::String?

def transform_http_headers: () -> Hash[::String, ::String]
end
end
end
5 changes: 5 additions & 0 deletions sig/shims/rack.rbs
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
31 changes: 31 additions & 0 deletions spec/hanami/lambda/env_spec.rb
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
9 changes: 0 additions & 9 deletions spec/hanami/lambda/rack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,4 @@
it { is_expected.to include(body: "Hello from Rack") }
it { is_expected.to include(headers: {}) }
end

describe "#env" do
subject(:env) { rack.build_env(event, headers, context) }

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
Loading