Sha256: 296fc30eb09c9eabce9f462861e959850dfdb134c126073707c74c99e9f9b25f

Contents?: true

Size: 1.63 KB

Versions: 9

Compression:

Stored size: 1.63 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
      create_directory
      write_metadata
      return false if self.has_file?
      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 write_metadata
      open(metadata_path, 'w') {|f|
        f.puts book.permalink_uri
      }  unless File.exists?(metadata_path)
    end

    def metadata_path
      File.join(book_path, 'metadata')
    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

9 entries across 9 versions & 1 rubygems

Version Path
kindai-1.9.0 lib/kindai/book_downloader.rb
kindai-1.8.0 lib/kindai/book_downloader.rb
kindai-1.7.4 lib/kindai/book_downloader.rb
kindai-1.7.2 lib/kindai/book_downloader.rb
kindai-1.7.1 lib/kindai/book_downloader.rb
kindai-1.7.0 lib/kindai/book_downloader.rb
kindai-1.6.2 lib/kindai/book_downloader.rb
kindai-1.6.1 lib/kindai/book_downloader.rb
kindai-1.6.0 lib/kindai/book_downloader.rb