Class: Mmonews::Article

Inherits:
Object
  • Object
show all
Defined in:
lib/mmonews/article.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#authorObject

Returns the value of attribute author.



3
4
5
# File 'lib/mmonews/article.rb', line 3

def author
  @author
end

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/mmonews/article.rb', line 3

def content
  @content
end

#date_publishedObject

Returns the value of attribute date_published.



3
4
5
# File 'lib/mmonews/article.rb', line 3

def date_published
  @date_published
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/mmonews/article.rb', line 3

def source
  @source
end

#summaryObject

Returns the value of attribute summary.



3
4
5
# File 'lib/mmonews/article.rb', line 3

def summary
  @summary
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/mmonews/article.rb', line 3

def title
  @title
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/mmonews/article.rb', line 3

def url
  @url
end

Class Method Details

.allObject



12
13
14
# File 'lib/mmonews/article.rb', line 12

def self.all
  @@all
end

.countObject



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


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.author} - #{article.date_published} - #{article.source}"
    puts "#{article.summary}\n"
  end
end

Instance Method Details

#fetch_articleObject



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


46
47
48
49
50
# File 'lib/mmonews/article.rb', line 46

def print_full
  puts self.title
  puts "\t#{self.author} - #{self.date_published} - #{self.source}"
  puts self.content
end

#saveObject



16
17
18
# File 'lib/mmonews/article.rb', line 16

def save
  self.class.all << self
end