rspec-apidoc 0.1.3 → 0.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
  SHA256:
3
- metadata.gz: cf1ff0187b5a8c5f96fb7fb0defbe0db044ba5cfff1935c02d4d6117903921d8
4
- data.tar.gz: 1148d0d99ad0515247f98553e7fe1e4652f62bc0f363f07016e80e7229993753
3
+ metadata.gz: 8700f45112bb1c1a6a8ca9770020b00c5e1887e673e4353b305ecc66f89f5d70
4
+ data.tar.gz: 9ce1fb0139b48f53dce57b0f51728ddcbf58546b64c99105437dc9b637bdf5ee
5
5
  SHA512:
6
- metadata.gz: 581a39046185cf4ad93da1d5a42c8cb9f15a088971570d2331d3ff57cfd58a54a55cb658ac8cffdad8207598adfa6c281b684e3171d5b74a6fc5e2148d46d13a
7
- data.tar.gz: 8000c199ef6ddaf9832183d7e1b11984f018d0f20d0126ed8df809319cdd49a063356e6030440986ebe56f563af5c6c135679f838fa58acaeeee1e117f727198
6
+ metadata.gz: 617d0a747bb1977de943ec794fde2c4975bc5058d93c5e6cdd1ae88a8075e24c20db6b187b3d8fb59661961974555305c50ace0558662697103bb60c3feab20c
7
+ data.tar.gz: 0c82c4f051092f3517279e1c5d4ca0cd4e7eaae8471dfe81b8240bad3517d0d023965e129cf0fb169d6df5b372cd23d7fa8d8b4b88f695ccceef35a85b1bac2e
data/README.md CHANGED
@@ -47,12 +47,17 @@ RSpec.configure do |config|
47
47
 
48
48
  config.apidoc_title = 'YOUR.APP API Documentation'
49
49
  config.apidoc_description = \
50
- 'A longer intro to add before the examples: authtication, status codes...'
50
+ 'A longer intro to add before the examples: authentication, status codes...'
51
51
 
52
52
  config.apidoc_host = 'https://api.YOUR.APP'
53
53
  # Optionally specify the output file path.
54
54
  config.apidoc_output_filename = 'apidoc.html'
55
55
 
56
+ # Customize the authentication header
57
+ config.apidoc_auth_header = lambda do |headers|
58
+ '"Authorization: Bearer $AUTH_TOKEN"' if headers['Authorization']
59
+ end
60
+
56
61
  # You can add it to any example based on the metadata.
57
62
  config.after(:each, type: :request) do |example|
58
63
  RSpec::Apidoc.add(self, example)
@@ -92,12 +92,12 @@
92
92
  <% examples.each do |controller, items| %>
93
93
  <% items.sort.each_with_index do |(action, action_items), mindex| %>
94
94
  <% action_items.each_with_index do |item, index| %>
95
- <div class="example">
95
+ <div class="example" id="<%= controller %>-<%= action %>-<%= mindex + index %>">
96
96
  <% if mindex == 0 %>
97
97
  <%= item[:controller_comment] %>
98
98
  <% end %>
99
99
 
100
- <% if index == 0 %>
100
+ <% if mindex == 0 && index == 0 %>
101
101
  <div class="example-doc" id="<%= controller %>-<%= action %>">
102
102
  <%= item[:action_comment] || ( item[:method] + ' ' + controller ) %>
103
103
  </div>
@@ -108,15 +108,20 @@
108
108
  <div class="request">
109
109
  <span>Request</span>
110
110
  <pre>
