Sha256: 6fef2656642283a6f56b98e014dd1ed25e4ad73e0d5951382bf168f7d3338ef5

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 KB

Contents

# -*- coding: utf-8 -*-
module Kindai
  class BookDownloader
    attr_accessor :book
    attr_accessor :retry_count
    attr_accessor :base_path

    def self.new_from_book(book)
      raise TypeError, "#{book} is not Kindai::Book" unless book.is_a? Kindai::Book
      me = self.new
      me.book = book
      me.retry_count = 30
      me.base_path = Dir.pwd
      me
    end

    def download
      return false if self.has_file?
      create_directory
      download_spreads
      return true
    end

    def book_path
      path = File.join(self.base_path, [@book.author, @book.title].compact.join(' - '))
      File.expand_path path
    end

    def create_directory
      Dir.mkdir(book_path) unless File.directory?(book_path)
    end

    def delete
      success = true
      FileUtils.rm_r(self.book_path) rescue success = false
      return success
    end

    def has_file?
      File.directory?(self.book_path) && self.spread_downloaders.all?(&:has_file?)
    end

    # --------------------------------------------------------------------
    protected

    def spread_downloaders
      self.book.spreads.map{|spread|
        dl = Kindai::SpreadDownloader.new_from_spread(spread)
        dl.retry_count = self.retry_count
        dl.book_path = self.book_path
        dl
      }
    end

    def download_spreads
      self.spread_downloaders.each{|dl|
        dl.download
      }
      return true
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kindai-1.5.1 lib/kindai/book_downloader.rb
kindai-1.5.0 lib/kindai/book_downloader.rb
kindai-1.4.0 lib/kindai/book_downloader.rb
kindai-1.3.0 lib/kindai/book_downloader.rb
kindai-1.1.0 lib/kindai/book_downloader.rb
kindai-1.0.0 lib/kindai/book_downloader.rb