dox 1.0.0 → 1.3.0

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: e3696fee9fddea2f824f129a0a25e61383b01cec
4
- data.tar.gz: c69809b3ce6618fc593dd8897938e5f34d8788c9
3
+ metadata.gz: bb133ea037943a10f5732ebcb2418ff01eb3664d
4
+ data.tar.gz: be31d122d267c510391b7813a662e2a8eb212d9f
5
5
  SHA512:
6
- metadata.gz: 28f6f85ab7471792a741658f83a1a1b522359da8bed3e4bd5f9e9a4cf71d9e28e80fe1a087fc459bd9c4c58afbde4cb797ceb0732d7a3980556c2c973bf7838e
7
- data.tar.gz: 2e71c5e388fc0c7bb727f057219e6376b4a2fe80a645738fef539ced75d1099227a27c8ac786b2c8c160c98e99fc99b22a2e0404dc414d7b3a0f2276b1e9bd7a
6
+ metadata.gz: d1deea6577862b637989fc13e783434a5120cfa6fd4c1b6d004fc25bcdc045316b45386f060eea5be0f0273655f2cd56ba0acee22e50a183eaf54422083fb729
7
+ data.tar.gz: e52dce445ee7da4fbad81137af4b8c6eb3a77e1b3700b45a11290c80d87ca8dfe1fa519c313c2ced88c293eac23b0dc410a20535715015d0293bfbec53b3bf59
data/CHANGES.md CHANGED
@@ -1,5 +1,49 @@
1
1
  # Changelog
2
2
 
3
+ ## Version 1.3.0
4
+
5
+ Released on March 26, 2021
6
+
7
+ Add:
8
+ - 'activesupport' as a runtime dependency
9
+
10
+ Drop:
11
+ - 'rails' as a runtime dependency
12
+
13
+ ## Version 1.2.0
14
+
15
+ Released on November 27, 2019
16
+
17
+ New:
18
+ - Support Multipart payload with pretty formatting (based on `content-type` header)
19
+
20
+ Fix:
21
+ - Explicit passing of an empty hash for `params` in actions now works as expected
22
+
23
+
24
+ ## Version 1.1.0
25
+
26
+ Released on February 19, 2018
27
+
28
+ New:
29
+ - Full RSpec failure dump to stderr if any test fails when running tests with Dox::Formatter
30
+ - Support any payload format with pretty formatting for JSON and XML (based on `content-type` header)
31
+
32
+ Fix:
33
+ - Ignore subdomain request header in headers output
34
+
35
+
36
+ ## Version 1.0.1
37
+
38
+ Released on June 10, 2017
39
+
40
+ New:
41
+ - Add Rake tasks for generating documentation to Readme
42
+
43
+ Fix:
44
+ - Fix printing request body for test examples
45
+
46
+
3
47
  ## Version 1.0.0
4
48
 
5
49
  Released on May 6, 2017
data/Gemfile CHANGED
@@ -5,4 +5,4 @@ gemspec
5
5
 
6
6
  rails_version = ENV['RAILS_VERSION'] || '5.0.1'
7
7
 
8
- gem 'rails', "~> #{rails_version}"
8
+ gem 'activesupport', "~> #{rails_version}"
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  # Dox
6
6
 
