require 'travis/api/app' require 'travis/api/app/endpoint/documentation/resources' class Travis::Api::App class Endpoint # Generated API documentation. class Documentation < Endpoint set prefix: '/docs', public_folder: File.expand_path('../documentation', __FILE__), static_cache_control: :public enable :inline_templates, :static # Don't cache general docs in development configure(:development) { before { @@general_docs = nil } } # HTML view for [/endpoints](#/endpoints/). get '/' do cache_control :public content_type :html endpoints = Endpoints.endpoints erb :index, {}, endpoints: endpoints.keys.sort.map { |k| endpoints[k] } end helpers do def icon_for(verb) # GET, POST, PATCH, PUT, DELETE" case verb when 'GET' then 'file' when 'POST' then 'edit' when 'PATCH' then 'wrench' when 'PUT' then 'share' when 'DELETE' then 'trash' else 'question-sign' end end def slug_for(route) return route['uri'] if route['verb'] == 'GET' route['verb'] + " " + route['uri'] end def docs_for(entry) with_code_highlighting markdown(entry['doc']) end private def with_code_highlighting(str) str. gsub(/json\(:([^)]+)\)/) { "
" + Resources::Helpers.json($1) + "
" }. gsub('/, ''). gsub(/TODO:?/, 'TODO') end def general_docs @@general_docs ||= doc_files.map do |file| header, content = File.read(file).split("\n", 2) content = markdown(content) subheaders = [] content.gsub!(/

(.*)<\/h2>/) do subheaders << $1 "

#{$1}

" end header.gsub! /^#* */, '' { id: header, title: header, content: with_code_highlighting(content), subheaders: subheaders } end end def doc_files pattern = File.expand_path('../../../../../../docs/*.md', __FILE__) Dir[pattern].sort end end end end end __END__ @@ index Travis CI API documentation

The Travis CI API

<% general_docs.each do |doc| %> <%= erb :entry, locals: doc %> <% end %> <% endpoints.each do |endpoint| %> <%= erb :entry, {}, id: endpoint['name'], title: endpoint['name'], content: erb(:endpoint_content, {}, endpoint: endpoint) %> <% end %>
@@ endpoint_content <% unless endpoint['doc'].to_s.empty? %> <%= docs_for endpoint %> <% end %> <% endpoint['routes'].each do |route| %>

<%= route['verb'] %> <%= route['uri'] %>

<% if route['scope'] %>

Required authorization scope: <%= route['scope'] %>

<% end %> <%= docs_for route %>
<% end %> @@ entry

<%= title %> #

<%= content %>