Class: Mangdown::Chapter

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Equality
Defined in:
lib/mangdown/chapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Equality

#<=>, #==, #eql?

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

#chapterObject (readonly)

Returns the value of attribute chapter.



6
7
8
# File 'lib/mangdown/chapter.rb', line 6

def chapter
  @chapter
end

#mangaObject (readonly)

Returns the value of attribute manga.



6
7
8
# File 'lib/mangdown/chapter.rb', line 6

def manga
  @manga
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/mangdown/chapter.rb', line 6

def name
  @name
end

#pagesObject (readonly)

Returns the value of attribute pages.



6
7
8
# File 'lib/mangdown/chapter.rb', line 6

def pages
  @pages
end

#uriObject (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

#eachObject

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

#inspectObject 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_chapterObject

explicit conversion to chapter



36
37
38
# File 'lib/mangdown/chapter.rb', line 36

def to_chapter
	self
end