Class: Openapi2ruby::Generator

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

Constant Summary collapse

TEMPLATE_PATH =
File.expand_path('../templates/serializer.rb.erb', __FILE__)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Generator

Returns a new instance of Generator.



17
18
19
# File 'lib/openapi2ruby/generator.rb', line 17

def initialize(schema)
  @schema = schema
end

Class Method Details

.generate(schema, output_path, template_path) ⇒ Object

Generate ruby class from OpenAPI schema

Parameters:

  • schema (Openapi2ruby::Openapi::Schema)

    parsed OpenAPI schema

  • output_path (String)

    parsed OpenAPI YAML

  • template_path (String)

    original template path



13
14
15
# File 'lib/openapi2ruby/generator.rb', line 13

def self.generate(schema, output_path, template_path)
  new(schema).generate(output_path, template_path)
end

Instance Method Details

#generate(output_path, template_path) ⇒ Object

Generate ruby class from OpenAPI schema

Parameters:

  • output_path (String)

    parsed OpenAPI YAML

  • template_path (String)

    original template path



24
25
26
27
28
29
30
31
32
# File 'lib/openapi2ruby/generator.rb', line 24

def generate(output_path, template_path)
  template_path = TEMPLATE_PATH if template_path.nil?
  template = File.read(template_path)
  generated_class = ERB.new(template, nil, '-').result(binding)

  output_file = Pathname.new(output_path).join("#{@schema.name.underscore}_serializer.rb")
  File.open(output_file.to_s, 'w') { |file| file << generated_class }
  output_file.to_s
end