droutes 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0dd26d0f65e282cbe83bf4fa70e15d4ef357676d
4
- data.tar.gz: 046266135cb824e8d15c9b380ba2ca45c848959a
3
+ metadata.gz: 47ee0524691de209a3dee20c9ca1176a3862714c
4
+ data.tar.gz: a162c36703e06c9bd4c1c063d15cc4f9f53f0d5c
5
5
  SHA512:
6
- metadata.gz: b955def5f076f732918404ed28dd7b224aad6e541adf4ca325562acbf5f4ddc2b9b33af8965b5ee1af5307fbf87c0ef090eb321a374c09b1c6ac9622dc97b42b
7
- data.tar.gz: 88cde63dd2810ed5ab710406c973847647450cd843617d9b5e81e1370573c5882f0458b3815533b4a8750299dde73ace2f0571a0353abdb183040ffed672a610
6
+ metadata.gz: 25bab484f40f837a89140723d921e24f15831729b6004b67a7b9994ad2c7f34f1fb7a5966716618a6c42b173036221950bf768e02946290c96429229099f2e60
7
+ data.tar.gz: faf84bb566832af5926278dfe9e5b1f566627eea596d2ae67df129c24caf837de6a9d204ee95ffb4e2effdc428abf79a21571bdbcbc206ff34202b9dfb9363fd
@@ -1,6 +1,6 @@
1
1
  module Droutes
2
2
  class ClassStruct
3
- attr_reader :name, :controller, :actions, :children, :docs
3
+ attr_reader :name, :controller, :paths, :children, :docs
4
4
 
5
5
  def initialize(class_name, docs="")
6
6
  @name = class_name
@@ -8,13 +8,13 @@ module Droutes
8
8
  controller = class_name.underscore
9
9
  @controller = controller[0, controller.length - "_controller".length]
10
10
  end
11
- @actions = {}
11
+ @paths = ::Hash.new { |h, k| h[k] = {} }
12
12
  @children = []
13
13
  @docs = docs || ""
14
14
  end
15
15
 
16
- def set_action(action, data)
17
- @actions[action] = data
16
+ def add_to_path(struct)
17
+ @paths[struct.path][struct.action] = struct
18
18
  end
19
19
  end
20
20
 
@@ -59,7 +59,6 @@ module Droutes
59
59
  protected
60
60
 
61
61
  def handle(ast, klass)
62
- data = ::Hash.new {|h, k| h[k] = []}
63
62
  ast.each {|node| handle_class(node, klass) if node.is_a?(YARD::Parser::Ruby::ClassNode)}
64
63
  end
65
64
 
@@ -78,7 +77,7 @@ module Droutes
78
77
  def handle_class(ast, klass)
79
78
  class_name = ast.class_name.path.join("::")
80
79
  newKlass = ClassStruct.new(class_name, ast.docstring)
81
- data = handle_node(ast, newKlass)
80
+ handle_node(ast, newKlass)
82
81
  klass.children.append(newKlass) if klass
83
82
  end
84
83
 
@@ -86,10 +85,10 @@ module Droutes
86
85
  action = ast.method_name(true).to_s
87
86
  route = @routes[klass.controller][action]
88
87
  return unless route
89
- klass.set_action(action, Struct.new(klass,
90
- action,
91
- route,
92
- YARD::Docstring.new(ast.docstring)))
88
+ klass.add_to_path(Struct.new(klass,
89
+ action,
90
+ route,
91
+ YARD::Docstring.new(ast.docstring)))
93
92
  end
94
93
  end
95
94
  end
@@ -4,12 +4,13 @@ module Droutes::Generators
4
4
 
5
5
  desc "Parse application routes and create REST API documentation."
6
6
  def create_docs
7
- root = Droutes::Parser.new(Rails.application.routes.routes).parse
8
- root.children.each do |klass|
9
- next if klass.actions.empty?
7
+ @root = Droutes::Parser.new(Rails.application.routes.routes).parse
8
+ @root.children.each do |klass|
9
+ next if klass.paths.empty?
10
10
  content = class_doc(klass)
11
11
  create_file(".droutes/#{klass.controller}.html", page_wrapper(klass.controller.camelcase, content))
12
12
  end
13
+ create_file(".droutes/index.html", index_page)
13
14
  end
14
15
 
15
16
  protected
@@ -20,6 +21,32 @@ module Droutes::Generators
20
21
 
21
22
  private
22
23
 
24
+ def index_page
25
+ <<HTML
26
+ <!doctype html>
27
+ <html lang="en">
28
+ <head>
29
+ <meta charset="utf-8">
30
+ <meta name="viewport" content="width=device-width, initial-scale=1">
31
+ <title>Route Index</title>
32
+ <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
33
+ </head>
34
+
35
+ <body>
36
+ <p>All available routes (sorted by name):</p>
37
+ <ul>
38
+ #{@root.children.collect do |node|
39
+ node.paths.collect do |path, actions|
40
+ struct = actions.values.first
41
+ " <li><a href=\"/#{struct.controller}.html##{action_id(struct)}\">#{path}</a></li>"
42
+ end.join("\n")
43
+ end.join("")}
44
+ </ul>
45
+ </body>
46
+ </html>
47
+ HTML
48
+ end
49
+
23
50
  def page_wrapper(title, content)
24
51
  <<HTML
25
52
  <!doctype html>
@@ -42,12 +69,25 @@ HTML
42
69
  end
43
70
 
44
71
  def class_doc(klass)
72
+ structs = []
73
+ klass.paths.keys.sort.each {|path| structs += klass.paths[path].values}
45
74
  <<DOC
46
75
  <div class="container-fluid">
47
- <h1>#{klass.controller.capitalize} API</h1>
76
+ <h1>#{klass.controller} API</h1>
48
77
  <div id="#{klass.controller}" class="controller">
49
78
  <p class="summary">#{klass.docs.gsub(/\n/, " ")}</p>
50
- #{klass.actions.collect {|action, struct| action_doc(struct)}.join("\n")}
79
+ <div class="toc">
80
+ <h3>Routes</h3>
81
+ <dl>
82
+ #{structs.collect do |struct|
83
+ " <dt><a href=\"##{action_id(struct)}\">#{struct.verb} #{struct.path}</a></dt><dd>#{struct.docs.summary}</dd>"
84
+ end.join("\n")}
85
+ </dl>
86
+ </div>
87
+ <div class="routes">
88
+ <h3>Documentation</h3>
89
+ #{structs.collect {|struct| action_doc(struct)}.join("\n")}
90
+ </div>
51
91
  </div>
52
92
  </div>
53
93
  DOC
@@ -57,7 +97,7 @@ DOC
57
97
  <<DOC
58
98
  <div id="#{action_id(struct)}" class="action panel panel-default">
59
99
  <div class="panel-heading">
60
- <h2 class="route panel-title"><code>#{struct.path}</code> <small>#{struct.verb}</small></h2>
100
+ <h2 class="route panel-title"><code>#{struct.verb} #{struct.path}</code> <small>#{struct.action}</small></h2>
61
101
  </div>
62
102
  <div class="comments panel-body">
63
103
  #{comments_doc(struct.docs)}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: droutes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Margison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-02 00:00:00.000000000 Z
11
+ date: 2014-07-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Generating documentation for REST endpoints for client developers can
14
14
  be a pain, but droutes can make it easy.