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

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

Instance Method Summary collapse

Methods inherited from Base

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

Methods included from Util::Camelize

#camelize

Constructor Details

#initialize(path, internal: false) ⇒ Resource

Returns a new instance of Resource.



3
4
5
6
7
8
# File 'lib/jets/cfn/resource/api_gateway/resource.rb', line 3

def initialize(path, internal: false)
  # The original implementation uses path without the leading slash.
  # Remove it here so the path_part is calculated correctly.
  @path = path.delete_prefix('/') # Examples: "posts/:id/edit" or "posts"
  @internal = internal
end

Instance Method Details

#definitionObject



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

def definition
  {
    resource_logical_id => {
      Type: "AWS::ApiGateway::Resource",
      Properties: {
        ParentId: parent_id,
        PathPart: path_part,
        RestApiId: "!Ref #{RestApi.logical_id(@internal)}",
      }
    }
  }
end

#descObject

For parameter description



40
41
42
# File 'lib/jets/cfn/resource/api_gateway/resource.rb', line 40

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

#outputsObject



23
24
25
26
27
# File 'lib/jets/cfn/resource/api_gateway/resource.rb', line 23

def outputs
  {
    logical_id => "!Ref #{logical_id}",
  }
end

#parent_idObject



54
55
56
# File 'lib/jets/cfn/resource/api_gateway/resource.rb', line 54

def parent_id
  "!Ref " + parent_path_parameter
end

#parent_path_parameterObject



44
45
46
47
48
49
50
51
52
# File 'lib/jets/cfn/resource/api_gateway/resource.rb', line 44

def parent_path_parameter
  if @path.include?('/') # posts/:id or posts/:id/edit
    parent_path = @path.split('/')[0..-2].join('/')
    parent_logical_id = path_logical_id(parent_path)
    Jets::Cfn::Resource.truncate_id(parent_logical_id, "ApiResource")
  else
    "RootResourceId"
  end
end

#pathObject

Modify the path to conform to API Gateway capture expressions



64
65
66
# File 'lib/jets/cfn/resource/api_gateway/resource.rb', line 64

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

#path_partObject



58
59
60
61
# File 'lib/jets/cfn/resource/api_gateway/resource.rb', line 58

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

#resource_logical_idObject



29
30
31
32
33
34
35
36
37
# File 'lib/jets/cfn/resource/api_gateway/resource.rb', line 29

def resource_logical_id
  if @path == ''
    # Not including ApiResource in the logical id so it doesn't collide with a
    # user-defined ApiResource that happens to be named RootResourceId.
    "RootResourceId"
  else
    Jets::Cfn::Resource.truncate_id(path_logical_id(@path), "ApiResource")
  end
end

#transform_capture(text) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jets/cfn/resource/api_gateway/resource.rb', line 68

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