Class: Jets::Resource::ApiGateway::Cors

Inherits:
Method
  • Object
show all
Defined in:
lib/jets/resource/api_gateway/cors.rb

Overview

Might be weird inheriting from Method because Method has Method#cors also but Cors is essentially a Method class.

Instance Method Summary collapse

Methods inherited from Method

#cors, #initialize, #method_logical_id, #props, #replacements

Methods inherited from Base

#replacements, #resource

Constructor Details

This class inherits a constructor from Jets::Resource::ApiGateway::Method

Instance Method Details

#cors_authorization_typeObject



53
54
55
# File 'lib/jets/resource/api_gateway/cors.rb', line 53

def cors_authorization_type
  Jets.config.api.cors_authorization_type || @route.authorization_type || "NONE"
end

#cors_headersObject

Always the pre-flight headers in this case



48
49
50
51
# File 'lib/jets/resource/api_gateway/cors.rb', line 48

def cors_headers
  rack = Jets::Controller::Middleware::Cors.new(Jets.application)
  rack.cors_headers(true)
end

#cors_logical_idObject



57
58
59
# File 'lib/jets/resource/api_gateway/cors.rb', line 57

def cors_logical_id
  "#{resource_logical_id}_cors_api_method"
end

#definitionObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jets/resource/api_gateway/cors.rb', line 5

def definition
  {
    cors_logical_id => {
      type: "AWS::ApiGateway::Method",

      properties: {
        resource_id: "!Ref #{resource_id}",
        rest_api_id: "!Ref #{RestApi.logical_id}",
        authorization_type: cors_authorization_type,
        http_method: "OPTIONS",
        method_responses: [{
          status_code: '200',
          response_parameters: response_parameters(true),
          response_models: {},
        }],
        request_parameters: {},
        integration: {
          type: "MOCK",
          request_templates: {
            "application/json": "{statusCode:200}",
          },
          integration_responses: [{
            status_code: '200',
            response_parameters: response_parameters,
            response_templates: {
              "application/json": '',
            },
          }] # closes integration_responses
        } # closes integration
      } # closes properties
    } # closes logical id
  } # closes definition
end

#response_parameters(method_response = false) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/jets/resource/api_gateway/cors.rb', line 39

def response_parameters(method_response=false)
  cors_headers.map do |k,v|
    k = "method.response.header.#{k}"
    v = method_response ? true : "'#{v}'" # surround value with single quotes
    [k,v]
  end.to_h
end