open_api_import 0.8.8 → 0.8.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +8 -1
- data/lib/open_api_import.rb +53 -29
- metadata +22 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 563a76eeef59c666b1e77ed1efbfefb456119338fa14a94aa1c6f51eef4b0bb0
|
4
|
+
data.tar.gz: a6ef472adda72c06d37ec444c693ba7eb7c15dcedfd3754eb488f397ee130098
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aea7ea0650e23d9010fa08627409e9579ebdc4295209362844142d9a128ae945d483875ce6ea01ba2f67b193c712fc2742806caccd378f9d8b2830ea4dcbd345
|
7
|
+
data.tar.gz: 4a67757d85aafbf2e5ae9d41db2f186ffb67cc241585c1766c1e92a5fe05bfb0cde9f46871a3d979a7f952390702447e42862e00fc1d8cbd6e1905fa1aeb76d6
|
data/.yardopts
CHANGED
@@ -2,4 +2,11 @@
|
|
2
2
|
--title 'open_api_import - OpenApiImport -- Import a swagger or Open API file and create a Ruby Request Hash file including all requests and responses'
|
3
3
|
--charset utf-8
|
4
4
|
--markup markdown
|
5
|
-
|
5
|
+
--exclude features
|
6
|
+
--no-private
|
7
|
+
--default-return void
|
8
|
+
|
9
|
+
lib/*.rb
|
10
|
+
lib/open_api_import/*.rb
|
11
|
+
-
|
12
|
+
LICENSE
|
data/lib/open_api_import.rb
CHANGED
@@ -7,21 +7,21 @@ require "logger"
|
|
7
7
|
|
8
8
|
class OpenApiImport
|
9
9
|
##############################################################################################
|
10
|
-
# Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses.
|
10
|
+
# Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses.
|
11
11
|
# The http methods that will be treated are: 'get','post','put','delete', 'patch'.
|
12
12
|
# @param swagger_file [String]. Path and file name. Could be absolute or relative to project root folder.
|
13
13
|
# @param include_responses [Boolean]. (default: true) if you want to add the examples of responses in the resultant file.
|
14
|
-
# @param mock_response [Boolean]. (default:false) Add the first response on the request as mock_response to be used.
|
14
|
+
# @param mock_response [Boolean]. (default:false) Add the first response on the request as mock_response to be used.
|
15
15
|
# In case using nice_http gem: if NiceHttp.use_mocks = true will use it instead of getting the real response from the WS.
|
16
|
-
# @param create_method_name [Symbol]. (:path, :operation_id, :operationId) (default: operation_id). How the name of the methods will be generated.
|
17
|
-
# 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
|
18
|
-
# operation_id: it will be used the operationId field but using the snake_case version, for example for listUsers: list_users
|
16
|
+
# @param create_method_name [Symbol]. (:path, :operation_id, :operationId) (default: operation_id). How the name of the methods will be generated.
|
17
|
+
# 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
|
18
|
+
# operation_id: it will be used the operationId field but using the snake_case version, for example for listUsers: list_users
|
19
19
|
# operationId: it will be used the operationId field like it is, for example: listUsers
|
20
|
-
# @param name_for_module [Symbol]. (:path, :path_file, :fixed, :tags, :tags_file) (default: :path). How the module names will be created.
|
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
|
-
# 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.
|
20
|
+
# @param name_for_module [Symbol]. (:path, :path_file, :fixed, :tags, :tags_file) (default: :path). How the module names will be created.
|
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
|
+
# 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.
|
25
25
|
# fixed: all the requests will be under the module Requests
|
26
26
|
##############################################################################################
|
27
27
|
def self.from(swagger_file, create_method_name: :operation_id, include_responses: true, mock_response: false, name_for_module: :path)
|
@@ -29,6 +29,7 @@ class OpenApiImport
|
|
29
29
|
f = File.new("#{swagger_file}_open_api_import.log", "w")
|
30
30
|
f.sync = true
|
31
31
|
@logger = Logger.new f
|
32
|
+
puts "Logs file: #{swagger_file}_open_api_import.log"
|
32
33
|
rescue StandardError => e
|
33
34
|
warn "Not possible to create the Logger file"
|
34
35
|
warn e
|
@@ -126,9 +127,11 @@ class OpenApiImport
|
|
126
127
|
params_path = []
|
127
128
|
params_query = []
|
128
129
|
params_required = []
|
130
|
+
params_data = []
|
129
131
|
description_parameters = []
|
130
132
|
data_form = []
|
131
133
|
data_required = []
|
134
|
+
#todo: add nested one.true.three to data_read_only
|
132
135
|
data_read_only = []
|
133
136
|
data_default = []
|
134
137
|
data_examples = []
|
@@ -137,6 +140,7 @@ class OpenApiImport
|
|
137
140
|
|
138
141
|
# for the case operationId is missing
|
139
142
|
cont[:operationId] = "undefined" unless cont.key?(:operationId)
|
143
|
+
|
140
144
|
if create_method_name == :path
|
141
145
|
method_name = (met.to_s + "_" + path.path.to_s).snake_case
|
142
146
|
method_name.chop! if method_name[-1] == "_"
|
@@ -415,6 +419,7 @@ class OpenApiImport
|
|
415
419
|
end
|
416
420
|
end
|
417
421
|
end
|
422
|
+
|
418
423
|
end
|
419
424
|
|
420
425
|
if description_parameters.size > 0
|
@@ -433,7 +438,7 @@ class OpenApiImport
|
|
433
438
|
paramst.concat params
|
434
439
|
params = paramst
|
435
440
|
end
|
436
|
-
|
441
|
+
|
437
442
|
output << "def self.#{method_name} (#{params.join(", ")})"
|
438
443
|
|
439
444
|
output << "{"
|
@@ -446,12 +451,12 @@ class OpenApiImport
|
|
446
451
|
|
447
452
|
unless data_required.empty?
|
448
453
|
output << "data_required: ["
|
449
|
-
output << "
|
454
|
+
output << ":'#{data_required.uniq.join("', :'")}'"
|
450
455
|
output << "],"
|
451
456
|
end
|
452
457
|
unless data_read_only.empty?
|
453
458
|
output << "data_read_only: ["
|
454
|
-
output << "
|
459
|
+
output << ":'#{data_read_only.uniq.join("', :'")}'"
|
455
460
|
output << "],"
|
456
461
|
end
|
457
462
|
unless data_default.empty?
|
@@ -478,7 +483,11 @@ class OpenApiImport
|
|
478
483
|
rescue
|
479
484
|
data_ex = {}
|
480
485
|
end
|
481
|
-
|
486
|
+
if (data_required.grep(/\./)).empty?
|
487
|
+
reqdata = filter(data_ex, data_required) #not nested
|
488
|
+
else
|
489
|
+
reqdata = filter(data_ex, data_required, true) #nested
|
490
|
+
end
|
482
491
|
unless reqdata.empty?
|
483
492
|
phsd = pretty_hash_symbolized(reqdata)
|
484
493
|
phsd[0]="data: {"
|
@@ -546,10 +555,12 @@ class OpenApiImport
|
|
546
555
|
output_txt = output.join("\n")
|
547
556
|
requests_file_path = file_to_convert + ".rb"
|
548
557
|
File.open(requests_file_path, "w") { |file| file.write(output_txt) }
|
549
|
-
`rufo #{requests_file_path}`
|
558
|
+
res_rufo = `rufo #{requests_file_path}`
|
550
559
|
message = "** Requests file: #{swagger_file}.rb that contains the code of the requests after importing the Swagger file"
|
551
560
|
puts message
|
552
561
|
@logger.info message
|
562
|
+
@logger.error " Error formating with rufo" unless res_rufo.to_s.match?(/\AFormat:.+$\s*\z/)
|
563
|
+
@logger.error " Syntax Error: #{`ruby -c #{requests_file_path}`}" unless `ruby -c #{requests_file_path}`.include?("Syntax OK")
|
553
564
|
else
|
554
565
|
unless files.key?(module_requests)
|
555
566
|
files[module_requests] = Array.new
|
@@ -566,19 +577,23 @@ class OpenApiImport
|
|
566
577
|
requests_file_path = file_to_convert + "_" + mod + ".rb"
|
567
578
|
requires_txt += "require_relative '#{File.basename(swagger_file)}_#{mod}'\n"
|
568
579
|
File.open(requests_file_path, "w") { |file| file.write(output_txt) }
|
569
|
-
`rufo #{requests_file_path}`
|
580
|
+
res_rufo = `rufo #{requests_file_path}`
|
570
581
|
message = " - #{requests_file_path}"
|
571
582
|
puts message
|
572
583
|
@logger.info message
|
573
|
-
|
584
|
+
@logger.error " Error formating with rufo" unless res_rufo.to_s.match?(/\AFormat:.+$\s*\z/)
|
585
|
+
@logger.error " Syntax Error: #{`ruby -c #{requests_file_path}`}" unless `ruby -c #{requests_file_path}`.include?("Syntax OK")
|
586
|
+
end
|
574
587
|
|
575
588
|
requests_file_path = file_to_convert + ".rb"
|
576
589
|
File.open(requests_file_path, "w") { |file| file.write(requires_txt) }
|
577
|
-
`rufo #{requests_file_path}`
|
590
|
+
res_rufo = `rufo #{requests_file_path}`
|
578
591
|
message = "** File that contains all the requires for all Request files: \n"
|
579
592
|
message += " - #{requests_file_path} "
|
580
593
|
puts message
|
581
594
|
@logger.info message
|
595
|
+
@logger.error " Error formating with rufo" unless res_rufo.to_s.match?(/\AFormat:.+$\s*\z/)
|
596
|
+
@logger.error " Syntax Error: #{`ruby -c #{requests_file_path}`}" unless `ruby -c #{requests_file_path}`.include?("Syntax OK")
|
582
597
|
end
|
583
598
|
|
584
599
|
begin
|
@@ -822,7 +837,7 @@ class OpenApiImport
|
|
822
837
|
body[:properties][key].key?(:required) and body[:properties][key][:required].size>0
|
823
838
|
dr = get_required_data(body[:properties][key])
|
824
839
|
dr.each do |k|
|
825
|
-
data_required.push({key
|
840
|
+
data_required.push("#{key}.#{k}".to_sym)
|
826
841
|
end
|
827
842
|
end
|
828
843
|
end
|
@@ -892,19 +907,28 @@ class OpenApiImport
|
|
892
907
|
end
|
893
908
|
|
894
909
|
#filter hash
|
895
|
-
|
910
|
+
def filter(hash, keys, nested = false)
|
896
911
|
result = {}
|
897
912
|
keys = [keys] unless keys.is_a?(Array)
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
913
|
+
if nested
|
914
|
+
result = hash.nice_filter(keys)
|
915
|
+
else
|
916
|
+
#to be backwards compatible
|
917
|
+
keys.each do |k|
|
918
|
+
if k.is_a?(Symbol) and hash.key?(k)
|
919
|
+
if hash[k].is_a?(Hash)
|
920
|
+
result[k] = {}
|
921
|
+
else
|
922
|
+
result[k] = hash[k]
|
923
|
+
end
|
924
|
+
elsif k.is_a?(Symbol) and k.to_s.include?('.') and hash.key?((k.to_s.scan(/(\w+)\./).join).to_sym) #nested 'uno.dos.tres
|
925
|
+
kn = k.to_s.split('.')
|
926
|
+
vn = kn[1].to_sym
|
927
|
+
result[kn.first.to_sym][vn] = filter(hash[kn.first.to_sym], vn).values[0]
|
928
|
+
elsif k.is_a?(Hash) and hash.key?(k.keys[0]) #nested {uno: {dos: :tres}}
|
929
|
+
result[k.keys[0]][k.values[0]] = filter(hash[k.keys[0]], k.values[0]).values[0]
|
930
|
+
end
|
904
931
|
end
|
905
|
-
elsif k.is_a?(Hash) and hash.key?(k.keys[0])
|
906
|
-
result[k.keys[0]][k.values[0]] = filter(hash[k.keys[0]], k.values[0]).values[0]
|
907
|
-
end
|
908
932
|
end
|
909
933
|
return result
|
910
934
|
end
|
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.8.
|
4
|
+
version: 0.8.9
|
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-
|
11
|
+
date: 2019-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oas_parser
|
@@ -16,60 +16,60 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.19.0
|
20
20
|
- - "~>"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '0.
|
22
|
+
version: '0.19'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 0.
|
29
|
+
version: 0.19.0
|
30
30
|
- - "~>"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '0.
|
32
|
+
version: '0.19'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: rufo
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "~>"
|
38
|
-
- !ruby/object:Gem::Version
|
39
|
-
version: '0.4'
|
40
37
|
- - ">="
|
41
38
|
- !ruby/object:Gem::Version
|
42
|
-
version: 0.
|
39
|
+
version: 0.7.0
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0.7'
|
43
43
|
type: :runtime
|
44
44
|
prerelease: false
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
46
46
|
requirements:
|
47
|
-
- - "~>"
|
48
|
-
- !ruby/object:Gem::Version
|
49
|
-
version: '0.4'
|
50
47
|
- - ">="
|
51
48
|
- !ruby/object:Gem::Version
|
52
|
-
version: 0.
|
49
|
+
version: 0.7.0
|
50
|
+
- - "~>"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0.7'
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
54
|
name: nice_hash
|
55
55
|
requirement: !ruby/object:Gem::Requirement
|
56
56
|
requirements:
|
57
|
-
- - ">="
|
58
|
-
- !ruby/object:Gem::Version
|
59
|
-
version: 1.14.0
|
60
57
|
- - "~>"
|
61
58
|
- !ruby/object:Gem::Version
|
62
|
-
version: '1.
|
59
|
+
version: '1.15'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 1.15.3
|
63
63
|
type: :runtime
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- - ">="
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version: 1.14.0
|
70
67
|
- - "~>"
|
71
68
|
- !ruby/object:Gem::Version
|
72
|
-
version: '1.
|
69
|
+
version: '1.15'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 1.15.3
|
73
73
|
- !ruby/object:Gem::Dependency
|
74
74
|
name: rspec
|
75
75
|
requirement: !ruby/object:Gem::Requirement
|