dyn-api-gen 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +21 -0
- data/dyn-api-gen.gemspec +20 -0
- data/lib/dyn_api_gen.rb +11 -0
- data/lib/dyn_api_gen/generator.rb +61 -0
- data/lib/dyn_api_gen/parameter.rb +20 -0
- data/lib/dyn_api_gen/request.rb +48 -0
- data/rakefile +8 -0
- data/test/helper.rb +10 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 111b28326bb3103ebbe599f9aff1c0954dc043772493b72d473f7c5dd7a280be
|
4
|
+
data.tar.gz: b29bc2a3e7444a41981ee241d8fd96270ffab3bc1fa324b0dd7b3838e47570e3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 40fb530f33a34df9330de1aec000c5b4663f67ffea43855afd018257ec51fedb11db0e1cca78ff3bf9a312886354729f7b0784b02a4a1b110c5bffbba201c013
|
7
|
+
data.tar.gz: 6bee1fbbdcdefd82197db66bc9431103999d75ded12e957f459ccd273f9458170b5e7a8597783fd752fb6c791314beb9e63d211c70d756f7674cb541ca552a53
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# dyn-api-gen
|
2
|
+
Dynamic API client generator. Aims to follow OpenAPI + JSON API standards
|
3
|
+
|
4
|
+
|
5
|
+
# Usage (WIP)
|
6
|
+
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
ApiClient = DynApiGen::Generator.generate('./swagger/v1/openapi.yaml')
|
10
|
+
|
11
|
+
request =
|
12
|
+
EmancuApiClient::SalesmatchingMakeAliases
|
13
|
+
.delete_salesmatching_make_alias
|
14
|
+
.with_headers
|
15
|
+
.with_dealership_id(5)
|
16
|
+
.with_page_size(4)
|
17
|
+
.with_filter_deal_type('some,valid,filters')
|
18
|
+
|
19
|
+
# EmancuApiClient.send_request(request, with_jwt: '123')
|
20
|
+
|
21
|
+
```
|
data/dyn-api-gen.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'dyn-api-gen'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = Time.now.strftime('%Y-%m-%d')
|
5
|
+
s.summary = 'Dynamic API client generator. Aims to follow OpenAPI + JSON API standards'
|
6
|
+
s.authors = ['Emiliano Mancuso']
|
7
|
+
s.email = ['emiliano.mancuso@gmail.com', 'developers@carwow.co.uk']
|
8
|
+
s.homepage = 'http://github.com/carwow/dyn-api-gen'
|
9
|
+
s.license = 'MIT'
|
10
|
+
|
11
|
+
s.files = Dir[
|
12
|
+
'README.md',
|
13
|
+
'rakefile',
|
14
|
+
'lib/**/*.rb',
|
15
|
+
'*.gemspec'
|
16
|
+
]
|
17
|
+
s.test_files = Dir['test/*.*']
|
18
|
+
|
19
|
+
s.add_development_dependency 'test-unit', '~> 3.3'
|
20
|
+
end
|
data/lib/dyn_api_gen.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DynApiGen
|
4
|
+
class Generator
|
5
|
+
RequestDefinition = Struct.new(:namespace, :name, :verb, :path, :parameters)
|
6
|
+
|
7
|
+
def initialize(openapi_path)
|
8
|
+
@openapi_path = openapi_path
|
9
|
+
@module = Module.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.generate(path)
|
13
|
+
new(path).generate
|
14
|
+
end
|
15
|
+
|
16
|
+
def generate
|
17
|
+
requests = []
|
18
|
+
|
19
|
+
openapi['paths'].each do |path, endpoints|
|
20
|
+
endpoints.each do |verb, detail|
|
21
|
+
name = to_underscore(detail.fetch('operationId'))
|
22
|
+
parameters = detail.fetch('parameters').map(&Parameter.method(:new))
|
23
|
+
|
24
|
+
detail.fetch('tags').each do |namespace|
|
25
|
+
requests << RequestDefinition.new(to_camelcase(namespace), name, verb, path, parameters)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
requests.group_by(&:namespace).each do |namespace, req_definitions|
|
31
|
+
namespace_module = Module.new
|
32
|
+
puts namespace
|
33
|
+
req_definitions.each do |req_def|
|
34
|
+
puts "> #{req_def.name}"
|
35
|
+
namespace_module.define_singleton_method(req_def.name) do
|
36
|
+
Request.new(req_def.to_h)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
@module.send(:const_set, namespace, namespace_module)
|
41
|
+
# @module.const_set(namespace, namespace_module)
|
42
|
+
end
|
43
|
+
|
44
|
+
@module
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def openapi
|
50
|
+
@openapi ||= YAML.load_file(@openapi_path)
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_underscore(str)
|
54
|
+
str.gsub(/(.)([A-Z])/, '\1_\2').downcase
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_camelcase(str)
|
58
|
+
str.split('_').map(&:capitalize).join
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DynApiGen
|
4
|
+
class Parameter
|
5
|
+
attr_reader :name, :in, :required, :default
|
6
|
+
|
7
|
+
def initialize(openapi_definition)
|
8
|
+
@name, @in, @required = openapi_definition.fetch_values('name', 'in', 'required')
|
9
|
+
@default = openapi_definition['default']
|
10
|
+
end
|
11
|
+
|
12
|
+
def header?
|
13
|
+
@in == 'header'
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_method_name
|
17
|
+
@name.gsub(/(\[|\])/, '[' => '_', ']' => '').downcase
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DynApiGen
|
4
|
+
class Request
|
5
|
+
def initialize(**kwargs)
|
6
|
+
@name, @verb, @path, @parameters =
|
7
|
+
kwargs.fetch_values(:name, :verb, :path, :parameters)
|
8
|
+
|
9
|
+
@with_params = {}
|
10
|
+
@with_headers = {}
|
11
|
+
|
12
|
+
create_with_methods!
|
13
|
+
fill_default_headers
|
14
|
+
fill_default_params
|
15
|
+
end
|
16
|
+
|
17
|
+
# Setthe headers to be used. It merges whatever is defined
|
18
|
+
def with_headers(new_headers)
|
19
|
+
@with_headers.merge!(new_headers)
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# This method will generate the `with_*` instance methods dynamically,
|
26
|
+
# to set the params needed by this request.
|
27
|
+
def create_with_methods!
|
28
|
+
@parameters.reject(&:header?).each do |parameter|
|
29
|
+
self.class.define_method(:"with_#{parameter.to_method_name}") do |value|
|
30
|
+
@with_params[parameter.name] = value
|
31
|
+
self
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def fill_default_headers
|
37
|
+
@parameters.select { |p| p.header? && p.default }.each do |parameter|
|
38
|
+
@with_headers[parameter.name] = parameter.default
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def fill_default_params
|
43
|
+
@parameters.select { |p| !p.header? && p.default }.each do |parameter|
|
44
|
+
@with_params[parameter.name] = parameter.default
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/rakefile
ADDED
data/test/helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'dyn_api_gen'
|
6
|
+
require 'test/unit'
|
7
|
+
|
8
|
+
def reject(condition, message="Expected condition to be unsatisfied")
|
9
|
+
assert !condition, message
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dyn-api-gen
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Emiliano Mancuso
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-07-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: test-unit
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.3'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- emiliano.mancuso@gmail.com
|
30
|
+
- developers@carwow.co.uk
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- README.md
|
36
|
+
- dyn-api-gen.gemspec
|
37
|
+
- lib/dyn_api_gen.rb
|
38
|
+
- lib/dyn_api_gen/generator.rb
|
39
|
+
- lib/dyn_api_gen/parameter.rb
|
40
|
+
- lib/dyn_api_gen/request.rb
|
41
|
+
- rakefile
|
42
|
+
- test/helper.rb
|
43
|
+
homepage: http://github.com/carwow/dyn-api-gen
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubygems_version: 3.1.6
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: Dynamic API client generator. Aims to follow OpenAPI + JSON API standards
|
66
|
+
test_files:
|
67
|
+
- test/helper.rb
|