summaryrefslogtreecommitdiff
path: root/lib/api/templates.rb
blob: b9e718147e10d1e9285c96e0ed582b0f1a023357 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module API
  class Templates < Grape::API
    GLOBAL_TEMPLATE_TYPES = {
      gitignores:     Gitlab::Template::GitignoreTemplate,
      gitlab_ci_ymls: Gitlab::Template::GitlabCiYmlTemplate
    }.freeze

    helpers do
      def render_response(template_type, template)
        not_found!(template_type.to_s.singularize) unless template
        present template, with: Entities::Template
      end
    end

    GLOBAL_TEMPLATE_TYPES.each do |template_type, klass|
      # Get the list of the available template
      #
      # Example Request:
      #   GET /gitignores
      #   GET /gitlab_ci_ymls
      get template_type.to_s do
        present klass.all, with: Entities::TemplatesList
      end

      # Get the text for a specific template present in local filesystem
      #
      # Parameters:
      #   name (required) - The name of a template
      #
      # Example Request:
      #   GET /gitignores/Elixir
      #   GET /gitlab_ci_ymls/Ruby
      get "#{template_type}/:name" do
        required_attributes! [:name]
        new_template = klass.find(params[:name])
        render_response(template_type, new_template)
      end
    end
  end
end