open_api_import 0.11.4 → 0.11.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.
@@ -1,701 +1,699 @@
1
1
  class OpenApiImport
2
- ##############################################################################################
3
- # Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses.
4
- # The http methods that will be treated are: 'get','post','put','delete', 'patch'.
5
- # @param swagger_file [String]. Path and file name. Could be absolute or relative to project root folder.
6
- # @param include_responses [Boolean]. (default: true) if you want to add the examples of responses in the resultant file.
7
- # @param mock_response [Boolean]. (default:false) Add the first response on the request as mock_response to be used.
8
- # In case using nice_http gem: if NiceHttp.use_mocks = true will use it instead of getting the real response from the WS.
9
- # @param create_method_name [Symbol]. (:path, :operation_id, :operationId) (default: operation_id). How the name of the methods will be generated.
10
- # path: it will be used the path and http method, for example for a GET on path: /users/list, the method name will be get_users_list
11
- # operation_id: it will be used the operationId field but using the snake_case version, for example for listUsers: list_users
12
- # operationId: it will be used the operationId field like it is, for example: listUsers
13
- # @param name_for_module [Symbol]. (:path, :path_file, :fixed, :tags, :tags_file) (default: :path). How the module names will be created.
14
- # @param create_constants [Boolean]. (default: false) For required arguments, it will create keyword arguments assigning by default a constant.
15
- # @param silent [Boolean]. (default: false) It will display only errors.
16
- # 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.
17
- # 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.
18
- # 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.
19
- # 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.
20
- # fixed: all the requests will be under the module Requests
21
- ##############################################################################################
22
- def self.from(swagger_file, create_method_name: :operation_id, include_responses: true, mock_response: false, name_for_module: :path, silent: false, create_constants: false)
23
- begin
24
- f = File.new("#{swagger_file}_open_api_import.log", "w")
25
- f.sync = true
26
- @logger = Logger.new f
27
- puts "Logs file: #{swagger_file}_open_api_import.log" unless silent
28
- rescue StandardError => e
29
- warn "Not possible to create the Logger file"
30
- warn e
31
- @logger = Logger.new nil
2
+ ##############################################################################################
3
+ # Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses.
4
+ # The http methods that will be treated are: 'get','post','put','delete', 'patch'.
5
+ # @param swagger_file [String]. Path and file name. Could be absolute or relative to project root folder.
6
+ # @param include_responses [Boolean]. (default: true) if you want to add the examples of responses in the resultant file.
7
+ # @param mock_response [Boolean]. (default:false) Add the first response on the request as mock_response to be used.
8
+ # In case using nice_http gem: if NiceHttp.use_mocks = true will use it instead of getting the real response from the WS.
9
+ # @param create_method_name [Symbol]. (:path, :operation_id, :operationId) (default: operation_id). How the name of the methods will be generated.
10
+ # path: it will be used the path and http method, for example for a GET on path: /users/list, the method name will be get_users_list
11
+ # operation_id: it will be used the operationId field but using the snake_case version, for example for listUsers: list_users
12
+ # operationId: it will be used the operationId field like it is, for example: listUsers
13
+ # @param name_for_module [Symbol]. (:path, :path_file, :fixed, :tags, :tags_file) (default: :path). How the module names will be created.
14
+ # @param create_constants [Boolean]. (default: false) For required arguments, it will create keyword arguments assigning by default a constant.
15
+ # @param silent [Boolean]. (default: false) It will display only errors.
16
+ # 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.
17
+ # 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.
18
+ # 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.
19
+ # 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.
20
+ # fixed: all the requests will be under the module Requests
21
+ ##############################################################################################
22
+ def self.from(swagger_file, create_method_name: :operation_id, include_responses: true, mock_response: false, name_for_module: :path, silent: false, create_constants: false)
23
+ begin
24
+ f = File.new("#{swagger_file}_open_api_import.log", "w")
25
+ f.sync = true
26
+ @logger = Logger.new f
27
+ puts "Logs file: #{swagger_file}_open_api_import.log" unless silent
28
+ rescue StandardError => e
29
+ warn "Not possible to create the Logger file"
30
+ warn e
31
+ @logger = Logger.new nil
32
+ end
33
+
34
+ begin
35
+ @logger.info "swagger_file: #{swagger_file}, include_responses: #{include_responses}, mock_response: #{mock_response}\n"
36
+ @logger.info "create_method_name: #{create_method_name}, name_for_module: #{name_for_module}\n"
37
+
38
+ file_to_convert = if swagger_file["./"].nil?
39
+ swagger_file
40
+ else
41
+ Dir.pwd.to_s + "/" + swagger_file.gsub("./", "")
42
+ end
43
+ unless File.exist?(file_to_convert)
44
+ raise "The file #{file_to_convert} doesn't exist"
32
45
  end
33
-
46
+
47
+ file_errors = file_to_convert + ".errors.log"
48
+ File.delete(file_errors) if File.exist?(file_errors)
49
+ import_errors = ""
50
+ required_constants = []
51
+
34
52
  begin
35
- @logger.info "swagger_file: #{swagger_file}, include_responses: #{include_responses}, mock_response: #{mock_response}\n"
36
- @logger.info "create_method_name: #{create_method_name}, name_for_module: #{name_for_module}\n"
37
-
38
- file_to_convert = if swagger_file["./"].nil?
39
- swagger_file
40
- else
41
- Dir.pwd.to_s + "/" + swagger_file.gsub("./", "")
53
+ definition = OasParser::Definition.resolve(swagger_file)
54
+ rescue Exception => stack
55
+ message = "There was a problem parsing the Open Api document using the oas_parser_reborn gem. The execution was aborted.\n"
56
+ message += "Visit the github for oas_parser_reborn gem for bugs and more info: https://github.com/MarioRuiz/oas_parser_reborn\n"
57
+ message += "Error: #{stack.message}"
58
+ puts message
59
+ @logger.fatal message
60
+ @logger.fatal stack.backtrace.join("\n")
61
+ exit!
62
+ end
63
+
64
+ raw = definition.raw.deep_symbolize_keys
65
+
66
+ if raw.key?(:openapi) && (raw[:openapi].to_f > 0)
67
+ raw[:swagger] = raw[:openapi]
68
+ end
69
+ if raw[:swagger].to_f < 2.0
70
+ raise "Unsupported Swagger version. Only versions >= 2.0 are valid."
71
+ end
72
+
73
+ base_host = ""
74
+ base_path = ""
75
+
76
+ base_host = raw[:host] if raw.key?(:host)
77
+ base_path = raw[:basePath] if raw.key?(:basePath)
78
+ module_name = raw[:info][:title].camel_case
79
+ module_version = "V#{raw[:info][:version].to_s.snake_case}"
80
+
81
+ output = []
82
+ output_header = []
83
+ output_header << "#" * 50
84
+ output_header << "# #{raw[:info][:title]}"
85
+ output_header << "# version: #{raw[:info][:version]}"
86
+ output_header << "# description: "
87
+ raw[:info][:description].to_s.split("\n").each do |d|
88
+ output_header << "# #{d}" unless d == ""
89
+ end
90
+ output_header << "#" * 50
91
+
92
+ output_header << "module Swagger"
93
+ output_header << "module #{module_name}"
94
+ output_header << "module #{module_version}"
95
+ output_header << "module Requests" if name_for_module == :fixed
96
+
97
+ files = {}
98
+
99
+ module_requests = ""
100
+
101
+ definition.paths.each do |path|
102
+ raw = path.raw.deep_symbolize_keys
103
+
104
+ if raw.key?(:parameters)
105
+ raw.each do |met, cont|
106
+ if met != :parameters
107
+ if raw[met].key?(:parameters)
108
+ #in case parameters for all methods in path is present
109
+ raw[met][:parameters] = raw[met][:parameters] + raw[:parameters]
110
+ else
111
+ raw[met][:parameters] = raw[:parameters]
112
+ end
113
+ end
42
114
  end
