class Rakali::Converter

Constants

DEFAULTS

Default options.

Attributes

config[RW]
documents[RW]
errors[RW]

Public Class Methods

new(file, options = {}) click to toggle source
# File lib/rakali/converter.rb, line 19
def initialize(file, options = {})
  begin
    config = read_config_file(file)

    # deep merge defaults to preserve nested keys
    @config = Utils.deep_merge_hashes(DEFAULTS, config)

    # print configuration
    Rakali.logger.info "Configuration:", to_yaml

    from_folder = @config.fetch('from').fetch('folder')
    from_format = @config.fetch('from').fetch('format')
    documents = Dir.glob("#{from_folder}/*.#{from_format}")

    # merge all documents into one file if merge flag is set
    # otherwise iterate through each file in source folder
    if @config.fetch('merge')
      Rakali::Document.new(documents, @config)
    else
      documents.each { |document| Rakali::Document.new(document, @config) }
    end
  rescue KeyError => e
    Rakali.logger.abort_with "Fatal:", "Configuration #{e.message}."
  rescue => e
    Rakali.logger.abort_with "Fatal:", "#{e.message}."
  end
end

Public Instance Methods

from_json(string) click to toggle source
# File lib/rakali/converter.rb, line 54
def from_json(string)
  JSON.parse(string)
rescue JSON::ParserError, TypeError
  nil
end
read_config_file(file) click to toggle source
# File lib/rakali/converter.rb, line 47
def read_config_file(file)
  # use an empty hash if the file is empty
  SafeYAML.load_file(file) || {}
rescue SystemCallError
  Rakali.logger.abort_with "Fatal:", "Configuration file not found: \"#{file}\"."
end
to_json() click to toggle source
# File lib/rakali/converter.rb, line 60
def to_json
  content.to_json
end
to_yaml() click to toggle source
# File lib/rakali/converter.rb, line 64
def to_yaml
  yaml = config.to_yaml.gsub(/---\n/, '').split("\n")
  yaml.join("\n                    ")
end