import_postman 1.5.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 25eac0e93409e3b9416aeed345a08766a975e5973b63cae485e74abbb9789863
4
+ data.tar.gz: f2c232428dd79b968abaa8b313f24ab534203b331077ad5b7a641873be36ad8e
5
+ SHA512:
6
+ metadata.gz: 596871c561d69c0f9bcb14d74dcf6fbe92a3e589335395619a188b3e3a8102a02983d474483d04cafce189fc6bbb6b1f117d00559f1c5896bf0a3578dc59e68d
7
+ data.tar.gz: af43a59b80d66a63ad93343dd1f31d231a328be49e60314bdab9bc1c2622a7ca33e1f40ae53fd2810b27e1535a4685454a6b48184819411082b12717474afe72
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Mario Ruiz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # ImportPostman
2
+ ## Description
3
+ Using import_postman method you will be able to import a postman collection and create a Ruby Requests Hash containing the path, headers, data...
4
+
5
+ You can also generate the Ruby tests from the tests you have declared on your collection by using the ImportPostman::Parsers module
6
+
7
+ Feel free to add more parsers or improve existing ones
8
+
9
+ ## Input parameters
10
+
11
+ The import_postman method has these arguments:
12
+
13
+ postman_collection_file_path: (positional) (String)
14
+ Specify just the name of the collection without the extension and in case we have on the same folder the environment and/or the global variable files they will be imported automatically also.
15
+ It is necessary in that case to use the default extensions from Postman: .postman_collection, .postman_environment, .postman_globals.
16
+ The postman collection need to be exported as v1
17
+
18
+ env_file: (optional) (keyword) (String)
19
+ Path to the environment postman file
20
+
21
+ global_file: (optional) (keyword) (String)
22
+ Path to the global postman file
23
+
24
+ output_file: (optional) (keyword) (String)
25
+ Path to output file
26
+
27
+ mock_vars: (optional) (keyword) (Hash)
28
+ Hash including all postman variables we want to mock for example: {"requestId": "return_requestId_method"}
29
+
30
+
31
+ ## Output
32
+ In case output_file is supplied the file will be exported to that location and name. In case not .rb extension it will be added.
33
+
34
+ In case a output_file is not supplied then automatically will create one with the same name as postman_collection_file_path and .rb extension on the same path
35
+
36
+ The postman environments variables will be added as global variables at the beginning of the resultant file
37
+
38
+ The postman global variables will be added as global variables at the beginning of the resultant file
39
+
40
+ All other postman variables will be added as parameters on the request methods created
41
+
42
+ The file when converted will be possible to be used in your code, for example:
43
+
44
+ ```ruby
45
+ request=RESTRequests::Customer.addAlias()
46
+ ```
47
+
48
+ The Postman Dynamic Variables will be converted into Ruby variables. Added $guid, $timestamp and $randomint
49
+
50
+ ## Examples of Use
51
+
52
+ ### Case #1
53
+
54
+ Only one parameter, the collection name with relative path and no extension supplied.
55
+
56
+ The file on the folder would be: MyCustomer.json.postman_collection
57
+
58
+ In case a MyCustomer.json.postman_environment and/or MyCustomer.json.postman_globals found on the same folder, they will be automatically imported also
59
+
60
+ ```ruby
61
+ import_postman "./interfaces/rest/postman/MyCustomer.json"
62
+ ```
63
+
64
+ ### Case #2
65
+
66
+ Relative paths, collection, environmental variables and global variables, with different extensions that the default ones
67
+
68
+ ```ruby
69
+ import_postman "./interfaces/rest/postman/collection.json.txt",
70
+ env_file: "./interfaces/rest/postman/env.json.txt",
71
+ global_file: "./interfaces/rest/postman/global.json.txt"
72
+ ```
73
+
74
+ ### Case #3
75
+
76
+ Full path for collection, environmental variables and global variables. relative path for resultant string. Different names and extensions
77
+
78
+ ```ruby
79
+ import_postman "c:/MyPostManFiles/MyCustomer_postman_collection.json",
80
+ env_file: "c:/MyPostManFiles/MyCustomer_env",
81
+ global_file: "c:/MyPostManFiles/MyCustomer_global",
82
+ output_file: "./interfaces/rest/customer_postman.rb"
83
+ ```
84
+
85
+ ### Case #4
86
+ Change the value of an environmental variable and create tests (mini-test) using the rest-client library
87
+
88
+ ```ruby
89
+ require 'string_pattern' #to generate the random value for requestId
90
+
91
+ require 'import_postman/parsers/rest_client' #to generate the tests
92
+ include ImportPostman::Parsers::RestClient
93
+
94
+ require 'import_postman'
95
+ import_postman "./my_collections/MyCustomer.json",
96
+ mock_vars: {"requestId": " '6:/Nx/'.gen"}
97
+ ```
98
+
99
+ ### Case #5
100
+ Change the value of an environmental variable and create tests (test-unit) using the rest-client library
101
+
102
+ ```ruby
103
+ require 'string_pattern' #to generate the random value for requestId
104
+
105
+ IP_TEST_FRAMEWORK='test-unit'
106
+ require 'import_postman/parsers/rest_client' #to generate the tests
107
+ include ImportPostman::Parsers::RestClient
108
+
109
+ require 'import_postman'
110
+ import_postman "./my_collections/MyCustomer.json",
111
+ mock_vars: {"requestId": " '6:/Nx/'.gen"}
112
+ ```
113
+
114
+ The tests file will be generated on the same folder than the output_file with the same name but finish by _tests
115
+
116
+ ## Contributing
117
+
118
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/import_postman.
119
+
120
+
121
+ ## License
122
+
123
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
124
+
@@ -0,0 +1,312 @@
1
+
2
+ require 'nice_hash'
3
+
4
+ ##################################################################################
5
+ # This method import a postman collection to be used as RequestHash object
6
+ # Input:
7
+ # postman_collection_file_path: specify just the name of the collection without the extension and in case we have on the same folder the environment and/or the global variable files they will be imported automatically also.
8
+ # It is necessary in that case to use the default extensions from Postman: .postman_collection, .postman_environment, .postman_globals.
9
+ # env_file: (optional) path to the environment postman file
10
+ # global_file: (optional) path to the global postman file
11
+ # output_file: (optional) path to output file
12
+ # mock_vars: (optional) Hash including all postman variables we want to mock for example: {"requestId": "return_requestId_method"}
13
+ # Output:
14
+ # In case output_file is supplied the file will be exported to that location and name. In case not .rb extension it will be added.
15
+ # In case a output_file is not supplied then automatically will create one with the same name as postman_collection_file_path and .rb extension on the same path
16
+ # The postman environments variables will be added as global variables at the beginning of the resultant file
17
+ # The postman global variables will be added as global variables at the beginning of the resultant file
18
+ # All other postman variables will be added as parameters for the request
19
+ # The file when converted will be possible to be used in your code, for example:
20
+ # request=RESTRequests::Customer.addAlias()
21
+ # The Postman Dynamic Variables will be converted into Ruby variables. Added $guid, $timestamp and $randomint
22
+ # examples:
23
+ # only one parameter, the collection name with relative path and no extension supplied.
24
+ # The file on the folder would be: MyCustomer.json.postman_collection
25
+ # In case a MyCustomer.json.postman_environment and/or MyCustomer.json.postman_globals found on the same folder, they will be automatically imported also
26
+ # import_postman "./interfaces/rest/postman/MyCustomer.json"
27
+ # relative paths, collection, environmental variables and global variables, with different extensions that the default ones
28
+ # import_postman "./interfaces/rest/postman/collection.json.txt", env_file: "./interfaces/rest/postman/env.json.txt", global_file: "./interfaces/rest/postman/global.json.txt"
29
+ # full path for collection, environmental variables and global variables. relative path for resultant string. Different names and extensions
30
+ # import_postman "c:/MyPostManFiles/MyCustomer_postman_collection.json", env_file: "c:/MyPostManFiles/MyCustomer_env", global_file: "c:/MyPostManFiles/MyCustomer_global", output_file: "./interfaces/rest/customer_postman.rb"
31
+ ##################################################################################
32
+ def import_postman(postman_collection_file_path, env_file: postman_collection_file_path+".postman_environment", global_file: postman_collection_file_path+".postman_globals", output_file: postman_collection_file_path+".rb", mock_vars: Hash.new())
33
+ #.postman_collection, .postman_environment, .postman_globals
34
+ require 'json'
35
+ postman_env_file_path = env_file
36
+ postman_global_file_path = global_file
37
+ output_file_path = output_file
38
+
39
+ if postman_collection_file_path["./"].nil? then
40
+ file_to_convert=postman_collection_file_path
41
+ else
42
+ file_to_convert=Dir.pwd.to_s() + "/" + postman_collection_file_path.gsub("./", "")
43
+ end
44
+
45
+ unless File.exist?(file_to_convert)
46
+ file_to_convert+=".postman_collection"
47
+ end
48
+
49
+ if output_file_path['.rb'].nil? then
50
+ output_file_path+=".rb"
51
+ end
52
+
53
+ unless output_file_path["./"].nil? then
54
+ output_file_path=Dir.pwd.to_s() + "/" + output_file_path.gsub("./", "")
55
+ end
56
+ env_vars = {}
57
+
58
+ if defined?(parse_postman_test)
59
+ test_output_file_path = output_file_path.gsub(/\.rb$/, "_tests.rb")
60
+ tests = ""
61
+ num_test = 0
62
+ end
63
+
64
+
65
+ unless postman_global_file_path==""
66
+ unless postman_global_file_path["./"].nil? then
67
+ postman_global_file_path=Dir.pwd.to_s() + "/" + postman_global_file_path.gsub("./", "")
68
+ end
69
+ if File.exist?(postman_global_file_path) then
70
+ begin
71
+ file=File.read(postman_global_file_path)
72
+ puts "** Postman Global file imported: #{postman_global_file_path}"
73
+ rescue Exception => stack
74
+ warn "* It seems there was a problem reading the Global file #{postman_global_file_path}"
75
+ warn stack.to_s()
76
+ return false
77
+ end
78
+
79
+ val=JSON.parse(file, {symbolize_names: true})
80
+ if val.keys.include?(:values) then
81
+ val=val[:values]
82
+ end
83
+ val.each {|v|
84
+ env_vars[v[:key]]=v[:value] if v[:key]!="url" and v[:key]!=:url and v[:key]!="host" and v[:key]!=:host
85
+ }
86
+ end
87
+ end
88
+
89
+ unless postman_env_file_path==""
90
+ unless postman_env_file_path["./"].nil? then
91
+ postman_env_file_path=Dir.pwd.to_s() + "/" + postman_env_file_path.gsub("./", "")
92
+ end
93
+ if File.exist?(postman_env_file_path) then
94
+ begin
95
+ file=File.read(postman_env_file_path)
96
+ puts "** Postman Env file imported: #{postman_env_file_path}"
97
+ rescue Exception => stack
98
+ warn "* It seems there was a problem reading the Env file #{postman_env_file_path}"
99
+ warn stack.to_s()
100
+ return false
101
+ end
102
+
103
+ val=JSON.parse(file, {symbolize_names: true})
104
+ val[:values].each {|v|
105
+ env_vars[v[:key]]=v[:value] if v[:key]!="url" and v[:key]!=:url and v[:key]!="host" and v[:key]!=:host
106
+ }
107
+ end
108
+ end
109
+
110
+ begin
111
+ if File.exist?(file_to_convert) then
112
+ file=File.read(file_to_convert)
113
+ puts "** Postman Collection file imported: #{file_to_convert}"
114
+ else
115
+ warn "* It seems the collection file #{file_to_convert} doesn't exist"
116
+ return false
117
+ end
118
+ rescue Exception => stack
119
+ warn "* It seems there was a problem reading the collection file #{file_to_convert}"
120
+ warn stack.to_s()
121
+ return false
122
+ end
123
+
124
+ val=JSON.parse(file, {symbolize_names: true})
125
+
126
+ module_main_name=val[:name].gsub(/^[\d\s\W]+/, "")
127
+ module_main_name=module_main_name.gsub(/\W+/, "_")
128
+ module_main_name=module_main_name[0, 1].upcase + module_main_name[1..-1] #to be sure first character is in capitalize
129
+ if defined?(parse_postman_test_headers)
130
+ tests="require './#{File.basename output_file_path}'\n"
131
+ tests+=parse_postman_test_headers(module_main_name)
132
+ end
133
+
134
+ requests={}
135
+ if val.keys.include?(:folders) and val.keys.include?(:requests) then
136
+ requests_added=[]
137
+ val[:folders].each {|folder|
138
+ folder[:order].each {|reqnum|
139
+ request=val[:requests].select {|f| f[:id]==reqnum}
140
+ if request.size==1 #found one
141
+ module_name=folder[:name].gsub(/^[\d\s\W]+/, "")
142
+ module_name=module_name.gsub(/\W+/, "_")
143
+ module_name=module_name[0, 1].upcase + module_name[1..-1] #to be sure first character is in capitalize
144
+ requests[module_name]=Array.new() unless requests.keys.include?(module_name)
145
+ requests[module_name].push(request[0])
146
+ requests_added.push(reqnum)
147
+ end
148
+ }
149
+ }
150
+ module_main_name="" if requests.keys.size>0 #there are folders
151
+ if val[:requests].size>requests_added.size then #if there are some requests that are not in folders
152
+ requests[""]=Array.new()
153
+ val[:requests].each {|req|
154
+ if !requests_added.include?(req[:id]) then
155
+ requests[""].push(req)
156
+ end
157
+ }
158
+ end
159
+ elsif val.keys.include?(:requests) then
160
+ requests[""]=Array.new()
161
+ val[:requests].each {|req|
162
+ requests[""].push(req)
163
+ }
164
+ end
165
+
166
+
167
+ rest_methods=""
168
+ all_variables_on_path=[]
169
+ all_variables_on_headers=[]
170
+ requests.each {|folder, requests_folder|
171
+ rest_methods+="\n \tmodule #{folder}\n\n" unless folder==""
172
+
173
+ requests_folder.each {|request|
174
+ #url, host or server are the postman variables for the testserver
175
+ path=request[:url].gsub(/\{\{url\}\}/, "").gsub(/\{\{server\}\}/, "").gsub(/\{\{host\}\}/, "").gsub(/https?:\/\//, "").gsub("{{", "\#{").gsub("}}", "}")
176
+ variables_on_path=path.scan(/{(\w+)}/)
177
+ variables_on_path.uniq!
178
+ if request[:rawModeData].to_s()!="" then
179
+ data=request[:rawModeData].gsub("\r", "")
180
+ variables_on_data=Array.new
181
+ begin
182
+ JSON.parse(data.gsub("{{",'"').gsub("}}",'"'))
183
+ data.gsub!(/"([^"]+)"\s*:/, '\1:')
184
+ variables_on_data=data.scan(/{([\w\-]+)}/)
185
+ variables_on_data.uniq!
186
+ if variables_on_data.size>0 and variables_on_data[0].size>0
187
+ variables_on_data[0].each {|v|
188
+ #for the case it is not a string
189
+ data.gsub!(": {{#{v}}}", ": #{v.gsub("-", "_").downcase}")
190
+ #for the case it is a string
191
+ data.gsub!("{{#{v}}}", "{{#{v.gsub("-", "_").downcase}}}")
192
+ v.gsub!("-", "_")
193
+ }
194
+ end
195
+ data.gsub!("{{", "\#{")
196
+ data.gsub!("}}", "}")
197
+ data.gsub!(/:\s*null/, ": nil")
198
+ rescue
199
+ data='"'+request[:rawModeData].gsub("\r", "") + '"'
200
+ end
201
+ else
202
+ data=""
203
+ variables_on_data=[]
204
+ end
205
+ all_variables_on_path+=variables_on_path
206
+ all_variables_on_path.uniq!
207
+ header=request[:headers].gsub(/\{\{url\}\}/, "").gsub(/\{\{server\}\}/, "").gsub(/\{\{host\}\}/, "").gsub(/https?:\/\//, "").gsub('"','\"')
208
+ #remove headers starting by //
209
+ header.gsub!(/^\/\/.+$/,'')
210
+ #remove empty lines
211
+ header.gsub!(/^\s*$\n/,'')
212
+ variables_on_header=header.scan(/{([\w\-]+)}/)
213
+ variables_on_header.uniq!
214
+ if variables_on_header.size>0 and variables_on_header[0].size>0
215
+ variables_on_header[0].each {|v|
216
+ header.gsub!("{{#{v}}}", "{{#{v.gsub("-", "_")}}}")
217
+ v.gsub!("-", "_")
218
+ }
219
+ end
220
+ header.gsub!("{{", '#{$')
221
+ header.gsub!('}}', '}')
222
+ header.gsub!("\n", '", ')
223
+ header.gsub!(/([^:,\s]+):\s+/, '"\1"=>"')
224
+ header.gsub!(/=>([^,"]+)/, '=>"\1"')
225
+ header.gsub!(/,\s*$/, '')
226
+ header+='"' if header.length>0 and header[-1]!='"'
227
+ all_variables_on_headers+=variables_on_header
228
+ all_variables_on_headers.uniq!
229
+ variables_txt=""
230
+ variables_on_path.each {|var|
231
+ variables_txt+="#{var.join}:$#{var.join}, "
232
+ }
233
+ if variables_on_data.size>0 then
234
+ variables_on_data.each {|var|
235
+ if !variables_on_path.include?(var) then
236
+ if env_vars.keys.include?(var.join) then
237
+ variables_txt+="#{var.join.downcase}:$#{var.join.downcase}, "
238
+ else
239
+ variables_txt+="#{var.join.downcase}:'', "
240
+ end
241
+ end
242
+ }
243
+ end
244
+ variables_txt.chop!
245
+ variables_txt.chop!
246
+ method_name=request[:name].gsub(/\W+/, "_")
247
+ method_name.chop! if method_name[-1]=="_"
248
+ method_name=method_name[0, 1].downcase + method_name[1..-1] #to be sure first character is in downcase
249
+ unless request[:description].to_s==""
250
+ request[:description].to_s.lines.each{|desc|
251
+ rest_methods+="\t\t# #{desc}"
252
+ }
253
+ rest_methods+="\n"
254
+ end
255
+ rest_methods+="\t\tdef self.#{method_name}(#{variables_txt})\n\t\t\treturn {\n"
256
+ rest_methods+="\t\t\t\tpath: \"#{path}\",\n"
257
+ rest_methods+="\t\t\t\theaders: {#{header}},\n"
258
+ if data!="" then
259
+ rest_methods+="\t\t\t\tdata: #{data.split("\n").join("\n\t\t\t\t")}"
260
+ end
261
+ rest_methods+="\n\t\t\t}\n\t\tend\n\n"
262
+ if request[:tests]!="" and defined?(parse_postman_test)
263
+ num_test+=1
264
+ #todo: add here preRequestScript or in the parse_postman_test method, decide
265
+ method_to_call= "RESTRequests"
266
+ method_to_call+="::#{module_main_name}" unless module_main_name.to_s()==""
267
+ method_to_call+="::#{folder}" unless folder.to_s()==""
268
+ method_to_call+=".#{method_name}"
269
+ method_to_call="#{request[:method].downcase}(#{method_to_call})"
270
+ rcode = parse_postman_test(request[:tests].to_s, num_test, method_name, method_to_call)
271
+ tests+=rcode
272
+ end
273
+ }
274
+ rest_methods+="\n \tend\n\n" unless folder==''
275
+ }
276
+ rest_methods+=" end" unless module_main_name.to_s()==""
277
+ rest_methods+="\nend"
278
+ header_vars="# Postman variables\n"
279
+ env_vars.each {|key, value|
280
+ header_vars+=" $#{key.gsub("-", "_").downcase}='#{value}'\n"
281
+ }
282
+ header_vars+="\n\nmodule RESTRequests\n"
283
+ header_vars+="\n module #{module_main_name}\n\n" if module_main_name.to_s()!=""
284
+
285
+ rest_methods= header_vars + rest_methods
286
+
287
+ #Postman dynamic variables
288
+ rest_methods.gsub!(/\$randomInt([^a-zA-Z=])/i, 'rand(1001)\1')
289
+ rest_methods.gsub!(/\$timestamp([^a-zA-Z=])/i, 'Time.now.strftime(\'%Y-%m-%dT%H:%M:%S.000Z\')\1')
290
+ rest_methods.gsub!(/\$guid([^a-zA-Z=])/i, 'SecureRandom.uuid\1')
291
+ mock_vars.each {|mvar, mvalue|
292
+ rest_methods.gsub!(/\$#{mvar}([^a-zA-Z=])/i, mvalue+'\1')
293
+ }
294
+ rest_methods.gsub!(/\s+,$/, ",") #to avoid orphan lines with only one comma
295
+ tests+="end" if defined?(parse_postman_test)
296
+ if output_file_path!=""
297
+ File.open(output_file_path, 'w').write(rest_methods)
298
+ puts "** Requests file: #{output_file_path} that contains the code of the requests and the environment variables after importing the Postman file"
299
+ if defined?(parse_postman_test)
300
+ File.open(test_output_file_path, 'w').write(tests)
301
+ puts "** Tests file: #{test_output_file_path} that contains the code of the tests after importing the Postman file"
302
+ end
303
+ end
304
+
305
+ begin
306
+ eval(rest_methods)
307
+ rescue Exception => stack
308
+ warn "* It seems there was a problem importing the postman file #{postman_collection_file_path}"
309
+ warn stack.to_s()
310
+ end
311
+
312
+ end
@@ -0,0 +1,219 @@
1
+
2
+ module ImportPostman
3
+ module Parsers
4
+ module RestClient
5
+ #can be test-unit or minitest
6
+ IP_TEST_FRAMEWORK='minitest' unless defined?(IP_TEST_FRAMEWORK)
7
+
8
+ def parse_postman_test_headers(module_main_name)
9
+ headers="require 'test-unit'\n" if IP_TEST_FRAMEWORK=='test-unit'
10
+ headers="require 'minitest/autorun'\n" if IP_TEST_FRAMEWORK=='minitest'
11
+ headers+="require 'json'\n"
12
+ headers+="require 'nice_hash'\n"
13
+ headers+="require 'rest-client'\n\n"
14
+
15
+ headers+="
16
+ class String
17
+ def json(*keys)
18
+ require 'json'
19
+ return JSON.parse(self, {symbolize_names: true})
20
+ end
21
+ def ==(par)
22
+ if par.kind_of?(Integer) or par.nil? or par.kind_of?(Float) then
23
+ super(par.to_s())
24
+ else
25
+ super(par)
26
+ end
27
+ end
28
+ end\n\n"
29
+ headers+="class Test#{module_main_name} < Test::Unit::TestCase\n\n" if IP_TEST_FRAMEWORK=='test-unit'
30
+ headers+="class Test#{module_main_name} < Minitest::Test\n\n" if IP_TEST_FRAMEWORK=='minitest'
31
+ headers+="i_suck_and_my_tests_are_order_dependent!\n\n" if IP_TEST_FRAMEWORK=='minitest'
32
+ headers+="\tdef setup\n\t\t"
33
+ headers+='@http=RestClient::Resource.new("http://#{$host}")'
34
+ headers+="\n\tend\n"
35
+ return headers
36
+ end
37
+
38
+ def parse_postman_test(js, num_test, test_name, method_to_call)
39
+ rcode_lines=Array.new
40
+ rcode_txt="\tdef test_#{num_test.to_s.rjust(3, "0")}_#{test_name}\n"
41
+ res=method_to_call.scan(/(\w+)\((.+)\)/)
42
+ met=res[0][0]
43
+ req=res[0][1]
44
+ if met=='post' or met=='put'
45
+ method_to_call="#{met} req[:data].to_json, req.headers"
46
+ else
47
+ method_to_call="#{met} req.headers"
48
+ end
49
+
50
+ rcode_txt+="\t\treq=#{req}\n"
51
+ rcode_txt+="\t\tbegin\n"
52
+ rcode_txt+="\t\t\tresponse=@http[req.path].#{method_to_call}\n"
53
+ rcode_txt+="\t\trescue RestClient::ExceptionWithResponse => res\n"
54
+ rcode_txt+="\t\t\tresponse=res.response\n"
55
+ rcode_txt+="\t\tend\n"
56
+
57
+ var_data=""
58
+ blocks=Array.new
59
+ blocks_add=Array.new
60
+ js.split(/$/).each {|line|
61
+
62
+ line_orig=line.dup
63
+ rcode=""
64
+ line.gsub!("===", "==")
65
+ line.gsub!(/;$/, "")
66
+ line.gsub!('\"','"')
67
+ line.gsub!("responseCode.code", "response.code")
68
+ line.gsub!(" : ", ": ")
69
+ #pm.environment.set("coupon_id_dgrd", jsonData.coupons[0].primaryCouponId)
70
+ if line.scan(/pm\.environment\.set\(\"(\w+)\",\s*(.+)\)/).size>0
71
+ rcode=line.gsub!(/pm\.environment\.set\(\"(\w+)\",\s*(.+)\)/, '$\1 = \2')
72
+ end
73
+ line.gsub!(/environment\["(\w+)"\]/, '$\1')
74
+ line.gsub!(/environment\./, '$')
75
+ line.gsub!(/postman.getEnvironmentVariable\(['"](\w+)['"]\)/, '$\1')
76
+ #postman.getResponseHeader('WWW-Authenticate')
77
+ line.gsub!(/postman\.getResponseHeader\(['"]([\w-]+)['"]\)/, 'response.headers["\1"]')
78
+ line.gsub!(/parseInt\(([\w\$\-]+)\)/, '\1.to_i')
79
+ line.gsub!(".indexOf", ".include?")
80
+ line.gsub!(/\.include\?\((\w+)\)==\s*-1\s*/, '.include?(\1)==false')
81
+ line.gsub!(/\.include\?\((\w+)\)\s*==\s*0/, '.include?(\1)==true')
82
+ line.gsub!("responseBody", "response.body")
83
+ line.gsub!(".has(", ".include?(")
84
+ line.gsub!(/(['"\w\s]+) in (\w+)/, '\1.include?(\2)') if line.scan(/for\s*\(/).size==0
85
+
86
+ #typeof jsonData.guaranteedPrizeData[0].gpData== "string"
87
+ if line.scan(/typeof (.+)\s*==\s*"(\w+)"/).size>0
88
+ res=line.scan(/typeof (.+)\s*==\s*"(\w+)"/)
89
+ var=res[0][0]
90
+ type=res[0][1].capitalize
91
+ line.gsub!(/typeof (.+)\s*==\s*"(\w+)"/, "#{var}.kind_of?(#{type})")
92
+ end
93
+
94
+ if blocks[-1]==:if_one_line and line.strip.scan(/\s*{/).size>0 #'if' is a block not a line
95
+ blocks[-1]=:if_block
96
+ end
97
+
98
+ if line.strip=="" or
99
+ (line.strip=="{" and (blocks[-1]==:if_one_line or blocks[-1]==:if_block or blocks[-1]==:for))
100
+ next
101
+ elsif blocks[-1]==:if_one_line and line.scan(/^\s*if\s+/).size==0 #we are in the next line after if
102
+ rcode="#{line.strip}\n\t\tend"
103
+ blocks.pop
104
+ elsif line.scan(/\s*\/\//).size>0
105
+ rcode="# #{line.scan(/\s*\/\/(.+)/).join}"
106
+ elsif blocks[-1]==:comment
107
+ rcode="# #{line.lstrip}"
108
+ if line.scan(/\s*\*\//).size>0
109
+ blocks.pop
110
+ end
111
+ elsif line.scan(/\s*\/\*/).size>0
112
+ rcode="# #{line.scan(/\s*\/\*(.+)/).join}"
113
+ unless line.scan(/\s*\*\//).size>0
114
+ blocks<<:comment
115
+ end
116
+ elsif line.scan(/^\s*try\s*{/).size>0
117
+ blocks<<:try
118
+ rcode="begin"
119
+ elsif line.strip.scan("}").size>0 and blocks[-1]==:try
120
+ blocks.pop
121
+ rcode=""
122
+ elsif line.scan(/^\s*catch\s*\((\w+)\)\s*{/).size>0
123
+ rcode="rescue Exception =>#{line.scan(/^\s*catch\s*\((\w+)\)\s*{/).join}"
124
+ if line.scan(/}\s*$/).size>0 #ends in one line
125
+ rcode+="\n\t\tend\n"
126
+ end
127
+ blocks<<:catch
128
+ elsif line.strip.scan("}").size>0 and blocks[-1]==:try
129
+ blocks.pop
130
+ rcode=""
131
+ elsif line.strip.scan(/else if\s*/).size>0 and rcode_lines.size>0 and rcode_lines[-1].strip=="end"
132
+ rcode_lines.pop
133
+ rcode=line.gsub("else if","elsif").strip.gsub(/\s*\{$/, '')
134
+ blocks<<:if_block
135
+ elsif line.strip.scan(/else\s*\{/).size>0 and rcode_lines.size>0 and rcode_lines[-1].strip=="end"
136
+ rcode_lines.pop
137
+ rcode="else"
138
+ blocks<<:if_block
139
+ elsif line.scan(/^\s*\w+\.push/).size>0
140
+ #arrayOfValuesFromhighestNumber4Comparing.push(highestNumber4Comparing[j][key])
141
+ rcode=line.strip.gsub(/;\s*$/, "")
142
+ elsif line.scan(/^\s*if\s*/).size>0
143
+ #if ( key === numberCount && numberCount4Comparing[k][key] != jsonData.games[i].properties.numberCount)\n"
144
+ if line.scan(/{\s*$/).size==0 #only one like for the if on next line, not a block
145
+ blocks<<:if_one_line
146
+ else
147
+ blocks<<:if_block
148
+ end
149
+ rcode=line.strip.gsub(/\s*\{$/, '')
150
+ elsif line.strip=="}"
151
+ if blocks[-1]==:for
152
+ rcode="#{blocks_add[blocks.size-1]}\t\tend"
153
+ blocks_add[blocks.size-1]=""
154
+ blocks.pop
155
+ else
156
+ rcode="end"
157
+ blocks_add[blocks.size-1]=""
158
+ blocks.pop
159
+ end
160
+ elsif line.scan(/for\s*\(var (\w+)\s+in/).size>0
161
+ #for(var key in highestNumber4Comparing[j])
162
+ var=line.scan(/for\s*\(var (\w+)\s+in\s+([^\)]+)\)/)
163
+ variab=var[0][0]
164
+ compar=var[0][1]
165
+ #for i in 0..jsonData.games.length-1
166
+ rcode="for #{variab} in #{compar}"
167
+ blocks<<:for
168
+ elsif line.scan(/for\s*\(var (\w+\s*=\s*\d+);([^;]+)/).size>0
169
+ #for(var i=0;i<jsonData.games.length;i++){
170
+ var=line.scan(/for\s*\(var (\w+\s*=\s*\d+);([^;]+)/)
171
+ variab=var[0][0]
172
+ compar=var[0][1]
173
+ rcode="#{variab}\n\t\twhile #{compar}\n"
174
+ blocks<<:for
175
+ blocks_add[blocks.size-1]="\t\t#{variab.scan(/(\w+)\s*=/).join}+=1\n"
176
+ elsif line.scan(/\w\s*;\s*\w/).size>0
177
+ #any line with more than one instruction
178
+ rcode="#Line not added: #{line.lstrip}"
179
+ elsif var_data=="" and line.scan(/var\s(\w+)\s*=\s*JSON\.parse\(response.body\)/).size>0
180
+ var_data = line.scan(/var\s(\w+)\s*=\s*JSON\.parse\(response.body\)/).join
181
+ rcode="#{var_data} = response.body.json"
182
+ elsif line.scan(/postman\.setEnvironmentVariable\(/).size>0
183
+ vart=line.scan(/postman\.setEnvironmentVariable\("(\w+)"/).join
184
+ valt=line.scan(/postman\.setEnvironmentVariable\("\w+",\s*(.+)\)$/).join
185
+ rcode="$#{vart} = #{valt}"
186
+ elsif line.scan(/var \w+\s*=/).size>0
187
+ vart=line.scan(/var\s+(\w+)\s*/).join
188
+ valt=line.scan(/var\s+\w+\s*=\s*(.+)$/).join
189
+ valt[-1]="" if valt[-1]=="," #multiple assigment
190
+ rcode="#{vart} = #{valt}"
191
+ elsif line.scan(/^\s*\w+\s*=\s*/).size>0
192
+ vart=line.scan(/^\s*(\w+)\s*/).join
193
+ valt=line.scan(/^\s*\w+\s*=\s*(.+)$/).join
194
+ valt[-1]="" if valt[-1]=="," #multiple assigment
195
+ rcode="#{vart} = #{valt}"
196
+ elsif line.scan(/tests\[/).size>0
197
+ msg=line.scan(/tests\["([^=]*)"\]/).join
198
+ val=line.scan(/tests\["[^=]*"\]\s*=\s*(.+)/).join
199
+ if msg==""
200
+ msg=line.scan(/tests\[(.*)\]\s=\s/).join
201
+ val=line.scan(/tests\[.*\]\s=\s(.+)/).join
202
+ else
203
+ msg="\"#{msg}\""
204
+ end
205
+ rcode="assert(#{val}, #{msg})"
206
+ elsif rcode==""
207
+ rcode="#Line not added: #{line_orig.lstrip}"
208
+ end
209
+ rcode_lines.push("\t\t#{rcode.to_s.lstrip}\n") unless rcode==""
210
+ }
211
+ rcode_txt+=rcode_lines.join
212
+ rcode_txt+="\tend\n\n"
213
+ return rcode_txt
214
+ end
215
+
216
+ end
217
+
218
+ end
219
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: import_postman
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Mario Ruiz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nice_hash
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ description: This gem imports a postman collection to be used as RequestHash object
34
+ email: marioruizs@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files:
38
+ - LICENSE
39
+ - README.md
40
+ files:
41
+ - LICENSE
42
+ - README.md
43
+ - lib/import_postman.rb
44
+ - lib/import_postman/parsers/rest_client.rb
45
+ homepage: https://github.com/MarioRuiz/import_postman
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.7.6
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: This gem imports a postman collection to be used as RequestHash object
69
+ test_files: []