Class: Apigatewayv2Rack::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/apigatewayv2_rack/response.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



24
25
26
# File 'lib/apigatewayv2_rack/response.rb', line 24

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



24
25
26
# File 'lib/apigatewayv2_rack/response.rb', line 24

def headers
  @headers
end

#statusObject (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_jsonObject



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(cookies_as_response)
end

#cookies_as_responseObject



72
73
74
75
76
77
78
79
80
# File 'lib/apigatewayv2_rack/response.rb', line 72

def cookies_as_response
  if elb?
    {}
  else
    hdr = headers.find { |k,_v|  k.downcase == 'set-cookie' }
    return {} unless hdr
    { cookies: header_value(hdr[1]) }
  end
end

#elb?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/apigatewayv2_rack/response.rb', line 26

def elb?
  @elb
end

#headers_as_responseObject



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

Returns:

  • (Boolean)


30
31
32
# File 'lib/apigatewayv2_rack/response.rb', line 30

def multivalued?
  @multivalued
end