Sha256: 7adee6022412d5e36142ea3682f860cabd8a58d85b014170af27859d1818e776

Contents?: true

Size: 1.83 KB

Versions: 48

Compression:

Stored size: 1.83 KB

Contents

module Jets::Resource::ApiGateway
  class Resource < Jets::Resource::Base
    def initialize(path)
      @path = path # Examples: "posts/:id/edit" or "posts"
    end

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

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

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

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

    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

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

    # Modify the path to conform to API Gateway capture expressions
    def path
      @path.split('/').map {|s| transform_capture(s) }.join('/')
    end

    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

  private
    # Similar path_logical_id method in resource/route.rb
    def path_logical_id(path)
      path.gsub('/','_').gsub(':','').gsub('*','').camelize
    end
  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
jets-1.1.5 lib/jets/resource/api_gateway/resource.rb
jets-1.1.4 lib/jets/resource/api_gateway/resource.rb
jets-1.1.3 lib/jets/resource/api_gateway/resource.rb
jets-1.1.2 lib/jets/resource/api_gateway/resource.rb
jets-1.1.1 lib/jets/resource/api_gateway/resource.rb
jets-1.1.0 lib/jets/resource/api_gateway/resource.rb
jets-1.0.18 lib/jets/resource/api_gateway/resource.rb
jets-1.0.17 lib/jets/resource/api_gateway/resource.rb
jets-1.0.16 lib/jets/resource/api_gateway/resource.rb
jets-1.0.15 lib/jets/resource/api_gateway/resource.rb
jets-1.0.13 lib/jets/resource/api_gateway/resource.rb
jets-1.0.12 lib/jets/resource/api_gateway/resource.rb
jets-1.0.11 lib/jets/resource/api_gateway/resource.rb
jets-1.0.10 lib/jets/resource/api_gateway/resource.rb
jets-1.0.9 lib/jets/resource/api_gateway/resource.rb
jets-1.0.8 lib/jets/resource/api_gateway/resource.rb
jets-1.0.7 lib/jets/resource/api_gateway/resource.rb
jets-1.0.6 lib/jets/resource/api_gateway/resource.rb
jets-1.0.5 lib/jets/resource/api_gateway/resource.rb
jets-1.0.4 lib/jets/resource/api_gateway/resource.rb