Class: CbzTools::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/cbz_tools/reader.rb

Defined Under Namespace

Classes: ImageNotFoundError

Constant Summary collapse

IMAGE_EXTENSIONS =
%w[.jpg .jpeg .png .webp].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Reader

Returns a new instance of Reader.



18
19
20
21
# File 'lib/cbz_tools/reader.rb', line 18

def initialize(path)
  @path = path
  @zip = Zip::File.open(@path)
end

Class Method Details

.open(path, &block) ⇒ Object



11
12
13
14
15
16
# File 'lib/cbz_tools/reader.rb', line 11

def self.open(path, &block)
  reader = new(path)
  block.call(reader)
ensure
  reader&.close
end

Instance Method Details

#closeObject



23
24
25
26
# File 'lib/cbz_tools/reader.rb', line 23

def close
  @zip&.close
  @zip = nil
end

#comic_infoObject



54
55
56
# File 'lib/cbz_tools/reader.rb', line 54

def comic_info
  read("ComicInfo.xml") || read("comicinfo.xml")
end

#first_imageObject



43
44
45
# File 'lib/cbz_tools/reader.rb', line 43

def first_image
  read(image_names.first)
end

#image(name) ⇒ Object

Raises:



47
48
49
50
51
52
# File 'lib/cbz_tools/reader.rb', line 47

def image(name)
  data = read(name)
  raise ImageNotFoundError, "Image '#{name}' not found in #{@path}" unless data

  data
end

#image_namesObject



28
29
30
31
32
33
34
# File 'lib/cbz_tools/reader.rb', line 28

def image_names
  @zip.entries
    .reject(&:directory?)
    .map(&:name)
    .select { |name| !name.include?("__macosx") && image?(name) }
    .sort
end

#read(name) ⇒ Object



36
37
38
39
40
41
# File 'lib/cbz_tools/reader.rb', line 36

def read(name)
  entry = @zip.find_entry(name)
  return nil unless entry

  entry.get_input_stream.read
end