class Mangdown::Manga

Mangdown manga object, which holds chapters

Attributes

manga[RW]

Public Class Methods

new(manga) click to toggle source
# File lib/mangdown/manga.rb, line 17
def initialize(manga)
  @manga = manga
end

Public Instance Methods

cbz() click to toggle source
# File lib/mangdown/manga.rb, line 27
def cbz
  CBZ.all(to_path)
end
chapters() click to toggle source
# File lib/mangdown/manga.rb, line 21
def chapters
  @chapters ||= manga
                .chapters
                .map { |chapter| Mangdown.chapter(chapter) }
end
download(*args) click to toggle source
# File lib/mangdown/manga.rb, line 31
def download(*args)
  download_to(nil, *args)
end
download_to(dir, start = 0, stop = -1, opts = { force_download: false }) click to toggle source

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

# File lib/mangdown/manga.rb, line 37
def download_to(dir, start = 0, stop = -1, opts = { force_download: false })
  start, stop = validate_indeces!(start, stop)
  setup_download_dir!(dir)
  failed = []
  succeeded = []
  skipped = []

  chapters[start..stop].each do |chapter|
    chapter_result = chapter.download_to(to_path, opts)

    if chapter_result[:failed].any?
      failed << [chapter, chapter_result]
    elsif chapter_result[:succeeded].any?
      succeeded << [chapter, chapter_result]
    elsif chapter_result[:skipped].any?
      skipped << [chapter, chapter_result]
    end

    next unless chapter_result[:failed].any?

    logger.error({
      msg: 'Chapter was not fully downloaded',
      uri: chapter.uri,
      chapter: chapter.name
    }.to_s)
  end

  { failed: failed, succeeded: succeeded, skipped: skipped }
end
path() click to toggle source

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength

# File lib/mangdown/manga.rb, line 69
def path
  @path ||= setup_path
end
Also aliased as: to_path
setup_path(dir = nil) click to toggle source
# File lib/mangdown/manga.rb, line 74
def setup_path(dir = nil)
  dir ||= DOWNLOAD_DIR
  path = Tools.file_join(dir, name)
  @path = Tools.relative_or_absolute_path(path)
end
to_path()
Alias for: path

Private Instance Methods

chapter_indeces(start, stop) click to toggle source
# File lib/mangdown/manga.rb, line 82
def chapter_indeces(start, stop)
  length = chapters.length
  [start, stop].map { |i| i.negative? ? length + i : i }
end
setup_download_dir!(dir) click to toggle source
# File lib/mangdown/manga.rb, line 87
def setup_download_dir!(dir)
  setup_path(dir)
  FileUtils.mkdir_p(to_path) unless Dir.exist?(to_path)
end
validate_indeces!(start, stop) click to toggle source
# File lib/mangdown/manga.rb, line 92
def validate_indeces!(start, stop)
  chapter_indeces(start, stop).tap do |i_start, i_stop|
    last = chapters.length - 1

    if i_start > last || i_stop > last
      error = "This manga has chapters in the range (0..#{last})"
      raise Mangdown::Error, error
    elsif i_stop < i_start
      error = 'Last index must be greater than or equal to first index'
      raise Mangdown::Error, error
    end
  end
end