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 +4 -4
- data/lib/swagger2_rbs/cli.rb +13 -0
- data/lib/swagger2_rbs/hash_helper.rb +17 -0
- data/lib/swagger2_rbs/rest_endpoint.rb +2 -1
- data/lib/swagger2_rbs.rb +9 -2
- data/lib/templates/http_client.rb.erb +1 -2
- data/lib/templates/http_client.rbs.erb +7 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8ac0ad5697156071c8422296d55ea3eb34bc39e8359b8eb3b6d796a2be4d298
|
4
|
+
data.tar.gz: 2b0172153de41682a387d9909b0db5bec00cbf4f0708b4b5cdf6728289ca8d16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae3b412fa2e72e7af7d66740b68ac9ecd107a479719e80354cfb1a6896e2fa693688c65017c6c9bb9dd4492c01171d4c6ee9a5cdbe9687078abcac036e58f83e
|
7
|
+
data.tar.gz: 458ed2649c08a5019d2491887996869b3d6f7d1f701193aff13264a0aec782035ff10a049609d2a8ab15d32973ce50c8d10c3db979c9b9b37bf93367bfc40590
|
data/lib/swagger2_rbs/cli.rb
CHANGED
@@ -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
|
-
|
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.
|
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|
|
@@ -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
|
|