Class: Jets::Controller::Rack::Adapter

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/controller/rack/adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, context) ⇒ Adapter

Returns a new instance of Adapter.



13
14
15
# File 'lib/jets/controller/rack/adapter.rb', line 13

def initialize(event, context)
  @event, @context = event, context
end

Class Method Details

.process(event, context) ⇒ Object

Returns back API Gateway response hash structure



8
9
10
11
# File 'lib/jets/controller/rack/adapter.rb', line 8

def self.process(event, context)
  adapter = new(event, context)
  adapter.process
end

Instance Method Details

#add_response_headers(resp, headers) ⇒ Object



46
47
48
49
50
51
# File 'lib/jets/controller/rack/adapter.rb', line 46

def add_response_headers(resp, headers)
  resp['headers'] = headers.reject { |_, val| val.is_a?(Array) }
  multi_value_headers = headers.select { |_, val| val.is_a?(Array) }

  resp['multiValueHeaders'] = multi_value_headers unless multi_value_headers.blank?
end

#adjust_for_elb(resp) ⇒ Object

Note: ELB is not officially support. This is just in case users wish to manually connect ELBs to the functions created by Jets.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jets/controller/rack/adapter.rb', line 55

def adjust_for_elb(resp)
  return resp unless from_elb?

  # ELB requires statusCode to be an Integer whereas API Gateway requires statusCode to be a String
  status = resp["statusCode"] = resp["statusCode"].to_i

  # ELB also requires statusDescription attribute
  status_desc = Rack::Utils::HTTP_STATUS_CODES[status]
  status_desc = status_desc.nil? ? status.to_s : "#{status} #{status_desc}"
  resp["statusDescription"] = status_desc

  resp
end

#convert_to_api_gateway(status, headers, body) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jets/controller/rack/adapter.rb', line 32

def convert_to_api_gateway(status, headers, body)
  base64 = headers["x-jets-base64"] == 'yes'
  body = body.respond_to?(:read) ? body.read : body
  body = Base64.encode64(body) if base64

  {}.tap do |resp|
    resp['statusCode'] = status
    resp['body'] = body
    resp['isBase64Encoded'] = base64
    add_response_headers(resp, headers)
    adjust_for_elb(resp)
  end
end

#envObject



25
26
27
# File 'lib/jets/controller/rack/adapter.rb', line 25

def env
  Env.new(@event, @context, adapter: true).convert # convert to Rack env
end

#from_elb?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/jets/controller/rack/adapter.rb', line 69

def from_elb?
  # NOTE: @event["requestContext"]["elb"] is set when the request is coming from an elb
  # Can set JETS_ELB=1 for local testing
  @event["requestContext"] && @event["requestContext"]["elb"] || ENV['JETS_ELB']
end

#processObject

  1. Convert API Gateway event event to Rack env

  2. Process using full Rack middleware stack

  3. Convert back to API gateway response structure payload



20
21
22
23
# File 'lib/jets/controller/rack/adapter.rb', line 20

def process
  status, headers, body = Jets.application.call(env)
  convert_to_api_gateway(status, headers, body)
end

#rack_vars(vars) ⇒ Object

Called from Jets::Controller::Base.process. Example:

adapter.rack_vars(
  'jets.controller' => self,
  'lambda.context' => context,
  'lambda.event' => event,
  'lambda.meth' => meth,
)

Passes a these special variables so we have access to them in the middleware. The controller instance is called in the Main middleware. The lambda.* info is used by the Rack::Local middleware to create a mimiced controller for the local server.



89
90
91
# File 'lib/jets/controller/rack/adapter.rb', line 89

def rack_vars(vars)
  env.merge!(vars)
end