rspec_api_docs 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rspec_api_docs/dir.rb +8 -0
- data/lib/rspec_api_docs/file.rb +9 -0
- data/lib/rspec_api_docs/version.rb +1 -1
- data/lib/rspec_api_docs.rb +55 -60
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ec7c0ef83d3f6f2707a9c107495e711f807f1f0
|
4
|
+
data.tar.gz: 5abb86689a0fdfcf3e5e4bbaf0c5a6058e29f55d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97c8b0f6fc41589fc312325e81f03161536e934e85d0b785e3c424b83a9d9c90fce743b76dd033757faaef29e407228f15ce5b7b0a43395d9e6a9162b907f4b8
|
7
|
+
data.tar.gz: 26c7238588d78e74ee89f4c62c53923eb398beaedd3577c9b7ce1615542723cf8185338ade81856938451b6e0b03721c7ad0ffc89aec2670d02929c791a2b02c
|
data/lib/rspec_api_docs.rb
CHANGED
@@ -1,19 +1,13 @@
|
|
1
1
|
require 'rspec/core'
|
2
2
|
require "rspec_api_docs/version"
|
3
|
+
require "rspec_api_docs/dir"
|
4
|
+
require "rspec_api_docs/file"
|
3
5
|
|
4
6
|
RSpec.configure do |config|
|
5
7
|
config.before(:suite) do
|
6
|
-
|
7
|
-
api_docs_folder_path = File.join(Rails.root, '/api_docs/')
|
8
|
-
else
|
9
|
-
api_docs_folder_path = File.join(File.expand_path('.'), '/api_docs/')
|
10
|
-
end
|
11
|
-
|
12
|
-
Dir.mkdir(api_docs_folder_path) unless Dir.exists?(api_docs_folder_path)
|
8
|
+
api_docs_folder_path = RspecApiDocs::Dir.find_or_create_api_docs_folder_in(Rails.root)
|
13
9
|
|
14
|
-
files_to_remove
|
15
|
-
|
16
|
-
files_to_remove.each do |f|
|
10
|
+
RspecApiDocs::File.files_to_remove(config.files_to_run, api_docs_folder_path).each do |f|
|
17
11
|
next unless f.match(/api\/v*.\/.*/)
|
18
12
|
|
19
13
|
file = f.match(/api\/v*.\/.*/)[0].gsub('/', '_').gsub('api_', '').gsub('_controller_test.rb', '.txt')
|
@@ -23,75 +17,76 @@ RSpec.configure do |config|
|
|
23
17
|
end
|
24
18
|
|
25
19
|
config.after(:each) do |example|
|
26
|
-
# exit unless this is under api/v*
|
27
|
-
next unless example.metadata[:file_path].match(/api\/v\d*/)
|
28
20
|
begin
|
21
|
+
# exit unless this is under api/v*
|
22
|
+
next unless example.metadata[:file_path].match(/api\/v\d*/)
|
29
23
|
next unless request && request.try(:symbolized_path_parameters)
|
30
|
-
rescue => e
|
31
|
-
# Continue anyway
|
32
|
-
next
|
33
|
-
end
|
34
24
|
|
35
|
-
|
36
|
-
|
37
|
-
|
25
|
+
if response
|
26
|
+
example_group = example.metadata[:example_group]
|
27
|
+
example_groups = []
|
38
28
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
29
|
+
while example_group
|
30
|
+
example_groups << example_group
|
31
|
+
example_group = example_group[:example_group]
|
32
|
+
end
|
43
33
|
|
44
|
-
|
34
|
+
file_name = request.symbolized_path_parameters[:controller].gsub(/\//, '_').gsub('api_', '')
|
45
35
|
|
46
|
-
|
47
|
-
|
48
|
-
|
36
|
+
id_symbol = request.symbolized_path_parameters.keys.find{|k| k.match /id/}
|
37
|
+
optional_param = request.symbolized_path_parameters[id_symbol] ? "/{:#{id_symbol}}" : ""
|
38
|
+
action = "#{request.request_method} #{request.symbolized_path_parameters[:controller]}#{optional_param}"
|
49
39
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
40
|
+
if defined? Rails
|
41
|
+
file = File.join(Rails.root, "/api_docs/#{file_name}.txt")
|
42
|
+
else
|
43
|
+
file = File.join(File.expand_path('.'), "/api_docs/#{file_name}.txt")
|
44
|
+
end
|
55
45
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
46
|
+
collection = action.match(/(POST|GET|PATCH|DELETE) (portal\/api|api)\/v\d*\/(.*)/)[3]
|
47
|
+
action_title = "#{collection.capitalize} #{request.symbolized_path_parameters[:action].capitalize} [#{request.method}]"
|
48
|
+
File.open(file, 'a') do |f|
|
49
|
+
if File.zero?(File.join(Rails.root, "/api_docs/#{file_name}.txt"))
|
50
|
+
f.write "FORMAT: 1A\n"
|
51
|
+
f.write "HOST: https://qa1.google.co.uk/api\n\n"
|
61
52
|
|
62
|
-
|
53
|
+
f.write "# #{collection.capitalize}\n\n"
|
63
54
|
|
64
|
-
|
65
|
-
|
55
|
+
f.write "description blah blah blah\n\n"
|
56
|
+
end
|
66
57
|
|
67
|
-
|
68
|
-
|
58
|
+
# skip if the action is already defined
|
59
|
+
next if File.read(File.join(Rails.root, "/api_docs/#{file_name}.txt")).include?(action_title)
|
69
60
|
|
70
|
-
|
61
|
+
f.write "## #{collection.capitalize} collection [/#{collection}]\n\n"
|
71
62
|
|
72
|
-
|
63
|
+
f.write "### #{action_title}\n\n"
|
73
64
|
|
74
|
-
|
75
|
-
|
76
|
-
|
65
|
+
# Request
|
66
|
+
request_body = request.env["action_dispatch.request.request_parameters"]
|
67
|
+
authorization_header = request.env ? request.env['Authorization'] : request.headers['Authorization']
|
77
68
|
|
78
|
-
|
79
|
-
|
69
|
+
if request_body.present? || authorization_header.present?
|
70
|
+
f.write "+ Request #{request.content_type}\n\n"
|
80
71
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
72
|
+
# Request Body
|
73
|
+
if request_body.present?# && request.content_type == 'application/json'
|
74
|
+
f.write "+ Body\n\n".indent(4)# if authorization_header
|
75
|
+
f.write "#{JSON.pretty_generate(JSON.parse(JSON.pretty_generate(request_body)))}\n\n".indent(authorization_header ? 12 : 8)
|
76
|
+
end
|
85
77
|
end
|
86
|
-
end
|
87
78
|
|
88
|
-
|
89
|
-
|
79
|
+
# Response
|
80
|
+
f.write "+ Response #{response.status} #{response.content_type}\n\n"
|
90
81
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
82
|
+
if response.body.present? && response.content_type =~ /application\/json/
|
83
|
+
f.write "#{JSON.pretty_generate(JSON.parse(response.body))}\n\n".indent(8)
|
84
|
+
end
|
85
|
+
end unless response.status.to_s =~ /4\d\d/ || response.status.to_s =~ /3\d\d/
|
86
|
+
end
|
87
|
+
rescue => e
|
88
|
+
# Just carry on as normal to that errors here don't iterfere with tests
|
89
|
+
Rails.logger.info "rspec_api_docs error: #{e}"
|
95
90
|
end
|
96
91
|
end
|
97
92
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec_api_docs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Kadwill
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,6 +70,8 @@ files:
|
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
72
|
- lib/rspec_api_docs.rb
|
73
|
+
- lib/rspec_api_docs/dir.rb
|
74
|
+
- lib/rspec_api_docs/file.rb
|
73
75
|
- lib/rspec_api_docs/version.rb
|
74
76
|
- rspec_api_docs.gemspec
|
75
77
|
homepage: https://github.com/tomkadwill/rspec_api_docs
|
@@ -92,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
94
|
version: '0'
|
93
95
|
requirements: []
|
94
96
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
97
|
+
rubygems_version: 2.4.6
|
96
98
|
signing_key:
|
97
99
|
specification_version: 4
|
98
100
|
summary: Generate API blueprint docs when running Rspec
|