rspec_api_docs 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ce20a3a723a208098abb0c4a8fa188281097892
4
- data.tar.gz: 49a7abd2d8a068bad4557d17afce34f25e199d15
3
+ metadata.gz: 8ec7c0ef83d3f6f2707a9c107495e711f807f1f0
4
+ data.tar.gz: 5abb86689a0fdfcf3e5e4bbaf0c5a6058e29f55d
5
5
  SHA512:
6
- metadata.gz: 8cf2776787063db78c086a0838b1826799f2d1c351ef4cb08ef1d4ef12ae5228a1271fb64aaf48fd2b47a98650f05291c354705038c819843e293fe458c62d1a
7
- data.tar.gz: 61955f7a1aa76a0852050e48847b768bd8226f96f41aeeb42b868ddca1f7cd6c27e5e390c6d04fafb049276e2f00c91488e9ed5c3932672ffab7ead4f7caac54
6
+ metadata.gz: 97c8b0f6fc41589fc312325e81f03161536e934e85d0b785e3c424b83a9d9c90fce743b76dd033757faaef29e407228f15ce5b7b0a43395d9e6a9162b907f4b8
7
+ data.tar.gz: 26c7238588d78e74ee89f4c62c53923eb398beaedd3577c9b7ce1615542723cf8185338ade81856938451b6e0b03721c7ad0ffc89aec2670d02929c791a2b02c
@@ -0,0 +1,8 @@
1
+ class RspecApiDocs::Dir
2
+ def self.find_or_create_api_docs_folder_in(path)
3
+ api_docs_folder_path = File.join(path, '/api_docs/')
4
+ Dir.mkdir(api_docs_folder_path) unless Dir.exists?(api_docs_folder_path)
5
+
6
+ api_docs_folder_path
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ class RspecApiDocs::File
2
+ def self.files_to_remove(files, api_docs_folder_path)
3
+ if files == []
4
+ Dir.glob(File.join(api_docs_folder_path, '*'))
5
+ else
6
+ files
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module RspecApiDocs
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -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
- if defined? Rails
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 = config.files_to_run == [] ? Dir.glob(File.join(api_docs_folder_path, '*')) : config.files_to_run
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
- if response
36
- example_group = example.metadata[:example_group]
37
- example_groups = []
25
+ if response
26
+ example_group = example.metadata[:example_group]
27
+ example_groups = []
38
28
 
39
- while example_group
40
- example_groups << example_group
41
- example_group = example_group[:example_group]
42
- end
29
+ while example_group
30
+ example_groups << example_group
31
+ example_group = example_group[:example_group]
32
+ end
43
33
 
44
- file_name = request.symbolized_path_parameters[:controller].gsub(/\//, '_').gsub('api_', '')
34
+ file_name = request.symbolized_path_parameters[:controller].gsub(/\//, '_').gsub('api_', '')
45
35
 
46
- id_symbol = request.symbolized_path_parameters.keys.find{|k| k.match /id/}
47
- optional_param = request.symbolized_path_parameters[id_symbol] ? "/{:#{id_symbol}}" : ""
48
- action = "#{request.request_method} #{request.symbolized_path_parameters[:controller]}#{optional_param}"
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
- if defined? Rails
51
- file = File.join(Rails.root, "/api_docs/#{file_name}.txt")
52
- else
53
- file = File.join(File.expand_path('.'), "/api_docs/#{file_name}.txt")
54
- end
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
- collection = action.match(/(POST|GET|PATCH|DELETE) (portal\/api|api)\/v\d*\/(.*)/)[3]
57
- File.open(file, 'a') do |f|
58
- if File.zero?(File.join(Rails.root, "/api_docs/#{file_name}.txt"))
59
- f.write "FORMAT: 1A\n"
60
- f.write "HOST: https://qa1.google.co.uk/api\n\n"
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
- f.write "# #{collection.capitalize}\n\n"
53
+ f.write "# #{collection.capitalize}\n\n"
63
54
 
64
- f.write "description blah blah blah\n\n"
65
- end
55
+ f.write "description blah blah blah\n\n"
56
+ end
66
57
 
67
- # skip if the action is already defined
68
- next if File.read(File.join(Rails.root, "/api_docs/#{file_name}.txt")).include?(action)
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
- f.write "## #{collection.capitalize} collection [/#{collection}]\n\n"
61
+ f.write "## #{collection.capitalize} collection [/#{collection}]\n\n"
71
62
 
72
- f.write "### #{collection.capitalize} #{request.symbolized_path_parameters[:action].capitalize} [#{request.method}]\n\n"
63
+ f.write "### #{action_title}\n\n"
73
64
 
74
- # Request
75
- request_body = request.env["action_dispatch.request.request_parameters"]
76
- authorization_header = request.env ? request.env['Authorization'] : request.headers['Authorization']
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
- if request_body.present? || authorization_header.present?
79
- f.write "+ Request #{request.content_type}\n\n"
69
+ if request_body.present? || authorization_header.present?
70
+ f.write "+ Request #{request.content_type}\n\n"
80
71
 
81
- # Request Body
82
- if request_body.present?# && request.content_type == 'application/json'
83
- f.write "+ Body\n\n".indent(4)# if authorization_header
84
- f.write "#{JSON.pretty_generate(JSON.parse(JSON.pretty_generate(request_body)))}\n\n".indent(authorization_header ? 12 : 8)
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
- # Response
89
- f.write "+ Response #{response.status} #{response.content_type}\n\n"
79
+ # Response
80
+ f.write "+ Response #{response.status} #{response.content_type}\n\n"
90
81
 
91
- if response.body.present? && response.content_type =~ /application\/json/
92
- f.write "#{JSON.pretty_generate(JSON.parse(response.body))}\n\n".indent(8)
93
- end
94
- end unless response.status.to_s =~ /4\d\d/ || response.status.to_s =~ /3\d\d/
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.2.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-16 00:00:00.000000000 Z
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.5.1
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