api_doc_generation 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 133aab2c0a7b1a4e619afcfad0698c1df8069131
4
+ data.tar.gz: d40029aa3a769a6e5ebc36229166d722e024da26
5
+ SHA512:
6
+ metadata.gz: f779971b6c8e266fb07554aa75e15fa96bbfb2cfe2097e3ef8235d04baea9192b85da90ea6391456ae629c8eb841b0e5a4a15c81e613c786549dcdb0d4baad5f
7
+ data.tar.gz: 61b5e7ba17d7a3adc2eac35d26a49aa5459b9dec58faf37a576916546eb0fc9797a2f4ef17ad5c9cc15cfa4888854a76e214d09ce0e1a399afa15cec4e94ad3d
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_api_doc.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 jiangzhi.xie
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # RailsApiDoc
2
+
3
+ rails api document generation
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'api_doc_generation'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install api_doc_generation
18
+
19
+ ## Usage
20
+
21
+ `rake doc:api [CODES_PATH=app/controllers/api]`
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'api_doc_generation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "api_doc_generation"
8
+ spec.version = ApiDocGeneration::VERSION
9
+ spec.authors = ["jiangzhi.xie"]
10
+ spec.email = ["jiangzhi.xie@edoctor.cn"]
11
+ spec.description = %q{generate rails api document}
12
+ spec.summary = %q{generate rails api document}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,3 @@
1
+ module ApiDocGeneration
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,156 @@
1
+ require "api_doc_generation/version"
2
+
3
+ if defined? Rake
4
+ require "rake/api_doc_generation"
5
+ end
6
+
7
+
8
+ module ApiDocGeneration; class Generation
9
+ def initialize(codes_path = nil, out_path = nil)
10
+ codes_path ||= File.expand_path('app/controllers/api', Rails.root)
11
+ out_path ||= File.expand_path('tmp/api_doc.html', Rails.root)
12
+ controller_documents = []
13
+
14
+ each_api_controllers_file(codes_path) do |path|
15
+ controller_path = path.split("app/controllers").last
16
+ class_name = controller_path.gsub('.rb', '').classify
17
+ klass = class_name.safe_constantize
18
+
19
+ controller_documents << generate_controller(path, klass)
20
+ end
21
+
22
+ generate_html_string(controller_documents)
23
+ end
24
+
25
+ def write_to_file(out_path)
26
+ File.open(out_path, 'w+') do |file|
27
+ file.write @result
28
+ end
29
+ end
30
+
31
+
32
+ def result
33
+ @result
34
+ end
35
+
36
+
37
+ private
38
+
39
+
40
+ # API_CONTROLLERS_DIR = File.expand_path('app/controllers/api/**/*.rb', Rails.root)
41
+ def each_api_controllers_file(codes_path, &block)
42
+
43
+ Dir[codes_path + '/**/*.rb'].each do |path|
44
+ next if path =~ /base_controller.rb$/
45
+ block.call path
46
+ end
47
+ end
48
+
49
+
50
+
51
+ # {
52
+ #
53
+ # :path => path,
54
+ # :actions => [
55
+ # {
56
+ # "Params" => ['xxx', 'yyy'],
57
+ # "zzz" => ["zz1", "zz2"]
58
+ # }
59
+ # ]
60
+ # }
61
+ def generate_controller(path, klass)
62
+ filelines = File.readlines(path)
63
+ actions = klass.action_methods - klass.superclass.action_methods
64
+
65
+ actions = actions.map do |action|
66
+ generate_action(klass, action, filelines)
67
+ end
68
+
69
+ {
70
+ :path => path,
71
+ :klass => klass.to_s,
72
+ :actions => actions
73
+ }
74
+ end
75
+
76
+
77
+
78
+ def generate_action(klass, action, filelines)
79
+ document = {:name => action.to_s}
80
+ method = klass.instance_method action
81
+ filepath, line = method.source_location
82
+ tmp = []; last_line = ""
83
+
84
+ (line - 2).downto(0) do |i|
85
+ line = filelines[i]
86
+
87
+ unless line =~ /^\s*(\#.*?\n)?$/
88
+ document[:desc] = last_line
89
+ document.delete last_line
90
+ break
91
+ end
92
+
93
+ line.gsub!(/\s*\#/, '')
94
+ next if line =~ /^\s*$/
95
+
96
+ m = line.match(/(?<level>\s*)(?<desc>(?<key>.*?)(\:(?<val>.*?))?)$/)
97
+ level = m[:level].length / 2
98
+ desc = m[:desc].gsub(/^\s*|\s*$/, '')
99
+ line.gsub!(/^\s*|\:?\s*$/, '')
100
+
101
+ if level == 0
102
+ if tmp.length == 0 && m[:val] && m[:val].length > 0
103
+ document[m[:key]] = m[:val]
104
+ else
105
+ document[m[:key] + (m[:val] || '')] = tmp.reverse
106
+ end
107
+ tmp = []
108
+ else
109
+ tmp << {:level => level, :desc => desc}
110
+ end
111
+
112
+ last_line = line
113
+ end
114
+
115
+ Hash[document.merge(get_routes(klass, action)).to_a.reverse]
116
+ end
117
+
118
+
119
+
120
+ def get_routes(klass, action)
121
+ controller = klass.to_s.gsub(/Controller$/, '').underscore
122
+
123
+
124
+ Rails.application.routes.named_routes.routes.each do |name, journey|
125
+ defaults = journey.defaults
126
+
127
+ if defaults[:controller] == controller && defaults[:action] == action
128
+ return {
129
+ :path => journey.path.spec.to_s.gsub(/\(\.\:format\)$/, ''),
130
+ :method => journey.verb.to_s.match(/\^(?<req_method>\w+)\$/)[:req_method]
131
+ }
132
+ end
133
+ end
134
+
135
+ {
136
+ :path => "",
137
+ :method => ""
138
+ }
139
+ end
140
+
141
+
142
+
143
+
144
+ def generate_html_string(controller_documents)
145
+ template = ERB.new(
146
+ File.read(File.expand_path('../../templates/doc.html.erb', __FILE__))
147
+ )
148
+ doc_binding = Object.new.instance_eval do
149
+ @documents = controller_documents
150
+ binding
151
+ end
152
+
153
+ @result = template.result(doc_binding)
154
+ end
155
+
156
+ end; end
@@ -0,0 +1,8 @@
1
+
2
+ module ApiDocGeneration
3
+ path = File.expand_path("../tasks", __FILE__)
4
+
5
+ Dir.open(path).each do |ext|
6
+ load File.expand_path(ext, path) if ext =~ /^\w+\.rake$/
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+
2
+ namespace :doc do
3
+ task :api => :environment do
4
+ require 'api_doc_generation'
5
+
6
+ codes_path = ENV['CODES_PATH'] || File.expand_path('app/controllers/api', Rails.root)
7
+
8
+ File.open("./tmp/api_doc.html", "w+") do |f|
9
+ f.write(ApiDocGeneration::Generation.new(codes_path).result)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,112 @@
1
+ <html>
2
+ <head>
3
+ <meta http-equiv="content-type" content="text/html;charset=utf-8">
4
+
5
+ <title>Uasthree Api Documents</title>
6
+ <style type='text/css'>
7
+ .title {
8
+ text-align: center;
9
+ color: blank;
10
+ }
11
+
12
+ .controller, .list {
13
+ width: 900px;
14
+ border: 1px solid;
15
+ margin: 20px auto 40 auto;
16
+ }
17
+
18
+ .controller-title {
19
+ text-align: center;
20
+ }
21
+
22
+ .action {
23
+ margin-top: 40px;
24
+ padding: 20px 40px 20px 0px;
25
+ }
26
+
27
+ .action-even {
28
+ background-color: #efffef;
29
+ }
30
+
31
+ .action-odd {
32
+ background-color: #fff;
33
+ }
34
+
35
+
36
+ .desc-one {
37
+ padding-left: 20px;
38
+ }
39
+
40
+ .desc-two {
41
+ padding-left: 40;
42
+ }
43
+
44
+ .desc-val {
45
+ color: #808;
46
+ }
47
+
48
+ .desc-key {
49
+ display: inline-block;
50
+ width: 100px;
51
+ }
52
+
53
+ .desc-many {
54
+ width: auto;
55
+ }
56
+ </style>
57
+ </head>
58
+
59
+ <body>
60
+ <h1 class='title'>Uasthree Api Document</h1>
61
+
62
+ <div class='list'>
63
+ <ul>
64
+ <% @documents.each do |controller| %>
65
+ <li>
66
+ <a href='#controller-<%= controller[:klass] %>'><%= controller[:klass] %></a>
67
+
68
+ <ul class='ul-controller'>
69
+ <% controller[:actions].each do |action| %>
70
+ <li><a href='#action-<%= action[:name] %>'><%= action[:desc] %></a></li>
71
+ <% end %>
72
+ </ul>
73
+
74
+ </li>
75
+ <% end %>
76
+ </ul>
77
+ </div>
78
+
79
+ <% @documents.each do |controller| %>
80
+ <div class='controller' id='controller-<%= controller[:klass] %>'>
81
+ <h2 class='controller-title'><%= controller[:klass] %></h2>
82
+
83
+ <% controller[:actions].each_with_index do |action, i| %>
84
+ <div class='action action-<%= i % 2 == 0 ? 'even' : 'odd' %>' id='action-<%= action[:name] %>'>
85
+ <strong class='desc-one'><%= action[:desc] %></strong>
86
+
87
+ <% action.each do |key, val| %>
88
+ <% if Array === val %>
89
+ <p class='desc-one'>
90
+ <span class='desc-key desc-many'><%= key %>:</span>
91
+ </p>
92
+
93
+ <% val.each do |v| %>
94
+ <p class='desc-two desc-val' style='padding-left: <%= (v[:level] + 1) * 20 %>px;'>
95
+ <%= v[:desc] %>
96
+ </p>
97
+ <% end %>
98
+ <% elsif not key.to_s =~ /^(desc|name)$/ %>
99
+ <p class='desc-one'>
100
+ <span class='desc-key'><%= key %><%= val.length > 0 ? ':' : '' %></span>
101
+ <span class='desc-val'><%= val %></span>
102
+ </p>
103
+ <% end %>
104
+
105
+ <% end %>
106
+ </div>
107
+ <% end %>
108
+
109
+ </div>
110
+ <% end %>
111
+ </body>
112
+ </html>
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_doc_generation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - jiangzhi.xie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: generate rails api document
42
+ email:
43
+ - jiangzhi.xie@edoctor.cn
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - api_doc_generation.gemspec
54
+ - lib/api_doc_generation.rb
55
+ - lib/api_doc_generation/version.rb
56
+ - lib/rake/api_doc_generation.rb
57
+ - lib/rake/tasks/api_doc.rake
58
+ - templates/doc.html.erb
59
+ homepage: ''
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.0.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: generate rails api document
83
+ test_files: []
84
+ has_rdoc: