open_api_import 0.9.2 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fad27ee774275917b7663c7701c7735ec6d8429e26a22c0189ce0eea2a3aa890
4
- data.tar.gz: 98cc63a6ca33f56e01e0b3bdae727b721a5f37ed51d0c82553ccbcacdc7689d9
3
+ metadata.gz: 34a7117aedab4cd987fe9af035ebb047c04439068aba13fdea8431fc872354f4
4
+ data.tar.gz: 3e6517fb5517a848b757e7105306d82a4a5e624771a3a9bfe5835266d42aa336
5
5
  SHA512:
6
- metadata.gz: 1e97a67f6e879e3c4336d91e91c0d616cf2a83c14a633d20145af9b9a891d6102fc59ed81513c81ef643c0d2788f6e2e3ddd67599a6012b829b855f65ff0af19
7
- data.tar.gz: 6692473b740317892ec580b1b9a2e6028a47e69fabe4b156a1fa6ee49610f03ec622363955afd065c1b995dc2d8c786ffe016cff93e765dc416bff23f90cb253
6
+ metadata.gz: c9c355b6f63e6e9b877aea3e58cc3f48d6c6f472fa7e09bd1d3c4044d8bbb4a0275e8d52044f1d8532a8fe7330651709cdb9ad31e89292b2c5e897c41512211d
7
+ data.tar.gz: dfed0eccfe012f5eeaf728f01cacb933c5352368e8811116d7b2d881d781eb3a61fea9035c5354a23cc466b2a299322d30e2dacbd07ef54e7d227173988799f4
data/README.md CHANGED
@@ -12,6 +12,8 @@ The output of this gem will be following the specification of Request Hashes: ht
12
12
 
13
13
  The Request Hashes generated will be able to be used with any Ruby Http Client and it is adapted even better with nice_http gem: https://github.com/MarioRuiz/nice_http
14
14
 
15
+ A beautiful way to access and use the generated files is by using *pry*: https://github.com/pry/pry
16
+
15
17
  To be able to generate random requests take a look at the documentation for nice_hash gem: https://github.com/MarioRuiz/nice_hash
16
18
 
17
19
  This is an example of a generated request hash:
@@ -108,6 +110,7 @@ In case no options supplied:
108
110
  -T, --tags_module It will be used the tags key to create the module name
109
111
  -F, --fixed_module all the requests will be under the module Requests
110
112
  -s, --silent It will display only errors
113
+ -c, --create_constants For required arguments, it will create keyword arguments assigning by default a constant.
111
114
  ```
112
115
 
113
116
 
@@ -487,6 +490,14 @@ It will include this on the output file:
487
490
  ...
488
491
  ```
489
492
 
493
+ ### create_constants
494
+
495
+ The methods will be generated using keyword arguments and for required arguments, it will create keyword arguments assigning by default a constant.
496
+
497
+ ```ruby
498
+ def self.get_products(latitude: LATITUDE, longitude: LONGITUDE)
499
+ ```
500
+
490
501
  ## Contributing
491
502
 
492
503
  Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/open_api_import.
@@ -46,6 +46,10 @@ optparse = OptionParser.new do |opts|
46
46
  options[:silent] = true
47
47
  end
48
48
 
49
+ opts.on("-c", "--create_constants", "For required arguments, it will create keyword arguments assigning by default a constant.") do
50
+ options[:create_constants] = true
51
+ end
52
+
49
53
 
50
54
  end
51
55
 
@@ -18,6 +18,7 @@ class OpenApiImport
18
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
20
  # @param name_for_module [Symbol]. (:path, :path_file, :fixed, :tags, :tags_file) (default: :path). How the module names will be created.
21
+ # @param create_constants [Boolean]. (default: false) For required arguments, it will create keyword arguments assigning by default a constant.
21
22
  # @param silent [Boolean]. (default: false) It will display only errors.
22
23
  # 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.
23
24
  # 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.
@@ -25,7 +26,7 @@ class OpenApiImport
25
26
  # 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.
