Class: Kindai::BookDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/kindai/book_downloader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



6
7
8
# File 'lib/kindai/book_downloader.rb', line 6

def base_path
  @base_path
end

#bookObject

Returns the value of attribute book.



4
5
6
# File 'lib/kindai/book_downloader.rb', line 4

def book
  @book
end

#retry_countObject

Returns the value of attribute retry_count.



5
6
7
# File 'lib/kindai/book_downloader.rb', line 5

def retry_count
  @retry_count
end

Class Method Details

.new_from_book(book) ⇒ Object

Raises:

  • (TypeError)


8
9
10
11
12
13
14
15
# File 'lib/kindai/book_downloader.rb', line 8

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

Instance Method Details

#book_pathObject



29
30
31
32
# File 'lib/kindai/book_downloader.rb', line 29

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

#create_directoryObject



34
35
36
# File 'lib/kindai/book_downloader.rb', line 34

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

#deleteObject



38
39
40
41
42
# File 'lib/kindai/book_downloader.rb', line 38

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

#downloadObject



17
18
19
20
21
22
23
# File 'lib/kindai/book_downloader.rb', line 17

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

#has_file?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/kindai/book_downloader.rb', line 54

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

#metadata_pathObject



50
51
52
# File 'lib/kindai/book_downloader.rb', line 50

def 
  File.join(book_path, 'metadata')
end

#safe_filename(filename) ⇒ Object



25
26
27
# File 'lib/kindai/book_downloader.rb', line 25

def safe_filename filename
  filename.gsub(File::SEPARATOR, '_')
end

#write_metadataObject



44
45
46
47
48
# File 'lib/kindai/book_downloader.rb', line 44

def 
  open(, 'w') {|f|
    f.puts book.permalink_uri
  }  unless File.exists?()
end