rspec-rails-swagger 0.1.3 → 0.1.4

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: 0a916826de8e3e0b5f89296dcd78ca114356c106
4
- data.tar.gz: e709fb01df49194a494f20e6b26a47958ae5d409
3
+ metadata.gz: ca8afd22d7d94c39871ce1136dfcc65f726df7fa
4
+ data.tar.gz: 8f42d67783da7d18d2bb6bd93ed4e86d19a6d636
5
5
  SHA512:
6
- metadata.gz: daca2e677f67f13e83259c5cfae1046c033a6bb3dd82853f2d0f6455c5d49de8ea57026531692d98d9988914c8752bf8daf36869494c33bd2353c0b6e16357ce
7
- data.tar.gz: cc2bcb1103465c99da748a33b0b18a3bf17745a15ecaef02e40a92905a1918f132d53f3e16ea687a492a47da5df69c6f60e02147422cadac18b4089e3d923ef2
6
+ metadata.gz: eddec5fcd5449d1cd397cf6684e89f47769fafde43d66d455ca7f46e03ac51306a2b648fe3465c97583957818244a55ee77802d3b2285fa3aeb4c6fa9b847f9e
7
+ data.tar.gz: 483a7470fa969fe6dfcfc48f08231309b63e46eb6b1da5e075b236d95e14841349527b94fe3df38ac8af14c1fa94c35984861f0a6681c0438dd24e7403441317
data/README.md CHANGED
@@ -63,3 +63,186 @@ bundle exec rake swagger
63
63
  Now you can use Swagger UI or the renderer of your choice to display the
