Class: Swagger::Docs::Generator

Inherits:
Object
  • Object
show all
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

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
# File 'lib/swagger/docs/generator.rb', line 63

def generate_doc(api_version, settings, config)
  root = { 
    "apiVersion" => api_version, 
    "swaggerVersion" => "1.2", 
    "basePath" => settings[:base_path], 
    :apis => [],
    :authorizations => settings[:authorizations]
  }
  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])
      resource_api = {
        path: "/#{Config.transform_path(trim_leading_slash(debased_path), api_version)}.{format}",
        description: ret[:klass].swagger_config[:description]
      }
      root[:apis] << resource_api
    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_methodsObject


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