Class: Reddit::Comment
- Includes:
- JsonListing
- Defined in:
- lib/ruby_reddit_api/comment.rb
Overview
Instance Attribute Summary collapse
-
#author ⇒ Object
readonly
Returns the value of attribute author.
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#body_html ⇒ Object
readonly
Returns the value of attribute body_html.
-
#created ⇒ Object
readonly
Returns the value of attribute created.
-
#created_utc ⇒ Object
readonly
Returns the value of attribute created_utc.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#downs ⇒ Object
readonly
Returns the value of attribute downs.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#likes ⇒ Object
readonly
Returns the value of attribute likes.
-
#link_id ⇒ Object
readonly
Returns the value of attribute link_id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#num_comments ⇒ Object
readonly
Returns the value of attribute num_comments.
-
#parent_id ⇒ Object
readonly
Returns the value of attribute parent_id.
-
#replies ⇒ Object
readonly
Returns the value of attribute replies.
-
#subreddit ⇒ Object
readonly
Returns the value of attribute subreddit.
-
#subreddit_id ⇒ Object
readonly
Returns the value of attribute subreddit_id.
-
#ups ⇒ Object
readonly
Returns the value of attribute ups.
Attributes inherited from Base
Instance Method Summary collapse
- #add_distinction(verb) ⇒ Object
- #admin_distinguish ⇒ Object
-
#approve ⇒ true, false
Approve a comment.
-
#comments ⇒ Array<Reddit::Comment>
Fetch comments.
-
#edit(newtext) ⇒ true, false
Modify a comment.
-
#handle_replies(data) ⇒ true
Parse nested comment hashes into a Comment.
-
#hide ⇒ true, false
Hide a comment.
- #indistinguish ⇒ Object
-
#initialize(json, options = {}) ⇒ Comment
constructor
A new instance of Comment.
- #inspect ⇒ Object
- #moderator_distinguish ⇒ Object
-
#remove ⇒ true, false
Remove a comment.
-
#reply(text) ⇒ true, false
Reply to another comment.
-
#short_body ⇒ String
Trimmed comment body suitable for #inspect.
- #to_s ⇒ String
Methods included from JsonListing
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, ={}) mode = .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
#author ⇒ Object (readonly)
Returns the value of attribute author.
6 7 8 |
# File 'lib/ruby_reddit_api/comment.rb', line 6 def @author end |
#body ⇒ Object (readonly)
Returns the value of attribute body.
6 7 8 |
# File 'lib/ruby_reddit_api/comment.rb', line 6 def body @body end |
#body_html ⇒ Object (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 |
#created ⇒ Object (readonly)
Returns the value of attribute created.
6 7 8 |
# File 'lib/ruby_reddit_api/comment.rb', line 6 def created @created end |
#created_utc ⇒ Object (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 |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
6 7 8 |
# File 'lib/ruby_reddit_api/comment.rb', line 6 def debug @debug end |
#downs ⇒ Object (readonly)
Returns the value of attribute downs.
6 7 8 |
# File 'lib/ruby_reddit_api/comment.rb', line 6 def downs @downs end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
6 7 8 |
# File 'lib/ruby_reddit_api/comment.rb', line 6 def kind @kind end |
#likes ⇒ Object (readonly)
Returns the value of attribute likes.
6 7 8 |
# File 'lib/ruby_reddit_api/comment.rb', line 6 def likes @likes end |
#link_id ⇒ Object (readonly)
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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/ruby_reddit_api/comment.rb', line 6 def name @name end |
#num_comments ⇒ Object (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_id ⇒ Object (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 |
#replies ⇒ Object (readonly)
Returns the value of attribute replies.
6 7 8 |
# File 'lib/ruby_reddit_api/comment.rb', line 6 def replies @replies end |
#subreddit ⇒ Object (readonly)
Returns the value of attribute subreddit.
6 7 8 |
# File 'lib/ruby_reddit_api/comment.rb', line 6 def subreddit @subreddit end |
#subreddit_id ⇒ Object (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 |
#ups ⇒ Object (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_distinguish ⇒ Object
78 79 80 |
# File 'lib/ruby_reddit_api/comment.rb', line 78 def admin_distinguish add_distinction "admin" end |
#approve ⇒ true, false
Approve a comment
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 |
#comments ⇒ Array<Reddit::Comment>
Fetch comments
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
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
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 |
#hide ⇒ true, false
Hide a comment
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 |
#indistinguish ⇒ Object
82 83 84 |
# File 'lib/ruby_reddit_api/comment.rb', line 82 def indistinguish add_distinction "no" end |
#inspect ⇒ Object
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_distinguish ⇒ Object
74 75 76 |
# File 'lib/ruby_reddit_api/comment.rb', line 74 def moderator_distinguish add_distinction "yes" end |
#remove ⇒ true, false
Remove a comment
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
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_body ⇒ String
Trimmed comment body suitable for #inspect
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_s ⇒ String
31 32 33 |
# File 'lib/ruby_reddit_api/comment.rb', line 31 def to_s body end |