Class: CbzTools::CbrConverter
- Inherits:
-
Object
- Object
- CbzTools::CbrConverter
- Defined in:
- lib/cbz_tools/cbr_converter.rb,
sig/cbz_tools.rbs
Overview
Converts a CBR (Comic Book RAR) archive into a CBZ (Comic Book ZIP) archive.
The CBR extraction step is delegated to an injectable extractor
— anything that responds to call(cbr_path, destination_dir) and
raises ExtractionError on failure. Callers (not this gem) own the
extractor; this class is deliberately agnostic about which binary,
library, or pure-Ruby implementation is used to expand the source.
Defined Under Namespace
Classes: ArchiveError, ExtractionError
Instance Attribute Summary collapse
-
#extractor ⇒ #call?
readonly
The configured extractor, or
nilif none was supplied.
Class Method Summary collapse
-
.convert(cbr_path, to:, overwrite: true, work_in: nil, extractor: nil) ⇒ String
The destination CBZ path.
Instance Method Summary collapse
-
#convert(cbr_path, to:, overwrite: true, work_in: nil) ⇒ String
The destination CBZ path.
- #ensure_extractor! ⇒ void
- #ensure_target_slot!(to, overwrite:) ⇒ void
- #in_workdir(work_in) {|arg0| ... } ⇒ void
-
#initialize(extractor: nil) ⇒ CbrConverter
constructor
A new instance of CbrConverter.
- #repack(source_dir, target_path) ⇒ void
Constructor Details
#initialize(extractor: nil) ⇒ CbrConverter
Returns a new instance of CbrConverter.
68 69 70 |
# File 'lib/cbz_tools/cbr_converter.rb', line 68 def initialize(extractor: nil) @extractor = extractor end |
Instance Attribute Details
#extractor ⇒ #call? (readonly)
Returns the configured extractor, or nil if none
was supplied. Read-only; there is no corresponding writer.
44 45 46 |
# File 'lib/cbz_tools/cbr_converter.rb', line 44 def extractor @extractor end |
Class Method Details
.convert(cbr_path, to:, overwrite: true, work_in: nil, extractor: nil) ⇒ String
Returns the destination CBZ path.
60 61 62 |
# File 'lib/cbz_tools/cbr_converter.rb', line 60 def self.convert(cbr_path, to:, overwrite: true, work_in: nil, extractor: nil) new(extractor: extractor).convert(cbr_path, to: to, overwrite: overwrite, work_in: work_in) end |
Instance Method Details
#convert(cbr_path, to:, overwrite: true, work_in: nil) ⇒ String
Returns the destination CBZ path.
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/cbz_tools/cbr_converter.rb', line 82 def convert(cbr_path, to:, overwrite: true, work_in: nil) ensure_extractor! ensure_target_slot!(to, overwrite: overwrite) File.delete(to) if overwrite && File.exist?(to) in_workdir(work_in) do |temp_dir| @extractor.call(cbr_path, temp_dir) repack(temp_dir, to) end to end |
#ensure_extractor! ⇒ void
This method returns an undefined value.
97 98 99 100 |
# File 'lib/cbz_tools/cbr_converter.rb', line 97 def ensure_extractor! raise ArchiveError, "No extractor configured; pass `extractor:` to CbrConverter.convert or CbrConverter.new" unless @extractor end |
#ensure_target_slot!(to, overwrite:) ⇒ void
This method returns an undefined value.
102 103 104 105 106 |
# File 'lib/cbz_tools/cbr_converter.rb', line 102 def ensure_target_slot!(to, overwrite:) return if overwrite || !File.exist?(to) raise ArchiveError, "Target #{to} already exists (pass overwrite: true to replace)" end |
#in_workdir(work_in) {|arg0| ... } ⇒ void
This method returns an undefined value.
108 109 110 111 112 113 114 115 |
# File 'lib/cbz_tools/cbr_converter.rb', line 108 def in_workdir(work_in, &block) if work_in FileUtils.mkdir_p(work_in) yield work_in else Dir.mktmpdir(&block) end end |
#repack(source_dir, target_path) ⇒ void
This method returns an undefined value.
117 118 119 120 121 122 123 |
# File 'lib/cbz_tools/cbr_converter.rb', line 117 def repack(source_dir, target_path) Zip::File.open(target_path, create: true) do |zipfile| each_file_in(source_dir) { |file| add_to_zip(zipfile, source_dir, file) } end rescue Zip::Error, SystemCallError => e raise ArchiveError, "Failed to write CBZ #{target_path}: #{e.class}: #{e.}" end |