43
- unless File.exist?(file_to_convert)
44
- raise "The file #{file_to_convert} doesn't exist"
45
- end
46
-
47
- file_errors = file_to_convert + ".errors.log"
48
- File.delete(file_errors) if File.exist?(file_errors)
49
- import_errors = ""
50
- required_constants = []
51
-
52
- begin
53
- definition = OasParser::Definition.resolve(swagger_file)
54
- rescue Exception => stack
55
- message = "There was a problem parsing the Open Api document using the oas_parser_reborn gem. The execution was aborted.\n"
56
- message += "Visit the github for oas_parser_reborn gem for bugs and more info: https://github.com/MarioRuiz/oas_parser_reborn\n"
57
- message += "Error: #{stack.message}"
58
- puts message
59
- @logger.fatal message
60
- @logger.fatal stack.backtrace.join("\n")
61
- exit!
62
- end
63
-
64
- raw = definition.raw.deep_symbolize_keys
65
-
66
- if raw.key?(:openapi) && (raw[:openapi].to_f > 0)
67
- raw[:swagger] = raw[:openapi]
68
- end
69
- if raw[:swagger].to_f < 2.0
70
- raise "Unsupported Swagger version. Only versions >= 2.0 are valid."
115
+ raw.delete(:parameters)
71
116
  end