7
- Automate your documentation writing proces! Dox generates API documentation from Rspec controller/request specs in a Rails application. It formats the tests output in the [API Blueprint](https://apiblueprint.org) format. Choose one of the [renderes](#renderers) to convert it to HTML or host it on [Apiary.io](https://apiary.io)
7
+ Automate your documentation writing process! Dox generates API documentation from Rspec controller/request specs in a Rails application. It formats the test's output in the [API Blueprint](https://apiblueprint.org) format. Choose one of the [renderers](#renderers) to convert it to HTML or host it on [Apiary.io](https://apiary.io)
8
8
 
9
9
  Here's a [demo app](https://github.com/infinum/dox-demo) and here are some examples:
10
10
 
@@ -19,7 +19,7 @@ Add this line to your application's Gemfile:
19
19
 
20
20
  ```ruby
21
21
  group :test do
22
- gem 'dox', require: 'false'
22
+ gem 'dox', require: false
23
23
  end
24
24
  ```
25
25
 
@@ -47,7 +47,7 @@ $ gem install dox
47
47
  and configure rspec with this:
48
48
 
49
49
  ``` ruby
50
- Rspec.configure do |config|
50
+ RSpec.configure do |config|
51
51
  config.after(:each, :dox) do |example|
52
52
  example.metadata[:request] = request
53
53
  example.metadata[:response] = response
@@ -242,13 +242,46 @@ end
242
242
  ### Generate documentation
243
243
  Documentation is generated in 2 steps:
244
244
 
245
- 1. generate API Blueprint markdown with dox command:
246
- ```bundle exec dox spec/controllers/api/v1 > docs.md```
245
+ 1. generate API Blueprint markdown:
246
+ ```bundle exec rspec spec/controllers/api/v1 -f Dox::Formatter --order defined --tag dox --out docs.md```
247
247
 
248
248
  2. render HTML with some renderer, for example, with Aglio:
249
249
  ```aglio -i docs.md -o docs.html```
250
250
 
251
251
 
252
+ #### Use rake tasks
253
+ It's recommendable to write a few rake tasks to make things easier. Here's an example:
254
+
255
+ ```ruby
256
+ namespace :api do
257
+ namespace :doc do
258
+ desc 'Generate API documentation markdown'
259
+ task :md do
260
+ require 'rspec/core/rake_task'
261
+
262
+ RSpec::Core::RakeTask.new(:api_spec) do |t|
263
+ t.pattern = 'spec/controllers/api/v1/'
264
+ t.rspec_opts = "-f Dox::Formatter --order defined --tag dox --out public/api/docs/v1/apispec.md"
265
+ end
266
+
267
+ Rake::Task['api_spec'].invoke
268
+ end
269
+
270
+ task html: :md do
271
+ `aglio -i public/api/docs/v1/apispec.md -o public/api/docs/v1/index.html`
272
+ end
273
+
274
+ task open: :html do
275
+ `open public/api/docs/v1/index.html`
276
+ end
277
+
278
+ task publish: :md do
279
+ `apiary publish --path=public/api/docs/v1/apispec.md --api-name=doxdemo`
280
+ end
281
+ end
282
+ end
283
+ ```
284
+
252
285
  #### Renderers
253
286
  You can render the HTML yourself with one of the renderers:
254
287
 
@@ -264,6 +297,16 @@ Or you can just take your generated markdown and host your documentation on [Api
264
297
 
265
298
  You might experience some strange issues when generating the documentation. Here are a few examples of what we've encountered so far.
266
299
 
300
+ #### Uninitialized constant errors
301
+
302
+ There seems to be a problem with **rspec-rails** versions 3.7 and later not automatically requiring the project's rails_helper.rb when run with the `--format` flag.
303
+
304
+ To fix this issue, generate your documentation with `--require rails_helper`:
305
+
306
+ ```
307
+ bundle exec rspec -f Dox::Formatter --order defined --tag dox --out docs.md --require rails_helper
308
+ ```
309
+
267
310
  #### Wrap parameters issue
268
311
  Rails wraps JSON parameters on all requests by default, which results with documented requests looking like this:
269
312
 
data/dox.gemspec CHANGED
@@ -13,6 +13,8 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = 'https://github.com/infinum/dox'
14
14
  spec.license = 'MIT'
15
15
 
16
+ spec.required_ruby_version = '>=2.0.0'
17
+
16
18
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
19
  # delete this section to allow pushing this gem to any host.
18
20
  if spec.respond_to?(:metadata)
@@ -26,6 +28,7 @@ Gem::Specification.new do |spec|
26
28
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
29
  spec.require_paths = ['lib']
28
30
 
31
+ spec.add_runtime_dependency 'activesupport', '>= 4.0'
29
32
  spec.add_development_dependency 'bundler', '~> 1.11'
30
33
  spec.add_development_dependency 'rake', '~> 10.0'
31
34
  spec.add_development_dependency 'rspec', '~> 3.0'
@@ -33,5 +36,4 @@ Gem::Specification.new do |spec|
33
36
  spec.add_development_dependency 'simplecov'
34
37
  spec.add_development_dependency 'codeclimate-test-reporter'
35
38
  spec.add_runtime_dependency 'rspec-core'
36
- spec.add_runtime_dependency 'rails', '>= 4.0'
37
39
  end
data/lib/dox.rb CHANGED
@@ -19,6 +19,11 @@ require 'dox/errors/invalid_action_error'
19
19
  require 'dox/errors/invalid_resource_error'
20
20
  require 'dox/errors/invalid_resource_group_error'
21
21
  require 'dox/formatter'
22
+ require 'dox/formatters/base'
23
+ require 'dox/formatters/json'
24
+ require 'dox/formatters/multipart'
25
+ require 'dox/formatters/plain'
26
+ require 'dox/formatters/xml'
22
27
  require 'dox/printers/base_printer'
23
28
  require 'dox/printers/action_printer'
24
29
  require 'dox/printers/document_printer'
@@ -22,7 +22,7 @@ module Dox
22
22
  action_verb: @verb.presence,
23
23
  action_path: @path.presence,
24
24
  action_desc: @desc.presence,
25
- action_params: @params.presence
25
+ action_params: @params
26
26
  }
27
27
  end
28
28
  end
@@ -30,7 +30,8 @@ module Dox
30
30
  end
31
31
 
32
32
  def path_params
33
- @path_params ||= request.path_parameters.symbolize_keys.except(:action, :controller, :format)
33
+ @path_params ||=
34
+ request.path_parameters.symbolize_keys.except(:action, :controller, :format, :subdomain)
34
35
  end
35
36
 
36
37
  def template_path_params
@@ -5,7 +5,6 @@ module Dox
5
5
 
6
6
  def_delegator :response, :status, :response_status
7
7
  def_delegator :response, :content_type, :response_content_type
8
- def_delegator :response, :body, :response_body
9
8
  def_delegator :request, :content_type, :request_content_type
10
9
  def_delegator :request, :method, :request_method
11
10
 
@@ -15,10 +14,12 @@ module Dox
15
14
  @response = response
16
15
  end
17
16
 
18
- def request_parameters
19
- request.parameters
20
- .except(*request.path_parameters.keys.map(&:to_s))
21
- .except(*request.query_parameters.keys.map(&:to_s))
17
+ def request_body
18
+ @request_body ||= format_content(request, request_content_type)
19
+ end
20
+
21
+ def response_body
22
+ @response_body ||= format_content(response, response_content_type)
22
23
  end
23
24
 
24
25
  def request_identifier
@@ -44,9 +45,26 @@ module Dox
44
45
 
45
46
  private
46
47
 
48
+ def format_content(http_env, content_type)
49
+ formatter(content_type).new(http_env).format
50
+ end
51
+
52
+ def formatter(content_type)
53
+ case content_type
54
+ when %r{application\/.*json}
55
+ Dox::Formatters::Json
56
+ when /xml/
57
+ Dox::Formatters::Xml
58
+ when /multipart/
59
+ Dox::Formatters::Multipart
60
+ else
61
+ Dox::Formatters::Plain
62
+ end
63
+ end
64
+
47
65
  # Rails 5.0.2 returns "" for request.path
48
66
  def request_path
49
- request.path.presence || request.fullpath.split("?")[0]
67
+ request.path.presence || request.fullpath.split('?')[0]
50
68
  end
51
69
 
52
70
  attr_reader :desc, :request, :response
data/lib/dox/formatter.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  require 'rspec/core'
2
2
  require 'rspec/core/formatters/base_formatter'
3
+ require 'rspec/core/formatters/console_codes'
3
4
 
4
5
  module Dox
5
6
  class Formatter < RSpec::Core::Formatters::BaseFormatter
6
7
  extend Forwardable
7
8
 
8
- RSpec::Core::Formatters.register self, :example_passed, :stop
9
+ RSpec::Core::Formatters.register self, :example_passed, :stop, :dump_summary
9
10
 
10
11
  def initialize(output)
11
12
  super
@@ -17,8 +18,18 @@ module Dox
17
18
  move_example_to_passed if current_example.document?
18
19
  end
19
20
 
20
- def stop(_notification)
21
- printer.print(passed_examples)
21
+ def stop(notification)
22
+ if notification.failed_examples.any?
23
+ $stderr.puts(notification.fully_formatted_failed_examples)
24
+ else
25
+ printer.print(passed_examples)
26
+ end
27
+ end
28
+
29
+ def dump_summary(summary)
30
+ return if summary.failed_examples.none?
31
+ $stderr.puts(summary.fully_formatted)
32
+ exit(-1)
22
33
  end
23
34
 
24
35
  private
@@ -0,0 +1,19 @@
1
+ module Dox
2
+ module Formatters
3
+ class Base
4
+ def initialize(http_env)
5
+ @http_env = http_env
6
+ http_env_body = http_env.body
7
+ @body = http_env_body.respond_to?(:read) ? http_env_body.read : http_env_body
8
+ end
9
+
10
+ def format
11
+ raise 'no format method defined in formatter'
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :http_env, :body
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ module Dox
2
+ module Formatters
3
+ class Json < Dox::Formatters::Base
4
+ def format
5
+ # in cases where the body isn't valid JSON
6
+ # and the headers specify the Content-Type is application/json
7
+ # an error should be raised
8
+ return '' if body.nil? || body.length < 2
9
+
10
+ JSON.pretty_generate(JSON.parse(body))
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ module Dox
2
+ module Formatters
3
+ class Multipart
4
+ def initialize(http_env)
5
+ @http_env = http_env
6
+ end
7
+
8
+ def format
9
+ JSON.pretty_generate(extracted_multipart)
10
+ end
11
+
12
+ private
13
+
14
+ def extracted_multipart
15
+ Rack::Multipart.extract_multipart(http_env)
16
+ end
17
+
18
+ attr_reader :http_env
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ module Dox
2
+ module Formatters
3
+ class Plain < Dox::Formatters::Base
4
+ def format
5
+ body
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Dox
2
+ module Formatters
3
+ class Xml < Dox::Formatters::Base
4
+ def format
5
+ doc = REXML::Document.new(body)
6
+ formatter = REXML::Formatters::Pretty.new
7
+ formatter.compact = true
8
+ result = ''
9
+ formatter.write(doc, result)
10
+ result
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,5 @@
1
+ require 'rexml/document'
2
+
1
3
  module Dox
2
4
  module Printers
3
5
  class ExamplePrinter < BasePrinter
@@ -14,7 +16,7 @@ module Dox
14
16
  def print_example_request
15
17
  @output.puts example_request_title
16
18
  @output.puts example_request_headers
17
- return unless example.request_parameters.present?
19
+ return unless example.request_body.present?
18
20
 
19
21
  @output.puts example_request_body
20
22
  end
@@ -52,7 +54,7 @@ module Dox
52
54
 
53
55
  + Body
54
56
 
55
- #{indent_lines(12, pretty_json(example.request_parameters))}
57
+ #{indent_lines(12, example.request_body)}
56
58
  HEREDOC
57
59
  end
58
60
 
@@ -77,22 +79,10 @@ module Dox
77
79
 
78
80
  + Body
79
81
 
80
- #{indent_lines(12, pretty_json(safe_json_parse(example.response_body)))}
82
+ #{indent_lines(12, example.response_body)}
81
83
  HEREDOC
82
84
  end
83
85
 
84
- def safe_json_parse(json_string)
85
- json_string.length >= 2 ? JSON.parse(json_string) : nil
86
- end
87
-
88
- def pretty_json(json_string)
89
- if json_string.present?
90
- JSON.pretty_generate(json_string)
91
- else
92
- ''
93
- end
94
- end
95
-
96
86
  def print_headers(headers)
97
87
  headers.map do |key, value|
98
88
  "#{key}: #{value}"
data/lib/dox/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dox
2
- VERSION = '1.0.0'
2
+ VERSION = '1.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dox
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Melita Kokot
@@ -9,8 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-05-06 00:00:00.000000000 Z
12
+ date: 2021-03-26 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '4.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '4.0'
14
28
  - !ruby/object:Gem::Dependency
15
29
  name: bundler
16
30
  requirement: !ruby/object:Gem::Requirement
@@ -109,20 +123,6 @@ dependencies:
109
123
  - - ">="
110
124
  - !ruby/object:Gem::Version
111
125
  version: '0'
112
- - !ruby/object:Gem::Dependency
113
- name: rails
114
- requirement: !ruby/object:Gem::Requirement
115
- requirements:
116
- - - ">="
117
- - !ruby/object:Gem::Version
118
- version: '4.0'
119
- type: :runtime
120
- prerelease: false
121
- version_requirements: !ruby/object:Gem::Requirement
122
- requirements:
123
- - - ">="
124
- - !ruby/object:Gem::Version
125
- version: '4.0'
126
126
  description:
127
127
  email:
128
128
  - melita.kokot@gmail.com
@@ -166,6 +166,11 @@ files:
166
166
  - lib/dox/errors/invalid_resource_error.rb
167
167
  - lib/dox/errors/invalid_resource_group_error.rb
168
168
  - lib/dox/formatter.rb
169
+ - lib/dox/formatters/base.rb
170
+ - lib/dox/formatters/json.rb
171
+ - lib/dox/formatters/multipart.rb
172
+ - lib/dox/formatters/plain.rb
173
+ - lib/dox/formatters/xml.rb
169
174
  - lib/dox/printers/action_printer.rb
170
175
  - lib/dox/printers/base_printer.rb
171
176
  - lib/dox/printers/document_printer.rb
@@ -187,7 +192,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
187
192
  requirements:
188
193
  - - ">="
189
194
  - !ruby/object:Gem::Version
190
- version: '0'
195
+ version: 2.0.0
191
196
  required_rubygems_version: !ruby/object:Gem::Requirement
192
197
  requirements:
193
198
  - - ">="