Class: Reddit::Comment

Inherits:
Thing show all
Includes:
JsonListing, Voteable
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 included from Voteable

included

Methods inherited from Thing

#id, #report, #toggle

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.



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

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.



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

def author
  @author
end

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#body_htmlObject (readonly)

Returns the value of attribute body_html.



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

def body_html
  @body_html
end

#createdObject (readonly)

Returns the value of attribute created.



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

def created
  @created
end

#created_utcObject (readonly)

Returns the value of attribute created_utc.



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

def created_utc
  @created_utc
end

#debugObject (readonly)

Returns the value of attribute debug.



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

def debug
  @debug
end

#downsObject (readonly)

Returns the value of attribute downs.



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

def downs
  @downs
end

#kindObject (readonly)

Returns the value of attribute kind.



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

def kind
  @kind
end

#likesObject (readonly)

Returns the value of attribute likes.



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

def likes
  @likes
end

Returns the value of attribute link_id.



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

def link_id
  @link_id
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#num_commentsObject (readonly)

Returns the value of attribute num_comments.



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

def num_comments
  @num_comments
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



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

def parent_id
  @parent_id
end

#repliesObject (readonly)

Returns the value of attribute replies.



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

def replies
  @replies
end

#subredditObject (readonly)

Returns the value of attribute subreddit.



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

def subreddit
  @subreddit
end

#subreddit_idObject (readonly)

Returns the value of attribute subreddit_id.



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

def subreddit_id
  @subreddit_id
end

#upsObject (readonly)

Returns the value of attribute ups.



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

def ups
  @ups
end

Instance Method Details

#add_distinction(verb) ⇒ Object



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

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



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

def admin_distinguish
  add_distinction "admin"
end

#approvetrue, false

Approve a comment

Returns:

  • (true, false)


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

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:



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

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)


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

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)


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

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)


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

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



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

def indistinguish
  add_distinction "no"
end

#inspectObject



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

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

#moderator_distinguishObject



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

def moderator_distinguish
  add_distinction "yes"
end

#removetrue, false

Remove a comment

Returns:

  • (true, false)


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

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)


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

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)


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

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)


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

def to_s
  body
end