class Pdi::Spoon

This class is the main wrapper for PDI's pan and kitchen scripts.

Constants

DEFAULT_KITCHEN
DEFAULT_PAN
TYPES_TO_ERRORS

Attributes

args[R]
dir[R]
executor[R]
kitchen[R]
pan[R]
parser[R]

Public Class Methods

new( args: [], dir:, kitchen: DEFAULT_KITCHEN, pan: DEFAULT_PAN, timeout_in_seconds: nil ) click to toggle source
# File lib/pdi/spoon.rb, line 29
def initialize(
  args: [],
  dir:,
  kitchen: DEFAULT_KITCHEN,
  pan: DEFAULT_PAN,
  timeout_in_seconds: nil
)
  assert_required(:dir, dir)
  assert_required(:kitchen, kitchen)
  assert_required(:pan, pan)

  @args           = Array(args)
  @dir            = File.expand_path(dir.to_s)
  @kitchen        = kitchen.to_s
  @pan            = pan.to_s
  @executor       = Executor.new(timeout_in_seconds: timeout_in_seconds)
  @parser         = Parser.new

  freeze
end

Public Instance Methods

run(options, &streaming_reader) click to toggle source

Returns a Pdi::Executor::Result instance when PDI returns error code 0 or else raises a PanError (transformation) or KitchenError (job).

An optional block may be passed in so that the output of a run is available in a streaming manner during the run. This block takes one parameter which is the current chunk of output and is called repeatedly throughout the run.

# File lib/pdi/spoon.rb, line 70
def run(options, &streaming_reader)
  options  = Options.make(options)
  all_args = run_args(options)

  executor.run(all_args, &streaming_reader).tap do |result|
    raise(error_constant(options), result) if result.code != 0
  end
end
version() click to toggle source

Returns a Spoon::Result instance when PDI returns error code 0 or else raises a KitchenError since Kitchen was used to run the version command.

# File lib/pdi/spoon.rb, line 52
def version
  final_args = [kitchen_path] + args + [Options::Arg.new(Options::Arg::Key::VERSION)]

  result       = executor.run(final_args)
  version_line = parser.version(result.out)

  raise(KitchenError, result) if result.code != 0

  Result.new(result, version_line)
end

Private Instance Methods

assert_required(name, value) click to toggle source
# File lib/pdi/spoon.rb, line 107
def assert_required(name, value)
  raise ArgumentError, "#{name} is required" if value.to_s.empty?
end
error_constant(options) click to toggle source
# File lib/pdi/spoon.rb, line 83
def error_constant(options)
  TYPES_TO_ERRORS.fetch(options.type)
end
kitchen_path() click to toggle source
# File lib/pdi/spoon.rb, line 99
def kitchen_path
  File.join(dir, kitchen)
end
pan_path() click to toggle source
# File lib/pdi/spoon.rb, line 103
def pan_path
  File.join(dir, pan)
end
run_args(options) click to toggle source
# File lib/pdi/spoon.rb, line 87
def run_args(options)
  [script_path(options.type)] + args + options.to_args
end
script_path(options_type) click to toggle source
# File lib/pdi/spoon.rb, line 91
def script_path(options_type)
  if options_type == Options::Type::JOB
    kitchen_path
  elsif options_type == Options::Type::TRANSFORMATION
    pan_path
  end
end