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) ⇒ Resource

Returns a new instance of Resource.



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

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

Instance Method Details

#definitionObject



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

def definition
  {
    resource_logical_id => {
      type: "AWS::ApiGateway::Resource",
      properties: {
        parent_id: parent_id,
        path_part: path_part,
        rest_api_id: "!Ref RestApi",
      }
    }
  }
end

#descObject

For parameter description



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

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

#outputsObject



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

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

#parent_idObject



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

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}ApiResource"
  else
    "!GetAtt RestApi.RootResourceId"
  end
end

#pathObject

Modify the path to conform to API Gateway capture expressions



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

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

#path_partObject



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

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

#resource_logical_idObject



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

def resource_logical_id
  if @path == ''
    "RootResourceId"
  else
    "#{path_logical_id(@path)}ApiResource"
  end
end

#transform_capture(text) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jets/resource/api_gateway/resource.rb', line 59

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