111
- $ curl '<%= host %><%= item[:path] %><%= ('?' + item[:query]) if item[:query] != '' %>' \
112
- -X <%= item[:method] %> \
113
- -H 'Content-Type: <%= item[:content_type] %>' <% if item[:request_body] != '' %> \
114
- @- &lt;&lt; EOF
115
- <% if item[:request_body].is_a?(String) %>
116
- <%=h item[:request_body] %>
111
+ $ curl -X <%= item[:method] %> '<%= host %><%= item[:path] %><%= ('?' + item[:query]) if item[:query] != '' %>' \
112
+ <%- if item[:auth_header] -%>
113
+ -H <%= item[:auth_header] %> <%- if item[:content_type] %>\<% end %>
114
+ <%- end -%>
115
+ <%- if item[:content_type] -%>
116
+ -H 'Content-Type: <%= item[:content_type] %>' <%- if item[:request_body] %>\<% end %>
117
+ <%- end -%>
118
+ <%- if item[:request_body] != '' -%>
119
+ -d - &lt;&lt; EOF
120
+ <% if item[:request_body].is_a?(String) -%>
121
+ <%=h item[:request_body] -%>
117
122
  <% else %>
118
123
  <%=h JSON.pretty_generate(item[:request_body]) %>
119
- <% end %>
124
+ <%- end %>
120
125
  EOF
121
126
  <% end %>
122
127
  </pre>
@@ -124,14 +129,14 @@ EOF
124
129
  <div class="response">
125
130
  <span>Response</span>
126
131
  <pre>
127
- &lt; Content-Type: <%= item[:response_content_type] %>
132
+ &lt; Content-Type: <%= item[:response_content_type] -%>
128
133
  &lt; Status: <%= item[:status] %>
129
134
 
130
- <% if item[:response_body].is_a?(String) %>
131
- <%=h item[:response_body] %>
135
+ <% if item[:response_body].is_a?(String) -%>
136
+ <%=h item[:response_body] -%>
132
137
  <% else %>
133
- <%=h JSON.pretty_generate(item[:response_body]) %>
134
- <% end %>
138
+ <%=h JSON.pretty_generate(item[:response_body]) -%>
139
+ <%- end -%>
135
140
  </pre>
136
141
  </div>
137
142
  </div>
@@ -148,6 +153,9 @@ EOF
148
153
  </div>
149
154
  <div class="footer">
150
155
  &copy; <%= Date.current.year %> <%= title %>
156
+ <p>
157
+ <small>Latest update at <%= DateTime.current.to_s %></small>
158
+ </p>
151
159
  </div>
152
160
  </body>
153
161
  </html>
data/lib/rspec/apidoc.rb CHANGED
@@ -13,6 +13,7 @@ module RSpec
13
13
  config.add_setting(:apidoc_title)
14
14
  config.add_setting(:apidoc_description)
15
15
  config.add_setting(:apidoc_host)
16
+ config.add_setting(:apidoc_auth_header, default: ->(headers) {})
16
17
  config.add_setting(
17
18
  :apidoc_template_path,
18
19
  default: File.expand_path('apidoc/static/template.html.erb', __dir__)
@@ -65,6 +66,14 @@ module RSpec
65
66
  RSpec.configuration.apidoc_host
66
67
  end
67
68
 
69
+ # Returns the API authentication header callback result
70
+ #
71
+ # @param headers [ActionDispatch::Http::Headers] object
72
+ # @return [Object]
73
+ def self.auth_header(headers)
74
+ RSpec.configuration.apidoc_auth_header.call(headers)
75
+ end
76
+
68
77
  # Returns the collected examples, sorted
69
78
  #
70
79
  # @return [Array] a list of [Hash] items
@@ -111,6 +120,7 @@ module RSpec
111
120
  action_comment: action_comment,
112
121
 
113
122
  content_type: spec.request.content_type,
123
+ auth_header: auth_header(spec.request.headers),
114
124
  method: spec.request.method,
115
125
  path: spec.request.path,
116
126
  query: spec.request.query_parameters.to_param,
@@ -153,7 +163,7 @@ module RSpec
153
163
  def close(_notification)
154
164
  return if examples.empty?
155
165
 
156
- erb = ERB.new(File.read(template_path))
166
+ erb = ERB.new(File.read(template_path), trim_mode: '-')
157
167
  File.write(output_filename, erb.result(self.binding))
158
168
  end
159
169
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-apidoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stas Suscov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-03 00:00:00.000000000 Z
11
+ date: 2021-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: method_source