Class: TildeScraper::Comment

Inherits:
Object
  • Object
show all
Extended by:
Memorable::ClassMethods
Includes:
Memorable::InstanceMethods
Defined in:
lib/tilde_scraper/comment.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Memorable::ClassMethods

create, create_from_array

Methods included from Memorable::InstanceMethods

#add_attributes

Constructor Details

#initialize(attributes) ⇒ Comment

Returns a new instance of Comment.



9
10
11
# File 'lib/tilde_scraper/comment.rb', line 9

def initialize(attributes)
  add_attributes(attributes.reject { |key, val| key == :children })
end

Instance Attribute Details

#ageObject

Returns the value of attribute age.



5
6
7
# File 'lib/tilde_scraper/comment.rb', line 5

def age
  @age
end

#authorObject

Returns the value of attribute author.



5
6
7
# File 'lib/tilde_scraper/comment.rb', line 5

def author
  @author
end

#childrenObject

Returns the value of attribute children.



5
6
7
# File 'lib/tilde_scraper/comment.rb', line 5

def children
  @children
end

#levelObject

Returns the value of attribute level.



5
6
7
# File 'lib/tilde_scraper/comment.rb', line 5

def level
  @level
end

#textObject

Returns the value of attribute text.



5
6
7
# File 'lib/tilde_scraper/comment.rb', line 5

def text
  @text
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/tilde_scraper/comment.rb', line 5

def url
  @url
end

#votesObject

Returns the value of attribute votes.



5
6
7
# File 'lib/tilde_scraper/comment.rb', line 5

def votes
  @votes
end

Class Method Details

.allObject



60
61
62
# File 'lib/tilde_scraper/comment.rb', line 60

def self.all
  @@all
end

.all_topObject



56
57
58
# File 'lib/tilde_scraper/comment.rb', line 56

def self.all_top
  @@all.select { |comment| comment.level == 0 }
end

.create_from_array(array) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/tilde_scraper/comment.rb', line 13

def self.create_from_array(array)
  array.map do |comment_hash|
    comment = create(comment_hash.reject { |key, val| key == :children })
    comment.children = self.create_from_array(comment_hash[:children])
    comment
  end
end

.display(array, indent = 0) ⇒ Object



49
50
51
52
53
54
# File 'lib/tilde_scraper/comment.rb', line 49

def self.display(array, indent = 0)
  array.each do |comment|
    comment.display(indent)
    display(comment.children, indent + 1)
  end
end

.display_page(url) ⇒ Object



45
46
47
# File 'lib/tilde_scraper/comment.rb', line 45

def self.display_page(url)
  display(find_top_by_url(url))
end

.find_by_url(url) ⇒ Object



21
22
23
# File 'lib/tilde_scraper/comment.rb', line 21

def self.find_by_url(url)
  all.select { |comment| comment.url == url }
end

.find_top_by_url(url) ⇒ Object



25
26
27
# File 'lib/tilde_scraper/comment.rb', line 25

def self.find_top_by_url(url)
  all.select { |comment| comment.url == url && comment.level == 0}
end

Instance Method Details

#display(indent = 0) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/tilde_scraper/comment.rb', line 29

def display(indent = 0)
  indent(indent)
  puts self.author
  display_text(indent)
  indent(indent)
  puts "Votes: " + self.votes if self.votes
  puts "-" * 10
end

#display_text(indent = 0) ⇒ Object



38
39
40
41
42
43
# File 'lib/tilde_scraper/comment.rb', line 38

def display_text(indent = 0)
  self.text.split("\n").each do |line|
    indent(indent)
    puts line
  end
end