Class: Hanami::Lambda::Rack Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/lambda/rack.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Rack interface for AWS Lambda.

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rack

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the Rack interface

Since:

  • 0.1.0



17
18
19
# File 'lib/hanami/lambda/rack.rb', line 17

def initialize(app)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



12
13
14
# File 'lib/hanami/lambda/rack.rb', line 12

def app
  @app
end

Instance Method Details

#build_env(event, headers, context) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build the Rack environment

Returns:

  • (Hash)

    the Rack environment

Since:

  • 0.1.0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hanami/lambda/rack.rb', line 43

def build_env(event, headers, context)
  {
    ::Rack::REQUEST_METHOD => event["httpMethod"],
    ::Rack::PATH_INFO => event["path"] || "",
    ::Rack::VERSION => ::Rack::VERSION,
    ::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") ||
                   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('-', '_')}" })
end

#call(event:, context:) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handle the request

Returns:

  • (Hash)

    the response

Since:

  • 0.1.0



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hanami/lambda/rack.rb', line 26

def call(event:, context:)
  headers = event["headers"] || {}
  env = build_env(event, headers, context)
  status_code, headers, body = app.call(env)

  {
    statusCode: status_code,
    headers: headers,
    body: body.join
  }
end