Class: Swagger::Docs::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger/docs/generator.rb

Class Method Summary collapse

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 = 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



34
35
36
# File 'lib/swagger/docs/generator.rb', line 34

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

.write_doc(api_version, config) ⇒ Object



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) # 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)} # clean output path

  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?(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] }] # rename :@instance hash keys
      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 = 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: "/#{demod}.{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



38
39
40
41
42
43
44
45
# File 'lib/swagger/docs/generator.rb', line 38

def write_docs(apis)
  results = {}
  set_real_methods
  Config.registered_apis.each do |api_version,config|
    results[api_version] = write_doc(api_version, config)
  end
  results
end