64
64
  formatted documentation. [swagger_engine](https://github.com/batdevis/swagger_engine)
65
65
  works pretty well and supports multiple documents.
66
+
67
+ ## RSpec DSL
68
+
69
+ The DSL follows the hierachy of the Swagger Schema:
70
+
71
+ - [Paths Object](http://swagger.io/specification/#paths-object-29)
72
+ - [Path Item Object](http://swagger.io/specification/#path-item-object-32)
73
+ - [Parameter Object](http://swagger.io/specification/#parameter-object-44)s (Optional)
74
+ - [Operation Object](http://swagger.io/specification/#operation-object-36)
75
+ - [Parameter Object](http://swagger.io/specification/#parameter-object-44)s (Optional)
76
+ - [Responses Object](http://swagger.io/specification/#responses-object-54)
77
+ - [Response Object](http://swagger.io/specification/#response-object-58)
78
+ - [Example Object](http://swagger.io/specification/#example-object-65)s (Optional)
79
+
80
+ Here's an example of a spec with comments to for the corresponding objects:
81
+
82
+ ```rb
83
+ require 'swagger_helper'
84
+
85
+ # Paths Object
86
+ RSpec.describe "Posts Controller", type: :request do
87
+ before { Post.new.save }
88
+
89
+ # Path Item Object
90
+ path '/posts' do
91
+ # Operation Object
92
+ operation "GET", summary: "fetch list" do
93
+ # Response Object
94
+ response 200, description: "successful"
95
+ end
96
+ end
97
+
98
+ # Path Object
99
+ path '/posts/{post_id}' do
100
+ # Parameter Object
101
+ parameter "post_id", {in: :path, type: :integer}
102
+ let(:post_id) { 1 }
103
+
104
+ # Operation Object
105
+ get summary: "fetch item" do
106
+ # Response Object
107
+ response 200, description: "success"
108
+ end
109
+ end
110
+ ```
111
+
112
+
113
+ ### Paths Object
114
+ These methods are available inside of an RSpec contexts with the `type: :request` tag.
115
+
116
+ #### `path(template, attributes = {}, &block)`
117
+ Defines a new Path Item.
118
+
119
+ ### Path Item Object
120
+ These methods are available inside of blocks passed to the `path` method.
121
+
122
+ #### `operation(method, attributes = {}, &block)`
123
+ Defines a new Operation Object. The `method` is case insensitive.
124
+
125
+ #### `delete(attributes = {}, &block)`
126
+ Alias for `operation(:delete, attributes, block)`.
127
+
128
+ #### `get(attributes = {}, &block)`
129
+ Alias for `operation(:get, attributes, block)`.
130
+
131
+ #### `head(attributes = {}, &block)`
132
+ Alias for `operation(:head, attributes, block)`.
133
+
134
+ #### `options(attributes = {}, &block)`
135
+ Alias for `operation(:options, attributes, block)`.
136
+
137
+ #### `patch(attributes = {}, &block)`
138
+ Alias for `operation(:patch, attributes, block)`.
139
+
140
+ #### `post(attributes = {}, &block)`
141
+ Alias for `operation(:post, attributes, block)`.
142
+
143
+ #### `put(attributes = {}, &block)`
144
+ Alias for `operation(:put, attributes, block)`.
145
+
146
+
147
+ ### Parameters
148
+ These methods are available inside of blocks passed to the `path` or `operation` method.
149
+
150
+ #### `parameter(name, attributes = {})`
151
+ Defines a new Parameter Object. You can define the parameter inline:
152
+ ```rb
153
+ parameter :callback_url, in: :query, type: :string, required: true
154
+ ```
155
+
156
+ Or, via reference:
157
+ ```rb
158
+ parameter ref: "#/parameters/site_id"
159
+ ```
160
+
161
+ Values for the parameters are set using `let`:
162
+ ```rb
163
+ post summary: "create" do
164
+ parameter "body", in: :body, schema: { foo: :bar}
165
+ let(:body) { { post: { title: 'asdf', body: "blah" } } }
166
+ # ...
167
+ end
168
+ ```
169
+
170
+
171
+ ### Operation Object
172
+ These methods are available inside of blocks passed to the `operation` method.
173
+
174
+ #### `consumes(*mime_types)`
175
+ Use this to add MIME types that are specific to the operation. They will be merged
176
+ with the Swagger Object's consumes field.
177
+ ```rb
178
+ consumes 'application/json', 'application/xml'
179
+ ```
180
+
181
+ #### `produces(*mime_types)`
182
+ Use this to add MIME types that are specific to the operation. They will be merged
183
+ with the Swagger Object's consumes field.
184
+ ```rb
185
+ produces 'application/json', 'application/xml'
186
+ ```
187
+
188
+ #### `response(status_code, attributes = {}, &block)`
189
+ Defines a new Response Object. `status_code` must be between 1 and 599. `attributes`
190
+ must include a `description`.
191
+
192
+ #### `tags(*tags)`
193
+ Adds operation specific tags.
194
+ ```rb
195
+ tags :accounts, :pets
196
+ ```
197
+
198
+ You can also provide tags through the RSpec context block and/or `path` method:
199
+ ```rb
200
+ RSpec.describe "Sample Requests", type: :request, tags: [:context_tag] do
201
+ path '/posts', tags: ['path_tag'] do
202
+ operation "GET", summary: "fetch list" do
203
+ produces 'application/json'
204
+ tags 'operation_tag'
205
+
206
+ response(200, {description: "successful"})
207
+ end
208
+ end
209
+ end
210
+ ```
211
+ These tags will be merged with those of the operation. The `GET /posts` operation
212
+ in this example will be tagged with `["context_tag", "path_tag", "operation_tag"]`.
213
+
214
+
215
+ ### Response Object
216
+ These methods are available inside of blocks passed to the `response` method.
217
+
218
+ #### `capture_example()`
219
+ This method will capture the response body from the test and create an Example
220
+ Object for the Response.
221
+
222
+ You could also set this in an RSpec context block if you'd like examples for
223
+ multiple operations or paths:
224
+ ```rb
225
+ describe 'Connections', type: :request, capture_examples: true do
226
+ # Any requests in this block will capture example responses
227
+ end
228
+ ```
229
+
230
+ #### `schema(definition)`
231
+ Sets the schema field for the Response Object. You can define it inline:
232
+ ```rb
233
+ schema(
234
+ type: :array,
235
+ items: {
236
+ type: :object,
237
+ properties: {
238
+ id: { type: :string },
239
+ name: { type: :string },
240
+ },
241
+ }
242
+ )
243
+ ```
244
+
245
+ Or, by reference:
246
+ ```rb
247
+ schema ref: '#/definitions/Account'
248
+ ```
@@ -42,13 +42,16 @@ module RSpec
42
42
  attributes.symbolize_keys!
43
43
 
44
44
  raise ArgumentError, "Path must start with a /" unless template.starts_with?('/')
45
-
46
45
  #TODO template might be a $ref
47
46
  meta = {
48
47
  swagger_object: :path_item,
49
48
  swagger_document: attributes[:swagger_document] || RSpec.configuration.swagger_docs.keys.first,
50
- swagger_path_item: {path: template}
49
+ swagger_path_item: {path: template},
51
50
  }
51
+ # Merge tags passed into the path with those from parent contexts.
52
+ if attributes[:tags]
53
+ meta[:tags] = (metadata.try(:[], :tags) || []) + attributes[:tags]
54
+ end
52
55
  describe(template, meta, &block)
53
56
  end
54
57
  end
@@ -58,6 +61,12 @@ module RSpec
58
61
 
59
62
  def operation method, attributes = {}, &block
60
63
  attributes.symbolize_keys!
64
+ # Include tags from parent contexts so you can tag all the paths
65
+ # in a controller at once.
66
+ if metadata.try(:[], :tags).present?
67
+ attributes[:tags] ||= []
68
+ attributes[:tags] += metadata[:tags]
69
+ end
61
70
 
62
71
  method = method.to_s.downcase
63
72
  validate_method! method
@@ -175,7 +184,8 @@ module RSpec
175
184
  end
176
185
 
177
186
  def tags *tags
178
- metadata[:swagger_operation][:tags] = tags
187
+ metadata[:swagger_operation][:tags] ||= []
188
+ metadata[:swagger_operation][:tags] += tags
179
189
  end
180
190
 
181
191
  def response status_code, attributes = {}, &block
@@ -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.3'
6
+ STRING = '0.1.4'
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.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - andrew morton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-21 00:00:00.000000000 Z
11
+ date: 2017-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails