Class: NcodeSyosetu::Model::Episode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, number, page) ⇒ Episode

Returns a new instance of Episode.



9
10
11
12
13
14
15
16
17
# File 'lib/ncode_syosetu/model/episode.rb', line 9

def initialize(title, number, page)
  @url = page.uri.to_s
  @title = title
  @number = number

  @body_html =
    page.search(".p-novel__title").to_html <<
    page.search(".p-novel__body").to_html
end

Instance Attribute Details

#body_htmlObject

Returns the value of attribute body_html.



7
8
9
# File 'lib/ncode_syosetu/model/episode.rb', line 7

def body_html
  @body_html
end

#numberObject

Returns the value of attribute number.



7
8
9
# File 'lib/ncode_syosetu/model/episode.rb', line 7

def number
  @number
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/ncode_syosetu/model/episode.rb', line 7

def title
  @title
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/ncode_syosetu/model/episode.rb', line 7

def url
  @url
end

Instance Method Details

#htmlObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ncode_syosetu/model/episode.rb', line 19

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

#markdownObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ncode_syosetu/model/episode.rb', line 47

def markdown
  doc = Nokogiri::HTML.fragment(@body_html)
  lines = []
  title_text = doc.at(".p-novel__title")&.text&.strip
  lines << "## #{title_text}" if title_text
  lines << ""
  doc.search(".p-novel__text p").each do |p|
    lines << ruby_to_markdown(p)
  end
  lines.join("\n")
end

#textObject



37
38
39
40
41
42
43
44
45
# File 'lib/ncode_syosetu/model/episode.rb', line 37

def text
  doc = Nokogiri::HTML.fragment(@body_html)
  lines = []
  lines << doc.at(".p-novel__title")&.text&.strip
  doc.search(".p-novel__text p").each do |p|
    lines << p.text
  end
  lines.compact.join("\n")
end