dox 1.1.0 → 1.2.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: f6271f722c9d2fe3887a8e0732ceff6228f4f9c9
4
- data.tar.gz: 8d44754eeefedee5c76d5f87ab529bb80a2ec0ab
3
+ metadata.gz: 1ff74ac34c68966c8eeb8ae9de609bd4eca79e4d
4
+ data.tar.gz: 4df71ab4f1315a7e1947fd94c2e4ff156a3fab25
5
5
  SHA512:
6
- metadata.gz: caa994fabaa650442c1e8e669ee99bc9b32ab16b06b7387e3b2bf128bed393cb5bffb8099b20d2db71b93cfef2abac2807ba9f037ed85e654eac33187ef2a98e
7
- data.tar.gz: b430db71dbfbadf6579c11da1d8a904c6914dd54b694cc24156e46f970228860fd0d33bee37aca42826bca1293687632be2c39b458bf7144a1b7abc331d83bc1
6
+ metadata.gz: 6140c2ebe30c6bb2cf621a480dfb1bf06c3caac0c4b337ba74089ae804c0cd5bfc80d3651c51d552cd020169bb50e5ef72ab77094341b8d13f293112edaeb14d
7
+ data.tar.gz: da8ec16081224370f2afed78fe15963a9c2f8bd6f45189faf966f8770fb02b33924bd7792a093a7dd0c148484175697e6e1cc4eed9fce0fd769a63170aea762a
data/CHANGES.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## Version 1.2.0
4
+
5
+ Released on November 27, 2019
6
+
7
+ New:
8
+ - Support Multipart payload with pretty formatting (based on `content-type` header)
9
+
10
+ Fix:
11
+ - Explicit passing of an empty hash for `params` in actions now works as expected
12
+
13
+
3
14
  ## Version 1.1.0
4
15
 
5
16
  Released on February 19, 2018
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
 
@@ -297,6 +297,16 @@ Or you can just take your generated markdown and host your documentation on [Api
297
297
 
298
298
  You might experience some strange issues when generating the documentation. Here are a few examples of what we've encountered so far.
299
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
+
300
310
  #### Wrap parameters issue
301
311
  Rails wraps JSON parameters on all requests by default, which results with documented requests looking like this:
302
312
 
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
@@ -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
 
@@ -16,7 +15,11 @@ module Dox
16
15
  end
17
16
 
18
17
  def request_body
19
- @request_body ||= request.body.read
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)
20
23
  end
21
24
 
22
25
  def request_identifier
@@ -42,6 +45,23 @@ module Dox
42
45
 
43
46
  private
44
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
+
45
65
  # Rails 5.0.2 returns "" for request.path
46
66
  def request_path
47
67
  request.path.presence || request.fullpath.split('?')[0]
@@ -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
@@ -54,7 +54,7 @@ module Dox
54
54
 
55
55
  + Body
56
56
 
57
- #{indent_lines(12, formatted_body(example.request_body, example.request_content_type))}
57
+ #{indent_lines(12, example.request_body)}
58
58
  HEREDOC
59
59
  end
60
60
 
@@ -79,42 +79,10 @@ module Dox
79
79
 
80
80
  + Body
81
81
 
82
- #{indent_lines(12, formatted_body(example.response_body, example.response_content_type))}
82
+ #{indent_lines(12, example.response_body)}
83
83
  HEREDOC
84
84
  end
85
85
 
86
- def formatted_body(body_str, content_type)
87
- case content_type
88
- when %r{application\/.*json}
89
- pretty_json(safe_json_parse(body_str))
90
- when /xml/
91
- pretty_xml(body_str)
92
- else
93
- body_str
94
- end
95
- end
96
-
97
- def safe_json_parse(json_string)
98
- json_string.length >= 2 ? JSON.parse(json_string) : nil
99
- end
100
-
101
- def pretty_json(json_string)
102
- if json_string.present?
103
- JSON.pretty_generate(json_string)
104
- else
105
- ''
106
- end
107
- end
108
-
109
- def pretty_xml(xml_string)
110
- doc = REXML::Document.new(xml_string)
111
- formatter = REXML::Formatters::Pretty.new
112
- formatter.compact = true
113
- result = ''
114
- formatter.write(doc, result)
115
- result
116
- end
117
-
118
86
  def print_headers(headers)
119
87
  headers.map do |key, value|
120
88
  "#{key}: #{value}"
@@ -1,3 +1,3 @@
1
1
  module Dox
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
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.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Melita Kokot
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2018-02-19 00:00:00.000000000 Z
12
+ date: 2019-11-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -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