rspec-rails-api 0.3.1 → 0.3.2

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: 5c81756e5b64eb7e5d7363231a498c947377879806b92dac3d8f11c0d28fd7bb
4
- data.tar.gz: 728634d884084a356b2f2d29ae410b6a54c70d47a8dfe9f0ac821bc75a80e160
3
+ metadata.gz: d574f6e06c94fc87d2db77079f80a341da7455b5776c3513a7e84b5b80165090
4
+ data.tar.gz: 23ddbfe59c098baa1ffcec4dce21d5d6ba25f8fe0143d965d11a8bee25771aad
5
5
  SHA512:
6
- metadata.gz: 6b50cb2972931d3a160bd2e3ab7ee73d73d4a762c7ee8e9264fae4903b3a98954d8d34aa6dc1aeecbd1fe40dea99cc6d02bd592e710a9a136b4665dd0285cd03
7
- data.tar.gz: 482ab0880d61cf4b709034903a7509b57750bf121dcdab9acba7bc519ef4fd8e9c079f2b263d3c7732db18e8fbb6f333743ccd700ebe286fcb548ca3c1b8cbde
6
+ metadata.gz: 3430d1d5da42f6c7bf9d28f6c6e6a74ff332fb3cd31cf92d170d9c87f76527dd0d64d6ae510ae19873d7ca078c4405a05154d867f100f804e806c6e6fc342049
7
+ data.tar.gz: f251c30d40efaeef76bfd3ea0f313834dc7323ab56dc07ddf1ecc845b033160dd29afd053d3de3b0a2d7afee7092e858a1844795296e6593d9598f0d17f4df58
data/.rubocop.yml CHANGED
@@ -7,6 +7,7 @@ AllCops:
7
7
  Exclude:
8
8
  - dummy/**/*
9
9
  - vendor/bundle/**/*
10
+ NewCops: enable
10
11
 
11
12
  Layout/LineLength:
12
13
  Max: 120
data/CHANGELOG.md CHANGED
@@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file.
3
3
 
4
4
  ## Not released
5
5
 
6
+ ## 0.3.2 - 2021-03-09
7
+ - Fix YAML rendering (ruby objects were sometimes rendered in documentation)
8
+ - Render examples results as YAML/JSON objects instead of text blocks.
9
+
10
+ ## 0.3.1 - 2020-04-09
11
+ - Add support for "test only" examples, allowing to write examples without documentation.
12
+
6
13
  ## 0.3.0 - 2019-12-26
7
14
 
8
15
  ### Changed
@@ -55,7 +55,7 @@ module RSpec
55
55
  end
56
56
 
57
57
  def prepare_request_params(description, request_params = {}, payload = {}, request_headers = {})
58
- example_params = description.split ' '
58
+ example_params = description.split
59
59
 
60
60
  {
61
61
  action: example_params[0].downcase,
@@ -11,7 +11,7 @@ module RSpec
11
11
  class FieldConfig
12
12
  attr_accessor :required, :type, :attributes, :description
13
13
 
14
- def initialize(type:, required: true, description:, attributes: nil, of: nil)
14
+ def initialize(type:, description:, required: true, attributes: nil, of: nil)
15
15
  @required = required
16
16
  @description = description
17
17
  raise "Field type not allowed: '#{type}'" unless Utils.check_attribute_type(type)
@@ -40,9 +40,10 @@ module RSpec
40
40
  private
41
41
 
42
42
  def define_attributes(attributes)
43
- @attributes = if attributes.is_a? Hash
43
+ @attributes = case attributes
44
+ when Hash
44
45
  @attributes = EntityConfig.new attributes
45
- elsif attributes.is_a? Symbol
46
+ when Symbol
46
47
  attributes
47
48
  end
48
49
  end
@@ -87,7 +87,7 @@ module RSpec
87
87
  @current_method = method
88
88
  end
89
89
 
90
- # rubocop:disable Metrics/LineLength
90
+ # rubocop:disable Layout/LineLength
91
91
  def add_status_code(status_code, description)
92
92
  check_current_context :resource, :url, :method
93
93
 
@@ -97,7 +97,7 @@ module RSpec
97
97
  example: { response: nil })
98
98
  @current_code = status_code
99
99
  end
100
- # rubocop:enable Metrics/LineLength
100
+ # rubocop:enable Layout/LineLength
101
101
 
102
102
  # rubocop:disable Metrics/ParameterLists
103
103
  def add_request_example(url: nil, action: nil, status_code: nil, response: nil, path_params: nil, params: nil)
@@ -125,25 +125,14 @@ module RSpec
125
125
 
126
126
  private
127
127
 
128
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength, Style/GuardClause
129
- def check_current_context(*scope)
128
+ def check_current_context(*scope) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
130
129
  scope ||= []
131
- if scope.include?(:resource)
132
- raise 'No resource declared' unless @current_resource
133
- end
134
- if scope.include?(:method)
135
- raise 'No action declared' unless @current_method
136
- end
137
- if scope.include?(:url)
138
- raise 'No url declared' unless @current_url
139
- end
140
- if scope.include?(:code)
141
- raise 'No status code declared' unless @current_code
142
- end
130
+ raise 'No resource declared' if scope.include?(:resource) && !@current_resource
131
+ raise 'No action declared' if scope.include?(:method) && !@current_method
132
+ raise 'No url declared' if scope.include?(:url) && !@current_url
133
+ raise 'No status code declared' if scope.include?(:code) && !@current_code
143
134
  end
144
135
 
145
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength, Style/GuardClause
146
-
147
136
  def path_param_scope(url_chunks, name)
