Class: Jets::Cfn::Builder::Api::Methods

Inherits:
Paged
  • Object
show all
Defined in:
lib/jets/cfn/builder/api/methods.rb

Instance Method Summary collapse

Methods inherited from Paged

build_pages, #initialize, pages_class

Methods inherited from Base

#initialize

Methods included from AwsServices

#apigateway, #aws_lambda, #aws_options, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from AwsServices::GlobalMemoist

included

Methods included from Interface

#add_description, #add_output, #add_outputs, #add_parameter, #add_parameters, #add_resource, #add_resources, #add_template_resource, #build, #build?, #template, #text, #write

Methods included from Util::Camelize

#camelize

Constructor Details

This class inherits a constructor from Jets::Cfn::Builder::Api::Paged

Instance Method Details

#add_api_gateway_parametersObject



27
28
29
30
31
32
# File 'lib/jets/cfn/builder/api/methods.rb', line 27

def add_api_gateway_parameters
  api_methods = Jets::Cfn::Params::Api::Methods.new(template: @template)
  api_methods.params.each do |key, _|
    add_parameter(key)
  end
end

#add_api_methodsObject

Resources Example (only showing keys we care about):

UpIndexGetApiMethod:
  Type: AWS::ApiGateway::Method
  Properties:
    ResourceId: !Ref UpApiResource
    RestApiId: !Ref RestApi
    Integration:
      Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${UpControllerIndexLambdaFunction}/invocations


20
21
22
23
24
25
# File 'lib/jets/cfn/builder/api/methods.rb', line 20

def add_api_methods
  routes.each_with_index do |route, i|
    method = Jets::Cfn::Resource::ApiGateway::Method.new(route)
    add_resource(method)
  end
end

#composeObject

interface method



4
5
6
7
8
# File 'lib/jets/cfn/builder/api/methods.rb', line 4

def compose
  return if routes.empty?
  add_api_methods
  add_api_gateway_parameters
end

#ensure_one_apigw_method_proxy_routes!Object

Only add missing root route with one_apigw_method_for_all_routes setting. Add the root route because that’s how it works locally. With one_apigw_method_for_all_routes, it all goes to one lambda function and routing is determined by config/routes.rb



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jets/cfn/builder/api/methods.rb', line 49

def ensure_one_apigw_method_proxy_routes!
  return unless Jets.config.cfn.build.routes == "one_apigw_method_for_all_routes"

  # find before modifications
  catchall_route = Jets::Router.routes.find { |route| route.path =~ /^\/\*/ }
  root_route = Jets::Router.routes.find { |route| route.http_method == "GET" && route.path == "/" }

  # modifications
  unless catchall_route
    # Note: catchall to route does not matter. In one_apigw_method_for_all_routes mode it all goes to one lambda function
    # and then gets routed by config/routes.rb
    Jets::Router.routes << Jets::Router::Route.new(path: '/*catchall', http_method: 'ANY', to: 'jets/public#show')
  end
  if !root_route && catchall_route
    Jets::Router.routes << Jets::Router::Route.new(path: '/', http_method: 'GET', to: catchall_route.to)
  end
end

#routesObject



34
35
36
37
38
39
40
41
42
# File 'lib/jets/cfn/builder/api/methods.rb', line 34

def routes
  ensure_one_apigw_method_proxy_routes!
  @items.map do |uid|
    Jets::Router.routes.find do |route|
      route.path == "/" && route.path == uid.split('|').last || # root route. any http method works
      "#{route.http_method}|#{route.path}" == uid # Match the method and path. IE: GET|posts/:id
    end
  end.compact
end

#template_pathObject

template_path is an interface method



68
69
70
# File 'lib/jets/cfn/builder/api/methods.rb', line 68

def template_path
  Jets::Names.api_methods_template_path(@page_number)
end