26
27
  # fixed: all the requests will be under the module Requests
27
28
  ##############################################################################################
28
- def self.from(swagger_file, create_method_name: :operation_id, include_responses: true, mock_response: false, name_for_module: :path, silent: false)
29
+ 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)
29
30
  begin
30
31
  f = File.new("#{swagger_file}_open_api_import.log", "w")
31
32
  f.sync = true
@@ -53,6 +54,7 @@ class OpenApiImport
53
54
  file_errors = file_to_convert + ".errors.log"
54
55
  File.delete(file_errors) if File.exist?(file_errors)
55
56
  import_errors = ""
57
+ required_constants = []
56
58
 
57
59
  begin
58
60
  definition = OasParser::Definition.resolve(swagger_file)
@@ -278,7 +280,12 @@ class OpenApiImport
278
280
  path_txt.gsub!("{#{p[:name]}}", "\#{#{param_name}}")
279
281
  end
280
282
  unless params_path.include?(param_name)
281
- params_path << param_name
283
+ if create_constants
284
+ params_path << "#{param_name}: #{param_name.upcase}"
285
+ required_constants << param_name.upcase
286
+ else
287
+ params_path << param_name
288
+ end
282
289
  #params_required << param_name if p[:required].to_s=="true"
283
290
  description_parameters << "# #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}"
284
291
  end
@@ -400,13 +407,27 @@ class OpenApiImport
400
407
  unless params_query.empty?
401
408
  path_txt += "?"
402
409
  params_required.each do |pr|
403
- if params_query.include?(pr)
404
- if create_method_name == :operationId
405
- path_txt += "#{pr}=\#{#{pr}}&"
406
- params << "#{pr}"
407
- else
408
- path_txt += "#{pr}=\#{#{pr.to_s.snake_case}}&"
409
- params << "#{pr.to_s.snake_case}"
410
+ if create_constants
411
+ if params_query.include?(pr)
412
+ if create_method_name == :operationId
413
+ path_txt += "#{pr}=\#{#{pr}}&"
414
+ params << "#{pr}: #{pr.upcase}"
415
+ required_constants << pr.upcase
416
+ else
417
+ path_txt += "#{pr}=\#{#{pr.to_s.snake_case}}&"
418
+ params << "#{pr.to_s.snake_case}: #{pr.to_s.snake_case.upcase}"
419
+ required_constants << pr.to_s.snake_case.upcase
420
+ end
421
+ end
422
+ else
423
+ if params_query.include?(pr)
424
+ if create_method_name == :operationId
425
+ path_txt += "#{pr}=\#{#{pr}}&"
426
+ params << "#{pr}"
427
+ else
428
+ path_txt += "#{pr}=\#{#{pr.to_s.snake_case}}&"
429
+ params << "#{pr.to_s.snake_case}"
430
+ end
410
431
  end
411
432
  end
412
433
  end
@@ -435,7 +456,12 @@ class OpenApiImport
435
456
  paramst = []