148
137
  if /:#{name}/.match?(url_chunks[0])
149
138
  :path
@@ -154,11 +143,12 @@ module RSpec
154
143
  end
155
144
  end
156
145
 
157
- def organize_params(fields) # rubocop:disable Metrics/AbcSize
146
+ def organize_params(fields) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
158
147
  out = { properties: {} }
159
148
  required = []
149
+ allowed_types = %i[array object]
160
150
  fields.each do |name, field|
161
- allowed_type = %i[array object].include?(field[:type]) || PARAM_TYPES.key?(field[:type])
151
+ allowed_type = allowed_types.include?(field[:type]) || PARAM_TYPES.key?(field[:type])
162
152
  raise "Field type not allowed: #{field[:type]}" unless allowed_type
163
153
 
164
154
  required.push name.to_s if field[:required]
@@ -39,10 +39,12 @@ module RSpec
39
39
  content = prepare_metadata
40
40
  path ||= ::Rails.root.join('tmp', 'rspec_api_rails')
41
41
 
42
+ file_types = %i[yaml json]
43
+
42
44
  only.each do |type|
43
- next unless %i[yaml json].include? type
45
+ next unless file_types.include? type
44
46
 
45
- File.write "#{path}.#{type}", content.send("to_#{type}")
47
+ File.write "#{path}.#{type}", JSON.parse(JSON.pretty_generate(content)).send("to_#{type}")
46
48
  end
47
49
  end
48
50
 
@@ -89,13 +91,14 @@ module RSpec
89
91
  end
90
92
 
91
93
  def process_resource(resource: nil, resource_config: nil) # rubocop:disable Metrics/MethodLength
94
+ http_verbs = %i[get post put patch delete]
92
95
  resource_config[:paths].each do |path_key, path|
93
96
  url = path_with_params path_key.to_s
94
97
  actions = {}
95
98
  parameters = path.key?(:path_params) ? process_path_params(path[:path_params]) : []
96
99
 
97
100
  path[:actions].each_key do |action|
98
- next unless %i[get post put patch delete].include? action
101
+ next unless http_verbs.include? action
99
102
 
100
103
  actions[action] = process_action resource: resource,
101
104
  path: path_key,
@@ -117,7 +120,7 @@ module RSpec
117
120
  parameters
118
121
  end
119
122
 
120
- def process_path_param(name, param) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
123
+ def process_path_param(name, param) # rubocop:disable Metrics/MethodLength
121
124
  parameter = {
122
125
  name: name.to_s,
123
126
  description: param[:description],
@@ -138,13 +141,12 @@ module RSpec
138
141
  responses = {}
139
142
  request_body = nil
140
143
 
141
- if %i[post put patch].include? action_config
142
- if path_config[:actions][action_config][:params].keys.count.positive?
143
- schema = path_config[:actions][action_config][:params]
144
- schema_ref = escape_operation_id("#{action_config}_#{path}")
145
- examples = process_examples(path_config[:actions][action_config][:statuses])
146
- request_body = process_request_body schema: schema, ref: schema_ref, examples: examples
147
- end
144
+ if %i[post put
145
+ patch].include?(action_config) && path_config[:actions][action_config][:params].keys.count.positive?
146
+ schema = path_config[:actions][action_config][:params]
147
+ schema_ref = escape_operation_id("#{action_config}_#{path}")
148
+ examples = process_examples(path_config[:actions][action_config][:statuses])
149
+ request_body = process_request_body schema: schema, ref: schema_ref, examples: examples
148
150
  end
149
151
 
150
152
  path_config[:actions][action_config][:statuses].each do |status_key, status|
@@ -190,7 +192,7 @@ module RSpec
190
192
 
191
193
  response[:content] = {
192
194
  'application/json': {
193
- examples: { default: { value: JSON.pretty_generate(JSON.parse(content)) } },
195
+ examples: { default: { value: JSON.parse(content) } },
194
196
  },
195
197
  }
196
198
 
@@ -220,7 +222,7 @@ module RSpec
220
222
  string.downcase.gsub(/[^\w]+/, '_')
221
223
  end
222
224
 
223
- def api_infos # rubocop:disable Metrics/CyclomaticComplexity
225
+ def api_infos
224
226
  @api_infos = {
225
227
  title: @api_title || 'Some sample app',
226
228
  version: @api_version || '1.0',
@@ -11,7 +11,7 @@ module RSpec
11
11
  path.split('.').inject(hash) do |sub_hash, key|
12
12
  return nil unless sub_hash.is_a?(Hash) && sub_hash.key?(key.to_sym)
13
13
 
14
- sub_hash[key.to_sym]
14
+ sub_hash[key.to_sym] # rubocop:disable Lint/UnmodifiedReduceAccumulator
15
15
  end
16
16
  end
17
17
 
@@ -27,7 +27,7 @@ module RSpec
27
27
  hash
28
28
  end
29
29
 
30
- def self.check_value_type(type, value) # rubocop:disable Metrics/CyclomaticComplexity
30
+ def self.check_value_type(type, value)
31
31
  return true if type == :boolean && (value.is_a?(TrueClass) || value.is_a?(FalseClass))
32
32
  return true if type == :array && value.is_a?(Array)
33
33
 
@@ -3,7 +3,7 @@
3
3
  module RSpec
4
4
  module Rails
5
5
  module Api
6
- VERSION = '0.3.1'
6
+ VERSION = '0.3.2'
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.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Tancoigne
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-20 00:00:00.000000000 Z
11
+ date: 2021-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport