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(help: false) ⇒ Object
- #format_links(anchors: self.scrape_links) ⇒ Object
-
#initialize(uri:) ⇒ Article
constructor
A new instance of Article.
- #name ⇒ Object
-
#scrape_links(help_links: false) ⇒ Object
TODO test help_links param in spec.
- #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
50 51 52 |
# File 'lib/wiki/article.rb', line 50 def self.is_valid_wiki_article?(uri:) uri =~ /.*wikipedia\.org\/wiki\/.+/ ? true : false end |
.remove_italic_tags(uri_list) ⇒ Object
46 47 48 |
# File 'lib/wiki/article.rb', line 46 def self.(uri_list) end |
Instance Method Details
#child_links(help: false) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/wiki/article.rb', line 21 def child_links(help: false) 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_links) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/wiki/article.rb', line 37 def format_links(anchors: self.scrape_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. ## TODO: take care of this in .scrape_links with a css selector (like the Help:IPA links) end uris.compact end |
#name ⇒ Object
32 33 34 35 |
# File 'lib/wiki/article.rb', line 32 def name @name ||= Nokogiri::HTML(open(self.uri)).css('#firstHeading').inner_html ## TODO: Cleanup end |
#scrape_links(help_links: false) ⇒ Object
TODO test help_links param in spec
28 29 30 |
# File 'lib/wiki/article.rb', line 28 def scrape_links(help_links: false) ## TODO test help_links param in spec help_links ? self.summary.css('p a') : self.summary.css('p a[href!="/wiki/Help:IPA/English"]') 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 |