Class: Wiki::Yggdrasil::Yggdrasil

Inherits:
Object
  • Object
show all
Defined in:
lib/wiki/yggdrasil.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:) ⇒ Yggdrasil

Returns a new instance of Yggdrasil.



10
11
12
13
# File 'lib/wiki/yggdrasil.rb', line 10

def initialize(uri:)
  @root     = Wiki::Yggdrasil::Article.new(uri: uri)
  @children = nil
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/wiki/yggdrasil.rb', line 8

def root
  @root
end

Instance Method Details

#children(depth: 4, article_children: self.root.child_links) ⇒ Object



15
16
17
# File 'lib/wiki/yggdrasil.rb', line 15

def children(depth: 4, article_children: self.root.child_links)
  @children ||= { name: self.root.name, children: recursive_scrape(depth: depth), index: 0, depth: 0 }
end

#recursive_scrape(depth: 1, children: @root.child_links) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wiki/yggdrasil.rb', line 19

def recursive_scrape(depth: 1, children: @root.child_links)
  children.each_with_index.map do |uri, index|
    article = Wiki::Yggdrasil::Article.new(uri: uri)
    if (depth == 1)
      {
        name: article.name,
        index: index + 1,
        level: depth,
        children: [],
      }
    else
      {
        name: article.name,
        index: index + 1,
        level: depth,
        children: recursive_scrape(depth - 1, article.child_links),
      }
    end
  end
end