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

Inherits:
Base
  • Object
show all
Defined in:
lib/jets/resource/api_gateway/method.rb

Direct Known Subclasses

Cors

Instance Method Summary collapse

Methods inherited from Base

#resource

Constructor Details

#initialize(route) ⇒ Method

route - Jets::Route



9
10
11
# File 'lib/jets/resource/api_gateway/method.rb', line 9

def initialize(route)
  @route = route
end

Instance Method Details

#corsObject



60
61
62
# File 'lib/jets/resource/api_gateway/method.rb', line 60

def cors
  Cors.new(@route)
end

#definitionObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jets/resource/api_gateway/method.rb', line 13

def definition
  {
    method_logical_id => {
      type: "AWS::ApiGateway::Method",
      properties: {
        resource_id: "!Ref #{resource_id}",
        rest_api_id: "!Ref #{RestApi.logical_id}",
        http_method: @route.method,
        request_parameters: {},
        authorization_type: authorization_type,
        integration: {
          integration_http_method: "POST",
          type: "AWS_PROXY",
          uri: "!Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${{namespace}LambdaFunction.Arn}/invocations"
        },
        method_responses: []
      }
    }
  }
end

#method_logical_idObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jets/resource/api_gateway/method.rb', line 34

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.method.downcase
  [path, "{namespace}_#{http_verb}_api_method"].compact.join('_')
end

#replacementsObject



52
53
54
55
56
57
58
# File 'lib/jets/resource/api_gateway/method.rb', line 52

def replacements
  # mimic task to grab replacements, we want the namespace to be the lambda function's namespace
  resources = [definition]
  task = Jets::Lambda::Task.new(@route.controller_name, @route.action_name,
           resources: resources)
  task.replacements
end