Class: RubyHackernews::Comment

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruby-hackernews/domain/comment/comment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text_html, voting, user_info, reply_link, absolute_link, parent_link) ⇒ Comment

Returns a new instance of Comment.



12
13
14
15
16
17
18
19
20
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 12

def initialize(text_html, voting, , reply_link, absolute_link, parent_link)
  @text_html = text_html
  @voting = voting
  @user = 
  @reply_link = reply_link
  @absolute_link = absolute_link
  @parent_link = parent_link
  @children = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



51
52
53
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 51

def method_missing(method, *args, &block)
  @children.send(method, *args, &block)
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 10

def parent
  @parent
end

#text_htmlObject (readonly)

Returns the value of attribute text_html.



6
7
8
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 6

def text_html
  @text_html
end

#userObject (readonly)

Returns the value of attribute user.



8
9
10
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 8

def user
  @user
end

#votingObject (readonly)

Returns the value of attribute voting.



7
8
9
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 7

def voting
  @voting
end

Class Method Details

.find(id) ⇒ Object



65
66
67
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 65

def self.find(id)
  return CommentService.new.find_by_id(id)
end

.newest(pages = 1) ⇒ Object



55
56
57
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 55

def self.newest(pages = 1)
  return CommentService.new.get_new_comments(pages)
end

.newest_with_extra(pages = 1, url = nil) ⇒ Object



59
60
61
62
63
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 59

def self.newest_with_extra(pages = 1, url = nil)
  args = [pages]
  args << url unless url.nil?
  return CommentService.new.get_new_comments_with_extra *args
end

Instance Method Details

#<<(comment) ⇒ Object



38
39
40
41
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 38

def <<(comment)
  comment.parent = self
  @children << comment
end

#<=>(other_comment) ⇒ Object



47
48
49
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 47

def <=>(other_comment)
  return other_comment.voting.score <=> @voting.score
end

#downvoteObject



79
80
81
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 79

def downvote
  VotingService.new.vote(@voting.downvote)
end

#each(&block) ⇒ Object



43
44
45
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 43

def each(&block)
  @children.each(&block)
end

#idObject



22
23
24
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 22

def id
  return @absolute_link.split("=")[1].to_i
end

#parent_idObject



26
27
28
29
30
31
32
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 26

def parent_id
  if parent
    parent.id
  elsif @parent_link
    @parent_link[/\d+/].to_i
  end
end

#reply(text) ⇒ Object



69
70
71
72
73
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 69

def reply(text)
  return false unless @reply_link
  CommentService.new.write_comment(@reply_link, text)
  return true
end

#textObject



34
35
36
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 34

def text
  @text ||= text_html.gsub(/<.{1,2}>/, "")
end

#upvoteObject



75
76
77
# File 'lib/ruby-hackernews/domain/comment/comment.rb', line 75

def upvote
  VotingService.new.vote(@voting.upvote)
end