Class: Swagger::Docs::Generator
- Inherits:
-
Object
- Object
- Swagger::Docs::Generator
- Defined in:
- lib/swagger/docs/generator.rb
Class Method Summary collapse
- .camelize_keys_deep!(h) ⇒ Object
- .get_api_path(spec) ⇒ Object
- .set_real_methods ⇒ Object
- .trim_leading_slash(str) ⇒ Object
- .trim_slashes(str) ⇒ Object
- .trim_trailing_slash(str) ⇒ Object
- .write_doc(api_version, config) ⇒ Object
- .write_docs(apis) ⇒ Object
Class Method Details
.camelize_keys_deep!(h) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/swagger/docs/generator.rb', line 6 def camelize_keys_deep!(h) h.keys.each do |k| ks = k.to_s.camelize(:lower) h[ks] = h.delete k camelize_keys_deep! h[ks] if h[ks].kind_of? Hash if h[ks].kind_of? Array h[ks].each do |a| next unless a.kind_of? Hash camelize_keys_deep! a end end end end |
.get_api_path(spec) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/swagger/docs/generator.rb', line 20 def get_api_path(spec) path_api = trim_leading_slash(spec.to_s.gsub("(.:format)", "")) parts_new = [] path_api.split("/").each do |path_part| part = path_part if part[0] == ":" part[0] = "{" part << "}" end parts_new << part end path_api = parts_new*"/" end |
.set_real_methods ⇒ Object
50 51 52 |
# File 'lib/swagger/docs/generator.rb', line 50 def set_real_methods ApplicationController.send(:include, Methods) # replace impotent methods with live ones end |
.trim_leading_slash(str) ⇒ Object
34 35 36 37 38 |
# File 'lib/swagger/docs/generator.rb', line 34 def trim_leading_slash(str) return str if !str return str unless str[0] == '/' str[1..-1] end |
.trim_slashes(str) ⇒ Object
46 47 48 |
# File 'lib/swagger/docs/generator.rb', line 46 def trim_slashes(str) trim_leading_slash(trim_trailing_slash(str)) end |
.trim_trailing_slash(str) ⇒ Object
40 41 42 43 44 |
# File 'lib/swagger/docs/generator.rb', line 40 def trim_trailing_slash(str) return str if !str return str unless str[-1] == '/' str[0..-2] end |
.write_doc(api_version, config) ⇒ Object
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 |
# File 'lib/swagger/docs/generator.rb', line 69 def write_doc(api_version, config) base_path = trim_trailing_slash(config[:base_path] || "") controller_base_path = trim_leading_slash(config[:controller_base_path] || "") api_file_path = config[:api_file_path] clean_directory = config[:clean_directory] || false results = {:processed => [], :skipped => []} # create output paths FileUtils.mkdir_p(api_file_path) # recursively create out output path Dir.foreach(api_file_path) {|f| fn = File.join(api_file_path, f); File.delete(fn) if !File.directory?(fn) and File.extname(fn) == '.json'} if clean_directory # clean output path base_path += "/#{controller_base_path}" unless controller_base_path.empty? header = { :api_version => api_version, :swagger_version => "1.2", :base_path => base_path + "/"} resources = header.merge({:apis => []}) paths = Rails.application.routes.routes.map{|i| "#{i.defaults[:controller]}" } paths = paths.uniq.select{|i| i.start_with?(controller_base_path)} paths.each do |path| next if path.empty? klass = "#{path.to_s.camelize}Controller".constantize if !klass.methods.include?(:swagger_config) or !klass.swagger_config[:controller] results[:skipped] << path next end apis = [] debased_path = path.gsub("#{controller_base_path}", "") 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] }] # rename :@instance hash keys operations[:method] = verb operations[:nickname] = "#{path.camelize}##{action}" apis << {:path => trim_slashes(get_api_path(trim_leading_slash(route.path.spec.to_s)).gsub("#{controller_base_path}","")), :operations => [operations]} end demod = "#{debased_path.to_s.camelize}".demodulize.camelize.underscore resource = header.merge({:resource_path => "#{demod}", :apis => apis}) camelize_keys_deep!(resource) # write controller resource file File.open("#{api_file_path}/#{demod}.json", 'w') { |file| file.write(resource.to_json) } # append resource to resources array (for writing out at end) resources[:apis] << {path: "#{trim_leading_slash(debased_path)}.{format}", description: klass.swagger_config[:description]} results[:processed] << path end # write master resource file camelize_keys_deep!(resources) File.open("#{api_file_path}/api-docs.json", 'w') { |file| file.write(resources.to_json) } results end |
.write_docs(apis) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/swagger/docs/generator.rb', line 54 def write_docs(apis) results = {} set_real_methods unless Config.registered_apis.empty? Config.registered_apis.each do |api_version,config| results[api_version] = write_doc(api_version, config) end else config = {:api_file_path => "public/", :base_path => "/"} puts "No swagger_docs config: Using default config #{config}" results["1.0"] = write_doc("1.0", config) end results end |