Class: NHKore::Defn

Inherits:
Object
  • Object
show all
Defined in:
lib/nhkore/defn.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefn

Returns a new instance of Defn.



24
25
26
27
28
29
30
# File 'lib/nhkore/defn.rb', line 24

def initialize
  super

  @hyoukis = []
  @text = ''.dup
  @words = []
end

Instance Attribute Details

#hyoukisObject (readonly)

Returns the value of attribute hyoukis.



20
21
22
# File 'lib/nhkore/defn.rb', line 20

def hyoukis
  @hyoukis
end

#textObject

Returns the value of attribute text.



21
22
23
# File 'lib/nhkore/defn.rb', line 21

def text
  @text
end

#wordsObject (readonly)

Returns the value of attribute words.



22
23
24
# File 'lib/nhkore/defn.rb', line 22

def words
  @words
end

Class Method Details

.scrape(hash, missingno: nil, url: nil) ⇒ Object

If no data, don’t raise errors; don’t care if have a definition or not.



33
34
35
36
37
38
39
40
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/nhkore/defn.rb', line 33

def self.scrape(hash,missingno: nil,url: nil)
  defn = Defn.new

  hyoukis = hash['hyouki']

  hyoukis&.each() do |hyouki|
    next if hyouki.nil?
    next if (hyouki = Util.strip_web_str(hyouki)).empty?

    defn.hyoukis << hyouki
  end

  def_str = hash['def']

  if Util.empty_web_str?(def_str)
    return defn.hyoukis.empty? ? nil : defn
  end

  doc = Nokogiri::HTML(def_str)
  doc = doc.css('body') # Auto-added by Nokogiri.

  doc.children.each do |child|
    name = Util.unspace_web_str(child.name).downcase if child.respond_to?(:name)

    is_text = false
    words = []

    if name == 'ruby'
      # Returns an array.
      words = Word.scrape_ruby_tag(child,missingno: missingno,url: url)
    elsif child.respond_to?(:text) # Don't do child.text?(), just want content.
      words << Word.scrape_text_node(child,url: url)
      is_text = true
    end

    # All word-scraping methods can return nil,
    #   so remove all nils for empty?() check.
    words = words&.compact

    if words.nil? || words.empty?
      defn.text << Util.reduce_jpn_space(child.text) if is_text
    else
      words.each do |word|
        defn.text << Util.reduce_jpn_space(word.word)
        defn.words << word unless Util.empty_web_str?(word.word)
      end
    end
  end

  return nil if defn.hyoukis.empty? && defn.words.empty?

  defn.text = Util.strip_web_str(defn.text)

  return defn
end

Instance Method Details

#to_sObject



89
90
91
# File 'lib/nhkore/defn.rb', line 89

def to_s
  return @text
end