class YoutubeDL::Runner

Public Class Methods

new(command, listener: Listener.new) click to toggle source
# File lib/youtube_dl/runner.rb, line 7
def initialize(command, listener: Listener.new)
  @command = command
  @listener = listener
end

Public Instance Methods

call() click to toggle source
# File lib/youtube_dl/runner.rb, line 12
def call
  parser = OutputParser.new
  run(command: @command, parser: parser)

  if parser.state.complete?
    parser.state.load_and_delete_info_json
    @listener.call(:complete, state: parser.state)
  elsif parser.state.error?
    @listener.call(:error, state: parser.state)
  else
    @listener.call(:unclear_exit_state, state: parser.state)
  end

  parser.state
end
method_missing(method, &block) click to toggle source
Calls superclass method
# File lib/youtube_dl/runner.rb, line 37
def method_missing(method, &block)
  return super unless method.to_s.start_with?('on_')

  event = method.to_s.sub(/^on_/, '').to_sym
  on(event, &block)
end
on(event, &block) click to toggle source
# File lib/youtube_dl/runner.rb, line 28
def on(event, &block)
  @listener.register(event, &block)
  self
end
respond_to_missing?(method, *) click to toggle source
Calls superclass method
# File lib/youtube_dl/runner.rb, line 33
def respond_to_missing?(method, *)
  method.to_s.start_with?('on_') || super
end

Private Instance Methods

run(command:, parser:) click to toggle source
# File lib/youtube_dl/runner.rb, line 46
def run(command:, parser:)
  PTY.spawn(command.to_s) do |stdout, stdin, pid|
    begin
      stdout.each do |line|
        type = parser.process(line)
        @listener.call(type, state: parser.state, line: line)
      end
    rescue Errno::EIO
    end
  end
end