Class: Jets::Cfn::Resource::ApiGateway::Method

Inherits:
Base
  • Object
show all
Includes:
Authorization
Defined in:
lib/jets/cfn/resource/api_gateway/method.rb,
lib/jets/cfn/resource/api_gateway/method/authorization.rb

Defined Under Namespace

Modules: Authorization

Instance Method Summary collapse

Methods inherited from Base

#attributes, #logical_id, #outputs, #parameters, #permission, #properties, #replacer, #standarize, #template, truncate_id, #type

Methods included from Util::Camelize

#camelize

Constructor Details

#initialize(route) ⇒ Method

route - Jets::Route



7
8
9
# File 'lib/jets/cfn/resource/api_gateway/method.rb', line 7

def initialize(route)
  @route = route
end

Instance Method Details

#definitionObject



11
12
13
14
15
16
17
18
# File 'lib/jets/cfn/resource/api_gateway/method.rb', line 11

def definition
  {
    method_logical_id => {
      Type: "AWS::ApiGateway::Method",
      Properties: props
    }
  }
end

#method_logical_idObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jets/cfn/resource/api_gateway/method.rb', line 54

def method_logical_id
  # https://stackoverflow.com/questions/6104240/how-do-i-strip-non-alphanumeric-characters-from-a-string-and-keep-spaces
  # Add path to the logical id to allow 2 different paths to be connected to the same controller action.
  # Example:
  #
  #   root "jets/public#show"
  #   any "*catchall", to: "jets/public#show"
  #
  # Without the path in the logical id, the logical id would be ShowApiMethod for both routes and only the
  # last one would be created in the CloudFormation template.
  path = @route.path.gsub('*','')
          .gsub(/[^0-9a-z]/i, ' ')
          .gsub(/\s+/, '_')
  path = nil if path == ''
  http_verb = @route.http_method.downcase
  [http_verb, path, "api_method"].compact.join('_')
end

#propsObject

Note: The namespace in

functions/${{namespace}LambdaFunction.Arn}/invocations

is replaced by Jets::Cfn::Resource::Replacer

Jets::Cfn::Resource::ApiGateway::Method
  Jets::Cfn::Resource (attributes delegate to resource)
  Jets::Cfn::Resource => replacer


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jets/cfn/resource/api_gateway/method.rb', line 28

def props
  function_logical_id = Jets.one_lambda_for_all_controllers? ?
      "JetsControllerLambdaFunction" :
      "{namespace}LambdaFunction"

  resource_id = ResourceId.new(@route.path).resource_id
  props = {
    ResourceId: "!Ref #{resource_id}",
    RestApiId: "!Ref #{RestApi.logical_id}",
    HttpMethod: @route.http_method,
    RequestParameters: {},
    AuthorizationType: authorization_type,
    ApiKeyRequired: api_key_required?,
    Integration: {
      IntegrationHttpMethod: "POST",
      Type: "AWS_PROXY",
      Uri: "!Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${#{function_logical_id}}/invocations"
    },
    MethodResponses: []
  }
  props[:AuthorizerId] = authorizer_id if authorizer_id
  props[:AuthorizationScopes] = authorization_scopes if authorization_scopes

  props
end

#replacementsObject



72
73
74
75
76
77
78
79
80
# File 'lib/jets/cfn/resource/api_gateway/method.rb', line 72

def replacements
  # Mimic task to grab replacements
  # Use functions/${namespace} in Uri
  resources = [definition]
  action_name = Jets.one_lambda_per_controller? ? "lambda_handler" : @route.action_name
  task = Jets::Lambda::Definition.new(@route.controller_name, action_name,
           resources: resources)
  task.replacements
end