rapi_doc 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/lib/doc_util.rb CHANGED
@@ -1,6 +1,8 @@
1
- class DocUtil
2
- # print some erb code to a template
3
- def self.print_erb(str, show=false)
4
- (show ? "<%= " : "<% ") + str + " %>"
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
- # This class holds methods about a doc.
2
- class MethodDoc
3
- attr_accessor :scope, :content, :request, :response, :code
4
-
5
- def initialize(type)
6
- @scope = type
7
- @variables = []
8
- @content = ""
9
- @code = ""
10
- @request = ""
11
- @response = ""
12
- end
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
- eval("@#{name}= \"#{value}\"")
22
-
23
- if name == "json"
24
- puts "Found json:"
25
- puts @json
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
@@ -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
@@ -1,3 +1,5 @@
1
+ include RapiDoc::RapiConfig
2
+
1
3
  desc "Generate the API Documentation"
2
4
  task :rapi_doc do
3
5
  begin
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 'config'
5
+ require 'rapi_config'
6
6
  require 'rapi_doc/railtie' if defined?(Rails)
7
7
 
8
- include Config # TODO FIXME
8
+ module RapiDoc
9
+ class RAPIDoc
9
10
 
10
- class RAPIDoc
11
+ include RapiConfig
11
12
 
12
- # Initalize the ApiDocGenerator
13
- def initialize(resources)
14
- puts "Apidoc started..."
15
- @resources = resources
16
- generate_templates!
17
- move_structure!
18
- puts "Finished."
19
- end
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
- # Iterates over the resources creates views for them.
22
- # Creates an index file
23
- def generate_templates!
22
+ # Iterates over the resources creates views for them.
23
+ # Creates an index file
24
+ def generate_templates!
24
25
 
25
- @resources.each do |r|
26
- r.parse_apidoc!
27
- r.generate_view!(@resources, temp_dir)
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
- # generate the index file for the api views
34
- def generate_index!
35
- template = ""
36
- File.open(index_layout_file(:target)).each { |line| template << line }
37
- parsed = ERB.new(template).result(binding)
38
- File.open(File.join(temp_dir, "index.html"), 'w') { |file| file.write parsed }
39
- end
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
- def move_structure!
42
- target_folder = "#{::Rails.root.to_s}/public/apidoc/"
43
- Dir.mkdir(target_folder) if (!File.directory?(target_folder))
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
- Dir.new(temp_dir).each do |d|
46
- if d =~ /^[a-zA-Z]+\.(html|css)$/ # Only want to copy over the .html files, not the .erb templates
47
- FileUtils.cp File.join(temp_dir + d), target_folder + d
48
- end
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
- #Clean up the no longer needed files
51
- filepath = "#{temp_dir}/#{d}"
52
- File.delete(filepath) unless File.directory?(filepath)
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
- def copy_styles!
57
- Dir[File.join(File.dirname(__FILE__), '..', 'templates/*')].each do |f|
58
- if f =~ /[\/a-zA-Z\.]+\.css$/i
59
- FileUtils.cp f, temp_dir
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
- private
65
+ private
65
66
 
66
- def temp_dir
67
- @temp_dir ||= "#{Dir.mktmpdir("apidoc")}/"
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
- # ResourceDoc holds the information a resource contains. It parses the class header and also the
4
- # method documentation, which will be contained in MethodDoc.
5
- class ResourceDoc
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
- unless File.exist?(controller_location)
19
- raise "Unable to find or open controller. Make sure it's set properly in config/rapidoc/config.yml File: #{controller_location}"
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
- File.open(controller_location).each do |line|
41
- if line =~ /=begin apidoc/
42
- current_scope = !inclass ? :class : :function
43
- current_api_block = MethodDoc.new(current_scope)
44
- elsif line =~ /=end/
45
- if current_api_block.nil?
46
- puts "#{controller_location}:#{lineno} - No starttag for =end found"
47
- exit
48
- elsif current_api_block.scope == :class
49
- @class_block = current_api_block
50
- elsif current_api_block.scope == :function
51
- @function_blocks << current_api_block
52
- end
53
- current_api_block = nil
54
- current_scope = :none
55
- elsif line =~ /class/
56
- inclass = true
57
- elsif line =~ /::response-end::/
58
- current_scope = :function
59
- elsif line =~ /::request-end::/
60
- current_scope = :function
61
- elsif current_scope == :response
62
- current_api_block.response += "#{line}"
63
- elsif current_scope == :request
64
- current_api_block.request += "#{line}"
65
- elsif current_scope == :class || current_scope == :function # check if we are looking at a api block
66
- # strip the # on the line
67
- #line = line[1..line.length].lstrip.rstrip
68
- # check if we are dealing with a variable
69
- # something in the format: # varname:: sometext
70
- if result = /(\w+)\:\:\s*(.+)/.match(line)
71
- if result[1] == "response" || result[1] == "request"
72
- puts "="*30
73
- puts "found response"
74
- puts "="*30; puts ""
75
- puts line
76
- current_scope = result[1].to_sym
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
- current_api_block.add_variable(result[1], result[2])
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
- puts "Generated #{name}.html"
89
- end
90
-
91
- def generate_view!(resources, temp_dir)
92
- @resources = resources
93
- @header_code = get_parsed_header unless @class_block.nil?
94
- function_blocks.each do |mb|
95
- @method_codes << get_parsed_method(mb)
96
- end
97
- # write it to a file
98
- template = ""
99
- File.open(resource_layout_file(:target)).each { |line| template << line }
100
- parsed = ERB.new(template).result(binding)
101
- File.open(File.join(temp_dir, name + ".html"), 'w') { |file| file.write parsed }
102
- end
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
- def get_parsed_header
106
- template = ""
107
- File.open(File.join(File.dirname(__FILE__), '..', 'templates', '_resource_header.html.erb')).each { |line| template << line }
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
- puts "-"*30
110
- puts ">>> inside: get_parsed_header"
111
- puts ERB.new(template).result(@class_block.get_binding)
112
- puts "-"*30; puts ""
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
- return ERB.new(template).result(@class_block.get_binding)
115
- end
115
+ return ERB.new(template).result(@class_block.get_binding)
116
+ end
116
117
 
117
118
 
118
- def get_parsed_method(method_block)
119
- template = ""
120
- File.open(File.join(File.dirname(__FILE__), '..', 'templates', '_resource_method.html.erb')).each { |line| template << line }
121
- return ERB.new(template).result(method_block.get_binding)
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.0
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-10-31 00:00:00 Z
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: 2005719792368905649
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