rspec-rails-swagger 0.1.4 → 0.1.5

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: ca8afd22d7d94c39871ce1136dfcc65f726df7fa
4
- data.tar.gz: 8f42d67783da7d18d2bb6bd93ed4e86d19a6d636
3
+ metadata.gz: 30e7c45fb94e4b7f988d7d989276764e32963b33
4
+ data.tar.gz: 4b4f4491f8de4ce2bf4a531f004fc37122c318a7
5
5
  SHA512:
6
- metadata.gz: eddec5fcd5449d1cd397cf6684e89f47769fafde43d66d455ca7f46e03ac51306a2b648fe3465c97583957818244a55ee77802d3b2285fa3aeb4c6fa9b847f9e
7
- data.tar.gz: 483a7470fa969fe6dfcfc48f08231309b63e46eb6b1da5e075b236d95e14841349527b94fe3df38ac8af14c1fa94c35984861f0a6681c0438dd24e7403441317
6
+ metadata.gz: 2c8267bd639942994777a10f7988f29fd2ffd291e52721998791f2c8567151addc095929907dae159abb57d30950304819036f674a4dc46a8d920e473bac8d78
7
+ data.tar.gz: 8f27c383b3d0b131986f3c5a2224b67cd479270433a52c3c96c56efc2ab77ebea9a67c8f14a73abdce3c768b6acd76cc38697e44fb28957b1c3e26b07d8bf9c8
data/README.md CHANGED
@@ -4,9 +4,9 @@
4
4
  [![Code Climate](https://codeclimate.com/github/drewish/rspec-rails-swagger/badges/gpa.svg)](https://codeclimate.com/github/drewish/rspec-rails-swagger)
5
5
 
6
6
  This gem helps you generate Swagger docs by using RSpec to document the paths.
7
- You execute a command to run the tests and generate the `.json` output. Running
8
- the tests ensures that your API and docs are in agreement, and generates output
9
- that can be used as examples.
7
+ You execute a command to run the tests and generate the `.yaml` or `.json` output.
8
+ Running the tests ensures that your API and docs are in agreement, and generates
9
+ output that can be saved as response examples.
10
10
 
11
11
  The design of this was heavily influenced by the awesome [swagger_rails gem](https://github.com/domaindrivendev/swagger_rails).
12
12
 
@@ -52,9 +52,9 @@ and some default requests filled in. With the structure in place you should only
52
52
  need to add `before` calls to create records and then update the `let`s to
53
53
  return the appropriate values.
54
54
 
55
- ## Generate the JSON
55
+ ## Generate the JSON or YAML
56
56
 
57
- To create the Swagger JSON files use the rake task:
57
+ To create the Swagger files use the rake task:
58
58
 
59
59
  ```
60
60
  bundle exec rake swagger
@@ -3,6 +3,7 @@ require 'rspec/rails/swagger/configuration'
3
3
  require 'rspec/rails/swagger/document'
4
4
  require 'rspec/rails/swagger/formatter'
5
5
  require 'rspec/rails/swagger/helpers'
6
+ require 'rspec/rails/swagger/response_formatters'
6
7
  require 'rspec/rails/swagger/request_builder'
7
8
  require 'rspec/rails/swagger/route_parser'
8
9
  require 'rspec/rails/swagger/version'
@@ -17,19 +17,19 @@ module RSpec
17
17
  end
18
18
 
19
19
  def example_group_started(notification)
20
- output.print *group_output(notification)
20
+ output.print(*group_output(notification))
21
21
  end
22
22
 
23
23
  def example_passed(notification)
24
- output.print RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :success)
24
+ output.print(RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :success))
25
25
  end
26
26
 
27
27
  def example_pending(notification)
28
- output.print RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :pending)
28
+ output.print(RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :pending))
29
29
  end
30
30
 
31
31
  def example_failed(notification)
32
- output.print RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :failure)
32
+ output.print(RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :failure))
33
33
  end
34
34
 
35
35
  def example_finished(notification)
@@ -44,7 +44,7 @@ module RSpec
44
44
  end
45
45
 
46
46
  def close(_notification)
47
- documents.each{|k, v| write_json(k, v)}
47
+ documents.each{|k, v| write_file(k, v)}
48
48
 
49
49
  self
50
50
  end
@@ -70,13 +70,33 @@ module RSpec
70
70
  " #{notification.example.metadata[:swagger_response][:status_code]}"
71
71
  end
72
72
 
73
- def write_json(name, document)
74
- root = ::RSpec.configuration.swagger_root
73
+ def write_file(name, document)
74
+ output =
75
+ if %w(.yaml .yml).include? File.extname(name)
76
+ YAML.dump(deep_stringify_keys(document))
77
+ else
78
+ JSON.pretty_generate(document) + "\n"
79
+ end
80
+
75
81
  # It would be good to at least warn if the name includes some '../' that
76
82
  # takes it out of root directory.
77
- target = Pathname(name).expand_path(root)
83
+ target = Pathname(name).expand_path(::RSpec.configuration.swagger_root)
78
84
  target.dirname.mkpath
79
- target.write(JSON.pretty_generate(document))
85
+ target.write(output)
86
+ end
87
+
88
+ # Lifted from ActiveSupport's Hash _deep_transform_keys_in_object
89
+ def deep_stringify_keys(object)
90
+ case object
91
+ when Hash
92
+ object.each_with_object({}) do |(key, value), result|
93
+ result[key.to_s] = deep_stringify_keys(value)
94
+ end
95
+ when Array
96
+ object.map { |e| deep_stringify_keys(e) }
97
+ else
98
+ object
99
+ end
80
100
  end
81
101
 
82
102
  def document_for(doc_name = nil)
@@ -130,12 +150,10 @@ module RSpec
130
150
  end
131
151
 
132
152
  def prepare_examples(examples)
133
- if examples['application/json'].kind_of? String
134
- begin
135
- examples['application/json'] = JSON.parse(examples['application/json'])
136
- rescue JSON::ParserError
137
- end
153
+ examples.each_pair do |format, resp|
154
+ examples[format] = ResponseFormatters[format].call(resp)
138
155
  end
156
+
139
157
  examples
140
158
  end
141
159
  end
@@ -99,7 +99,8 @@ module RSpec
99
99
  def body
100
100
  # And here all we need is the first half of the key to find the body
101
101
  # parameter and its name to fetch a value.
102
- if key = parameters(:body).keys.first
102
+ key = parameters(:body).keys.first
103
+ if key
103
104
  instance.send(key.split('&').last).to_json
104
105
  end
105
106
  end
@@ -0,0 +1,37 @@
1
+ module RSpec
2
+ module Rails
3
+ module Swagger
4
+ class ResponseFormatters
5
+
6
+ class JSON
7
+ def call(resp)
8
+ if resp.kind_of? String
9
+ ::JSON.parse(resp)
10
+ else
11
+ resp
12
+ end
13
+ rescue ::JSON::ParserError
14
+ resp
15
+ end
16
+ end
17
+
18
+ @formatters = {
19
+ :default => ->(resp) { resp },
20
+ 'application/json' => JSON.new
21
+ }
22
+
23
+ class << self
24
+
25
+ def register(format, callable)
26
+ @formatters[format] = callable
27
+ end
28
+
29
+ def [](key)
30
+ @formatters[key] || @formatters[:default]
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end
@@ -3,7 +3,7 @@ module RSpec
3
3
  # Version information for RSpec Swagger.
4
4
  module Swagger
5
5
  module Version
6
- STRING = '0.1.4'
6
+ STRING = '0.1.5'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-rails-swagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - andrew morton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-17 00:00:00.000000000 Z
11
+ date: 2018-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -59,6 +59,7 @@ files:
59
59
  - lib/rspec/rails/swagger/formatter.rb
60
60
  - lib/rspec/rails/swagger/helpers.rb
61
61
  - lib/rspec/rails/swagger/request_builder.rb
62
+ - lib/rspec/rails/swagger/response_formatters.rb
62
63
  - lib/rspec/rails/swagger/route_parser.rb
63
64
  - lib/rspec/rails/swagger/tasks/swagger.rake
64
65
  - lib/rspec/rails/swagger/version.rb
@@ -82,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
83
  version: '0'
83
84
  requirements: []
84
85
  rubyforge_project:
85
- rubygems_version: 2.5.1
86
+ rubygems_version: 2.5.2.1
86
87
  signing_key:
87
88
  specification_version: 4
88
89
  summary: Generate Swagger docs from RSpec integration tests