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

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

Instance Method Summary collapse

Methods inherited from Base

#replacements, #resource

Constructor Details

#initialize(path, internal: false) ⇒ Resource

Returns a new instance of Resource.



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

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

Instance Method Details

#definitionObject



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

def definition
  {
    resource_logical_id => {
      type: "AWS::ApiGateway::Resource",
      properties: {
        parent_id: parent_id,
        path_part: path_part,
        rest_api_id: "!Ref #{RestApi.logical_id(@internal)}",
      }
    }
  }
end

#descObject

For parameter description



36
37
38
# File 'lib/jets/resource/api_gateway/resource.rb', line 36

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

#outputsObject



21
22
23
24
25
# File 'lib/jets/resource/api_gateway/resource.rb', line 21

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

#parent_idObject



50
51
52
# File 'lib/jets/resource/api_gateway/resource.rb', line 50

def parent_id
  "!Ref " + parent_path_parameter
end

#parent_path_parameterObject



40
41
42
43
44
45
46
47
48
# File 'lib/jets/resource/api_gateway/resource.rb', line 40

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::Resource.truncate_id(parent_logical_id, "ApiResource")
  else
    "RootResourceId"
  end
end

#pathObject

Modify the path to conform to API Gateway capture expressions



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

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

#path_partObject



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

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

#resource_logical_idObject



27
28
29
30
31
32
33
# File 'lib/jets/resource/api_gateway/resource.rb', line 27

def resource_logical_id
  if @path == ''
    "RootResourceId"
  else
    Jets::Resource.truncate_id path_logical_id(@path), "ApiResource"
  end
end

#transform_capture(text) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jets/resource/api_gateway/resource.rb', line 64

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