swagger-to-rbs 0.3.0 → 0.4.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: 4609330c97363e8fc3f14d202f55904f026124a77a13c9e9bfcf8728a75aab42
4
- data.tar.gz: 4ebb0337d6d72947aa3ce27ce4e2d8a99cbc63eb7f33a8015a3dccacb668696a
3
+ metadata.gz: f8ac0ad5697156071c8422296d55ea3eb34bc39e8359b8eb3b6d796a2be4d298
4
+ data.tar.gz: 2b0172153de41682a387d9909b0db5bec00cbf4f0708b4b5cdf6728289ca8d16
5
5
  SHA512:
6
- metadata.gz: 7899e345942d1014a68dccf6a866295a2afafefabc80d4a2abc439a15ab4f6bb35d7ab708f3ef4eb7b548602902b66c850d015fae56b3b4efdc7ab3c4dca5781
7
- data.tar.gz: 58a8e1db7ae6b58d0e3e0611c4e4b6346e3871ee5556f3f01018d812eb2027703d2bfa3733f63c346304cfbd322b68ec316c8ecb031dfdb356be8a3075a9157a
6
+ metadata.gz: ae3b412fa2e72e7af7d66740b68ac9ecd107a479719e80354cfb1a6896e2fa693688c65017c6c9bb9dd4492c01171d4c6ee9a5cdbe9687078abcac036e58f83e
7
+ data.tar.gz: 458ed2649c08a5019d2491887996869b3d6f7d1f701193aff13264a0aec782035ff10a049609d2a8ab15d32973ce50c8d10c3db979c9b9b37bf93367bfc40590
@@ -22,6 +22,19 @@ module Swagger2Rbs
22
22
  File.write("#{file_name}.rbs", ::Swagger2Rbs.generate_rbs(name, data.dup))
23
23
  end
24
24
 
25
+ desc 'genearte-yaml', 'generate yaml file'
26
+ option :name, desc: 'Name'
27
+ option :spec, desc: 'Swagger file path'
28
+ def generate_yaml
29
+ @name = options[:name]
30
+ @swagger_path = options[:spec]
31
+ @debug = options[:debug]
32
+ swagger_spec = JSON.parse(File.read(@swagger_path))
33
+
34
+ file_name = to_underscore(name)
35
+ File.write("#{file_name}.yaml", ::Swagger2Rbs.swagger_to_rest_api_yaml(swagger_spec))
36
+ end
37
+
25
38
  private
26
39
  def fetch_data
27
40
  if swagger_path
@@ -1,5 +1,22 @@
1
1
  module Swagger2Rbs
2
2
  class HashHelper
3
+
4
+ # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/hash/keys.rb#L129
5
+ def self.deep_transform_keys_in_object!(object, &block)
6
+ case object
7
+ when Hash
8
+ object.keys.each do |key|
9
+ value = object.delete(key)
10
+ object[yield(key)] = deep_transform_keys_in_object!(value, &block)
11
+ end
12
+ object
13
+ when Array
14
+ object.map! { |e| deep_transform_keys_in_object!(e, &block) }
15
+ else
16
+ object
17
+ end
18
+ end
19
+
3
20
  def self.set_value(hash, key, value)
4
21
  arr = key.split(".")
5
22
  last_key = arr.pop()
@@ -34,9 +34,10 @@ module Swagger2Rbs
34
34
  {
35
35
  path: path,
36
36
  method: method,
37
- parameters: parameters,
37
+ path_parameters: parameters,
38
38
  method_name: method_name,
39
39
  body: body,
40
+ response: response_typed,
40
41
  }
41
42
  rescue => e
42
43
  raise e, "Context: #{path} #{method} Message: #{e.message}"
data/lib/swagger2_rbs.rb CHANGED
@@ -24,18 +24,25 @@ module Swagger2Rbs
24
24
  new_swagger_spec
25
25
  end
26
26
 
27
- def self.swagger_to_rest_api(swagger_spec)
27
+ def self.swagger_to_rest_api(swagger_spec, parse_method = :to_h)
28
28
  result = []
29
29
  resolve_all_ref(swagger_spec)["paths"].each do |path, data|
30
30
  data.each do |method, props|
31
31
  rest_data = RestEndpoint.new(path, method, props)
32
- result << rest_data.to_h
32
+ result << rest_data.send(parse_method)
33
33
  end
34
34
  end
35
35
 
36
36
  { base_uri: swagger_spec["servers"].first["url"], endpoints: result }
37
37
  end
38
38
 
39
+ def self.swagger_to_rest_api_yaml(swagger_spec)
40
+ response = swagger_to_rest_api(swagger_spec, :to_yaml)
41
+ YAML.dump(
42
+ HashHelper.deep_transform_keys_in_object!(response, &:to_s)
43
+ ).gsub("---\n", "")
44
+ end
45
+
39
46
  def self.rest_api_all(spec)
40
47
  result = []
41
48
  spec[:endpoints].each do |endpoint|
@@ -1,5 +1,4 @@
1
-
2
- require 'httparty'
1
+ # require 'httparty'
3
2
  require 'json'
4
3
 
5
4
  class <%= @module_name %>
@@ -10,6 +10,13 @@ end
10
10
  class <%= @module_name %>
11
11
  include HTTParty
12
12
 
13
+ def self.headers: (untyped headers) -> void
14
+ def self.base_uri: (String uri) -> void
15
+ def self.get: (String path, untyped options) -> void
16
+ def self.post: (String path, untyped options) -> void
17
+ def self.put: (String path, untyped options) -> void
18
+ def self.delete: (String path, untyped options) -> void
19
+
13
20
  def initialize: -> void
14
21
  <%- @data[:endpoints].each do |endpoint| -%>
15
22
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagger-to-rbs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Savignano