forked from Apipie/apipie-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractor.rb
180 lines (149 loc) · 5.06 KB
/
extractor.rb
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
require 'singleton'
require 'fileutils'
require 'set'
require 'yaml'
require 'apipie/extractor/recorder'
require 'apipie/extractor/writer'
require 'apipie/extractor/collector'
class Apipie::Railtie
initializer 'apipie.extractor' do |app|
ActiveSupport.on_load :action_controller do
before_action do |controller|
if Apipie.configuration.record
Apipie::Extractor.call_recorder.analyse_controller(controller)
end
end
end
app.middleware.use ::Apipie::Extractor::Recorder::Middleware
ActionController::TestCase.send(:prepend, Apipie::Extractor::Recorder::FunctionalTestRecording)
ActionController::TestCase::Behavior.send(:prepend, Apipie::Extractor::Recorder::FunctionalTestRecording)
end
end
module Apipie
module Extractor
class << self
def start(record)
Apipie.configuration.record = record
Apipie.configuration.force_dsl = true
end
def finish
record_params, record_examples = false, false
case Apipie.configuration.record
when "params" then record_params = true
when "examples" then record_examples = true
when "all" then record_params = true, record_examples = true
end
if record_examples
puts "Writing examples to a file"
write_examples
end
if record_params
puts "Updating auto-generated documentation"
write_docs
end
end
def logger
Rails.logger
end
def call_recorder
Thread.current[:apipie_call_recorder] ||= Recorder.new
end
def call_finished
@collector ||= Collector.new
if record = call_recorder.record
@collector.handle_record(record)
end
end
def clean_call_recorder
Thread.current[:apipie_call_recorder] = nil
end
def write_docs
Writer.new(@collector).write_docs if @collector
end
def write_examples
Writer.new(@collector).write_examples if @collector
end
def apis_from_routes
return @apis_from_routes if @apis_from_routes
@api_prefix = Apipie.api_base_url.sub(%r{/$},"")
populate_api_routes
update_api_descriptions
@apis_from_routes
end
def controller_path(name)
Apipie.api_controllers_paths.detect { |p| p.include?("#{name}_controller.rb") }
end
private
def all_api_routes
all_routes = Apipie.configuration.api_routes.routes.map do |r|
{
:verb => case r.verb
when Regexp then r.verb.source[/\w+/]
else r.verb.to_s
end,
:path => case
when r.path.respond_to?(:spec) then r.path.spec.to_s
else r.path.to_s
end,
:controller => r.requirements[:controller],
:action => r.requirements[:action]
}
end
return all_routes.find_all do |r|
r[:path].starts_with?(Apipie.api_base_url)
end
end
def populate_api_routes
@apis_from_routes = Hash.new { |h, k| h[k] = [] }
all_api_routes.each do |route|
controller_path, action = route[:controller], route[:action]
next unless controller_path && action
controller_path = controller_path.split('::').map(&:camelize).join('::')
controller = "#{controller_path}Controller"
path = if /^#{Regexp.escape(@api_prefix)}(.*)$/ =~ route[:path]
$1.sub(/\(\.:format\)$/,'')
else
nil
end
if route[:verb].present?
@apis_from_routes[[controller, action]] << {:method => route[:verb], :path => path}
end
end
end
def all_apis_from_docs
resource_descriptions = Apipie.resource_descriptions.values.map(&:values).flatten
method_descriptions = resource_descriptions.map(&:method_descriptions).flatten
return method_descriptions.reduce({}) do |h, desc|
apis = desc.method_apis_to_json.map do |api|
{ :method => api[:http_method],
:path => api[:api_url],
:desc => api[:short_description] }
end
h.update(desc.id => apis)
end
end
def update_api_descriptions
apis_from_docs = all_apis_from_docs
@apis_from_routes.each do |(controller, action), new_apis|
method_key = "#{Apipie.get_resource_id(controller.safe_constantize || next)}##{action}"
old_apis = apis_from_docs[method_key] || []
new_apis.each do |new_api|
new_api[:path]&.sub!(/\(\.:format\)$/,"")
old_api = old_apis.find do |api|
api[:path] == "#{@api_prefix}#{new_api[:path]}"
end
if old_api
new_api[:desc] = old_api[:desc]
end
end
end
end
end
end
end
if ENV["APIPIE_RECORD"]
Apipie::Extractor.start ENV["APIPIE_RECORD"]
end
at_exit do
Apipie::Extractor.finish
end