yardbird 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c462a82143467e59909e2bb11fd44140f059fe78
4
+ data.tar.gz: e56a4d6754f264b887dd544b9d2d447b48375796
5
+ SHA512:
6
+ metadata.gz: 9dcf3f085302c37755dea9461ef4ff6f6839c2e3bbc0a0fda15ea0b706f7d47f9bced024dabefe8935a85165142e7de668d01055a995650edf19b339e4d10c1f
7
+ data.tar.gz: 71349277759eec01bf424df58bf4b19aaf6dfae1f736c1ccebc8e206a126a2848ac41ad3dada9c4892600dcc802a22c1103a1c9bb81953cc5fda953e2823e75e
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yardbird.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alexander Staubo
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.
@@ -0,0 +1,68 @@
1
+ # Yardbird
2
+
3
+ Documentation generator that uses Yardoc as the parser. It's suitable for both Sinatra and Rails applications.
4
+
5
+ ## Installation
6
+
7
+ Install as a Rubygem:
8
+
9
+ sudo gem install yardbird
10
+
11
+ Then run:
12
+
13
+ $ yardbird
14
+
15
+ ## Usage
16
+
17
+ $ yardbird generate myproject/ -o docs.md
18
+
19
+ ## Yardoc extensions
20
+
21
+ Every API endpoint comment must start with `@apidoc`.
22
+
23
+ ### `@category <label>`
24
+
25
+ Specify the category — a logic grouping — that the endpoint belongs.
26
+
27
+ ### `@path <path>`
28
+
29
+ Specify the path to the endpoint.
30
+
31
+ ### `@method <method>`
32
+
33
+ Specify the method. (Deprecated: `@http`.)
34
+
35
+ ### `@optional [<type>] <name> <description>`
36
+
37
+ Specify optional parameter. May be repeated.
38
+
39
+ ### `@required [<type>] <name> <description>`
40
+
41
+ Specify required parameter. May be required.
42
+
43
+ ### `@example <path and query>`
44
+
45
+ Give example endpoint usage.
46
+
47
+ ### `@status <code> <description>`
48
+
49
+ Describe response status. May be repeated.
50
+
51
+ ## Example comment
52
+
53
+ # @apidoc
54
+ # Returns a list of all agents.
55
+ #
56
+ # @category Agents
57
+ # @path /api/v1/agents
58
+ # @http GET
59
+ # @required [boolean] featured Return featured agents only.
60
+ # @optional [integer] offset Return agents from the given offset.
61
+ # @optional ['yyyy-mm-ddThh:mm'] after Return all agents created after the given date.
62
+ # @status 403 If basic auth username is not 'api' or password is incorrect
63
+ # @status 400 If an invalid parameter is provided.
64
+ # @example /api/v1/agents?featured=true&offset=200&limit=100&before=2014-04-16&after=2014-01-01
65
+ #
66
+ get '/agents' do
67
+ ...
68
+ end
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mercenary'
4
+
5
+ require_relative '../lib/yardbird'
6
+
7
+ Mercenary.program(:yardbird) do |p|
8
+ p.version Yardbird::VERSION
9
+ p.description 'Yardbird extracts REST API comments as a Markdown document'
10
+ p.syntax "yardbird <subcommand> [options]"
11
+
12
+ p.command :generate do |c|
13
+ c.syntax "generate PATH"
14
+ c.description "Generates from PATH"
15
+ c.option :outfile, '-o TARGET', '--out TARGET', 'Write to TARGET. Defaults to standard output.'
16
+ c.option :section_level, '--start-section LEVEL', 'Begin sections at level LEVEL. Default is 0.', Integer
17
+
18
+ c.action do |args, options|
19
+ abort "Specify a path." if args.empty?
20
+ if (file = options[:outfile])
21
+ stream = File.open(file, 'w:utf-8')
22
+ else
23
+ stream = $stdout
24
+ end
25
+ generator = Yardbird::Generator.new
26
+ generator.section_level = options[:section_level]
27
+ generator.paths |= args
28
+ generator.generate(stream)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ require_relative 'yardbird/version'
2
+ require_relative 'yardbird/endpoint'
3
+ require_relative 'yardbird/parser'
4
+ require_relative 'yardbird/generator'
5
+ require_relative 'yardbird/writer'
@@ -0,0 +1,61 @@
1
+ require 'active_support/core_ext/object/try'
2
+
3
+ module Yarddown
4
+
5
+ class Endpoint
6
+
7
+ attr_accessor :method,
8
+ :path,
9
+ :docstring,
10
+ :params,
11
+ :example,
12
+ :example_params,
13
+ :category,
14
+ :status
15
+
16
+ def initialize(yardoc)
17
+ @yardoc = yardoc
18
+ parse
19
+ end
20
+
21
+ def parse
22
+ self.method = @yardoc.tags(:http)[0].try(:text)
23
+ self.method ||= 'GET'
24
+
25
+ self.path = @yardoc.tags(:path)[0].try(:text)
26
+ self.docstring = @yardoc.docstring
27
+ if @yardoc.tags(:category).any?
28
+ self.category = @yardoc.tags(:category)[0].try(:text)
29
+ else
30
+ self.category = 'Uncategorized'
31
+ end
32
+ self.status = @yardoc.tags(:status).map do |s|
33
+ {
34
+ code: s.name.to_i,
35
+ doc: s.text
36
+ }
37
+ end
38
+ self.status = self.status.sort_by { |s| s[:code] }
39
+ self.params = [@yardoc.tags(:required), @yardoc.tags(:optional)].flatten.map do |param|
40
+ {
41
+ name: param.name,
42
+ types: param.types,
43
+ doc: param.text,
44
+ type: param.tag_name
45
+ }
46
+ end
47
+
48
+ self.example = @yardoc.tags(:example)[0].try(:name)
49
+ self.example ||= self.path
50
+ if @yardoc.tags(:example)[0].try(:text)
51
+ self.example_params = @yardoc.tags(:example)[0].text.split.map {|p| p.split(':')}
52
+ end
53
+ end
54
+
55
+ def doc_path
56
+ "#{path}.#{method}"
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,99 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative './endpoint'
4
+ require_relative './parser'
5
+ require_relative './writer'
6
+
7
+ require 'active_support/core_ext/object/blank'
8
+
9
+ module Yardbird
10
+ class Generator
11
+
12
+ attr_accessor :paths
13
+ attr_accessor :options
14
+ attr_accessor :section_level
15
+
16
+ def initialize(options = {})
17
+ @paths = []
18
+ end
19
+
20
+ def generate(stream)
21
+ endpoints = Yarddown::Parser.parse(@paths)
22
+
23
+ grouped_by_category = endpoints.group_by { |e| e.category }
24
+
25
+ writer = Writer.new(stream, section_level: @section_level)
26
+
27
+ writer.line "<section id='toc'>"
28
+ writer.section "Table of Contents" do
29
+ categories = grouped_by_category.keys.uniq.sort_by { |s| s.downcase }
30
+ categories.each do |category|
31
+ writer.bullet "[#{category}](##{category_anchor_name(category)})"
32
+ writer.indent(2) do
33
+ eps = grouped_by_category[category]
34
+ eps.each do |ep|
35
+ writer.bullet "[`#{ep.method} #{ep.path}`](##{endpoint_anchor_name(ep)})"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ writer.line "</section>"
41
+
42
+ grouped_by_category.each do |category, eps|
43
+ writer.heading category, anchor: category_anchor_name(category)
44
+ writer.section do
45
+ eps.each do |ep|
46
+ writer.heading "`#{ep.method} #{ep.path}`", anchor: endpoint_anchor_name(ep)
47
+ writer.section do
48
+ writer.line ep.docstring
49
+ if ep.params.any?
50
+ writer.heading "Parameters"
51
+ writer.section do
52
+ ep.params.each do |param|
53
+ writer.line "`#{param[:name]}` (",
54
+ [param[:types]].flatten.join(', '),
55
+ param[:type] == 'required' ? ", **required**" : '',
56
+ ")<br/>#{param[:doc]}"
57
+ writer.blank
58
+ end
59
+ end
60
+ end
61
+ if ep.status.present?
62
+ writer.heading "Status Codes"
63
+ writer.section do
64
+ ep.status.each do |s|
65
+ writer.line "**#{s[:code]}** — #{s[:doc]}"
66
+ writer.blank
67
+ end
68
+ end
69
+ end
70
+ if ep.example.present?
71
+ writer.heading "Example"
72
+ writer.code_block do
73
+ writer.line "#{ep.method} #{ep.example} HTTP/1.1"
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ writer.flush
82
+ stream
83
+ end
84
+
85
+ private
86
+
87
+ def category_anchor_name(category)
88
+ category.downcase
89
+ end
90
+
91
+ def endpoint_anchor_name(endpoint)
92
+ [endpoint.category, endpoint.method, endpoint.path.gsub(/[\/:]/, '-')].
93
+ join('-').
94
+ gsub(/-+/, '-').
95
+ downcase
96
+ end
97
+
98
+ end
99
+ end
@@ -0,0 +1,30 @@
1
+ require 'yard'
2
+
3
+ YARD::Config.load_plugin("sinatra")
4
+
5
+ YARD::Tags::Library.define_tag("API Doc", :apidoc)
6
+ YARD::Tags::Library.define_tag("Endpoint path", :path)
7
+ YARD::Tags::Library.define_tag("Category", :category)
8
+ YARD::Tags::Library.define_tag("API example", :example, :with_name)
9
+ YARD::Tags::Library.define_tag("HTTP verb", :http)
10
+ YARD::Tags::Library.define_tag("Return status", :status, :with_name)
11
+ YARD::Tags::Library.define_tag("Required param", :required, :with_types_and_name)
12
+ YARD::Tags::Library.define_tag("Optional param", :optional, :with_types_and_name)
13
+
14
+ module Yarddown
15
+
16
+ class Parser
17
+ def self.parse(paths)
18
+ paths.each do |path|
19
+ YARD.parse path
20
+ end
21
+
22
+ # Only keep stuff with @path on it
23
+ YARD::Registry.all.
24
+ reject { |r| r.tags(:apidoc).empty? }.
25
+ map { |t| Endpoint.new(t) }.
26
+ sort_by { |e| e.path || '' }
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,3 @@
1
+ module Yardbird
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,63 @@
1
+ module Yardbird
2
+ class Writer
3
+
4
+ def initialize(stream, options = {})
5
+ @stream = stream
6
+ @section_level = options[:section_level] || 0
7
+ @indent_level = 0
8
+ end
9
+
10
+ def flush
11
+ @stream.flush
12
+ end
13
+
14
+ def line(*args)
15
+ if @need_blank
16
+ @stream.write("\n")
17
+ @need_blank = false
18
+ end
19
+ @stream.write(' ' * @indent_level)
20
+ @stream.write(args.join)
21
+ @stream.write("\n")
22
+ end
23
+
24
+ def section(title = nil, &block)
25
+ heading(title) if title
26
+
27
+ @section_level += 1
28
+ yield
29
+ @section_level -= 1
30
+ end
31
+
32
+ def blank
33
+ @need_blank = true
34
+ end
35
+
36
+ def heading(s, options = {})
37
+ if (anchor = options[:anchor])
38
+ s = "<a name='#{anchor}'>#{s}</a>"
39
+ end
40
+
41
+ blank
42
+ line(('#' * (@section_level + 1)) + " #{s}")
43
+ blank
44
+ end
45
+
46
+ def bullet(s)
47
+ line("* #{s}")
48
+ end
49
+
50
+ def code_block(&block)
51
+ indent(4) do
52
+ yield
53
+ end
54
+ end
55
+
56
+ def indent(count = 1, &block)
57
+ @indent_level += count
58
+ yield
59
+ @indent_level -= count
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'yardbird/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "yardbird"
10
+ spec.version = Yardbird::VERSION
11
+ spec.authors = ["Alexander Staubo"]
12
+ spec.email = ["alex@bengler.no"]
13
+ spec.summary =
14
+ spec.description = %q{Yardbird produces REST API documentation from Yardoc comments in Ruby code, via Markdown.}
15
+ spec.homepage = ""
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency 'yard', '~> 0.8'
24
+ spec.add_runtime_dependency 'yard-sinatra', '~> 1.0'
25
+ spec.add_runtime_dependency 'mercenary', '~> 0.3'
26
+ spec.add_runtime_dependency 'activesupport', '~> 4.0'
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.6"
29
+ spec.add_development_dependency "rake"
30
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yardbird
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Staubo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard-sinatra
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mercenary
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Yardbird produces REST API documentation from Yardoc comments in Ruby
98
+ code, via Markdown.
99
+ email:
100
+ - alex@bengler.no
101
+ executables:
102
+ - yardbird
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - .gitignore
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/yardbird
112
+ - lib/yardbird.rb
113
+ - lib/yardbird/endpoint.rb
114
+ - lib/yardbird/generator.rb
115
+ - lib/yardbird/parser.rb
116
+ - lib/yardbird/version.rb
117
+ - lib/yardbird/writer.rb
118
+ - yardbird.gemspec
119
+ homepage: ''
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.0.3
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Yardbird produces REST API documentation from Yardoc comments in Ruby code,
143
+ via Markdown.
144
+ test_files: []
145
+ has_rdoc: