2012-08-02 16:11:37 +08:00
|
|
|
require 'kramdown'
|
|
|
|
|
2012-07-19 16:37:46 +08:00
|
|
|
module Grape
|
|
|
|
class API
|
|
|
|
class << self
|
|
|
|
attr_reader :combined_routes
|
|
|
|
|
|
|
|
alias original_mount mount
|
|
|
|
|
|
|
|
def mount(mounts)
|
|
|
|
original_mount mounts
|
|
|
|
@combined_routes ||= {}
|
2012-07-27 19:30:56 +08:00
|
|
|
mounts::routes.each do |route|
|
2013-01-24 07:04:51 +08:00
|
|
|
resource = route.route_path.match('\/(\w*?)[\.\/\(]').captures.first
|
|
|
|
next if resource.empty?
|
2012-07-27 19:30:56 +08:00
|
|
|
@combined_routes[resource.downcase] ||= []
|
|
|
|
@combined_routes[resource.downcase] << route
|
|
|
|
end
|
2012-07-19 16:37:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_swagger_documentation(options={})
|
|
|
|
documentation_class = create_documentation_class
|
|
|
|
|
2012-07-19 22:15:14 +08:00
|
|
|
documentation_class.setup({:target_class => self}.merge(options))
|
2012-07-19 16:37:46 +08:00
|
|
|
mount(documentation_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-07-19 22:15:14 +08:00
|
|
|
def create_documentation_class
|
2012-07-24 22:47:58 +08:00
|
|
|
|
2012-07-19 16:37:46 +08:00
|
|
|
Class.new(Grape::API) do
|
|
|
|
class << self
|
|
|
|
def name
|
2012-07-19 22:15:14 +08:00
|
|
|
@@class_name
|
2012-07-19 16:37:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-19 22:15:14 +08:00
|
|
|
def self.setup(options)
|
|
|
|
defaults = {
|
|
|
|
:target_class => nil,
|
|
|
|
:mount_path => '/swagger_doc',
|
|
|
|
:base_path => nil,
|
|
|
|
:api_version => '0.1',
|
2012-10-19 22:49:43 +08:00
|
|
|
:markdown => false,
|
|
|
|
:hide_documentation_path => false
|
2012-07-19 22:15:14 +08:00
|
|
|
}
|
|
|
|
options = defaults.merge(options)
|
|
|
|
|
|
|
|
@@target_class = options[:target_class]
|
|
|
|
@@mount_path = options[:mount_path]
|
|
|
|
@@class_name = options[:class_name] || options[:mount_path].gsub('/','')
|
2012-08-06 16:09:07 +08:00
|
|
|
@@markdown = options[:markdown]
|
2012-10-19 22:49:43 +08:00
|
|
|
@@hide_documentation_path = options[:hide_documentation_path]
|
2012-07-25 22:47:36 +08:00
|
|
|
api_version = options[:api_version]
|
|
|
|
base_path = options[:base_path]
|
2012-07-19 22:15:14 +08:00
|
|
|
|
|
|
|
desc 'Swagger compatible API description'
|
|
|
|
get @@mount_path do
|
|
|
|
header['Access-Control-Allow-Origin'] = '*'
|
|
|
|
header['Access-Control-Request-Method'] = '*'
|
|
|
|
routes = @@target_class::combined_routes
|
|
|
|
|
2012-10-19 22:49:43 +08:00
|
|
|
if @@hide_documentation_path
|
|
|
|
routes.reject!{ |route, value| "/#{route}/".index(parse_path(@@mount_path, nil) << '/') == 0 }
|
|
|
|
end
|
|
|
|
|
2013-01-18 23:20:56 +08:00
|
|
|
routes_array = routes.keys.map do |local_route|
|
|
|
|
{ :path => "#{parse_path(route.route_path.gsub('(.:format)', ''),route.route_version)}/#{local_route}.{format}" }
|
2012-07-19 22:15:14 +08:00
|
|
|
end
|
|
|
|
{
|
2012-07-25 22:47:36 +08:00
|
|
|
apiVersion: api_version,
|
2012-07-19 22:15:14 +08:00
|
|
|
swaggerVersion: "1.1",
|
2013-01-19 05:22:55 +08:00
|
|
|
basePath: base_path || request.base_url,
|
2012-07-19 22:15:14 +08:00
|
|
|
operations:[],
|
|
|
|
apis: routes_array
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Swagger compatible API description for specific API', :params =>
|
|
|
|
{
|
2012-08-02 16:11:37 +08:00
|
|
|
"name" => { :desc => "Resource name of mounted API", :type => "string", :required => true },
|
2012-07-19 22:15:14 +08:00
|
|
|
}
|
|
|
|
get "#{@@mount_path}/:name" do
|
|
|
|
header['Access-Control-Allow-Origin'] = '*'
|
|
|
|
header['Access-Control-Request-Method'] = '*'
|
|
|
|
routes = @@target_class::combined_routes[params[:name]]
|
|
|
|
routes_array = routes.map do |route|
|
2012-08-06 16:09:07 +08:00
|
|
|
notes = route.route_notes && @@markdown ? Kramdown::Document.new(route.route_notes.strip_heredoc).to_html : route.route_notes
|
2012-07-19 22:15:14 +08:00
|
|
|
{
|
2012-08-17 00:07:00 +08:00
|
|
|
:path => parse_path(route.route_path, api_version),
|
2012-07-19 22:15:14 +08:00
|
|
|
:operations => [{
|
2012-08-02 16:11:37 +08:00
|
|
|
:notes => notes,
|
2012-07-19 22:15:14 +08:00
|
|
|
:summary => route.route_description || '',
|
2012-08-06 19:18:48 +08:00
|
|
|
:nickname => route.route_method + route.route_path.gsub(/[\/:\(\)\.]/,'-'),
|
2012-07-19 22:15:14 +08:00
|
|
|
:httpMethod => route.route_method,
|
2012-08-17 03:47:18 +08:00
|
|
|
:parameters => parse_header_params(route.route_headers) +
|
|
|
|
parse_params(route.route_params, route.route_path, route.route_method)
|
2012-07-19 22:15:14 +08:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
2012-07-25 22:47:36 +08:00
|
|
|
apiVersion: api_version,
|
2012-07-19 22:15:14 +08:00
|
|
|
swaggerVersion: "1.1",
|
2013-01-19 05:22:55 +08:00
|
|
|
basePath: base_path || request.base_url,
|
2012-07-19 22:15:14 +08:00
|
|
|
resourcePath: "",
|
|
|
|
apis: routes_array
|
|
|
|
}
|
|
|
|
end
|
2012-07-19 16:37:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
helpers do
|
2012-07-26 20:41:47 +08:00
|
|
|
def parse_params(params, path, method)
|
2012-08-17 03:47:18 +08:00
|
|
|
if params
|
|
|
|
params.map do |param, value|
|
|
|
|
value[:type] = 'file' if value.is_a?(Hash) && value[:type] == 'Rack::Multipart::UploadedFile'
|
|
|
|
|
|
|
|
dataType = value.is_a?(Hash) ? value[:type]||'String' : 'String'
|
|
|
|
description = value.is_a?(Hash) ? value[:desc] : ''
|
|
|
|
required = value.is_a?(Hash) ? !!value[:required] : false
|
|
|
|
paramType = path.match(":#{param}") ? 'path' : (method == 'POST') ? 'body' : 'query'
|
2012-09-05 19:51:48 +08:00
|
|
|
name = (value.is_a?(Hash) && value[:full_name]) || param
|
2012-08-17 03:47:18 +08:00
|
|
|
{
|
|
|
|
paramType: paramType,
|
2012-09-05 19:51:48 +08:00
|
|
|
name: name,
|
2012-08-17 03:47:18 +08:00
|
|
|
description: description,
|
|
|
|
dataType: dataType,
|
|
|
|
required: required
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def parse_header_params(params)
|
|
|
|
if params
|
2012-08-18 07:41:23 +08:00
|
|
|
params.map do |param, value|
|
2012-08-17 03:47:18 +08:00
|
|
|
dataType = 'String'
|
2012-08-18 07:41:23 +08:00
|
|
|
description = value.is_a?(Hash) ? value[:description] : ''
|
|
|
|
required = value.is_a?(Hash) ? !!value[:required] : false
|
2012-08-17 03:47:18 +08:00
|
|
|
paramType = "header"
|
|
|
|
{
|
|
|
|
paramType: paramType,
|
2012-08-18 07:41:23 +08:00
|
|
|
name: param,
|
2012-08-17 03:47:18 +08:00
|
|
|
description: description,
|
|
|
|
dataType: dataType,
|
|
|
|
required: required
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
[]
|
2012-07-19 16:37:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-17 00:07:00 +08:00
|
|
|
def parse_path(path, version)
|
2012-07-19 16:37:46 +08:00
|
|
|
# adapt format to swagger format
|
|
|
|
parsed_path = path.gsub('(.:format)', '.{format}')
|
2013-01-18 23:20:56 +08:00
|
|
|
# This is attempting to emulate the behavior of
|
|
|
|
# Rack::Mount::Strexp. We cannot use Strexp directly because
|
|
|
|
# all it does is generate regular expressions for parsing URLs.
|
|
|
|
# TODO: Implement a Racc tokenizer to properly generate the
|
2012-10-17 00:12:59 +08:00
|
|
|
# parsed path.
|
|
|
|
parsed_path = parsed_path.gsub(/:([a-zA-Z_]\w*)/, '{\1}')
|
2012-08-17 00:07:00 +08:00
|
|
|
# add the version
|
|
|
|
parsed_path = parsed_path.gsub('{version}', version) if version
|
|
|
|
parsed_path
|
2012-07-19 16:37:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-08-02 16:11:37 +08:00
|
|
|
|
2012-08-06 16:09:07 +08:00
|
|
|
class Object
|
|
|
|
##
|
|
|
|
# @person ? @person.name : nil
|
|
|
|
# vs
|
|
|
|
# @person.try(:name)
|
2012-09-05 19:23:46 +08:00
|
|
|
#
|
2012-08-08 16:25:00 +08:00
|
|
|
# File activesupport/lib/active_support/core_ext/object/try.rb#L32
|
|
|
|
def try(*a, &b)
|
|
|
|
if a.empty? && block_given?
|
|
|
|
yield self
|
|
|
|
else
|
|
|
|
__send__(*a, &b)
|
|
|
|
end
|
2012-08-06 16:09:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-02 16:11:37 +08:00
|
|
|
class String
|
|
|
|
# strip_heredoc from rails
|
|
|
|
# File activesupport/lib/active_support/core_ext/string/strip.rb, line 22
|
|
|
|
def strip_heredoc
|
|
|
|
indent = scan(/^[ \t]*(?=\S)/).min.try(:size) || 0
|
|
|
|
gsub(/^[ \t]{#{indent}}/, '')
|
|
|
|
end
|
|
|
|
end
|