class Rack::Shelf::APIGateway

Converts AWS API Gateway events given to Lambda to Rack environment instances.

Attributes

builder[R]

Rack environment builder instance. @return [EnvironmentBuilder]

context[R]

Context from the Lambda handler. @return [Object]

event[R]

Event from the Lambda handler. @return [Hash]

Public Class Methods

env(event, context) click to toggle source

Produces a Rack compatible environment instance from a Lambda event and context. @param event [Hash] Event from the Lambda handler. @param context [Object] Context from the Lambda handler. @return [Hash] Rack environment.

# File lib/rack/shelf/api_gateway.rb, line 18
def self.env(event, context)
  new(event, context).build
end
new(event, context) click to toggle source

Creates an instance dedicated to building a Rack environment for a single event and context. @param event [Hash] Event from the Lambda handler. @param context [Object] Context from the Lambda handler.

# File lib/rack/shelf/api_gateway.rb, line 26
def initialize(event, context)
  @event   = event
  @context = context
  @builder = EnvironmentBuilder.new
end

Public Instance Methods

build() click to toggle source

Builds the rack environment. @return [Hash] Rack environment.

# File lib/rack/shelf/api_gateway.rb, line 34
def build
  build_common
  build_headers
  build_body
  build_lambda_context

  builder.build
end

Private Instance Methods

build_body() click to toggle source

Configures the client request body portion. @return [void]

# File lib/rack/shelf/api_gateway.rb, line 122
def build_body
  return unless (body = event['body'])

  if event['isBase64Encoded']
    builder.base64_body(body)
  else
    builder.body(body)
  end
end
build_common() click to toggle source

Configures common Rack environment attributes. @return [void]

# File lib/rack/shelf/api_gateway.rb, line 103
def build_common # rubocop:disable Metrics/AbcSize
  builder.request_method(request_method)
  builder.path_info(path_info)
  builder.query_params(query_params)
  builder.server_name(server_name)
  builder.server_port(server_port)
  builder.url_scheme(url_scheme)
end
build_headers() click to toggle source

Configures the HTTP request headers. @return [void]

# File lib/rack/shelf/api_gateway.rb, line 114
def build_headers
  headers.each do |key, value|
    builder.header(key, value)
  end
end
headers() click to toggle source

Retrieves the HTTP headers from the Lambda event. @return [Hash] HTTP headers.

# File lib/rack/shelf/api_gateway.rb, line 59
def headers
  event['headers'] || {}
end
path_info() click to toggle source

Retrieves the path information, or a default value. @return [String]

# File lib/rack/shelf/api_gateway.rb, line 71
def path_info
  event['path'] || '/'
end
query_params() click to toggle source

Retrieves the query parameters from the Lambda event. @return [Hash]

# File lib/rack/shelf/api_gateway.rb, line 77
def query_params
  event['queryStringParameters'] || {}
end
request_method() click to toggle source

Retrieves the HTTP request method from the Lambda event. @return [String] HTTP request method.

# File lib/rack/shelf/api_gateway.rb, line 65
def request_method
  event.fetch('httpMethod')
end
server_name() click to toggle source

Retrieves the server hostname, or a default value. @return [String]

# File lib/rack/shelf/api_gateway.rb, line 83
def server_name
  event['Host'] || 'localhost'
end
server_port() click to toggle source

Retrieves the server port, or a default value. @return [Integer]

# File lib/rack/shelf/api_gateway.rb, line 89
def server_port
  event['X-Forwarded-Port'] || 80
end
url_scheme() click to toggle source

Retrieves the URL scheme, or a default value. @return [String]

# File lib/rack/shelf/api_gateway.rb, line 95
def url_scheme
  headers.fetch('CloudFront-Forwarded-Proto') do
    headers.fetch('X-Forwarded-Proto', 'http')
  end
end