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
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 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 |
.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 |
.print_articles ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/mmonews/article.rb', line 30 def self.print_articles self.all.each_with_index do |article, i| puts "#{i+1}. #{article.title}" puts "\t#{article.author} - #{article.date_published} - #{article.source}" puts "#{article.summary}\n" end end |
Instance Method Details
#fetch_article ⇒ Object
24 25 26 27 28 |
# File 'lib/mmonews/article.rb', line 24 def fetch_article data = Scraper.new(url).document self.content = data.search('div.commonContent').text.strip.gsub(/[\t\r\n]/, '') self end |
#print_full ⇒ Object
38 39 40 41 42 |
# File 'lib/mmonews/article.rb', line 38 def print_full puts "#{self.title}" puts "\t#{self.author} - #{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 |