def mount(mounts, opts = {})
mounts = { mounts => '/' } unless mounts.respond_to?(:each_pair)
mounts.each_pair do |app, path|
if app.respond_to?(:mount_instance)
mount(app.mount_instance(configuration: opts[:with] || {}) => path)
next
end
in_setting = inheritable_setting
if app.respond_to?(:inheritable_setting, true)
mount_path = Grape::Router.normalize_path(path)
app.top_level_setting.namespace_stackable[:mount_path] = mount_path
app.inherit_settings(inheritable_setting)
in_setting = app.top_level_setting
app.change!
change!
end
endpoints << Grape::Endpoint.new(
in_setting,
method: :any,
path: path,
app: app,
route_options: { anchor: false },
forward_match: !app.respond_to?(:inheritable_setting),
for: self
)
end
end
def route(methods, paths = ['/'], route_options = {}, &block)
methods = '*' if methods == :any
endpoint_options = {
method: methods,
path: paths,
for: self,
route_options: {
params: namespace_stackable_with_hash(:params) || {}
}.deep_merge(route_setting(:description) || {}).deep_merge(route_options || {})
}
new_endpoint = Grape::Endpoint.new(inheritable_setting, endpoint_options, &block)
endpoints << new_endpoint unless endpoints.any? { |e| e.equals?(new_endpoint) }
route_end
reset_validations!
end
%w[get post put head delete options patch].each do |meth|
define_method meth do |*args, &block|
options = args.extract_options!
paths = args.first || ['/']
route(meth.upcase, paths, options, &block)
end
end
def namespace(space = nil, options = {}, &block)
if space || block_given?
within_namespace do
previous_namespace_description = @namespace_description
@namespace_description = (@namespace_description || {}).deep_merge(namespace_setting(:description) || {})
nest(block) do
if space
namespace_stackable(:namespace, Namespace.new(space, options))
end
end
@namespace_description = previous_namespace_description
end
else
Namespace.joined_space_path(namespace_stackable(:namespace))
end
end
alias group namespace
alias resource namespace
alias resources namespace
alias segment namespace
def routes
@routes ||= prepare_routes
end
def reset_routes!
endpoints.each(&:reset_routes!)
@routes = nil
end
def reset_endpoints!
@endpoints = []
end
def route_param(param, options = {}, &block)
options = options.dup
options[:requirements] = {
param.to_sym => options[:requirements]
} if options[:requirements].is_a?(Regexp)
Grape::Validations::ParamsScope.new(api: self) do
requires param, type: options[:type]
end if options.key?(:type)
namespace(":#{param}", options, &block)
end
def versions
@versions ||= []
end
end
end