rspec-rails-api 0.1.2 → 0.1.3

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: ae80a5b196a345ad25674bfae11ae466c979f216cca68912d205eb856c7096b9
4
- data.tar.gz: 5201f1632652b2a46a2b436272497d7f0dc345b5710a8f218cfc0f80ac51241c
3
+ metadata.gz: dd54b9e82ea28dd61a4741ed3bc758b6ea4fdefe58dc55e3d1034f7d21b66740
4
+ data.tar.gz: a619a33620956af983cb443a8ce2e591b34a449641d983760714f58f06096a8b
5
5
  SHA512:
6
- metadata.gz: 52e87c17a14f52fbec2b85e5c37d0b3c20b6059a0d5592bf10d11bc092be10cb4cb183446328bd1fd1f6b766cd145516a1ce83965d3e6915e530d4dfc01e86c0
7
- data.tar.gz: 8ce1bd90eb8c0c99ca561fa2410b41eb0d9492414283e926e9c60143b970a9a23b5e1abdb82e99d5f0be6b9647a0444502a5778560abb12a83f82aad205ba3a4
6
+ metadata.gz: ba2e3a9cb38a2cb9147994f4870c31fe610fbbab96bef8d9b1416f0e7d7d142478d0163a2d8fcac1fa453a419cc3b9c482b6dcc4ff7e3cf3e7b77ee9fdec1e93
7
+ data.tar.gz: 6bcd3b295c0e56c5e429ebae3cb84086cbfeb0ca10172f397f4de436061b242248c1cbb1e2841103b3ef8ad1261f3d11604634f35ea9f54a1683922069c98978
data/README.md CHANGED
@@ -37,14 +37,22 @@ RSpec.configure do |config|
37
37
  config.include Rspec::Rails::Api::DSL::Example
38
38
  end
39
39
 
40
- renderer = Rspec::Rails::Api::OpenApiRenderer.new
40
+ renderer = RSpec::Rails::Api::OpenApiRenderer.new
41
+ renderer.api_servers = [{ url: 'https://example.com' }]
42
+ renderer.api_title = 'A nice API for a nice application'
43
+ renderer.api_version = '1'
44
+ renderer.api_description = 'Access update data in this project'
45
+ # renderer.api_tos = 'http://example.com/tos.html'
46
+ # renderer.api_contact = { name: 'Admin', email: 'admin@example.com', 'http://example.com/contact' }
47
+ # renderer.api_license = { name: 'Apache', url: 'https://opensource.org/licenses/Apache-2.0' }
41
48
 
42
49
  RSpec.configuration.after(:context, type: :acceptance) do |context|
43
50
  renderer.merge_context context.class.metadata[:rrad].to_h
44
51
  end
45
52
 
46
53
  RSpec.configuration.after(:suite) do
47
- renderer.write_files
54
+ # Default path is 'tmp/rspec_rails_api_output.json/yaml'
55
+ renderer.write_files Rails.root.join('public', 'swagger_doc'), only: :json
48
56
  end
49
57
  ```
50
58
 
@@ -14,16 +14,17 @@ module RSpec
14
14
  # renderer.write_files
15
15
  # ```
16
16
  class OpenApiRenderer # rubocop:disable Metrics/ClassLength
17
+ attr_writer :api_servers, :api_title, :api_version, :api_description, :api_tos
18
+
17
19
  def initialize
18
- @metadata = {
19
- resources: {},
20
- entities: {},
21
- }
20
+ @metadata = { resources: {}, entities: {} }
22
21
  @api_infos = {}
23
22
  @api_servers = {}
24
23
  @api_paths = {}
25
24
  @api_components = {}
26
25
  @api_tags = []
26
+ @api_contact = {}
27
+ @api_license = {}
27
28
  end
28
29
 
29
30
  def merge_context(context)
@@ -31,10 +32,15 @@ module RSpec
31
32
  @metadata[:entities].deep_merge! context[:entities]
32
33
  end
33
34
 
34
- def write_files
35
+ def write_files(path = nil, only: %i[yaml json])
35
36
  content = prepare_metadata
36
- File.write ::Rails.root.join('tmp', 'out.yaml'), content.to_yaml
37
- File.write ::Rails.root.join('tmp', 'out.json'), content.to_json
37
+ path ||= ::Rails.root.join('tmp', 'rspec_api_rails')
38
+
39
+ only.each do |type|
40
+ next unless %i[yaml json].include? type
41
+
42
+ File.write "#{path}.#{type}", content.send("to_#{type}")
43
+ end
38
44
  end
39
45
 
40
46
  def prepare_metadata
@@ -50,12 +56,23 @@ module RSpec
50
56
  }.deep_stringify_keys
51
57
  end
52
58
 
59
+ def api_contact=(name: nil, email: nil, url: nil)
60
+ @api_contact[:name] = name if name
61
+ @api_contact[:email] = email if email
62
+ @api_contact[:url] = url if url
63
+ end
64
+
65
+ def api_license=(name: nil, url: nil)
66
+ @api_license[:name] = name if name
67
+ @api_license[:url] = url if url
68
+ end
69
+
53
70
  private
54
71
 
55
72
  def extract_metadatas
56
73
  extract_from_resources
57
- extract_api_infos
58
- extract_api_servers
74
+ api_infos
75
+ api_servers
59
76
  end
60
77
 
61
78
  def extract_from_resources
@@ -97,7 +114,7 @@ module RSpec
97
114
  parameters
98
115
  end
99
116
 
100
- def process_path_param(name, param) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
117
+ def process_path_param(name, param) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
101
118
  parameter = {
102
119
  name: name.to_s,
103
120
  description: param[:description],
@@ -144,7 +161,6 @@ module RSpec
144
161
 
145
162
  action
146
163
  end
147
-
148
164
  # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
149
165
 
150
166
  def process_request_body(schema: nil, ref: nil)
@@ -186,26 +202,21 @@ module RSpec
186
202
  string.downcase.gsub(/[^\w]+/, '_')
187
203
  end
188
204
 
189
- def extract_api_infos # rubocop:disable Metrics/MethodLength
205
+ def api_infos # rubocop:disable Metrics/CyclomaticComplexity
190
206
  @api_infos = {
191
- title: 'Some sample app',
192
- version: '1.0',
193
- description: 'A nice API',
194
- termsOfService: 'https://example.com/tos',
195
- contact: {
196
- name: 'API Team',
197
- email: 'api-team@example.com',
198
- url: 'http://example.com',
199
- },
200
- license: {
201
- name: 'Apache 2',
202
- url: 'https://url-to-license.com',
203
- },
207
+ title: @api_title || 'Some sample app',
208
+ version: @api_version || '1.0',
204
209
  }
210
+ @api_infos[:description] = @api_description if @api_description
211
+ @api_infos[:termsOfService] = @api_tos if @api_tos
212
+ @api_infos[:contact] = @api_contact if @api_contact[:name]
213
+ @api_infos[:license] = @api_license if @api_license[:name]
214
+
215
+ @api_infos
205
216
  end
206
217
 
207
- def extract_api_servers
208
- @api_servers = [
218
+ def api_servers
219
+ @api_servers || [
209
220
  { url: 'http://api.example.com' },
210
221
  ]
211
222
  end
@@ -3,7 +3,7 @@
3
3
  module RSpec
4
4
  module Rails
5
5
  module Api
6
- VERSION = '0.1.2'
6
+ VERSION = '0.1.3'
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-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Tancoigne
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-22 00:00:00.000000000 Z
11
+ date: 2019-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport