rasti-web-api_doc 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cc62167910daa3a5fc15cade8b2d3fd3056ea7c0
4
+ data.tar.gz: 819145533aa50ee96802f4f0ed97119bf21ed7f7
5
+ SHA512:
6
+ metadata.gz: 766997829111040e197f0a6265972ff17ad10a994746ff4e2d66aa6d311fa1555c67c4c62ce649c28d161d3908c0f5842ef85512e32b482f71b27ba7c624bbda
7
+ data.tar.gz: 49922f7ae586de7be2cac0ed1a2fb21fb90f654f625b11b979453f294d951cf591bb09dac396e4a403d6f78f60d866688e33f584cdd2185d6f873dd167309e90
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rasti-web-api_doc.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Gabriel Naiman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Rasti::Web::ApiDoc
2
+
3
+ Generate documentation of endpoint usage based on tests.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rasti-web-api_doc'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rasti-web-api_doc
20
+
21
+ ## Usage
22
+
23
+ Add following task in your Rakefile
24
+
25
+ ```ruby
26
+ require 'rasti/web/api_doc'
27
+
28
+ Rasti::Web::ApiDoc::Task.new(:doc) do |t|
29
+ t.env = './environment.rb'
30
+ t.app = 'MyApp::Web'
31
+ t.path = 'spec' # Optional
32
+ t.pattern = 'spec/**/*_spec.rb' # Optional
33
+ t.output = 'API.md' # Optional
34
+ end
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gabynaiman/rasti-web-api_doc.
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
51
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rasti/web/api_doc"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,51 @@
1
+ module Rasti
2
+ module Web
3
+ module ApiDoc
4
+ class Request
5
+
6
+ def initialize(env)
7
+ @env = env
8
+ end
9
+
10
+ def method
11
+ env['REQUEST_METHOD']
12
+ end
13
+
14
+ def path_info
15
+ env['PATH_INFO']
16
+ end
17
+
18
+ def route
19
+ env['PATH_INFO'].dup.tap do |path_info|
20
+ route_params.each do |name, value|
21
+ path_info.sub! value, ":#{name}"
22
+ end
23
+ end
24
+ end
25
+
26
+ def route_params
27
+ env['rack.request.route_params'] || {}
28
+ end
29
+
30
+ def query_params
31
+ env['rack.request.query_hash'] || {}
32
+ end
33
+
34
+ def form_params
35
+ env['rack.request.form_hash'] || {}
36
+ end
37
+
38
+ def headers
39
+ {}.tap do |hash|
40
+ hash['Content-Type'] = env['CONTENT_TYPE'] if env['CONTENT_TYPE']
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :env
47
+
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ module Rasti
2
+ module Web
3
+ module ApiDoc
4
+ class Response
5
+
6
+ attr_reader :status, :headers
7
+
8
+ def initialize(status, headers, body_proxy)
9
+ @status = status
10
+ @headers = headers
11
+ @body_proxy = body_proxy
12
+ end
13
+
14
+ def body
15
+ @body_proxy.body
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,113 @@
1
+ module Rasti
2
+ module Web
3
+ module ApiDoc
4
+ class Task < Rake::TaskLib
5
+
6
+ attr_reader :name
7
+ attr_accessor :env, :app, :output, :pattern, :path
8
+
9
+ def initialize(name=:doc)
10
+ @name = name
11
+ @output = 'API.md'
12
+ @pattern = 'spec/**/*_spec.rb'
13
+ @path = 'spec'
14
+
15
+ yield self if block_given?
16
+
17
+ build_task
18
+ end
19
+
20
+ private
21
+
22
+ def build_task
23
+ desc 'Build API documentation'
24
+ task name do
25
+ ENV['RACK_ENV'] ||= 'test'
26
+ require env if env
27
+ application = Object.const_get app
28
+
29
+ Rasti::Web::ApiDoc.tracker = Tracker.new application.all_routes
30
+
31
+ # Hide test runner output
32
+ $stdout = StringIO.new
33
+ $stderr = StringIO.new
34
+
35
+ at_exit do
36
+ write_file
37
+ print_summary
38
+ end
39
+
40
+ # Run all tests
41
+ STDOUT.puts 'Building documentation'
42
+ $LOAD_PATH.unshift path
43
+ Dir.glob(File.expand_path(pattern)).each { |f| require f }
44
+ end
45
+ end
46
+
47
+ def write_file
48
+ File.open(File.expand_path(output), 'w') do |f|
49
+ f.puts '# **Endpoints**'
50
+
51
+ Rasti::Web::ApiDoc.tracker.tracks.each do |method, routes|
52
+ f.puts "\n## #{method}"
53
+ routes.each do |route, info|
54
+ f.puts "\n### **#{route}**"
55
+ if info
56
+ request, response = info
57
+
58
+ f.puts "**REQUEST**"
59
+ f.puts "- Path info: #{request.path_info}"
60
+ f.puts '- Headers' unless request.headers.empty?
61
+ request.headers.each do |name, value|
62
+ f.puts " - #{name}: #{value}"
63
+ end
64
+ f.puts '- Route params' unless request.route_params.empty?
65
+ request.route_params.each do |name, value|
66
+ f.puts " - #{name}: #{value}"
67
+ end
68
+ f.puts '- Query params' unless request.query_params.empty?
69
+ request.query_params.each do |name, value|
70
+ f.puts " - #{name}: #{value}"
71
+ end
72
+ f.puts '- Form params' unless request.form_params.empty?
73
+ request.form_params.each do |name, value|
74
+ f.puts " - #{name}: #{value}"
75
+ end
76
+
77
+ f.puts "\n**RESPONSE**"
78
+ f.puts "- Status: #{response.status}"
79
+ f.puts '- Headers' unless response.headers.empty?
80
+ response.headers.each do |name, value|
81
+ f.puts " - #{name}: #{value}"
82
+ end
83
+ f.puts "- Body: #{response.body}"
84
+ else
85
+ f.puts "Pending"
86
+ end
87
+ end
88
+ f.puts "\n---"
89
+ end
90
+ end
91
+ end
92
+
93
+ def print_summary
94
+ summary = Rasti::Web::ApiDoc.tracker.summary
95
+
96
+ STDOUT.puts "\nPending endpoints" unless summary[:pending_list].empty?
97
+ summary[:pending_list].each do |method, routes|
98
+ routes.each do |route|
99
+ STDOUT.puts " #{method} #{route}".red
100
+ end
101
+ end
102
+
103
+ STDOUT.puts
104
+ STDOUT.print "#{summary[:endpoints]} endpoints, "
105
+ STDOUT.print "#{summary[:documented]} documented".colorize(summary[:documented] == 0 ? :white : :green)
106
+ STDOUT.print ", "
107
+ STDOUT.puts "#{summary[:pending]} pending".colorize(summary[:pending] == 0 ? :white : :red)
108
+ end
109
+
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,43 @@
1
+ module Rasti
2
+ module Web
3
+ module ApiDoc
4
+ class Tracker
5
+
6
+ attr_reader :tracks
7
+
8
+ def initialize(routes)
9
+ @tracks = Hash.new { |h,k| h[k] = {} }
10
+ routes.each do |method, routes|
11
+ routes.each do |route|
12
+ tracks[method][route] = nil
13
+ end
14
+ end
15
+ end
16
+
17
+ def track(env, response)
18
+ req = Rasti::Web::ApiDoc::Request.new(env)
19
+ res = Rasti::Web::ApiDoc::Response.new(*response)
20
+
21
+ if !tracks[req.method][req.route]
22
+ STDOUT.puts " #{req.method} #{req.route}".green
23
+ tracks[req.method][req.route] = [req, res]
24
+ end
25
+ end
26
+
27
+ def summary
28
+ endpoints = tracks.values.flat_map(&:keys).count
29
+ documented = tracks.values.flat_map(&:values).compact.count
30
+ pending = Hash[tracks.map { |method, routes| [method, routes.select { |route, info| info.nil? }.keys ] }].select { |method, routes| !routes.empty? }
31
+
32
+ {
33
+ endpoints: endpoints,
34
+ documented: documented,
35
+ pending: pending.count,
36
+ pending_list: pending
37
+ }
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ module Rasti
2
+ module Web
3
+ module ApiDoc
4
+ VERSION = '0.1.0'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ require 'colorize'
2
+ require 'stringio'
3
+ require 'rake'
4
+ require 'rack/test'
5
+ require 'rasti/web'
6
+
7
+ require 'rasti/web/api_doc/request'
8
+ require 'rasti/web/api_doc/response'
9
+ require 'rasti/web/api_doc/task'
10
+ require 'rasti/web/api_doc/tracker'
11
+ require 'rasti/web/api_doc/version'
12
+
13
+ module Rasti
14
+ module Web
15
+ module ApiDoc
16
+
17
+ def self.tracker
18
+ @tracker
19
+ end
20
+
21
+ def self.tracker=(tracker)
22
+ @tracker = tracker
23
+ end
24
+
25
+ end
26
+ end
27
+ end
28
+
29
+ module Rack
30
+ class MockSession
31
+
32
+ alias_method :__request__, :request
33
+
34
+ def request(uri, env)
35
+ response = __request__ uri, env
36
+ Rasti::Web::ApiDoc.tracker.track env, response
37
+ response
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rasti/web/api_doc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rasti-web-api_doc'
8
+ spec.version = Rasti::Web::ApiDoc::VERSION
9
+ spec.authors = ['Gabriel Naiman']
10
+ spec.email = ['gabynaiman@gmail.com']
11
+
12
+ spec.homepage = 'https://github.com/gabynaiman/rasti-web-api_doc'
13
+ spec.description = 'Generate documentation of endpoint usage based on tests'
14
+ spec.summary = 'Generate documentation of endpoint usage based on tests'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'rasti-web', '~> 0.0.6'
23
+ spec.add_dependency 'colorize', '~> 0.7'
24
+ spec.add_dependency 'rake', '~> 10.0'
25
+ spec.add_dependency 'rack-test', '~> 0.6'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.11'
28
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rasti-web-api_doc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Naiman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rasti-web
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.6'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '0.6'
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.11'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.11'
83
+ description: Generate documentation of endpoint usage based on tests
84
+ email:
85
+ - gabynaiman@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/setup
97
+ - lib/rasti/web/api_doc.rb
98
+ - lib/rasti/web/api_doc/request.rb
99
+ - lib/rasti/web/api_doc/response.rb
100
+ - lib/rasti/web/api_doc/task.rb
101
+ - lib/rasti/web/api_doc/tracker.rb
102
+ - lib/rasti/web/api_doc/version.rb
103
+ - rasti-web-api_doc.gemspec
104
+ homepage: https://github.com/gabynaiman/rasti-web-api_doc
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.4.8
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Generate documentation of endpoint usage based on tests
128
+ test_files: []