Class: KrakendOpenAPI::KrakendWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/writers/krakend_writer.rb

Overview

Writes KrakenD configuration to a file

Instance Method Summary collapse

Constructor Details

#initialize(endpoints, importer_config) ⇒ KrakendWriter

Returns a new instance of KrakendWriter.



8
9
10
11
12
# File 'lib/writers/krakend_writer.rb', line 8

def initialize(endpoints, importer_config)
  @endpoints = endpoints
  @importer_config = importer_config
  @output_file_path = @importer_config['output'] || 'output.json'
end

Instance Method Details

#file_pathObject



24
25
26
27
28
29
30
31
# File 'lib/writers/krakend_writer.rb', line 24

def file_path
  pwd = File.expand_path(Dir.pwd)
  if Pathname.new(@output_file_path).absolute?
    @output_file_path
  else
    File.expand_path(@output_file_path, pwd)
  end
end

#writeObject



14
15
16
17
18
19
20
21
22
# File 'lib/writers/krakend_writer.rb', line 14

def write
  pretty_output = !!@importer_config['pretty'] # rubocop:disable Style/DoubleNegation
  json_generate = pretty_output ? ->(obj) { JSON.pretty_generate(obj) } : ->(obj) { JSON.dump(obj) }
  File.write(file_path, json_generate.call({
                                             '$schema': 'https://www.krakend.io/schema/v3.json',
                                             version: 3,
                                             endpoints: @endpoints
                                           }))
end