Class: Jets::Cfn::TemplateMappers::GatewayResourceMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/cfn/template_mappers/gateway_resource_mapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ GatewayResourceMapper

Returns a new instance of GatewayResourceMapper.



3
4
5
# File 'lib/jets/cfn/template_mappers/gateway_resource_mapper.rb', line 3

def initialize(path)
  @path = path # Examples: "posts/:id/edit" or "posts"
end

Instance Method Details

#cors_logical_idObject



17
18
19
# File 'lib/jets/cfn/template_mappers/gateway_resource_mapper.rb', line 17

def cors_logical_id
  "#{path_logical_id(@path)}CorsApiGatewayResource"
end

#descObject



26
27
28
# File 'lib/jets/cfn/template_mappers/gateway_resource_mapper.rb', line 26

def desc
  path.empty? ? 'Homepage route: /' : "Route for: /#{path}"
end

#logical_idObject

Returns: “ApiGatewayResourcePostsController”



8
9
10
11
12
13
14
15
# File 'lib/jets/cfn/template_mappers/gateway_resource_mapper.rb', line 8

def logical_id
  homepage = @path == ''
  if homepage
    "RootResourceId"
  else
    "#{path_logical_id(@path)}ApiGatewayResource"
  end
end

#parent_idObject



42
43
44
45
46
47
48
49
50
# File 'lib/jets/cfn/template_mappers/gateway_resource_mapper.rb', line 42

def parent_id
  if @path.include?('/') # posts/:id or posts/:id/edit
    parent_path = @path.split('/')[0..-2].join('/')
    parent_logical_id = path_logical_id(parent_path)
    "!Ref #{parent_logical_id}ApiGatewayResource"
  else
    "!GetAtt RestApi.RootResourceId"
  end
end

#pathObject

Modify the path to conform to API Gateway capture expressions



22
23
24
# File 'lib/jets/cfn/template_mappers/gateway_resource_mapper.rb', line 22

def path
  @path.split('/').map {|s| transform_capture(s) }.join('/')
end

#path_partObject



52
53
54
55
# File 'lib/jets/cfn/template_mappers/gateway_resource_mapper.rb', line 52

def path_part
  last_part = path.split('/').last
  last_part.split('/').map {|s| transform_capture(s) }.join('/')
end

#transform_capture(text) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jets/cfn/template_mappers/gateway_resource_mapper.rb', line 30

def transform_capture(text)
  if text.starts_with?(':')
    text = text.sub(':','')
    text = "{#{text}}" # :foo => {foo}
  end
  if text.starts_with?('*')
    text = text.sub('*','')
    text = "{#{text}+}" # *foo => {foo+}
  end
  text
end