Class: Mmonews::Article
- Inherits:
-
Object
- Object
- Mmonews::Article
- Defined in:
- lib/mmonews/article.rb
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#author ⇒ Object
Returns the value of attribute author.
-
#content ⇒ Object
Returns the value of attribute content.
-
#date_published ⇒ Object
Returns the value of attribute date_published.
-
#source ⇒ Object
Returns the value of attribute source.
-
#summary ⇒ Object
Returns the value of attribute summary.
-
#title ⇒ Object
Returns the value of attribute title.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
- .all ⇒ Object
- .count ⇒ Object
- .create(article_hash) ⇒ Object
- .find(id) ⇒ Object
- .print_articles ⇒ Object
Instance Method Summary collapse
- #fetch_article ⇒ Object
-
#initialize(article_attributes) ⇒ Article
constructor
A new instance of Article.
- #print_full ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(article_attributes) ⇒ Article
Returns a new instance of Article.
8 9 10 |
# File 'lib/mmonews/article.rb', line 8 def initialize(article_attributes) article_attributes.each{ |key, value| send("#{key}=", value) } end |
Instance Attribute Details
#author ⇒ Object
Returns the value of attribute author.
3 4 5 |
# File 'lib/mmonews/article.rb', line 3 def @author end |
#content ⇒ Object
Returns the value of attribute content.
3 4 5 |
# File 'lib/mmonews/article.rb', line 3 def content @content end |
#date_published ⇒ Object
Returns the value of attribute date_published.
3 4 5 |
# File 'lib/mmonews/article.rb', line 3 def date_published @date_published end |
#source ⇒ Object
Returns the value of attribute source.
3 4 5 |
# File 'lib/mmonews/article.rb', line 3 def source @source end |
#summary ⇒ Object
Returns the value of attribute summary.
3 4 5 |
# File 'lib/mmonews/article.rb', line 3 def summary @summary end |
#title ⇒ Object
Returns the value of attribute title.
3 4 5 |
# File 'lib/mmonews/article.rb', line 3 def title @title end |
#url ⇒ Object
Returns the value of attribute url.
3 4 5 |
# File 'lib/mmonews/article.rb', line 3 def url @url end |
Class Method Details
.all ⇒ Object
12 13 14 |
# File 'lib/mmonews/article.rb', line 12 def self.all @@all end |
.count ⇒ Object
28 29 30 |
# File 'lib/mmonews/article.rb', line 28 def self.count self.all.length end |
.create(article_hash) ⇒ Object
20 21 22 |
# File 'lib/mmonews/article.rb', line 20 def self.create(article_hash) self.new(article_hash).tap{ |s| s.save } end |
.find(id) ⇒ Object
24 25 26 |
# File 'lib/mmonews/article.rb', line 24 def self.find(id) self.all[id - 1] end |
.print_articles ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/mmonews/article.rb', line 38 def self.print_articles self.all.each_with_index do |article, i| puts "#{i+1}. #{article.title}" puts "\t#{article.} - #{article.date_published} - #{article.source}" puts "#{article.summary}\n" end end |
Instance Method Details
#fetch_article ⇒ Object
32 33 34 35 36 |
# File 'lib/mmonews/article.rb', line 32 def fetch_article data = Mmonews::Scraper.new(url).document self.content = data.search('div.commonContent').text.strip.gsub(/[\t\r\n]/, '') self end |
#print_full ⇒ Object
46 47 48 49 50 |
# File 'lib/mmonews/article.rb', line 46 def print_full puts self.title puts "\t#{self.} - #{self.date_published} - #{self.source}" puts self.content end |
#save ⇒ Object
16 17 18 |
# File 'lib/mmonews/article.rb', line 16 def save self.class.all << self end |