436
457
  prms = path_txt.scan(/[^#]{(\w+)}/)
437
458
  prms.each do |p|
438
- paramst<<p[0].to_s.snake_case
459
+ #if create_constants
460
+ # paramst<<"#{p[0].to_s.snake_case}: #{p[0].to_s.snake_case.upcase}"
461
+ # required_constants << p[0].to_s.snake_case.upcase
462
+ #else
463
+ paramst<<p[0].to_s.snake_case
464
+ #end
439
465
  path_txt.gsub!("{#{p[0]}}", "\#{#{p[0].to_s.snake_case}}")
440
466
  end
441
467
  paramst.concat params
@@ -547,7 +573,6 @@ class OpenApiImport
547
573
  end
548
574
  end
549
575
  end
550
-
551
576
  output_footer = []
552
577
 
553
578
  output_footer << "end" unless (module_requests == "") && ([:path, :path_file, :tags, :tags_file].include?(name_for_module))
@@ -589,7 +614,18 @@ class OpenApiImport
589
614
  end
590
615
 
591
616
  requests_file_path = file_to_convert + ".rb"
592
- File.open(requests_file_path, "w") { |file| file.write(requires_txt) }
617
+ if required_constants.size > 0
618
+ rconsts = "# Required constants\n"
619
+ required_constants.uniq!
620
+ required_constants.each do |rq|
621
+ rconsts += "#{rq} ||= ENV['#{rq}'] ||=''\n"
622
+ end
623
+ rconsts += "\n\n"
624
+ else
625
+ rconsts = ''
626
+ end
627
+
628
+ File.open(requests_file_path, "w") { |file| file.write(rconsts + requires_txt) }
593
629
  res_rufo = `rufo #{requests_file_path}`
594
630
  message = "** File that contains all the requires for all Request files: \n"
595
631
  message += " - #{requests_file_path} "
@@ -801,6 +837,7 @@ class OpenApiImport
801
837
  rs.gsub!(/@(\w+):/,'\'@\1\':')
802
838
  end
803
839
  end
840
+
804
841
  return response_example
805
842
  end
806
843
 
@@ -871,7 +908,7 @@ class OpenApiImport
871
908
  data_pattern << "'#{dpk}': #{dpv[:minimum]}..#{dpv[:maximum]}"
872
909
  elsif dpv.key?(:minimum) and !dpv.key?(:maximum)
873
910
  if RUBY_VERSION >= '2.6.0'
874
- data_pattern << "#'#{dpk}': #{dpv[:minimum]}.. # Disabled until bug on rufo gem is fixed"
911
+ data_pattern << "'#{dpk}': #{dpv[:minimum]}.. "
875
912
  else
876
913
  data_pattern << "#'#{dpk}': #{dpv[:minimum]}.. # INFINITE only working on ruby>=2.6.0"
877
914
  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.9.2
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-25 00:00:00.000000000 Z
11
+ date: 2020-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oas_parser
@@ -34,22 +34,22 @@ dependencies:
34
34
  name: rufo
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '0.7'
40
37
  - - ">="
41
38
  - !ruby/object:Gem::Version
42
- version: 0.7.0
39
+ version: 0.11.0
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '0.11'
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.7'
50
47
  - - ">="
51
48
  - !ruby/object:Gem::Version
52
- version: 0.7.0
49
+ version: 0.11.0
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '0.11'
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: nice_hash
55
55
  requirement: !ruby/object:Gem::Requirement
@@ -59,7 +59,7 @@ dependencies:
59
59
  version: '1.15'
60
60
  - - ">="
61
61
  - !ruby/object:Gem::Version
62
- version: 1.15.3
62
+ version: 1.15.4
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
@@ -69,27 +69,27 @@ dependencies:
69
69
  version: '1.15'
70
70
  - - ">="
71
71
  - !ruby/object:Gem::Version
72
- version: 1.15.3
72
+ version: 1.15.4
73
73
  - !ruby/object:Gem::Dependency
74
74
  name: rspec
75
75
  requirement: !ruby/object:Gem::Requirement
76
76
  requirements:
77
- - - "~>"
78
- - !ruby/object:Gem::Version
79
- version: '3.8'
80
77
  - - ">="
81
78
  - !ruby/object:Gem::Version
82
79
  version: 3.8.0
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.8'
83
83
  type: :development
84
84
  prerelease: false
85
85
  version_requirements: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '3.8'
90
87
  - - ">="
91
88
  - !ruby/object:Gem::Version
92
89
  version: 3.8.0
90
+ - - "~>"
91
+ - !ruby/object:Gem::Version
92
+ version: '3.8'
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: coveralls
95
95
  requirement: !ruby/object:Gem::Requirement
@@ -146,8 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  - !ruby/object:Gem::Version
147
147
  version: '0'
148
148
  requirements: []
149
- rubyforge_project:
150
- rubygems_version: 2.7.6
149
+ rubygems_version: 3.0.3
151
150
  signing_key:
152
151
  specification_version: 4
153
152
  summary: OpenApiImport -- Import a Swagger or Open API file and create a Ruby Request