Class: Article

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

Constant Summary collapse

@@articles =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = nil, url = nil, author = nil, story = nil) ⇒ Article

Returns a new instance of Article.



8
9
10
11
12
13
# File 'lib/article.rb', line 8

def initialize(title = nil, url = nil, author = nil,  story =  nil)
  @title = title
  @author = author
  @url = url
  @story = story
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



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

def author
  @author
end

#storyObject

Returns the value of attribute story.



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

def story
  @story
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.allObject



27
28
29
# File 'lib/article.rb', line 27

def self.all
  @@articles
end

.create_article_from_hash(hash) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/article.rb', line 32

def self.create_article_from_hash(hash)
  a = Article.new
  a.title = hash[:title]
  a.url = hash[:url]
  a.author = hash[:author]
  string = hash[:story]
  a.story = self.word_wrap(hash[:story])
  a
end

.create_articles_from_array(array) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/article.rb', line 16

def self.create_articles_from_array(array)

  array.each do |article|
    title = article[:title]
    url = article[:url]
    a =  Article.new(title, url)
    @@articles << a
  end
  @@articles
end

.p_wrap(s, width = 78) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/article.rb', line 59

def self.p_wrap(s, width=78)
  lines = []
  line = ""
  s.split(/\s+/).each do |word|
    if line.size + word.size >= width
      lines << line
      line = word
    elsif line.empty?
     line = word
    else
     line << " " << word
   end
   end
   lines << line if line
  return lines.join "\n"
end

.word_wrap(text) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/article.rb', line 42

def self.word_wrap(text)
 
  paragraphs = []

  paragraphs = text.split(/\n/)
  finshed_text = ""
  
  paragraphs.each do |p|
    final_p = self.p_wrap(p, 78)
    finshed_text << "\n" + final_p
  end
  finshed_text
end