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/", 
  :base_path => "/", 
  :clean_directory => false, 
  :formatting => :pretty
}

Class Method Summary collapse

Class Method Details

.camelize_keys_deep!(h) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/swagger/docs/generator.rb', line 15

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, extension) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/swagger/docs/generator.rb', line 29

def get_api_path(spec, extension)
  extension = ".#{extension}" if extension
  path_api = trim_leading_slash(spec.to_s.gsub("(.:format)", extension.to_s))
  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_methodsObject



60
61
62
# File 'lib/swagger/docs/generator.rb', line 60

def set_real_methods
  Config.base_api_controller.send(:include, Methods) # replace impotent methods with live ones
end

.trim_leading_slash(str) ⇒ Object



44
45
46
47
48
# File 'lib/swagger/docs/generator.rb', line 44

def trim_leading_slash(str)
  return str if !str
  return str unless str[0] == '/'
  str[1..-1]
end

.trim_slashes(str) ⇒ Object



56
57
58
# File 'lib/swagger/docs/generator.rb', line 56

def trim_slashes(str)
  trim_leading_slash(trim_trailing_slash(str))
end

.trim_trailing_slash(str) ⇒ Object



50
51
52
53
54
# File 'lib/swagger/docs/generator.rb', line 50

def trim_trailing_slash(str)
  return str if !str
  return str unless str[-1] == '/'
  str[0..-2]
end

.write_doc(api_version, config) ⇒ Object



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

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), config[:api_extension_type]).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
    write_to_file "#{api_file_path}/#{demod}.json", resource, config
    # 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)

  write_to_file "#{api_file_path}/api-docs.json", resources, config
  results
end

.write_docs(apis = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/swagger/docs/generator.rb', line 64

def write_docs(apis = nil)
  apis ||= Config.registered_apis
  results = {}
  set_real_methods
  unless apis.empty?
    apis.each do |api_version,config|
      config.reverse_merge!(DEFAULT_CONFIG)
      results[api_version] = write_doc(api_version, config)
    end
  else
    results[DEFAULT_VER] = write_doc(DEFAULT_VER, DEFAULT_CONFIG)
  end
  results
end

.write_to_file(path, structure, config = {}) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/swagger/docs/generator.rb', line 130

def write_to_file(path, structure, config={})
  content = case config[:formatting]
    when :pretty; JSON.pretty_generate structure
    else;         structure.to_json
  end
  File.open(path, 'w') { |file| file.write content }
end