72
-
73
- base_host = ""
74
- base_path = ""
75
-
76
- base_host = raw[:host] if raw.key?(:host)
77
- base_path = raw[:basePath] if raw.key?(:basePath)
78
- module_name = raw[:info][:title].camel_case
79
- module_version = "V#{raw[:info][:version].to_s.snake_case}"
80
-
81
- output = []
82
- output_header = []
83
- output_header << "#" * 50
84
- output_header << "# #{raw[:info][:title]}"
85
- output_header << "# version: #{raw[:info][:version]}"
86
- output_header << "# description: "
87
- raw[:info][:description].to_s.split("\n").each do |d|
88
- output_header << "# #{d}" unless d == ""
89
- end
90
- output_header << "#" * 50
91
-
92
- output_header << "module Swagger"
93
- output_header << "module #{module_name}"
94
- output_header << "module #{module_version}"
95
- output_header << "module Requests" if name_for_module == :fixed
96
-
97
- files = {}
98
-
99
- module_requests = ""
100
-
101
- definition.paths.each do |path|
102
- raw = path.raw.deep_symbolize_keys
103
-
104
- if raw.key?(:parameters)
105
- raw.each do |met, cont|
106
- if met != :parameters
107
- if raw[met].key?(:parameters)
108
- #in case parameters for all methods in path is present
109
- raw[met][:parameters] = raw[met][:parameters] + raw[:parameters]
110
- else
111
- raw[met][:parameters] = raw[:parameters]
117
+
118
+ raw.each do |met, cont|
119
+ if %w[get post put delete patch].include?(met.to_s.downcase)
120
+ params = []
121
+ params_path = []
122
+ params_query = []
123
+ params_required = []
124
+ params_data = []
125
+ description_parameters = []
126
+ data_form = []
127
+ data_required = []
128
+ #todo: add nested one.true.three to data_read_only
129
+ data_read_only = []
130
+ data_default = []
131
+ data_examples = []
132
+ data_pattern = []
133
+ responses = []
134
+
135
+ # for the case operationId is missing
136
+ cont[:operationId] = "undefined" unless cont.key?(:operationId)
137
+
138
+ if create_method_name == :path
139
+ method_name = (met.to_s + "_" + path.path.to_s).snake_case
140
+ method_name.chop! if method_name[-1] == "_"
141
+ elsif create_method_name == :operation_id
142
+ 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
143
+ metnametmp = cont[:operationId].gsub(/^#{cont[:tags].join}[\s_]*/, "")
144
+ cont[:tags].join.split(" ").each do |tag|
145
+ metnametmp.gsub!(/^#{tag}[\s_]*/i, "")
112
146
  end
147
+ metnametmp = met if metnametmp == ""
148
+ else
149
+ metnametmp = cont[:operationId]
150
+ end
151
+ method_name = metnametmp.to_s.snake_case
152
+ else
153
+ 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
154
+ method_name = cont[:operationId].gsub(/^#{cont[:tags].join}[\s_]*/, "")
155
+ cont[:tags].join.split(" ").each do |tag|
156
+ method_name.gsub!(/^#{tag}[\s_]*/i, "")
157
+ end
158
+ method_name = met if method_name == ""
159
+ else
160
+ method_name = cont[:operationId]
113
161
  end
114
162
  end
115
- raw.delete(:parameters)
116
- end
117
-
118
- raw.each do |met, cont|
119
- if %w[get post put delete patch].include?(met.to_s.downcase)
120
- params = []
121
- params_path = []
122
- params_query = []
123
- params_required = []
124
- params_data = []
125
- description_parameters = []
126
- data_form = []
127
- data_required = []
128
- #todo: add nested one.true.three to data_read_only
129
- data_read_only = []
130
- data_default = []
131
- data_examples = []
132
- data_pattern = []
133
- responses = []
134
-
135
- # for the case operationId is missing
136
- cont[:operationId] = "undefined" unless cont.key?(:operationId)
137
-
138
- if create_method_name == :path
139
- method_name = (met.to_s + "_" + path.path.to_s).snake_case
140
- method_name.chop! if method_name[-1] == "_"
141
- elsif create_method_name == :operation_id
142
- 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
143
- metnametmp = cont[:operationId].gsub(/^#{cont[:tags].join}[\s_]*/, "")
144
- cont[:tags].join.split(" ").each do |tag|
145
- metnametmp.gsub!(/^#{tag}[\s_]*/i, "")
146
- end
147
- metnametmp = met if metnametmp == ""
163
+ path_txt = path.path.dup.to_s
164
+ if [:path, :path_file, :tags, :tags_file].include?(name_for_module)
165
+ old_module_requests = module_requests
166
+ if [:path, :path_file].include?(name_for_module)
167
+ # to remove version from path fex: /v1/Customer
168
+ path_requests = path_txt.gsub(/^\/v[\d\.]*\//i, "")
169
+ # to remove version from path fex: /1.0/Customer
170
+ path_requests = path_requests.gsub(/^\/[\d\.]*\//i, "")
171
+ if (path_requests == path_txt) && (path_txt.scan("/").size == 1)
172
+ # no folder in path
173
+ module_requests = "Root"
148
174
  else
149
- metnametmp = cont[:operationId]
175
+ res_path = path_requests.scan(/(\w+)/)
176
+ module_requests = res_path[0][0].camel_case
150
177
  end
151
- method_name = metnametmp.to_s.snake_case
152
178
  else
153
- 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
154
- method_name = cont[:operationId].gsub(/^#{cont[:tags].join}[\s_]*/, "")
155
- cont[:tags].join.split(" ").each do |tag|
156
- method_name.gsub!(/^#{tag}[\s_]*/i, "")
157
- end
158
- method_name = met if method_name == ""
179
+ if cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size > 0
180
+ module_requests = cont[:tags].join(" ").camel_case
159
181
  else
160
- method_name = cont[:operationId]
182
+ module_requests = "Undefined"
161
183
  end
162
184
  end
163
- path_txt = path.path.dup.to_s
164
- if [:path, :path_file, :tags, :tags_file].include?(name_for_module)
165
- old_module_requests = module_requests
166
- if [:path, :path_file].include?(name_for_module)
167
- # to remove version from path fex: /v1/Customer
168
- path_requests = path_txt.gsub(/^\/v[\d\.]*\//i, "")
169
- # to remove version from path fex: /1.0/Customer
170
- path_requests = path_requests.gsub(/^\/[\d\.]*\//i, "")
171
- if (path_requests == path_txt) && (path_txt.scan("/").size == 1)
172
- # no folder in path
173
- module_requests = "Root"
174
- else
175
- res_path = path_requests.scan(/(\w+)/)
176
- module_requests = res_path[0][0].camel_case
177
- end
178
- else
179
- if cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size > 0
180
- module_requests = cont[:tags].join(" ").camel_case
181
- else
182
- module_requests = "Undefined"
183
- end
184
- end
185
-
186
- # to remove from method_name: v1_list_regions and add it to module
187
- if /^(?<vers>v\d+)/i =~ method_name
188
- method_name.gsub!(/^#{vers}_?/, "")
189
- module_requests = (vers.capitalize + module_requests).camel_case unless module_requests.start_with?(vers)
190
- end
191
-
192
- if old_module_requests != module_requests
193
- output << "end" unless old_module_requests == "" or name_for_module == :path_file or name_for_module == :tags_file
194
- if name_for_module == :path or name_for_module == :tags
195
- # to add the end for the previous module unless is the first one
196
- output << "module #{module_requests}"
197
- else #:path_file, :tags_file
198
- if old_module_requests != ""
199
- unless files.key?(old_module_requests)
200
- files[old_module_requests] = Array.new
201
- end
202
- files[old_module_requests].concat(output)
203
- output = Array.new
185
+
186
+ # to remove from method_name: v1_list_regions and add it to module
187
+ if /^(?<vers>v\d+)/i =~ method_name
188
+ method_name.gsub!(/^#{vers}_?/, "")
189
+ module_requests = (vers.capitalize + module_requests).camel_case unless module_requests.start_with?(vers)
190
+ end
191
+
192
+ if old_module_requests != module_requests
193
+ output << "end" unless old_module_requests == "" or name_for_module == :path_file or name_for_module == :tags_file
194
+ if name_for_module == :path or name_for_module == :tags
195
+ # to add the end for the previous module unless is the first one
196
+ output << "module #{module_requests}"
197
+ else #:path_file, :tags_file
198
+ if old_module_requests != ""
199
+ unless files.key?(old_module_requests)
200
+ files[old_module_requests] = Array.new
204
201
  end
205
- output << "module #{module_requests}" unless files.key?(module_requests) # don't add in case already existed
202
+ files[old_module_requests].concat(output)
203
+ output = Array.new
206
204
  end
205
+ output << "module #{module_requests}" unless files.key?(module_requests) # don't add in case already existed
207
206
  end
208
207
  end
209
-
210
- output << ""
211
- output << "# operationId: #{cont[:operationId]}, method: #{met}"
212
- output << "# summary: #{cont[:summary].split("\n").join("\n# ")}" if cont.key?(:summary)
213
- if !cont[:description].to_s.split("\n").empty?
214
- output << "# description: "
215
- cont[:description].to_s.split("\n").each do |d|
216
- output << "# #{d}" unless d == ""
217
- end
218
- else
219
- output << "# description: #{cont[:description]}"
208
+ end
209
+
210
+ output << ""
211
+ output << "# operationId: #{cont[:operationId]}, method: #{met}"
212
+ output << "# summary: #{cont[:summary].split("\n").join("\n# ")}" if cont.key?(:summary)
213
+ if !cont[:description].to_s.split("\n").empty?
214
+ output << "# description: "
215
+ cont[:description].to_s.split("\n").each do |d|
216
+ output << "# #{d}" unless d == ""
220
217
  end
221
-
222
- mock_example = []
223
-
224
- if include_responses && cont.key?(:responses) && cont[:responses].is_a?(Hash)
225
- cont[:responses].each do |k, v|
226
- response_example = []
227
- response_example = get_response_examples(v)
228
-
229
- data_pattern += get_patterns("", v[:schema]) if v.key?(:schema)
230
- data_pattern.uniq!
231
- v[:description] = v[:description].to_s.gsub("'", %q(\\\'))
232
- if !response_example.empty?
233
- responses << "'#{k}': { "
234
- responses << "message: '#{v[:description]}', "
235
- responses << "data: "
236
- responses << response_example
237
- responses << "},"
238
- if mock_response and mock_example.size == 0
239
- mock_example << "code: '#{k}',"
240
- mock_example << "message: '#{v[:description]}',"
241
- mock_example << "data: "
242
- mock_example << response_example
243
- end
244
- else
245
- responses << "'#{k}': { message: '#{v[:description]}'}, "
218
+ else
219
+ output << "# description: #{cont[:description]}"
220
+ end
221
+
222
+ mock_example = []
223
+
224
+ if include_responses && cont.key?(:responses) && cont[:responses].is_a?(Hash)
225
+ cont[:responses].each do |k, v|
226
+ response_example = []
227
+ response_example = get_response_examples(v)
228
+
229
+ data_pattern += get_patterns("", v[:schema]) if v.key?(:schema)
230
+ data_pattern.uniq!
231
+ v[:description] = v[:description].to_s.gsub("'", %q(\\\'))
232
+ if !response_example.empty?
233
+ responses << "'#{k}': { "
234
+ responses << "message: '#{v[:description]}', "
235
+ responses << "data: "
236
+ responses << response_example
237
+ responses << "},"
238
+ if mock_response and mock_example.size == 0
239
+ mock_example << "code: '#{k}',"
240
+ mock_example << "message: '#{v[:description]}',"
241
+ mock_example << "data: "
242
+ mock_example << response_example
246
243
  end
244
+ else
245
+ responses << "'#{k}': { message: '#{v[:description]}'}, "
247
246
  end
248
247
  end
249
- # todo: for open api 3.0 add the new Link feature: https://swagger.io/docs/specification/links/
250
- # todo: for open api 3.0 is not getting the required params in all cases
251
-
252
- # for the case open api 3 with cont.requestBody.content.'applicatin/json'.schema
253
- # example: petstore-expanded.yaml operationId=addPet
254
- if cont.key?(:requestBody) and cont[:requestBody].key?(:content) and
255
- cont[:requestBody][:content].key?(:'application/json') and cont[:requestBody][:content][:'application/json'].key?(:schema)
256
- cont[:parameters] = [] unless cont.key?(:parameters)
257
- cont[:parameters] << { in: "body", schema: cont[:requestBody][:content][:'application/json'][:schema] }
258
- end
259
-
260
- data_examples_all_of = false
261
- if cont.key?(:parameters) && cont[:parameters].is_a?(Array)
262
- cont[:parameters].each do |p|
263
- if p.keys.include?(:schema) and p[:schema].include?(:type)
264
- type = p[:schema][:type]
265
- elsif p.keys.include?(:type)
266
- type = p[:type]
248
+ end
249
+ # todo: for open api 3.0 add the new Link feature: https://swagger.io/docs/specification/links/
250
+ # todo: for open api 3.0 is not getting the required params in all cases
251
+
252
+ # for the case open api 3 with cont.requestBody.content.'applicatin/json'.schema
253
+ # example: petstore-expanded.yaml operationId=addPet
254
+ if cont.key?(:requestBody) and cont[:requestBody].key?(:content) and
255
+ cont[:requestBody][:content].key?(:'application/json') and cont[:requestBody][:content][:'application/json'].key?(:schema)
256
+ cont[:parameters] = [] unless cont.key?(:parameters)
257
+ cont[:parameters] << { in: "body", schema: cont[:requestBody][:content][:'application/json'][:schema] }
258
+ end
259
+
260
+ data_examples_all_of = false
261
+ if cont.key?(:parameters) && cont[:parameters].is_a?(Array)
262
+ cont[:parameters].each do |p|
263
+ if p.keys.include?(:schema) and p[:schema].include?(:type)
264
+ type = p[:schema][:type]
265
+ elsif p.keys.include?(:type)
266
+ type = p[:type]
267
+ else
268
+ type = ""
269
+ end
270
+
271
+ if p[:in] == "path"
272
+ if create_method_name == :operationId
273
+ param_name = p[:name]
274
+ path_txt.gsub!("{#{param_name}}", "\#{#{param_name}}")
267
275
  else
268
- type = ""
276
+ param_name = p[:name].to_s.snake_case
277
+ path_txt.gsub!("{#{p[:name]}}", "\#{#{param_name}}")
269
278
  end
270
-
271
- if p[:in] == "path"
272
- if create_method_name == :operationId
273
- param_name = p[:name]
274
- path_txt.gsub!("{#{param_name}}", "\#{#{param_name}}")
279
+ unless params_path.include?(param_name)
280
+ if create_constants
281
+ params_path << "#{param_name}: #{param_name.upcase}"
282
+ required_constants << param_name.upcase
275
283
  else
276
- param_name = p[:name].to_s.snake_case
277
- path_txt.gsub!("{#{p[:name]}}", "\#{#{param_name}}")
278
- end
279
- unless params_path.include?(param_name)
280
- if create_constants
281
- params_path << "#{param_name}: #{param_name.upcase}"
282
- required_constants << param_name.upcase
283
- else
284
- params_path << param_name
285
- end
286
- #params_required << param_name if p[:required].to_s=="true"
287
- @logger.warn "Description key is missing for #{met} #{path.path} #{p[:name]}" if p[:description].nil?
288
- description_parameters << "# #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s == "true"} #{p[:description].to_s.split("\n").join("\n#\t\t\t")}"
284
+ params_path << param_name
289
285
  end
290
- elsif p[:in] == "query"
291
- params_query << p[:name]
292
- params_required << p[:name] if p[:required].to_s == "true"
286
+ #params_required << param_name if p[:required].to_s=="true"
293
287
  @logger.warn "Description key is missing for #{met} #{path.path} #{p[:name]}" if p[:description].nil?
294
288
  description_parameters << "# #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s == "true"} #{p[:description].to_s.split("\n").join("\n#\t\t\t")}"
295
- elsif p[:in] == "formData" or p[:in] == "formdata"
296
- #todo: take in consideration: default, required
297
- #todo: see if we should add the required as params to the method and not required as options
298
- #todo: set on data the required fields with the values from args
299
-
300
- description_parameters << "# #{p[:name]}: (#{p[:type]}) #{p[:description].split("\n").join("\n#\t\t\t")}"
301
-
302
- case p[:type]
303
- when /^string$/i
304
- data_form << "#{p[:name]}: ''"
305
- when /^boolean$/i
306
- data_form << "#{p[:name]}: true"
307
- when /^number$/i
308
- data_form << "#{p[:name]}: 0"
309
- when /^integer$/i
310
- data_form << "#{p[:name]}: 0"
289
+ end
290
+ elsif p[:in] == "query"
291
+ params_query << p[:name]
292
+ params_required << p[:name] if p[:required].to_s == "true"
293
+ @logger.warn "Description key is missing for #{met} #{path.path} #{p[:name]}" if p[:description].nil?
294
+ description_parameters << "# #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s == "true"} #{p[:description].to_s.split("\n").join("\n#\t\t\t")}"
295
+ elsif p[:in] == "formData" or p[:in] == "formdata"
296
+ #todo: take in consideration: default, required
297
+ #todo: see if we should add the required as params to the method and not required as options
298
+ #todo: set on data the required fields with the values from args
299
+
300
+ description_parameters << "# #{p[:name]}: (#{p[:type]}) #{p[:description].split("\n").join("\n#\t\t\t")}"
301
+
302
+ case p[:type]
303
+ when /^string$/i
304
+ data_form << "#{p[:name]}: ''"
305
+ when /^boolean$/i
306
+ data_form << "#{p[:name]}: true"
307
+ when /^number$/i
308
+ data_form << "#{p[:name]}: 0"
309
+ when /^integer$/i
310
+ data_form << "#{p[:name]}: 0"
311
+ else
312
+ puts "! on formData not supported type #{p[:type]}"
313
+ end
314
+ elsif p[:in] == "body"
315
+ if p.keys.include?(:schema)
316
+ if p[:schema].key?(:oneOf)
317
+ bodies = p[:schema][:oneOf]
318
+ elsif p[:schema].key?(:anyOf)
319
+ bodies = p[:schema][:anyOf]
320
+ elsif p[:schema].key?(:allOf)
321
+ data_examples_all_of, bodies = get_data_all_of_bodies(p)
322
+ bodies.unshift(p[:schema]) if p[:schema].key?(:required) or p.key?(:required) #todo: check if this 'if' is necessary
323
+ data_examples_all_of = true # because we are on data and allOf already
311
324
  else
312
- puts "! on formData not supported type #{p[:type]}"
325
+ bodies = [p[:schema]]
313
326
  end
314
- elsif p[:in] == "body"
315
- if p.keys.include?(:schema)
316
- if p[:schema].key?(:oneOf)
317
- bodies = p[:schema][:oneOf]
318
- elsif p[:schema].key?(:anyOf)
319
- bodies = p[:schema][:anyOf]
320
- elsif p[:schema].key?(:allOf)
321
- data_examples_all_of, bodies = get_data_all_of_bodies(p)
322
- bodies.unshift(p[:schema]) if p[:schema].key?(:required) or p.key?(:required) #todo: check if this 'if' is necessary
323
- data_examples_all_of = true # because we are on data and allOf already
324
- else
325
- bodies = [p[:schema]]
326
- end
327
-
328
- params_data = []
329
-
330
- bodies.each do |body|
331
- data_required += get_required_data(body)
332
- all_properties = []
333
- all_properties << body[:properties] if body.keys.include?(:properties) and body[:properties].size > 0
334
- if body.key?(:allOf)
335
- body[:allOf].each do |item|
336
- all_properties << item[:properties] if item.key?(:properties)
337
- end
327
+
328
+ params_data = []
329
+
330
+ bodies.each do |body|
331
+ data_required += get_required_data(body)
332
+ all_properties = []
333
+ all_properties << body[:properties] if body.keys.include?(:properties) and body[:properties].size > 0
334
+ if body.key?(:allOf)
335
+ body[:allOf].each do |item|
336
+ all_properties << item[:properties] if item.key?(:properties)
338
337
  end
338
+ end
339
339
 
340
- all_properties.each do |props|
341
- props.each { |dpk, dpv|
342
- if dpv.keys.include?(:example)
343
- if dpv[:example].is_a?(Array) and dpv.type != "array"
344
- valv = dpv[:example][0]
345
- else
346
- valv = dpv[:example].to_s
347
- end
340
+ all_properties.each do |props|
341
+ props.each { |dpk, dpv|
342
+ if dpv.keys.include?(:example)
343
+ if dpv[:example].is_a?(Array) and dpv.type != "array"
344
+ valv = dpv[:example][0]
348
345
  else
349
- if dpv.type == "object"
350
- if dpv.key?(:properties)
351
- valv = get_examples(dpv[:properties], :key_value, true).join("\n")
352
- else
353
- valv = "{}"
354
- end
355
- elsif dpv.type == "array"
356
- if dpv.key?(:items)
357
- valv = get_examples({ dpk => dpv }, :only_value)
358
- valv = valv.join("\n")
359
- else
360
- valv = "[]"
361
- end
362
- else
363
- valv = ""
364
- end
346
+ valv = dpv[:example].to_s
365
347
  end
366
-
367
- if dpv.keys.include?(:description)
368
- description_parameters << "# #{dpk}: (#{dpv[:type]}) #{dpv[:description].split("\n").join("\n#\t\t\t")}"
369
- end
370
-
371
- data_pattern += get_patterns(dpk, dpv)
372
- data_pattern.uniq!
373
- dpkeys = []
374
- data_pattern.reject! do |dp|
375
- dpkey = dp.scan(/^'[\w\.]+'/)
376
-
377
- if dpkeys.include?(dpkey)
378
- true
348
+ else
349
+ if dpv.type == "object"
350
+ if dpv.key?(:properties)
351
+ valv = get_examples(dpv[:properties], :key_value, true).join("\n")
379
352
  else
380
- dpkeys << dpkey
381
- false
353
+ valv = "{}"
382
354
  end
383
- end
384
-
385
- if dpv.keys.include?(:readOnly) and dpv[:readOnly] == true
386
- data_read_only << dpk
387
- end
388
- if dpv.keys.include?(:default)
389
- if dpv[:default].nil?
390
- data_default << "#{dpk}: nil"
391
- elsif dpv.type != "string"
392
- data_default << "#{dpk}: #{dpv[:default]}"
355
+ elsif dpv.type == "array"
356
+ if dpv.key?(:items)
357
+ valv = get_examples({ dpk => dpv }, :only_value)
358
+ valv = valv.join("\n")
393
359
  else
394
- data_default << "#{dpk}: '#{dpv[:default]}'"
360
+ valv = "[]"
395
361
  end
362
+ else
363
+ valv = ""
396
364
  end
397
-
398
- #todo: consider check default and insert it
399
- #todo: remove array from here and add the option to get_examples for the case thisisthekey: ['xxxx']
400
- if dpv.key?(:type) and dpv[:type] != "array"
401
- params_data << get_examples({ dpk => dpv }, :only_value, true).join
402
- params_data[-1].chop!.chop! if params_data[-1].to_s[-2..-1] == ", "
403
- params_data.pop if params_data[-1].match?(/^\s*$/im)
365
+ end
366
+
367
+ if dpv.keys.include?(:description)
368
+ description_parameters << "# #{dpk}: (#{dpv[:type]}) #{dpv[:description].split("\n").join("\n#\t\t\t")}"
369
+ end
370
+
371
+ data_pattern += get_patterns(dpk, dpv)
372
+ data_pattern.uniq!
373
+ dpkeys = []
374
+ data_pattern.reject! do |dp|
375
+ dpkey = dp.scan(/^'[\w\.]+'/)
376
+
377
+ if dpkeys.include?(dpkey)
378
+ true
404
379
  else
405
- if valv.to_s == ""
406
- valv = '""'
407
- elsif valv.include?('"')
408
- valv.gsub!('"', "'") unless valv.include?("'")
409
- end
410
- params_data << "#{dpk}: #{valv}"
380
+ dpkeys << dpkey
381
+ false
411
382
  end
412
- }
413
- if params_data.size > 0
414
- if data_examples_all_of == true and data_examples.size > 0
415
- data_examples[0] += params_data
383
+ end
384
+
385
+ if dpv.keys.include?(:readOnly) and dpv[:readOnly] == true
386
+ data_read_only << dpk
387
+ end
388
+ if dpv.keys.include?(:default)
389
+ if dpv[:default].nil?
390
+ data_default << "#{dpk}: nil"
391
+ elsif dpv.type != "string"
392
+ data_default << "#{dpk}: #{dpv[:default]}"
416
393
  else
417
- data_examples << params_data
394
+ data_default << "#{dpk}: '#{dpv[:default]}'"
418
395
  end
419
- params_data = []
420
396
  end
397
+
398
+ #todo: consider check default and insert it
399
+ #todo: remove array from here and add the option to get_examples for the case thisisthekey: ['xxxx']
400
+ if dpv.key?(:type) and dpv[:type] != "array"
401
+ params_data << get_examples({ dpk => dpv }, :only_value, true).join
402
+ params_data[-1].chop!.chop! if params_data[-1].to_s[-2..-1] == ", "
403
+ params_data.pop if params_data[-1].match?(/^\s*$/im)
404
+ else
405
+ if valv.to_s == ""
406
+ valv = '""'
407
+ elsif valv.include?('"')
408
+ valv.gsub!('"', "'") unless valv.include?("'")
409
+ end
410
+ params_data << "#{dpk}: #{valv}"
411
+ end
412
+ }
413
+ if params_data.size > 0
414
+ if data_examples_all_of == true and data_examples.size > 0
415
+ data_examples[0] += params_data
416
+ else
417
+ data_examples << params_data
418
+ end
419
+ params_data = []
421
420
  end
422
421
  end
422
+ end
423
423
 
424
- unless data_required.empty?
425
- data_required.uniq!
426
- output << "# required data: #{data_required.inspect}"
427
- end
424
+ unless data_required.empty?
425
+ data_required.uniq!
426
+ output << "# required data: #{data_required.inspect}"
428
427
  end
429
- elsif p[:in] == "header"
430
- #todo: see how we can treat those cases
431
- else
432
- puts "! not imported data with :in:#{p[:in]} => #{p.inspect}"
433
- end
428
+ end
429
+ elsif p[:in] == "header"
430
+ #todo: see how we can treat those cases
431
+ else
432
+ puts "! not imported data with :in:#{p[:in]} => #{p.inspect}"
434
433
  end
434
+ end
435
435
 
436
- params = params_path
437
-
438
- unless params_query.empty?
439
- path_txt += "?"
440
- params_required.each do |pr|
441
- if create_constants
442
- if params_query.include?(pr)
443
- if create_method_name == :operationId
444
- path_txt += "#{pr}=\#{#{pr}}&"
445
- params << "#{pr}: #{pr.upcase}"
446
- required_constants << pr.upcase
447
- else
448
- path_txt += "#{pr}=\#{#{pr.to_s.snake_case}}&"
449
- params << "#{pr.to_s.snake_case}: #{pr.to_s.snake_case.upcase}"
450
- required_constants << pr.to_s.snake_case.upcase
451
- end
452
- end
453
- else
454
- if params_query.include?(pr)
455
- if create_method_name == :operationId
456
- path_txt += "#{pr}=\#{#{pr}}&"
457
- params << "#{pr}"
458
- else
459
- path_txt += "#{pr}=\#{#{pr.to_s.snake_case}}&"
460
- params << "#{pr.to_s.snake_case}"
461
- end
436
+ params = params_path
437
+
438
+ unless params_query.empty?
439
+ path_txt += "?"
440
+ params_required.each do |pr|
441
+ if create_constants
442
+ if params_query.include?(pr)
443
+ if create_method_name == :operationId
444
+ path_txt += "#{pr}=\#{#{pr}}&"
445
+ params << "#{pr}: #{pr.upcase}"
446
+ required_constants << pr.upcase
447
+ else
448
+ path_txt += "#{pr}=\#{#{pr.to_s.snake_case}}&"
449
+ params << "#{pr.to_s.snake_case}: #{pr.to_s.snake_case.upcase}"
450
+ required_constants << pr.to_s.snake_case.upcase
462
451
  end
463
452
  end
464
- end
465
- params_query.each do |pq|
466
- unless params_required.include?(pq)
453
+ else
454
+ if params_query.include?(pr)
467
455
  if create_method_name == :operationId
468
- path_txt += "#{pq}=\#{#{pq}}&"
469
- params << "#{pq}: ''"
456
+ path_txt += "#{pr}=\#{#{pr}}&"
457
+ params << "#{pr}"
470
458
  else
471
- path_txt += "#{pq}=\#{#{pq.to_s.snake_case}}&"
472
- params << "#{pq.to_s.snake_case}: ''"
459
+ path_txt += "#{pr}=\#{#{pr.to_s.snake_case}}&"
460
+ params << "#{pr.to_s.snake_case}"
473
461
  end
474
462
  end
475
463
  end
476
464
  end
477
- end
478
-
479
- if description_parameters.size > 0
480
- output << "# parameters description: "
481
- output << description_parameters.uniq
482
- end
483
-
484
- #for the case we still have some parameters on path that were not in 'parameters'
485
- if path_txt.scan(/[^#]{\w+}/).size > 0
486
- paramst = []
487
- prms = path_txt.scan(/[^#]{(\w+)}/)
488
- prms.each do |p|
489
- #if create_constants
490
- # paramst<<"#{p[0].to_s.snake_case}: #{p[0].to_s.snake_case.upcase}"
491
- # required_constants << p[0].to_s.snake_case.upcase
492
- #else
493
- paramst << p[0].to_s.snake_case
494
- #end
495
- path_txt.gsub!("{#{p[0]}}", "\#{#{p[0].to_s.snake_case}}")
465
+ params_query.each do |pq|
466
+ unless params_required.include?(pq)
467
+ if create_method_name == :operationId
468
+ path_txt += "#{pq}=\#{#{pq}}&"
469
+ params << "#{pq}: ''"
470
+ else
471
+ path_txt += "#{pq}=\#{#{pq.to_s.snake_case}}&"
472
+ params << "#{pq.to_s.snake_case}: ''"
473
+ end
474
+ end
496
475
  end
497
- paramst.concat params
498
- params = paramst
499
476
  end
500
- params.uniq!
501
- output << "def self.#{method_name} (#{params.join(", ")})"
502
-
503
- output << "{"
504
-
505
- output << "name: \"#{module_requests}.#{method_name}\","
506
-
507
- output << "path: \"#{base_path}#{path_txt}\","
508
-
509
- output << "method: :#{met}," if met.to_s != ""
510
-
511
- unless data_required.empty?
512
- output << "data_required: ["
513
- output << ":'#{data_required.uniq.join("', :'")}'"
514
- output << "],"
515
- end
516
- unless data_read_only.empty?
517
- output << "data_read_only: ["
518
- output << ":'#{data_read_only.uniq.join("', :'")}'"
519
- output << "],"
520
- end
521
- unless data_default.empty?
522
- output << "data_default: {"
523
- data_default.uniq!
524
- output << data_default.join(", \n")
525
- output << "},"
526
- end
527
-
528
- unless data_pattern.empty?
529
- output << "data_pattern: {"
530
- output << data_pattern.uniq.join(", \n")
531
- output << "},"
532
- end
533
-
534
- unless data_form.empty?
535
- data_examples << data_form
477
+ end
478
+
479
+ if description_parameters.size > 0
480
+ output << "# parameters description: "
481
+ output << description_parameters.uniq
482
+ end
483
+
484
+ #for the case we still have some parameters on path that were not in 'parameters'
485
+ if path_txt.scan(/[^#]{\w+}/).size > 0
486
+ paramst = []
487
+ prms = path_txt.scan(/[^#]{(\w+)}/)
488
+ prms.each do |p|
489
+ #if create_constants
490
+ # paramst<<"#{p[0].to_s.snake_case}: #{p[0].to_s.snake_case.upcase}"
491
+ # required_constants << p[0].to_s.snake_case.upcase
492
+ #else
493
+ paramst << p[0].to_s.snake_case
494
+ #end
495
+ path_txt.gsub!("{#{p[0]}}", "\#{#{p[0].to_s.snake_case}}")
536
496
  end
497
+ paramst.concat params
498
+ params = paramst
499
+ end
500
+ params.uniq!
501
+ output << "def self.#{method_name} (#{params.join(", ")})"
537
502
 
538
- unless data_examples.empty?
539
- unless data_required.empty?
540
- reqdata = []
541
- begin
542
- data_examples[0].uniq!
543
- data_ex = eval("{#{data_examples[0].join(", ")}}")
544
- rescue SyntaxError
545
- data_ex = {}
546
- @logger.warn "Syntax error: #{met} for path: #{path.path} evaluating data_examples[0] => #{data_examples[0].inspect}"
547
- rescue
548
- data_ex = {}
549
- @logger.warn "Syntax error: #{met} for path: #{path.path} evaluating data_examples[0] => #{data_examples[0].inspect}"
550
- end
551
- if (data_required.grep(/\./)).empty?
552
- reqdata = filter(data_ex, data_required) #not nested
553
- else
554
- reqdata = filter(data_ex, data_required, true) #nested
555
- end
556
- unless reqdata.empty?
557
- reqdata.uniq!
558
- phsd = pretty_hash_symbolized(reqdata)
559
- phsd[0] = "data: {"
560
- output += phsd
561
- end
503
+ output << "{"
504
+
505
+ output << "name: \"#{module_requests}.#{method_name}\","
506
+
507
+ output << "path: \"#{base_path}#{path_txt}\","
508
+
509
+ output << "method: :#{met}," if met.to_s != ""
510
+
511
+ unless data_required.empty?
512
+ output << "data_required: ["
513
+ output << ":'#{data_required.uniq.join("', :'")}'"
514
+ output << "],"
515
+ end
516
+ unless data_read_only.empty?
517
+ output << "data_read_only: ["
518
+ output << ":'#{data_read_only.uniq.join("', :'")}'"
519
+ output << "],"
520
+ end
521
+ unless data_default.empty?
522
+ output << "data_default: {"
523
+ data_default.uniq!
524
+ output << data_default.join(", \n")
525
+ output << "},"
526
+ end
527
+
528
+ unless data_pattern.empty?
529
+ output << "data_pattern: {"
530
+ output << data_pattern.uniq.join(", \n")
531
+ output << "},"
532
+ end
533
+
534
+ unless data_form.empty?
535
+ data_examples << data_form
536
+ end
537
+
538
+ unless data_examples.empty?
539
+ unless data_required.empty?
540
+ reqdata = []
541
+ begin
542
+ data_examples[0].uniq!
543
+ data_ex = eval("{#{data_examples[0].join(", ")}}")
544
+ rescue SyntaxError
545
+ data_ex = {}
546
+ @logger.warn "Syntax error: #{met} for path: #{path.path} evaluating data_examples[0] => #{data_examples[0].inspect}"
547
+ rescue
548
+ data_ex = {}
549
+ @logger.warn "Syntax error: #{met} for path: #{path.path} evaluating data_examples[0] => #{data_examples[0].inspect}"
562
550
  end
563
- unless data_read_only.empty? or !data_required.empty?
564
- reqdata = []
565
- #remove read only fields from :data
566
- data_examples[0].each do |edata|
567
- read_only = false
568
- data_read_only.each do |rdata|
569
- if edata.scan(/^#{rdata}:/).size > 0
570
- read_only = true
571
- break
572
- elsif edata.scan(/:/).size == 0
573
- break
574
- end
551
+ if (data_required.grep(/\./)).empty?
552
+ reqdata = filter(data_ex, data_required) #not nested
553
+ else
554
+ reqdata = filter(data_ex, data_required, true) #nested
555
+ end
556
+ unless reqdata.empty?
557
+ reqdata.uniq!
558
+ phsd = pretty_hash_symbolized(reqdata)
559
+ phsd[0] = "data: {"
560
+ output += phsd
561
+ end
562
+ end
563
+ unless data_read_only.empty? or !data_required.empty?
564
+ reqdata = []
565
+ #remove read only fields from :data
566
+ data_examples[0].each do |edata|
567
+ read_only = false
568
+ data_read_only.each do |rdata|
569
+ if edata.scan(/^#{rdata}:/).size > 0
570
+ read_only = true
571
+ break
572
+ elsif edata.scan(/:/).size == 0
573
+ break
575
574
  end
576
- reqdata << edata unless read_only
577
- end
578
- unless reqdata.empty?
579
- reqdata.uniq!
580
- output << "data: {"
581
- output << reqdata.join(", \n")
582
- output << "},"
583
575
  end
576
+ reqdata << edata unless read_only
584
577
  end
585
-
586
- output << "data_examples: ["
587
- data_examples.each do |data|
588
- output << "{"
589
- data.uniq!
590
- output << data.join(", \n")
591
- output << "}, "
578
+ unless reqdata.empty?
579
+ reqdata.uniq!
580
+ output << "data: {"
581
+ output << reqdata.join(", \n")
582
+ output << "},"
592
583
  end
593
- output << "],"
594
- end
595
-
596
- unless mock_example.empty?
597
- output << "mock_response: {"
598
- output << mock_example
599
- output << "},"
600
584
  end
601
-
602
- unless responses.empty?
603
- output << "responses: {"
604
- output << responses
605
- output << "},"
585
+
586
+ output << "data_examples: ["
587
+ data_examples.each do |data|
588
+ output << "{"
589
+ data.uniq!
590
+ output << data.join(", \n")
591
+ output << "}, "
606
592
  end
607
-
608
- output << "}"
609
- output << "end"
610
- else
611
- @logger.warn "Not imported method: #{met} for path: #{path.path} since it is not supported by OpenApiImport"
593
+ output << "],"
594
+ end
595
+
596
+ unless mock_example.empty?
597
+ output << "mock_response: {"
598
+ output << mock_example
599
+ output << "},"
600
+ end
601
+
602
+ unless responses.empty?
603
+ output << "responses: {"
604
+ output << responses
605
+ output << "},"
612
606
  end
607
+
608
+ output << "}"
609
+ output << "end"
610
+ else
611
+ @logger.warn "Not imported method: #{met} for path: #{path.path} since it is not supported by OpenApiImport"
613
612
  end
614
613
  end
615
- output_footer = []
616
-
617
- output_footer << "end" unless (module_requests == "") && ([:path, :path_file, :tags, :tags_file].include?(name_for_module))
618
- output_footer << "end" << "end" << "end"
619
-
620
- if files.size == 0
621
- output = output_header + output + output_footer
614
+ end
615
+ output_footer = []
616
+
617
+ output_footer << "end" unless (module_requests == "") && ([:path, :path_file, :tags, :tags_file].include?(name_for_module))
618
+ output_footer << "end" << "end" << "end"
619
+ if files.size == 0 and !create_constants
620
+ output = output_header + output + output_footer
621
+ output_txt = output.join("\n")
622
+ requests_file_path = file_to_convert + ".rb"
623
+ File.open(requests_file_path, "w") { |file| file.write(output_txt) }
624
+ res_rufo = `rufo #{requests_file_path}`
625
+ message = "** Requests file: #{swagger_file}.rb that contains the code of the requests after importing the Swagger file"
626
+ puts message unless silent
627
+ @logger.info message
628
+ @logger.error " Error formating with rufo" unless res_rufo.to_s.match?(/\AFormat:.+$\s*\z/)
629
+ @logger.error " Syntax Error: #{`ruby -c #{requests_file_path}`}" unless `ruby -c #{requests_file_path}`.include?("Syntax OK")
630
+ else
631
+ unless files.key?(module_requests)
632
+ files[module_requests] = Array.new
633
+ end
634
+ files[module_requests].concat(output) #for the last one
635
+
636
+ requires_txt = ""
637
+ message = "** Generated files that contain the code of the requests after importing the Swagger file: "
638
+ puts message unless silent
639
+ @logger.info message
640
+ files.each do |mod, out_mod|
641
+ output = output_header + out_mod + output_footer
622
642
  output_txt = output.join("\n")
623
- requests_file_path = file_to_convert + ".rb"
643
+ requests_file_path = file_to_convert + "_" + mod + ".rb"
644
+ requires_txt += "require_relative '#{File.basename(swagger_file)}_#{mod}'\n"
624
645
  File.open(requests_file_path, "w") { |file| file.write(output_txt) }
625
646
  res_rufo = `rufo #{requests_file_path}`
626
- message = "** Requests file: #{swagger_file}.rb that contains the code of the requests after importing the Swagger file"
627
- puts message unless silent
628
- @logger.info message
629
- @logger.error " Error formating with rufo" unless res_rufo.to_s.match?(/\AFormat:.+$\s*\z/)
630
- @logger.error " Syntax Error: #{`ruby -c #{requests_file_path}`}" unless `ruby -c #{requests_file_path}`.include?("Syntax OK")
631
- else
632
- unless files.key?(module_requests)
633
- files[module_requests] = Array.new
634
- end
635
- files[module_requests].concat(output) #for the last one
636
-
637
- requires_txt = ""
638
- message = "** Generated files that contain the code of the requests after importing the Swagger file: "
639
- puts message unless silent
640
- @logger.info message
641
- files.each do |mod, out_mod|
642
- output = output_header + out_mod + output_footer
643
- output_txt = output.join("\n")
644
- requests_file_path = file_to_convert + "_" + mod + ".rb"
645
- requires_txt += "require_relative '#{File.basename(swagger_file)}_#{mod}'\n"
646
- File.open(requests_file_path, "w") { |file| file.write(output_txt) }
647
- res_rufo = `rufo #{requests_file_path}`
648
- message = " - #{requests_file_path}"
649
- puts message unless silent
650
- @logger.info message
651
- @logger.error " Error formating with rufo" unless res_rufo.to_s.match?(/\AFormat:.+$\s*\z/)
652
- @logger.error " Syntax Error: #{`ruby -c #{requests_file_path}`}" unless `ruby -c #{requests_file_path}`.include?("Syntax OK")
653
- end
654
-
655
- requests_file_path = file_to_convert + ".rb"
656
- if required_constants.size > 0
657
- rconsts = "# Required constants\n"
658
- required_constants.uniq!
659
- required_constants.each do |rq|
660
- rconsts += "#{rq} ||= ENV['#{rq}'] ||=''\n"
661
- end
662
- rconsts += "\n\n"
663
- else
664
- rconsts = ""
665
- end
666
-
667
- File.open(requests_file_path, "w") { |file| file.write(rconsts + requires_txt) }
668
- res_rufo = `rufo #{requests_file_path}`
669
- message = "** File that contains all the requires for all Request files: \n"
670
- message += " - #{requests_file_path} "
647
+ message = " - #{requests_file_path}"
671
648
  puts message unless silent
672
649
  @logger.info message
673
650
  @logger.error " Error formating with rufo" unless res_rufo.to_s.match?(/\AFormat:.+$\s*\z/)
674
651
  @logger.error " Syntax Error: #{`ruby -c #{requests_file_path}`}" unless `ruby -c #{requests_file_path}`.include?("Syntax OK")
675
652
  end
676
-
677
- begin
678
- res = eval(output_txt)
679
- rescue Exception => stack
680
- import_errors += "\n\nResult evaluating the ruby file generated: \n" + stack.to_s
681
- end
682
-
683
- if import_errors.to_s != ""
684
- File.open(file_errors, "w") { |file| file.write(import_errors) }
685
- message = "* It seems there was a problem importing the Swagger file #{file_to_convert}\n"
686
- message += "* Take a look at the detected errors at #{file_errors}\n"
687
- warn message
688
- @logger.fatal message
689
- return false
653
+
654
+ requests_file_path = file_to_convert + ".rb"
655
+ if required_constants.size > 0
656
+ rconsts = "# Required constants\n"
657
+ required_constants.uniq!
658
+ required_constants.each do |rq|
659
+ rconsts += "#{rq} ||= ENV['#{rq}'] ||=''\n"
660
+ end
661
+ rconsts += "\n\n"
690
662
  else
691
- return true
663
+ rconsts = ""
692
664
  end
693
- rescue StandardError => stack
694
- puts stack.message
695
- @logger.fatal stack.message
696
- @logger.fatal stack.backtrace
697
- puts stack.backtrace
665
+
666
+ File.open(requests_file_path, "w") { |file| file.write(rconsts + requires_txt) }
667
+ res_rufo = `rufo #{requests_file_path}`
668
+ message = "** File that contains all the requires for all Request files: \n"
669
+ message += " - #{requests_file_path} "
670
+ puts message unless silent
671
+ @logger.info message
672
+ @logger.error " Error formating with rufo" unless res_rufo.to_s.match?(/\AFormat:.+$\s*\z/)
673
+ @logger.error " Syntax Error: #{`ruby -c #{requests_file_path}`}" unless `ruby -c #{requests_file_path}`.include?("Syntax OK")
674
+ end
675
+
676
+ begin
677
+ res = eval(output_txt)
678
+ rescue Exception => stack
679
+ import_errors += "\n\nResult evaluating the ruby file generated: \n" + stack.to_s
680
+ end
681
+
682
+ if import_errors.to_s != ""
683
+ File.open(file_errors, "w") { |file| file.write(import_errors) }
684
+ message = "* It seems there was a problem importing the Swagger file #{file_to_convert}\n"
685
+ message += "* Take a look at the detected errors at #{file_errors}\n"
686
+ warn message
687
+ @logger.fatal message
688
+ return false
689
+ else
690
+ return true
698
691
  end
692
+ rescue StandardError => stack
693
+ puts stack.message
694
+ @logger.fatal stack.message
695
+ @logger.fatal stack.backtrace
696
+ puts stack.backtrace
699
697
  end
700
698
  end
701
-
699
+ end