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
|
# File 'lib/swagger/docs/generator.rb', line 47
def write_doc(api_version, config)
base_path = config[:controller_base_path]
api_file_path = config[:api_file_path]
results = {:processed => [], :skipped => []}
FileUtils.mkdir_p(api_file_path) Dir.foreach(api_file_path) {|f| fn = File.join(api_file_path, f); File.delete(fn) if !File.directory?(fn)}
= { :api_version => api_version, :swagger_version => "1.2", :base_path => "/#{base_path}"}
resources = .merge({:apis => []})
paths = Rails.application.routes.routes.map{|i| "#{i.defaults[:controller]}" }
paths = paths.uniq.select{|i| i.start_with?(base_path)}
paths.each do |path|
klass = "#{path.to_s.camelize}Controller".constantize
if !klass.methods.include?(:swagger_config) or !klass.swagger_config[:controller]
results[:skipped] << path
next
end
apis = []
Rails.application.routes.routes.select{|i| i.defaults[:controller] == path}.each do |route|
action = route.defaults[:action]
verb = route.verb.source.to_s.delete('$'+'^').downcase.to_sym
next if !operations = klass.swagger_actions[action.to_sym]
operations = Hash[operations.map {|k, v| [k.to_s.gsub("@","").to_sym, v] }] operations[:method] = verb
operations[:nickname] = "#{path.camelize}##{action}"
apis << {:path => get_api_path(route.path.spec).gsub("/#{base_path}",""), :operations => [operations]}
end
demod = "#{path.to_s.camelize}".demodulize.camelize.underscore
resource = .merge({:resource_path => "/#{demod}", :apis => apis})
camelize_keys_deep!(resource)
File.open("#{api_file_path}#{demod}.json", 'w') { |file| file.write(resource.to_json) }
resources[:apis] << {path: "/#{demod}.{format}", description: klass.swagger_config[:description]}
results[:processed] << path
end
camelize_keys_deep!(resources)
File.open("#{api_file_path}api-docs.json", 'w') { |file| file.write(resources.to_json) }
results
end
|