Class: Reddit::Comment

Inherits:
Thing show all
Includes:
JsonListing
Defined in:
lib/ruby_reddit_api/comment.rb

Overview

Author:

  • James Cook

Instance Attribute Summary collapse

Attributes inherited from Base

#last_action

Instance Method Summary collapse

Methods included from JsonListing

included

Methods inherited from Thing

#downvote, #id, #report, #toggle, #upvote

Methods inherited from Base

base_headers, #base_headers, #cookie, #logged_in?, #login, #logout, #modhash, #read, #user, #user_agent, user_agent, #user_id

Constructor Details

#initialize(json, options = {}) ⇒ Comment

Returns a new instance of Comment.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_reddit_api/comment.rb', line 7

def initialize(json, options={})
  mode = options.fetch(:mode){ :json }
  if mode == :json
    parse(json)
  end

  if mode == :replies
    json.keys.each do |key|
      instance_variable_set("@#{key}", json[key])
    end
  end

  if replies.is_a?(Hash)
    handle_replies(replies)
  end

  @debug = StringIO.new
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def author
  @author
end

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def body
  @body
end

#body_htmlObject (readonly)

Returns the value of attribute body_html.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def body_html
  @body_html
end

#createdObject (readonly)

Returns the value of attribute created.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def created
  @created
end

#created_utcObject (readonly)

Returns the value of attribute created_utc.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def created_utc
  @created_utc
end

#debugObject (readonly)

Returns the value of attribute debug.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def debug
  @debug
end

#downsObject (readonly)

Returns the value of attribute downs.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def downs
  @downs
end

#kindObject (readonly)

Returns the value of attribute kind.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def kind
  @kind
end

#likesObject (readonly)

Returns the value of attribute likes.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def likes
  @likes
end

Returns the value of attribute link_id.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def link_id
  @link_id
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def name
  @name
end

#num_commentsObject (readonly)

Returns the value of attribute num_comments.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def num_comments
  @num_comments
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def parent_id
  @parent_id
end

#repliesObject (readonly)

Returns the value of attribute replies.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def replies
  @replies
end

#subredditObject (readonly)

Returns the value of attribute subreddit.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def subreddit
  @subreddit
end

#subreddit_idObject (readonly)

Returns the value of attribute subreddit_id.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def subreddit_id
  @subreddit_id
end

#upsObject (readonly)

Returns the value of attribute ups.



6
7
8
# File 'lib/ruby_reddit_api/comment.rb', line 6

def ups
  @ups
end

Instance Method Details

#add_distinction(verb) ⇒ Object



106
107
108
109
# File 'lib/ruby_reddit_api/comment.rb', line 106

def add_distinction(verb)
  resp=self.class.post("/api/distinguish/#{verb}", {:body => {:id => id, :uh => modhash, :r => subreddit, :executed => "distinguishing..."}, :headers => base_headers, :debug_output => @debug })
  resp.code == 200
end

#admin_distinguishObject



78
79
80
# File 'lib/ruby_reddit_api/comment.rb', line 78

def admin_distinguish
  add_distinction "admin"
end

#approvetrue, false

Approve a comment

Returns:

  • (true, false)


69
70
71
72
# File 'lib/ruby_reddit_api/comment.rb', line 69

def approve
  resp=self.class.post("/api/approve", {:body => {:id => id, :uh => modhash, :r => subreddit}, :headers => base_headers, :debug_output => @debug })
  resp.code == 200
end

#commentsArray<Reddit::Comment>

Fetch comments

Returns:



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

def comments
  opts = {:handler => "Comment",
          :verb => "post",
          :body =>
            {:link_id => id, :depth => 10, :r => subreddit, :uh => modhash, :renderstyle => "json", :pv_hex => "", :id => id}
          }
  return read("/api/morechildren", opts )
end

#edit(newtext) ⇒ true, false

Modify a comment

Returns:

  • (true, false)


48
49
50
51
# File 'lib/ruby_reddit_api/comment.rb', line 48

def edit(newtext)
  resp=self.class.post("/api/editusertext", {:body => {:thing_id => id, :uh => modhash, :r => subreddit, :text => newtext }, :headers => base_headers, :debug_output => @debug })
  resp.code == 200
end

#handle_replies(data) ⇒ true

Parse nested comment hashes into a Comment

Parameters:

  • JSON (Hash)

    containing reply data

Returns:

  • (true)


114
115
116
117
118
119
120
121
122
# File 'lib/ruby_reddit_api/comment.rb', line 114

def handle_replies(data)
   dup = data.dup
   _kind, data       = dup["kind"],     dup["data"]
   modhash, children = data["modhash"], data["children"]
   if children.is_a?(Array)
     @replies = children.map{|reply| Comment.new(reply["data"], :mode => :replies) }
   end
   true
end

#hidetrue, false

Hide a comment

Returns:

  • (true, false)


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

def hide
  resp=self.class.post("/api/del", {:body => {:id => id, :uh => modhash, :r => subreddit, :executed => "removed" }, :headers => base_headers, :debug_output => @debug })
  resp.code == 200
end

#indistinguishObject



82
83
84
# File 'lib/ruby_reddit_api/comment.rb', line 82

def indistinguish
  add_distinction "no"
end

#inspectObject



26
27
28
# File 'lib/ruby_reddit_api/comment.rb', line 26

def inspect
  "<Reddit::Comment author='#{@author}' body='#{short_body}' replies='#{replies.size}' ups='#{ups}' downs='#{downs}'>"
end

#moderator_distinguishObject



74
75
76
# File 'lib/ruby_reddit_api/comment.rb', line 74

def moderator_distinguish
  add_distinction "yes"
end

#removetrue, false

Remove a comment

Returns:

  • (true, false)


62
63
64
65
# File 'lib/ruby_reddit_api/comment.rb', line 62

def remove
  resp=self.class.post("/api/remove", {:body => {:id => id, :uh => modhash, :r => subreddit}, :headers => base_headers, :debug_output => @debug })
  resp.code == 200
end

#reply(text) ⇒ true, false

Reply to another comment

Returns:

  • (true, false)


88
89
90
91
# File 'lib/ruby_reddit_api/comment.rb', line 88

def reply(text)
  resp = self.class.post("/api/comment", {:body => {:thing_id => id, :text => text, :uh => modhash, :r => subreddit }, :headers => base_headers, :debug_output => @debug })
  resp.code == 200
end

#short_bodyString

Trimmed comment body suitable for #inspect

Returns:

  • (String)


95
96
97
98
99
100
101
102
103
104
# File 'lib/ruby_reddit_api/comment.rb', line 95

def short_body
  str = body.to_s.strip
  if str.size > 15
    sb = str[0..15] + "..."
  else
    sb = body
  end

  sb.gsub(/[\n\r]/,'\n')
end

#to_sString

Returns:

  • (String)


31
32
33
# File 'lib/ruby_reddit_api/comment.rb', line 31

def to_s
  body
end