Class: NcodeSyosetu::Model::Toc

Inherits:
Object
  • Object
show all
Defined in:
lib/ncode_syosetu/model/toc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Toc

Returns a new instance of Toc.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ncode_syosetu/model/toc.rb', line 6

def initialize(page)
  @url = page.uri.to_s
  @title = page.title
  @author = page.search(".p-novel__author").text.chomp
  @abstract = page.search(".p-novel__summary").text.chomp

  @episodes = []
  eplist = page.at(".p-eplist")
  if eplist
    eplist.children.each do |sub_item|
      next unless sub_item.matches?('.p-eplist__chapter-title, .p-eplist__sublist')
      episode = { text: sub_item.text.gsub(/\s+/, " ").chomp }
      link = sub_item.search("a")
      unless link.empty?
        if link.attr("href").value =~ %r[/(\d+)/?$]
          episode[:number] = $1.to_i
        end
      end
      @episodes << episode
    end

    @body_html =
      page.search(".p-novel__author").to_html <<
      page.search(".p-novel__summary").to_html <<
      page.search(".p-eplist").to_html
  else
    # Short story: the toc page itself contains the body.
    @short_story = true
    @page = page

    @body_html =
      page.search(".p-novel__author").to_html <<
      page.search(".p-novel__summary").to_html
  end
end

Instance Attribute Details

#abstract ⇒ Object

Returns the value of attribute abstract.



4
5
6
# File 'lib/ncode_syosetu/model/toc.rb', line 4

def abstract
  @abstract
end

#author ⇒ Object

Returns the value of attribute author.



4
5
6
# File 'lib/ncode_syosetu/model/toc.rb', line 4

def author
  @author
end

#episodes ⇒ Object

Returns the value of attribute episodes.



4
5
6
# File 'lib/ncode_syosetu/model/toc.rb', line 4

def episodes
  @episodes
end

#title ⇒ Object

Returns the value of attribute title.



4
5
6
# File 'lib/ncode_syosetu/model/toc.rb', line 4

def title
  @title
end

#url ⇒ Object

Returns the value of attribute url.



4
5
6
# File 'lib/ncode_syosetu/model/toc.rb', line 4

def url
  @url
end

Instance Method Details

#html ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ncode_syosetu/model/toc.rb', line 51

def html
  <<-HTML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>#{@title}</title>
</head>
<body>

#{@body_html}

</body>
</html>
  HTML
end

#short_story? ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/ncode_syosetu/model/toc.rb', line 42

def short_story?
  !!@short_story
end

#to_episode ⇒ Object



46
47
48
49
# File 'lib/ncode_syosetu/model/toc.rb', line 46

def to_episode
  raise "not a short story" unless short_story?
  Episode.new(@title, 1, @page)
end