Class: Apigatewayv2Rack::Response
- Inherits:
-
Object
- Object
- Apigatewayv2Rack::Response
- Defined in:
- lib/apigatewayv2_rack/response.rb
Overview
Convert rack response to API Gateway V2 event response or ALB lambda target response (ELBv2) docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #as_json ⇒ Object
- #cookies_as_response ⇒ Object
- #elb? ⇒ Boolean
- #headers_as_response ⇒ Object
-
#initialize(status:, headers:, body:, elb: false, multivalued: false) ⇒ Response
constructor
A new instance of Response.
- #multivalued? ⇒ Boolean
Constructor Details
#initialize(status:, headers:, body:, elb: false, multivalued: false) ⇒ Response
Returns a new instance of Response.
15 16 17 18 19 20 21 22 |
# File 'lib/apigatewayv2_rack/response.rb', line 15 def initialize(status:, headers:, body:, elb: false, multivalued: false) @status = status @headers = headers @body = body @elb = elb @multivalued = multivalued end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
24 25 26 |
# File 'lib/apigatewayv2_rack/response.rb', line 24 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
24 25 26 |
# File 'lib/apigatewayv2_rack/response.rb', line 24 def headers @headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
24 25 26 |
# File 'lib/apigatewayv2_rack/response.rb', line 24 def status @status end |
Instance Method Details
#as_json ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/apigatewayv2_rack/response.rb', line 82 def as_json { statusCode: status, isBase64Encoded: true, body: Base64.strict_encode64(consume_body), } .merge(headers_as_response) .merge() end |
#cookies_as_response ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/apigatewayv2_rack/response.rb', line 72 def if elb? {} else hdr = headers.find { |k,_v| k.downcase == 'set-cookie' } return {} unless hdr { cookies: header_value(hdr[1]) } end end |
#elb? ⇒ Boolean
26 27 28 |
# File 'lib/apigatewayv2_rack/response.rb', line 26 def elb? @elb end |
#headers_as_response ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/apigatewayv2_rack/response.rb', line 61 def headers_as_response case when elb? && multivalued? {multiValueHeaders: headers.map { |k,v| [k.downcase, header_value(v)] }.to_h} when elb? {headers: headers.map { |k,v| [k.downcase, header_value(v).join(?,)] }.to_h} else {headers: headers.map { |k,v| [k.downcase, header_value(v).join(?,)] }.reject { |k,v| k == 'set-cookie' }.to_h} end end |
#multivalued? ⇒ Boolean
30 31 32 |
# File 'lib/apigatewayv2_rack/response.rb', line 30 def multivalued? @multivalued end |