Class: Swagger::Docs::Generator
- Inherits:
-
Object
- Object
- Swagger::Docs::Generator
- Defined in:
- lib/swagger/docs/generator.rb
Constant Summary collapse
- DEFAULT_VER =
"1.0"- DEFAULT_CONFIG =
{ :api_file_path => "public/", :api_file_name => "api-docs.json", :base_path => "/", :clean_directory => false, :formatting => :pretty }
Class Method Summary collapse
- .generate_doc(api_version, settings, config) ⇒ Object
- .generate_docs(apis = nil) ⇒ Object
- .set_real_methods ⇒ Object
- .write_doc(result) ⇒ Object
- .write_docs(apis = nil) ⇒ Object
Class Method Details
.generate_doc(api_version, settings, config) ⇒ Object
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 |
# File 'lib/swagger/docs/generator.rb', line 63 def generate_doc(api_version, settings, config) root = { "swagger" => "2.0", # "info" => settings[:info], "host" => settings[:host], "basePath" => settings[:base_path], "schemes" => [ "http" ], :paths => {}, } results = {:processed => [], :skipped => []} resources = [] get_route_paths(settings[:controller_base_path]).each do |path| ret = process_path(path, root, config, settings) results[ret[:action]] << ret if ret[:action] == :processed resources << generate_resource(ret[:path], ret[:apis], ret[:models], settings, root, config, ret[:klass].swagger_config) debased_path = get_debased_path(ret[:path], settings[:controller_base_path]) ret[:apis] && ret[:apis].each do |rea| name = rea[:path].gsub('.json', '') name += '/' if root[:paths].has_key?(name) api_content = rea[:operations][0] description = ret[:klass].swagger_config[:description] resource_api = { # path: "/#{Config.transform_path(trim_leading_slash(debased_path), api_version)}.{format}", "#{name}" => { "#{api_content[:method]}" => { "summary" => api_content[:summary], "description" => description, "parameters" => api_content[:parameters].map{|pr| { "name" => pr[:name], "in" => pr[:param_type], "required" => pr[:required] ? true : false, "description" => pr[:description], "type" => pr[:type] } }, "responses" => { "200" => { "description" => description, "schema" => { # "type": "array", # "items": { # "properties": { # "firstName": { # "type": "string" # }, # "lastName": { # "type": "string" # }, # "username": { # "type": "string" # } # } # } } } } } } }.as_json # root[:apis] << resource_api root[:paths].merge!(resource_api) end end end root['resources'] = resources results[:root] = root results end |
.generate_docs(apis = nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/swagger/docs/generator.rb', line 46 def generate_docs(apis=nil) apis ||= Config.registered_apis results = {} set_real_methods apis[DEFAULT_VER] = DEFAULT_CONFIG if apis.empty? apis.each do |api_version, config| settings = get_settings(api_version, config) config.reverse_merge!(DEFAULT_CONFIG) results[api_version] = generate_doc(api_version, settings, config) results[api_version][:settings] = settings results[api_version][:config] = config end results end |
.set_real_methods ⇒ Object
16 17 18 19 20 21 |
# File 'lib/swagger/docs/generator.rb', line 16 def set_real_methods # replace impotent methods with live ones Config.base_api_controllers.each do |controller| controller.send(:include, Methods) end end |
.write_doc(result) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/swagger/docs/generator.rb', line 28 def write_doc(result) settings = result[:settings] config = result[:config] create_output_paths(settings[:api_file_path]) clean_output_paths(settings[:api_file_path]) if config[:clean_directory] || false root = result[:root] resources = root.delete 'resources' root.merge!(config[:attributes] || {}) # merge custom user attributes like info # write the api-docs file write_to_file("#{settings[:api_file_path]}/#{config[:api_file_name]}", root, config) # write the individual resource files resources.each do |resource| resource_file_path = resource.delete 'resourceFilePath' write_to_file(File.join(settings[:api_file_path], "#{resource_file_path}.json"), resource, config) end result end |
.write_docs(apis = nil) ⇒ Object
23 24 25 26 |
# File 'lib/swagger/docs/generator.rb', line 23 def write_docs(apis = nil) results = generate_docs(apis) results.each{|api_version, result| write_doc(result) } end |