Class: DynApiGen::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/dyn_api_gen/generator.rb

Defined Under Namespace

Classes: RequestDefinition

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(openapi_path) ⇒ Generator

Returns a new instance of Generator.



7
8
9
10
# File 'lib/dyn_api_gen/generator.rb', line 7

def initialize(openapi_path)
  @openapi_path = openapi_path
  @module = Module.new
end

Class Method Details

.generate(path) ⇒ Object



12
13
14
# File 'lib/dyn_api_gen/generator.rb', line 12

def self.generate(path)
  new(path).generate
end

Instance Method Details

#generateObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dyn_api_gen/generator.rb', line 16

def generate
  requests = []

  openapi['paths'].each do |path, endpoints|
    endpoints.each do |verb, detail|
      name = to_underscore(detail.fetch('operationId'))
      parameters = detail.fetch('parameters').map(&Parameter.method(:new))

      detail.fetch('tags').each do |namespace|
        requests << RequestDefinition.new(to_camelcase(namespace), name, verb, path, parameters)
      end
    end
  end

  requests.group_by(&:namespace).each do |namespace, req_definitions|
    namespace_module = Module.new
    puts namespace
    req_definitions.each do |req_def|
      puts "> #{req_def.name}"
      namespace_module.define_singleton_method(req_def.name) do
        Request.new(req_def.to_h)
      end
    end

    @module.send(:const_set, namespace, namespace_module)
    # @module.const_set(namespace, namespace_module)
  end

  @module
end