rapi_doc 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/doc_util.rb +6 -4
- data/lib/method_doc.rb +25 -28
- data/lib/rapi_config.rb +27 -0
- data/lib/rapi_doc/tasks/rapi_doc_tasks.rake +2 -0
- data/lib/rapi_doc.rb +49 -47
- data/lib/resource_doc.rb +109 -107
- metadata +4 -4
- data/lib/config.rb +0 -25
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/doc_util.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
(show
|
1
|
+
module RapiDoc
|
2
|
+
class DocUtil
|
3
|
+
# print some erb code to a template
|
4
|
+
def self.print_erb(str, show=false)
|
5
|
+
(show ? "<%= " : "<% ") + str + " %>"
|
6
|
+
end
|
5
7
|
end
|
6
8
|
end
|
data/lib/method_doc.rb
CHANGED
@@ -1,32 +1,29 @@
|
|
1
|
-
|
2
|
-
class
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
def add_variable(name, value)
|
16
|
-
if name == "param"
|
17
|
-
@variables << value
|
18
|
-
return
|
1
|
+
module RapiDoc
|
2
|
+
# This class holds methods about a doc.
|
3
|
+
class MethodDoc
|
4
|
+
attr_accessor :scope, :content, :request, :response, :code
|
5
|
+
|
6
|
+
def initialize(type)
|
7
|
+
@scope = type
|
8
|
+
@variables = []
|
9
|
+
@content = ""
|
10
|
+
@code = ""
|
11
|
+
@request = ""
|
12
|
+
@response = ""
|
19
13
|
end
|
14
|
+
|
15
|
+
|
16
|
+
def add_variable(name, value)
|
17
|
+
if name == "param"
|
18
|
+
@variables << value
|
19
|
+
return
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
eval("@#{name}= \"#{value}\"")
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_binding
|
26
|
+
binding
|
26
27
|
end
|
27
28
|
end
|
28
|
-
|
29
|
-
def get_binding
|
30
|
-
binding
|
31
|
-
end
|
32
|
-
end
|
29
|
+
end
|
data/lib/rapi_config.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module RapiDoc
|
2
|
+
module RapiConfig
|
3
|
+
def config_dir
|
4
|
+
File.join(::Rails.root.to_s, 'config/rapi_doc')
|
5
|
+
end
|
6
|
+
|
7
|
+
def template_dir
|
8
|
+
File.join(File.dirname(__FILE__), '/../templates')
|
9
|
+
end
|
10
|
+
|
11
|
+
def config_file(location)
|
12
|
+
File.join(find_location(location), 'config.yml')
|
13
|
+
end
|
14
|
+
|
15
|
+
def index_layout_file(location)
|
16
|
+
File.join(find_location(location), 'index.html.erb')
|
17
|
+
end
|
18
|
+
|
19
|
+
def resource_layout_file(location)
|
20
|
+
File.join(find_location(location), 'resource.html.erb')
|
21
|
+
end
|
22
|
+
|
23
|
+
def find_location(location)
|
24
|
+
location == :target ? config_dir : template_dir
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/rapi_doc.rb
CHANGED
@@ -2,68 +2,70 @@ require 'erb'
|
|
2
2
|
require 'fileutils'
|
3
3
|
require 'doc_util'
|
4
4
|
require 'resource_doc'
|
5
|
-
require '
|
5
|
+
require 'rapi_config'
|
6
6
|
require 'rapi_doc/railtie' if defined?(Rails)
|
7
7
|
|
8
|
-
|
8
|
+
module RapiDoc
|
9
|
+
class RAPIDoc
|
9
10
|
|
10
|
-
|
11
|
+
include RapiConfig
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
13
|
+
# Initalize the ApiDocGenerator
|
14
|
+
def initialize(resources)
|
15
|
+
puts "Apidoc started..."
|
16
|
+
@resources = resources
|
17
|
+
generate_templates!
|
18
|
+
move_structure!
|
19
|
+
puts "Finished."
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
# Iterates over the resources creates views for them.
|
23
|
+
# Creates an index file
|
24
|
+
def generate_templates!
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
@resources.each do |r|
|
27
|
+
r.parse_apidoc!
|
28
|
+
r.generate_view!(@resources, temp_dir)
|
29
|
+
end
|
30
|
+
generate_index!
|
31
|
+
copy_styles!
|
28
32
|
end
|
29
|
-
generate_index!
|
30
|
-
copy_styles!
|
31
|
-
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
34
|
+
# generate the index file for the api views
|
35
|
+
def generate_index!
|
36
|
+
template = ""
|
37
|
+
File.open(index_layout_file(:target)).each { |line| template << line }
|
38
|
+
parsed = ERB.new(template).result(binding)
|
39
|
+
File.open(File.join(temp_dir, "index.html"), 'w') { |file| file.write parsed }
|
40
|
+
end
|
40
41
|
|
41
|
-
|
42
|
-
|
43
|
-
|
42
|
+
def move_structure!
|
43
|
+
target_folder = "#{::Rails.root.to_s}/public/apidoc/"
|
44
|
+
Dir.mkdir(target_folder) if (!File.directory?(target_folder))
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
46
|
+
Dir.new(temp_dir).each do |d|
|
47
|
+
if d =~ /^[a-zA-Z]+\.(html|css)$/ # Only want to copy over the .html files, not the .erb templates
|
48
|
+
FileUtils.cp File.join(temp_dir + d), target_folder + d
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
#Clean up the no longer needed files
|
52
|
+
filepath = "#{temp_dir}/#{d}"
|
53
|
+
File.delete(filepath) unless File.directory?(filepath)
|
54
|
+
end
|
53
55
|
end
|
54
|
-
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
def copy_styles!
|
58
|
+
Dir[File.join(File.dirname(__FILE__), '..', 'templates/*')].each do |f|
|
59
|
+
if f =~ /[\/a-zA-Z\.]+\.css$/i
|
60
|
+
FileUtils.cp f, temp_dir
|
61
|
+
end
|
60
62
|
end
|
61
63
|
end
|
62
|
-
end
|
63
64
|
|
64
|
-
|
65
|
+
private
|
65
66
|
|
66
|
-
|
67
|
-
|
67
|
+
def temp_dir
|
68
|
+
@temp_dir ||= "#{Dir.mktmpdir("apidoc")}/"
|
69
|
+
end
|
68
70
|
end
|
69
|
-
end
|
71
|
+
end
|
data/lib/resource_doc.rb
CHANGED
@@ -1,124 +1,126 @@
|
|
1
1
|
require 'method_doc'
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
|
6
|
-
|
7
|
-
attr_reader :name, :resource_location, :controller_name, :function_blocks, :class_block
|
8
|
-
|
9
|
-
# Initializes RAPIDoc.
|
10
|
-
def initialize(name, resource_location, controller_name, options = {})
|
11
|
-
@name = name
|
12
|
-
@standard_methods = options[:standard_methods] || [:put, :post, :get, :delete]
|
13
|
-
@resource_location, @controller_name = resource_location, controller_name
|
14
|
-
@function_blocks = []
|
15
|
-
@method_codes = []
|
16
|
-
@header_code = ""
|
3
|
+
module RapiDoc
|
4
|
+
# ResourceDoc holds the information a resource contains. It parses the class header and also the
|
5
|
+
# method documentation, which will be contained in MethodDoc.
|
6
|
+
class ResourceDoc
|
17
7
|
|
18
|
-
|
19
|
-
|
8
|
+
attr_reader :name, :resource_location, :controller_name, :function_blocks, :class_block
|
9
|
+
|
10
|
+
# Initializes RAPIDoc.
|
11
|
+
def initialize(name, resource_location, controller_name, options = {})
|
12
|
+
@name = name
|
13
|
+
@standard_methods = options[:standard_methods] || [:put, :post, :get, :delete]
|
14
|
+
@resource_location, @controller_name = resource_location, controller_name
|
15
|
+
@function_blocks = []
|
16
|
+
@method_codes = []
|
17
|
+
@header_code = ""
|
18
|
+
|
19
|
+
unless File.exist?(controller_location)
|
20
|
+
raise "Unable to find or open controller. Make sure it's set properly in config/rapidoc/config.yml File: #{controller_location}"
|
21
|
+
end
|
20
22
|
end
|
21
|
-
end
|
22
|
-
|
23
|
-
# returns the location of the controller that is to be parsed
|
24
|
-
def controller_location
|
25
|
-
"#{::Rails.root.to_s}/app/controllers/#{controller_name}"
|
26
|
-
end
|
27
|
-
|
28
|
-
def get_binding
|
29
|
-
binding
|
30
|
-
end
|
31
|
-
|
32
|
-
# parse the controller
|
33
|
-
def parse_apidoc!
|
34
|
-
current_api_block = nil
|
35
|
-
current_scope = :none
|
36
|
-
block_holder = []
|
37
|
-
lineno = 0
|
38
|
-
inclass = false
|
39
23
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
24
|
+
# returns the location of the controller that is to be parsed
|
25
|
+
def controller_location
|
26
|
+
"#{::Rails.root.to_s}/app/controllers/#{controller_name}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_binding
|
30
|
+
binding
|
31
|
+
end
|
32
|
+
|
33
|
+
# parse the controller
|
34
|
+
def parse_apidoc!
|
35
|
+
current_api_block = nil
|
36
|
+
current_scope = :none
|
37
|
+
block_holder = []
|
38
|
+
lineno = 0
|
39
|
+
inclass = false
|
40
|
+
|
41
|
+
File.open(controller_location).each do |line|
|
42
|
+
if line =~ /=begin apidoc/
|
43
|
+
current_scope = !inclass ? :class : :function
|
44
|
+
current_api_block = MethodDoc.new(current_scope)
|
45
|
+
elsif line =~ /=end/
|
46
|
+
if current_api_block.nil?
|
47
|
+
puts "#{controller_location}:#{lineno} - No starttag for =end found"
|
48
|
+
exit
|
49
|
+
elsif current_api_block.scope == :class
|
50
|
+
@class_block = current_api_block
|
51
|
+
elsif current_api_block.scope == :function
|
52
|
+
@function_blocks << current_api_block
|
53
|
+
end
|
54
|
+
current_api_block = nil
|
55
|
+
current_scope = :none
|
56
|
+
elsif line =~ /class/
|
57
|
+
inclass = true
|
58
|
+
elsif line =~ /::response-end::/
|
59
|
+
current_scope = :function
|
60
|
+
elsif line =~ /::request-end::/
|
61
|
+
current_scope = :function
|
62
|
+
elsif current_scope == :response
|
63
|
+
current_api_block.response += "#{line}"
|
64
|
+
elsif current_scope == :request
|
65
|
+
current_api_block.request += "#{line}"
|
66
|
+
elsif current_scope == :class || current_scope == :function # check if we are looking at a api block
|
67
|
+
# strip the # on the line
|
68
|
+
#line = line[1..line.length].lstrip.rstrip
|
69
|
+
# check if we are dealing with a variable
|
70
|
+
# something in the format: # varname:: sometext
|
71
|
+
if result = /(\w+)\:\:\s*(.+)/.match(line)
|
72
|
+
if result[1] == "response" || result[1] == "request"
|
73
|
+
puts "="*30
|
74
|
+
puts "found response"
|
75
|
+
puts "="*30; puts ""
|
76
|
+
puts line
|
77
|
+
current_scope = result[1].to_sym
|
78
|
+
else
|
79
|
+
current_api_block.add_variable(result[1], result[2])
|
80
|
+
end
|
77
81
|
else
|
78
|
-
|
82
|
+
# add line to block
|
83
|
+
current_api_block.content << line
|
79
84
|
end
|
80
|
-
else
|
81
|
-
# add line to block
|
82
|
-
current_api_block.content << line
|
83
85
|
end
|
86
|
+
lineno += 1
|
84
87
|
end
|
85
|
-
lineno += 1
|
86
|
-
end
|
87
88
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
89
|
+
puts "Generated #{name}.html"
|
90
|
+
end
|
91
|
+
|
92
|
+
def generate_view!(resources, temp_dir)
|
93
|
+
@resources = resources
|
94
|
+
@header_code = get_parsed_header unless @class_block.nil?
|
95
|
+
function_blocks.each do |mb|
|
96
|
+
@method_codes << get_parsed_method(mb)
|
97
|
+
end
|
98
|
+
# write it to a file
|
99
|
+
template = ""
|
100
|
+
File.open(resource_layout_file(:target)).each { |line| template << line }
|
101
|
+
parsed = ERB.new(template).result(binding)
|
102
|
+
File.open(File.join(temp_dir, name + ".html"), 'w') { |file| file.write parsed }
|
103
|
+
end
|
103
104
|
|
104
105
|
|
105
|
-
|
106
|
-
|
107
|
-
|
106
|
+
def get_parsed_header
|
107
|
+
template = ""
|
108
|
+
File.open(File.join(File.dirname(__FILE__), '..', 'templates', '_resource_header.html.erb')).each { |line| template << line }
|
108
109
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
110
|
+
puts "-"*30
|
111
|
+
puts ">>> inside: get_parsed_header"
|
112
|
+
puts ERB.new(template).result(@class_block.get_binding)
|
113
|
+
puts "-"*30; puts ""
|
113
114
|
|
114
|
-
|
115
|
-
|
115
|
+
return ERB.new(template).result(@class_block.get_binding)
|
116
|
+
end
|
116
117
|
|
117
118
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
119
|
+
def get_parsed_method(method_block)
|
120
|
+
template = ""
|
121
|
+
File.open(File.join(File.dirname(__FILE__), '..', 'templates', '_resource_method.html.erb')).each { |line| template << line }
|
122
|
+
return ERB.new(template).result(method_block.get_binding)
|
123
|
+
end
|
124
|
+
|
122
125
|
end
|
123
|
-
|
124
|
-
end
|
126
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rapi_doc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Husein Choroomi
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-11-02 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: shoulda
|
@@ -75,9 +75,9 @@ files:
|
|
75
75
|
- VERSION
|
76
76
|
- init.rb
|
77
77
|
- install.rb
|
78
|
-
- lib/config.rb
|
79
78
|
- lib/doc_util.rb
|
80
79
|
- lib/method_doc.rb
|
80
|
+
- lib/rapi_config.rb
|
81
81
|
- lib/rapi_doc.rb
|
82
82
|
- lib/rapi_doc/railtie.rb
|
83
83
|
- lib/rapi_doc/tasks/rapi_doc_tasks.rake
|
@@ -103,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
103
103
|
requirements:
|
104
104
|
- - ">="
|
105
105
|
- !ruby/object:Gem::Version
|
106
|
-
hash:
|
106
|
+
hash: 4485947497076302486
|
107
107
|
segments:
|
108
108
|
- 0
|
109
109
|
version: "0"
|
data/lib/config.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
module Config
|
2
|
-
def config_dir
|
3
|
-
File.join(::Rails.root.to_s, 'config/rapi_doc')
|
4
|
-
end
|
5
|
-
|
6
|
-
def template_dir
|
7
|
-
File.join(File.dirname(__FILE__), '/../templates')
|
8
|
-
end
|
9
|
-
|
10
|
-
def config_file(location)
|
11
|
-
File.join(find_location(location), 'config.yml')
|
12
|
-
end
|
13
|
-
|
14
|
-
def index_layout_file(location)
|
15
|
-
File.join(find_location(location), 'index.html.erb')
|
16
|
-
end
|
17
|
-
|
18
|
-
def resource_layout_file(location)
|
19
|
-
File.join(find_location(location), 'resource.html.erb')
|
20
|
-
end
|
21
|
-
|
22
|
-
def find_location(location)
|
23
|
-
location == :target ? config_dir : template_dir
|
24
|
-
end
|
25
|
-
end
|