class Kindai::BookDownloader

Attributes

base_path[RW]
book[RW]
retry_count[RW]

Public Class Methods

new_from_book(book) click to toggle source
# File lib/kindai/book_downloader.rb, line 7
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

Public Instance Methods

book_path() click to toggle source
# File lib/kindai/book_downloader.rb, line 28
def book_path
  path = File.join(self.base_path, safe_filename([@book.author, @book.title].compact.join(' - ')))
  File.expand_path path
end
create_directory() click to toggle source
# File lib/kindai/book_downloader.rb, line 33
def create_directory
  Dir.mkdir(book_path) unless File.directory?(book_path)
end
delete() click to toggle source
# File lib/kindai/book_downloader.rb, line 37
def delete
  success = true
  FileUtils.rm_r(self.book_path) rescue success = false
  return success
end
download() click to toggle source
# File lib/kindai/book_downloader.rb, line 16
def download
  create_directory
  write_metadata
  return false if self.has_file?
  download_spreads
  return true
end
has_file?() click to toggle source
# File lib/kindai/book_downloader.rb, line 53
def has_file?
  File.directory?(self.book_path) && self.spread_downloaders.all?(&:has_file?)
end
metadata_path() click to toggle source
# File lib/kindai/book_downloader.rb, line 49
def metadata_path
  File.join(book_path, 'metadata')
end
safe_filename(filename) click to toggle source
# File lib/kindai/book_downloader.rb, line 24
def safe_filename filename
  filename.gsub(File::SEPARATOR, '_')
end
write_metadata() click to toggle source
# File lib/kindai/book_downloader.rb, line 43
def write_metadata
  open(metadata_path, 'w') {|f|
    f.puts book.permalink_uri
  }  unless File.exists?(metadata_path)
end

Protected Instance Methods

download_spreads() click to toggle source
# File lib/kindai/book_downloader.rb, line 69
def download_spreads
  is_first = true
  self.spread_downloaders.each{ |dl|
    next if dl.has_file?
    sleep 20 unless is_first
    is_first = false
    dl.download
  }

  return true
end
spread_downloaders() click to toggle source

# File lib/kindai/book_downloader.rb, line 60
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