-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRakefile
60 lines (49 loc) · 1.6 KB
/
Rakefile
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'bundler/setup'
require_relative 'config/application'
load 'tasks/otr-activerecord.rake'
desc 'Default: run specs.'
task :default do
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |config|
config.verbose = false
end
Rake::Task['spec'].invoke
end
namespace :db do
task :environment do
end
end
namespace :grape do
desc 'Print Grape routes'
task :routes do
mapping = method_mapping
grape_klasses = ObjectSpace.each_object(Class).select { |klass| klass < Grape::API }
routes = grape_klasses.
flat_map(&:routes).
uniq { |r| r.send(mapping[:path]) + r.send(mapping[:method]).to_s }
method_width, path_width, version_width, desc_width = widths(routes, mapping)
routes.each do |api|
method = api.send(mapping[:method]).to_s.rjust(method_width)
path = api.send(mapping[:path]).to_s.ljust(path_width)
version = api.send(mapping[:version]).to_s.ljust(version_width)
desc = api.send(mapping[:description]).to_s.ljust(desc_width)
puts " #{method} | #{path} | #{version} | #{desc}"
end
end
def widths(routes, mapping)
[
routes.map { |r| r.send(mapping[:method]).try(:length) }.compact.max || 0,
routes.map { |r| r.send(mapping[:path]).try(:length) }.compact.max || 0,
routes.map { |r| r.send(mapping[:version]).try(:length) }.compact.max || 0,
routes.map { |r| r.send(mapping[:description]).try(:length) }.compact.max || 0
]
end
def method_mapping
{
method: 'request_method',
path: 'path',
version: 'version',
description: 'description'
}
end
end