Class: YoutubeDL::Command

Inherits:
Object
  • Object
show all
Extended by:
Dry::Configurable
Defined in:
lib/youtube_dl/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, **options) ⇒ Command

Returns a new instance of Command.



21
22
23
24
25
26
27
# File 'lib/youtube_dl/command.rb', line 21

def initialize(url, **options)
  @url = url
  @options =
    config.default_options
    .merge(options)
    .merge(config.forced_options)
end

Instance Method Details

#argumentsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/youtube_dl/command.rb', line 29

def arguments
  @options.flat_map do |option, value|
    option = option.to_s.gsub('_', '-')
    case value
    when [] then nil
    when true then "--#{option}"
    when false then "--no-#{option}"
    else
      # Handle array values by repeating the option.
      #
      # Example: { paths: ['temp:/tmp', '/complete'] }
      # Becomes: ['--paths', 'temp:/tmp', '--paths', '/complete']
      #
      # Example: { paths: '/downloads' }
      # Becomes: ['--paths', '/downloads]
      [*value].flat_map { |v| ["--#{option}", v] }
    end
  end
end

#configObject



8
9
10
# File 'lib/youtube_dl/command.rb', line 8

def config
  self.class.config
end

#to_aObject



49
50
51
# File 'lib/youtube_dl/command.rb', line 49

def to_a
  [config.executable] + arguments + [@url]
end

#to_sObject



53
54
55
# File 'lib/youtube_dl/command.rb', line 53

def to_s
  Shellwords.join(to_a)
end