class Rack::Shelf::APIGateway
Converts AWS API Gateway events given to Lambda to Rack
environment instances.
Attributes
Rack
environment builder instance. @return [EnvironmentBuilder]
Context from the Lambda handler. @return [Object]
Event from the Lambda handler. @return [Hash]
Public Class Methods
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
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
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
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
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
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
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
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
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
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
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
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
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