Class: Wiki::Yggdrasil::Article
- Inherits:
-
Object
- Object
- Wiki::Yggdrasil::Article
- Defined in:
- lib/wiki/article.rb
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
- #child_links ⇒ Object
- #format_links(anchors: self.scrape_all_summary_links) ⇒ Object
-
#initialize(uri:) ⇒ Article
constructor
A new instance of Article.
- #name ⇒ Object
- #scrape_all_summary_links ⇒ Object
- #summary ⇒ Object
Constructor Details
#initialize(uri:) ⇒ Article
Returns a new instance of Article.
9 10 11 12 13 14 15 |
# File 'lib/wiki/article.rb', line 9 def initialize(uri:) raise ArgumentError unless Wiki::Yggdrasil::Article.is_valid_wiki_article?(uri: uri) @uri = uri @summary = nil @child_links = nil @name = nil end |
Instance Attribute Details
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
7 8 9 |
# File 'lib/wiki/article.rb', line 7 def uri @uri end |
Class Method Details
.is_valid_wiki_article?(uri:) ⇒ Boolean
44 45 46 47 |
# File 'lib/wiki/article.rb', line 44 def self.is_valid_wiki_article?(uri:) ## Is this URI a wikipedia article? uri =~ /.*wikipedia\.org\/wiki\/.+/ ? true : false end |
Instance Method Details
#child_links ⇒ Object
21 22 23 24 25 |
# File 'lib/wiki/article.rb', line 21 def child_links formatted_links = format_links validated_links = formatted_links.select { |uri| Wiki::Yggdrasil::Article.is_valid_wiki_article?(uri: uri) } @child_links ||= validated_links end |
#format_links(anchors: self.scrape_all_summary_links) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/wiki/article.rb', line 36 def format_links(anchors: self.scrape_all_summary_links) uris = anchors.map do |anchor| anchor.nil? || anchor['href'].nil? ? next : 'https://en.wikipedia.org' << anchor['href'] ## nil href attributes are often self refs (but possibly not always). Ignore them. end uris.compact end |
#name ⇒ Object
31 32 33 34 |
# File 'lib/wiki/article.rb', line 31 def name @name ||= Nokogiri::HTML(open(self.uri)).css('#firstHeading').inner_html ## TODO: Cleanup end |
#scrape_all_summary_links ⇒ Object
27 28 29 |
# File 'lib/wiki/article.rb', line 27 def scrape_all_summary_links self.summary.css('p a') end |
#summary ⇒ Object
17 18 19 |
# File 'lib/wiki/article.rb', line 17 def summary @summary ||= Nokogiri::HTML(Nokogiri::HTML(open(self.uri)).to_s.split('<div id="toc" class="toc">')[0]).css('p') ## TODO: Cleanup end |