Class: APISpec::Generator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Generator

Returns a new instance of Generator.



7
8
9
10
11
12
13
14
15
16
# File 'lib/apispec/generator.rb', line 7

def initialize(options = {})
  @options = options
  # logging
  @logger = Logger.new(STDOUT)
  @logger.level = Logger::WARN unless options[:verbose]
  @logger.formatter = proc do |severity, datetime, progname, msg|
    "#{datetime.strftime("%Y-%m-%d %H:%M:%S")} [#{severity}]: #{msg}\n"
  end
  @namespace = APISpec::Namespace.new(nil)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/apispec/generator.rb', line 5

def logger
  @logger
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



5
6
7
# File 'lib/apispec/generator.rb', line 5

def namespace
  @namespace
end

Instance Method Details

#create(node) ⇒ Object

create a doc file for the passed node



94
95
96
97
98
99
100
101
102
103
# File 'lib/apispec/generator.rb', line 94

def create(node)
  dir = File.dirname(node.to_path)
  file_name = File.basename(node.to_path)
  path = File.join("#{@options[:output]}", dir)
  FileUtils.mkdir_p path
  File.open(File.join(path, file_name), "w") do |file|
    @logger.info "create page for #{node}"
    file.write(node.to_html(self))
  end
end

#create_index!Object

create the index frame



40
41
42
43
44
45
# File 'lib/apispec/generator.rb', line 40

def create_index!
  @logger.info "create index frame"
  File.open(File.join("#{@options[:output]}", "index.html"), "w") do |file|
    file.write(template(binding, :index))
  end    
end

#create_links!Object

create the links page (left part of frame)



48
49
50
51
52
53
# File 'lib/apispec/generator.rb', line 48

def create_links!
  @logger.info "create links page"
  File.open(File.join("#{@options[:output]}", "links.html"), "w") do |file|
    file.write(template(binding, :links))
  end
end

#create_objects_and_interfaces!Object

create the documentation files itself



56
57
58
59
60
61
62
63
# File 'lib/apispec/generator.rb', line 56

def create_objects_and_interfaces!
  @namespace.objects.each do |object|
    create(object)
  end
  @namespace.interfaces.each do |interface|
    create(interface)
  end
end

#create_output_folder!Object

creates the output folder



33
34
35
36
37
# File 'lib/apispec/generator.rb', line 33

def create_output_folder!
  @logger.info "remove and create the output folder"
  FileUtils.rm_rf "#{@options[:output]}"
  FileUtils.mkdir_p @options[:output]
end

#create_resources!Object

create the resources for all subfolders and main folder



66
67
68
69
70
71
72
73
# File 'lib/apispec/generator.rb', line 66

def create_resources!
  Dir[File.join(@options[:output], "**/*")].map do |path|
    File.dirname(File.expand_path(path))
  end.uniq.each do |path|
    @logger.info "copy template resources to #{path}"
    FileUtils.cp Dir[File.join(@options[:resource], "*")], path
  end
end

#parse_files!Object

read all ruby files that can be found in the working directory



24
25
26
27
28
29
30
# File 'lib/apispec/generator.rb', line 24

def parse_files!
  @logger.info "parse all files"
  Dir[File.join(@options[:workspace], "**", "*.rb")].each do |path|
    logger.info "read file #{path}..."
    @namespace.read_file(path)
  end
end

#path(dir) ⇒ Object

returns the dir with the root based in the working directory



19
20
21
# File 'lib/apispec/generator.rb', line 19

def path(dir)
  File.join(@options[:workspace], dir)
end

#start!Object

start the generator



76
77
78
79
80
81
82
83
# File 'lib/apispec/generator.rb', line 76

def start!
  parse_files!
  create_output_folder!
  create_index!
  create_links!
  create_objects_and_interfaces!
  create_resources!
end

#template(object, template_name) ⇒ Object

render a template with the passed object as binding



86
87
88
89
90
91
# File 'lib/apispec/generator.rb', line 86

def template(object, template_name)
  path = File.join(@options[:template], "#{template_name}.html.erb")
  erb = ERB.new(File.read(path))
  erb.filename = path
  erb.result(object)
end