open_api_import 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +36 -1
  3. data/lib/open_api_import.rb +45 -20
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6272d1b18625fd79a6bce972cc8f5d59a9f227887a9f49bd75428b00ce8ead31
4
- data.tar.gz: 2df97329435c449358524f6df94c0fcfb396a01907bf5956e39978b26d9db509
3
+ metadata.gz: 1f87b9b81f1429055c87f8a1008e79051eab82269f976fd4c1aa4fbe8ed4f748
4
+ data.tar.gz: 82a6fdc38c437e350dd16cac5daad820af3f1bffa7705799f33a5aa92b65cbf8
5
5
  SHA512:
6
- metadata.gz: c26f0ddd02df9ebca565c2f3988e5bd76c4323669dfa1dd5c329b1eb697cf251ff4026a25e9955820d2680f7490c6084bffe04bd311e46963fcdfb1de1e0fbdf
7
- data.tar.gz: e40ff5eff7f1755369c0bf83e0072c7e2c335b6ef4efa443ff539c57a5c231562a412be0a2bb18f29150f03c03edfa4c315aa4b5d90c9489397b4577d5a90d04
6
+ metadata.gz: 0ad5187d6bfd18bbbdacd09968a8c142c687bab121600a2f052e9fcc91729fddc2b7b11327cc1b79227b4e273921ec2a3d7b5893b613dfbd488471f22ed7b393
7
+ data.tar.gz: 5eafedf7aba260a9c4ad6aecd621a1f0fc2607993a519bd883c46814d67195f08d95c5d3019cd8618549659be26add433f2b02c534df7b6a607073d4c3e5d869
data/README.md CHANGED
@@ -196,12 +196,16 @@ The output will generate methods like this:
196
196
 
197
197
  How the module names will be created.
198
198
 
199
- Accepts three different options: :path, :path_file and :fixed. By default :path.
199
+ Accepts five different options: :path, :path_file, :tags, :tags_file and :fixed. By default :path.
200
200
 
201
201
  path: It will be used the first folder of the path to create the module name, for example the path /users/list will be in the module Users and all the requests from all modules in the same file.
202
202
 
203
203
  path_file: It will be used the first folder of the path to create the module name, for example the path /users/list will be in the module Users and each module will be in a new requests file.
204
204
 
205
+ tags: It will be used the tags key to create the module name, for example the tags: \[users, list] will create the module UsersList and all the requests from all modules in the same file. In case the tags are equal to the beginning of the operationId then it will be removed from the method name.
206
+
207
+ tags_file: It will be used the tags key to create the module name, for example the tags: \[users, list] will create the module UsersList and and each module will be in a new requests file. In case the tags are equal to the beginning of the operationId then it will be removed from the method name.
208
+
205
209
  fixed: all the requests will be under the module Requests
206
210
 
207
211
  ```ruby
@@ -280,6 +284,37 @@ This is the output of the run:
280
284
  - /petstore-simple.yaml.rb
281
285
  ```
282
286
 
287
+ In case using :tags
288
+
289
+ ```ruby
290
+ require 'open_api_import'
291
+
292
+ OpenApiImport.from "./spec/fixtures/v2.0/yaml/uber.yaml", name_for_module: :tags, create_method_name: :path
293
+
294
+ ```
295
+
296
+ It will generate just one file including every request under the module generated from the first folder of the path
297
+
298
+ ```ruby
299
+ module Swagger
300
+ module UberApi
301
+ module V1_0_0
302
+ module Products
303
+
304
+ # operationId: unknown, method: get
305
+ # summary: Product Types
306
+ # description:
307
+ # The Products endpoint returns information about the Uber products offered at a given location.
308
+ # The response includes the display name and other details about each product, and lists the products in the proper display order.
309
+ # parameters description:
310
+ # latitude: (number) (required) Latitude component of location.
311
+ # longitude: (number) (required) Longitude component of location.
312
+ def self.get_products(latitude, longitude)
313
+ ...
314
+ ...
315
+ ```
316
+
317
+
283
318
  ### include_responses
284
319
 
285
320
  If you want to add the examples of responses in the resultant file.
@@ -20,6 +20,8 @@ class OpenApiImport
20
20
  # @param name_for_module [Symbol]. (:path, :path_file, :fixed) (default: :path). How the module names will be created.
21
21
  # path: It will be used the first folder of the path to create the module name, for example the path /users/list will be in the module Users and all the requests from all modules in the same file.
22
22
  # path_file: It will be used the first folder of the path to create the module name, for example the path /users/list will be in the module Users and each module will be in a new requests file.
23
+ # tags: It will be used the tags key to create the module name, for example the tags: [users,list] will create the module UsersList and all the requests from all modules in the same file.
24
+ # tags_file: It will be used the tags key to create the module name, for example the tags: [users,list] will create the module UsersList and and each module will be in a new requests file.
23
25
  # fixed: all the requests will be under the module Requests
24
26
  ##############################################################################################
25
27
  def self.from(swagger_file, create_method_name: :operation_id, include_responses: true, mock_response: false, name_for_module: :path)
@@ -93,13 +95,15 @@ class OpenApiImport
93
95
  module_requests = ""
94
96
 
95
97
  definition.paths.each do |path|
98
+
96
99
  raw = path.raw.deep_symbolize_keys
97
100
 
98
101
  if raw.key?(:parameters)
99
102
  raw.each do |met, cont|
100
103
  if met != :parameters
101
104
  if raw[met].key?(:parameters)
