Class: NHKore::Defn

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

Overview

Author:

  • Jonathan Bradley Whited

Since:

  • 0.2.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefn

Returns a new instance of Defn.

Since:

  • 0.2.0



28
29
30
31
32
33
34
# File 'lib/nhkore/defn.rb', line 28

def initialize
  super()

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

Instance Attribute Details

#hyoukisObject (readonly)

Since:

  • 0.2.0



24
25
26
# File 'lib/nhkore/defn.rb', line 24

def hyoukis
  @hyoukis
end

#textObject

Since:

  • 0.2.0



25
26
27
# File 'lib/nhkore/defn.rb', line 25

def text
  @text
end

#wordsObject (readonly)

Since:

  • 0.2.0



26
27
28
# File 'lib/nhkore/defn.rb', line 26

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.

Since:

  • 0.2.0



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
88
89
90
91
# File 'lib/nhkore/defn.rb', line 37

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

Since:

  • 0.2.0



93
94
95
# File 'lib/nhkore/defn.rb', line 93

def to_s
  return @text
end