swagger-to-rbs 0.1.2 → 0.2.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: 4c6bed3da0a9afc4136a9ed5fd605bc9331d1cabecd60dffc687061de701e985
4
- data.tar.gz: b820d03e4f501e6ec75f5756144b1ab67f35b5fce82914704e1c04122e30b71e
3
+ metadata.gz: 651f699800da6263854e5f030b5b2e9084570686b17b92b164319c8c18dbe28f
4
+ data.tar.gz: 23e3253fd8e999fa5042045c9548227e5addd97ed26fe230c0303850665a6d85
5
5
  SHA512:
6
- metadata.gz: 0b405ea2f717d6a2a7a5473dc092b3163c0a7305849d4730d9c0d69e8aa7ebecef3915b0d517f730f141900004ba8dd5124223d606ddac609f807d305fdd9ade
7
- data.tar.gz: 0c38554cbb91292be48311a8ee0e7de88663ef1c0de7e432e08525b9ce2fb308ca302482025ec68fe117231b7cc0aad0859a43d2da17f8b3ac9f2a27ad335703
6
+ metadata.gz: 6ceb2196aaf7cb48385a7c3d200f45a1c28880385ce00da876a6dcb9d19c08ca1a991fbc461e5d026e4276f78266e8f5d3ccc444f26d6fb61c309b9e5edae73b
7
+ data.tar.gz: 27fc3959ff9f6a56846322e03b14e83fd60a96659e006abfe23c6ccb069bd404e4a4a89b0472edc92c4b87415bedf6b1bbad47ef96721a47473adfc1fd3b59c9
data/README.md CHANGED
@@ -11,7 +11,7 @@ curl https://petstore3.swagger.io/api/v3/openapi.json > swagger.json
11
11
  Generate http client based on [Httparty gem](https://github.com/jnunemaker/httparty)
12
12
 
13
13
  ```
14
- ruby lib/index.rb MyApi
14
+ swagger-to-rbs generate --name MyApi --spec swagger.json
15
15
  ```
16
16
 
17
17
  Result:
@@ -0,0 +1,24 @@
1
+ module Swagger2Rbs
2
+ class HashHelper
3
+ def self.set_value(hash, key, value)
4
+ arr = key.split(".")
5
+ last_key = arr.pop()
6
+ hash.dig(*arr)[last_key] = value
7
+ hash
8
+ rescue => e
9
+ hash
10
+ end
11
+
12
+ def self.walk(hash, &block)
13
+ hash.each do |k, v|
14
+ if v.is_a?(Hash)
15
+ walk(v) do |k2, v2|
16
+ yield "#{k}.#{k2}", v2
17
+ end
18
+ else
19
+ yield k, v
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
data/lib/swagger2_rbs.rb CHANGED
@@ -1,12 +1,32 @@
1
1
  require 'json'
2
2
  require 'erb'
3
3
  require_relative 'swagger2_rbs/rest_endpoint'
4
+ require_relative 'swagger2_rbs/hash_helper'
4
5
 
5
6
  module Swagger2Rbs
6
7
 
8
+ def self.resolve_ref(hash, key, value)
9
+ ref_key = value.gsub("#/", "").split("/")
10
+ data = hash.dig(*ref_key)
11
+ update_key = key.split(".").reject{|k| k == "$ref"}.join(".")
12
+
13
+ [update_key, data]
14
+ end
15
+
16
+ def self.resolve_all_ref(swagger_spec)
17
+ new_swagger_spec = swagger_spec.dup
18
+ HashHelper.walk(swagger_spec) do |key, value|
19
+ if key.split(".").last == "$ref"
20
+ update_key, data = resolve_ref(swagger_spec, key, value)
21
+ HashHelper.set_value(new_swagger_spec, update_key, data)
22
+ end
23
+ end
24
+ new_swagger_spec
25
+ end
26
+
7
27
  def self.swagger_to_rest_api(swagger_spec)
8
28
  result = []
9
- swagger_spec["paths"].each do |path, data|
29
+ resolve_all_ref(swagger_spec)["paths"].each do |path, data|
10
30
  data.each do |method, props|
11
31
  rest_data = RestEndpoint.new(path, method, props)
12
32
  result << rest_data.to_h
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.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Savignano
@@ -50,6 +50,7 @@ files:
50
50
  - lib/index.rb
51
51
  - lib/swagger2_rbs.rb
52
52
  - lib/swagger2_rbs/cli.rb
53
+ - lib/swagger2_rbs/hash_helper.rb
53
54
  - lib/swagger2_rbs/rest_endpoint.rb
54
55
  - lib/templates/http_client.rb.erb
55
56
  - lib/templates/http_client.rbs.erb