Class: Mangdown::Chapter
- Inherits:
-
Object
- Object
- Mangdown::Chapter
- Includes:
- Enumerable, Equality
- Defined in:
- lib/mangdown/chapter.rb
Instance Attribute Summary collapse
-
#chapter ⇒ Object
readonly
Returns the value of attribute chapter.
-
#manga ⇒ Object
readonly
Returns the value of attribute manga.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#pages ⇒ Object
readonly
Returns the value of attribute pages.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#download_to(dir) ⇒ Object
download all pages in a chapter.
-
#each ⇒ Object
enumerates through pages.
-
#initialize(name, uri) ⇒ Chapter
constructor
A new instance of Chapter.
- #inspect ⇒ Object (also: #to_s)
-
#to_chapter ⇒ Object
explicit conversion to chapter.
Methods included from Equality
Constructor Details
#initialize(name, uri) ⇒ Chapter
Returns a new instance of Chapter.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/mangdown/chapter.rb', line 8 def initialize(name, uri) # use a valid name @name = name.sub(/\s(\d+)$/) { |num| ' ' + num.to_i.to_s.rjust(3, '0') } @manga = name.slice(/(^.+)\s/, 1) @chapter = name.slice(/\d+\z/).to_i @uri = Mangdown::Uri.new(uri) @properties = Properties.new(@uri) @pages = [] get_pages @pages.sort_by! {|page| page.name} end |
Instance Attribute Details
#chapter ⇒ Object (readonly)
Returns the value of attribute chapter.
6 7 8 |
# File 'lib/mangdown/chapter.rb', line 6 def chapter @chapter end |
#manga ⇒ Object (readonly)
Returns the value of attribute manga.
6 7 8 |
# File 'lib/mangdown/chapter.rb', line 6 def manga @manga end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/mangdown/chapter.rb', line 6 def name @name end |
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
6 7 8 |
# File 'lib/mangdown/chapter.rb', line 6 def pages @pages end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
6 7 8 |
# File 'lib/mangdown/chapter.rb', line 6 def uri @uri end |
Instance Method Details
#download_to(dir) ⇒ Object
download all pages in a chapter
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mangdown/chapter.rb', line 41 def download_to(dir) dir = Tools.relative_or_absolute_path(dir, @name) pages = map {|page| page.to_page} failed = [] succeeded = [] Dir.mkdir(dir) unless Dir.exists?(dir) Tools.hydra_streaming(pages) do |stage, page, data=nil| case stage when :failed failed << page when :succeeded succeeded << page when :before path = page.file_path(dir) !File.exist?(path) when :body unless failed.include?(page) path = page.file_path(dir) File.open(path, 'ab') { |file| file.write(data) } end when :complete unless failed.include?(page) path = page.file_path(dir) FileUtils.mv(path, "#{path}.#{Tools.file_type(path)}") end end end FileUtils.rm_rf(dir) if succeeded.empty? !succeeded.empty? end |
#each ⇒ Object
enumerates through pages
31 32 33 |
# File 'lib/mangdown/chapter.rb', line 31 def each block_given? ? @pages.each { |page| yield(page) } : @pages.each end |
#inspect ⇒ Object Also known as: to_s
23 24 25 26 27 |
# File 'lib/mangdown/chapter.rb', line 23 def inspect "#<#{self.class} @name=#{name} @uri=#{uri} " + "@pages=[#{pages.first(10).join(',')}" + "#{",..." if pages.length > 10}]>" end |
#to_chapter ⇒ Object
explicit conversion to chapter
36 37 38 |
# File 'lib/mangdown/chapter.rb', line 36 def to_chapter self end |