102
- raw[met][:parameters] = raw[met][:parameters] & raw[:parameters]
105
+ #todo: check if in some cases the parameters on the method can be added to the ones in the path
106
+ #raw[met][:parameters] = raw[met][:parameters] & raw[:parameters]
103
107
  else
104
108
  raw[met][:parameters] = raw[:parameters]
105
109
  end
@@ -109,6 +113,7 @@ class OpenApiImport
109
113
  end
110
114
 
111
115
  raw.each do |met, cont|
116
+
112
117
  if %w[get post put delete patch].include?(met.to_s.downcase)
113
118
  params = []
114
119
  params_path = []
@@ -129,31 +134,48 @@ class OpenApiImport
129
134
  method_name = (met.to_s + "_" + path.path.to_s).snake_case
130
135
  method_name.chop! if method_name[-1] == "_"
131
136
  elsif create_method_name == :operation_id
132
- method_name = (cont[:operationId]).to_s.snake_case
137
+ if (name_for_module == :tags or name_for_module == :tags_file) and cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size>0
138
+ metnametmp = cont[:operationId].gsub(/^#{cont[:tags].join}[\s_]*/, '')
139
+ else
140
+ metnametmp = cont[:operationId]
141
+ end
142
+ method_name = metnametmp.to_s.snake_case
133
143
  else
134
- method_name = cont[:operationId]
144
+ if (name_for_module == :tags or name_for_module == :tags_file) and cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size>0
145
+ method_name = cont[:operationId].gsub(/^#{cont[:tags].join}[\s_]*/, '')
146
+ else
147
+ method_name = cont[:operationId]
148
+ end
135
149
  end
136
150
 
137
151
  path_txt = path.path.dup.to_s
138
- if name_for_module == :path or name_for_module == :path_file
152
+ if [:path, :path_file, :tags, :tags_file].include?(name_for_module)
139
153
  old_module_requests = module_requests
140
- # to remove version from path fex: /v1/Customer
141
- path_requests = path_txt.gsub(/^\/v[\d\.]*\//i, "")
142
- # to remove version from path fex: /1.0/Customer
143
- path_requests = path_requests.gsub(/^\/[\d\.]*\//i, "")
144
- if (path_requests == path_txt) && (path_txt.scan("/").size == 1)
145
- # no folder in path
146
- module_requests = "Root"
154
+ if [:path, :path_file].include?(name_for_module)
155
+ # to remove version from path fex: /v1/Customer
156
+ path_requests = path_txt.gsub(/^\/v[\d\.]*\//i, "")
157
+ # to remove version from path fex: /1.0/Customer
158
+ path_requests = path_requests.gsub(/^\/[\d\.]*\//i, "")
159
+ if (path_requests == path_txt) && (path_txt.scan("/").size == 1)
160
+ # no folder in path
161
+ module_requests = "Root"
162
+ else
163
+ res_path = path_requests.scan(/(\w+)/)
164
+ module_requests = res_path[0][0].camel_case
165
+ end
147
166
  else
148
- res_path = path_requests.scan(/(\w+)/)
149
- module_requests = res_path[0][0].camel_case
167
+ if cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size>0
168
+ module_requests = cont[:tags].join(" ").camel_case
169
+ else
170
+ module_requests = "Unknown"
171
+ end
150
172
  end
151
173
  if old_module_requests != module_requests
152
- output << "end" unless old_module_requests == "" or name_for_module == :path_file
153
- if name_for_module == :path
174
+ output << "end" unless old_module_requests == "" or name_for_module == :path_file or name_for_module == :tags_file
175
+ if name_for_module == :path or name_for_module == :tags
154
176
  # to add the end for the previous module unless is the first one
155
177
  output << "module #{module_requests}"
156
- else #:path_file
178
+ else #:path_file, :tags_file
157
179
  if old_module_requests != ""
158
180
  unless files.key?(old_module_requests)
159
181
  files[old_module_requests] = Array.new
@@ -161,7 +183,7 @@ class OpenApiImport
161
183
  files[old_module_requests].concat(output)
162
184
  output = Array.new
163
185
  end
164
- output << "module #{module_requests}" unless files.key?(module_requests) # dont add in case already existed
186
+ output << "module #{module_requests}" unless files.key?(module_requests) # don't add in case already existed
165
187
  end
166
188
  end
167
189
  end
@@ -186,7 +208,6 @@ class OpenApiImport
186
208
 
187
209
  response_example = get_response_examples(v)
188
210
 
189
-
190
211
  if !response_example.empty?
191
212
  responses << "'#{k}': { "
192
213
  responses << "message: '#{v[:description]}', "
@@ -257,7 +278,11 @@ class OpenApiImport
257
278
  if dpv.keys.include?(:example)
258
279
  valv = dpv[:example]
259
280
  else
260
- valv = ""
281
+ if dpv.type == "object"
282
+ valv = "{}"
283
+ else
284
+ valv = ""
285
+ end
261
286
  end
262
287
  if dpv.keys.include?(:description)
263
288
  description_parameters << "# #{dpk}: (#{dpv[:type]}) #{dpv[:description]}"
@@ -405,7 +430,7 @@ class OpenApiImport
405
430
 
406
431
  output_footer = []
407
432
 
408
- output_footer << "end" unless (module_requests == "") && (name_for_module == :path or name_for_module == :path_file)
433
+ output_footer << "end" unless (module_requests == "") && ([:path, :path_file, :tags, :tags_file].include?(name_for_module))
409
434
  output_footer << "end" << "end" << "end"
410
435
 
411
436
  if files.size == 0
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_api_import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-05 00:00:00.000000000 Z
11
+ date: 2019-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oas_parser