Class: ScriptRipper::ScriptWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/script_ripper/script_writer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code_blocks:, from_url:, group_name: nil, io: $stdout) ⇒ ScriptWriter

Returns a new instance of ScriptWriter.



5
6
7
8
9
10
# File 'lib/script_ripper/script_writer.rb', line 5

def initialize(code_blocks:, from_url:, group_name: nil, io: $stdout)
  @code_blocks = code_blocks
  @group_name = group_name
  @io = io
  @from_url = from_url
end

Instance Attribute Details

#code_blocksObject (readonly)

Returns the value of attribute code_blocks.



3
4
5
# File 'lib/script_ripper/script_writer.rb', line 3

def code_blocks
  @code_blocks
end

#from_urlObject (readonly)

Returns the value of attribute from_url.



3
4
5
# File 'lib/script_ripper/script_writer.rb', line 3

def from_url
  @from_url
end

#group_nameObject (readonly)

Returns the value of attribute group_name.



3
4
5
# File 'lib/script_ripper/script_writer.rb', line 3

def group_name
  @group_name
end

#ioObject (readonly)

Returns the value of attribute io.



3
4
5
# File 'lib/script_ripper/script_writer.rb', line 3

def io
  @io
end

Class Method Details

.call(code_blocks:, from_url:, group_name: nil, io: $stdout) ⇒ Object



35
36
37
# File 'lib/script_ripper/script_writer.rb', line 35

def call(code_blocks:, from_url:, group_name: nil, io: $stdout)
  new(code_blocks: code_blocks, from_url: from_url, group_name: group_name, io: io).call
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/script_ripper/script_writer.rb', line 12

def call
  io.puts "#!/usr/bin/env bash\n\n"
  io.puts "# #{from_url}\n"
  io.puts "# #{group_name}\n" if group_name
  io.puts "\n"

  code_blocks_count = code_blocks.length

  code_blocks.each_with_index do |code_block, i|
    io.puts(description_to_comment(code_block.description))
    code_suffix = (i == code_blocks_count - 1) ? "\n" : "\n\n"

    io.puts("#{code_block.code}#{code_suffix}")
  end

  # if $record
  #   File.open("test/fixtures/jellyfin-ubuntu.sh", "w") do |file|
  #     file.write(io.string)
  #   end
  # end
end