Class: Reddit::Comment
- Includes:
- JsonListing, Voteable
- 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 included from Voteable
Methods inherited from Thing
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, ={}) 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.
7 8 9 |
# File 'lib/ruby_reddit_api/comment.rb', line 7 def @author end |
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/ruby_reddit_api/comment.rb', line 7 def body @body end |
#body_html ⇒ Object (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 |
#created ⇒ Object (readonly)
Returns the value of attribute created.
7 8 9 |
# File 'lib/ruby_reddit_api/comment.rb', line 7 def created @created end |
#created_utc ⇒ Object (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 |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
7 8 9 |
# File 'lib/ruby_reddit_api/comment.rb', line 7 def debug @debug end |
#downs ⇒ Object (readonly)
Returns the value of attribute downs.
7 8 9 |
# File 'lib/ruby_reddit_api/comment.rb', line 7 def downs @downs end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
7 8 9 |
# File 'lib/ruby_reddit_api/comment.rb', line 7 def kind @kind end |
#likes ⇒ Object (readonly)
Returns the value of attribute likes.
7 8 9 |
# File 'lib/ruby_reddit_api/comment.rb', line 7 def likes @likes end |
#link_id ⇒ Object (readonly)
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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/ruby_reddit_api/comment.rb', line 7 def name @name end |
#num_comments ⇒ Object (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_id ⇒ Object (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 |
#replies ⇒ Object (readonly)
Returns the value of attribute replies.
7 8 9 |
# File 'lib/ruby_reddit_api/comment.rb', line 7 def replies @replies end |
#subreddit ⇒ Object (readonly)
Returns the value of attribute subreddit.
7 8 9 |
# File 'lib/ruby_reddit_api/comment.rb', line 7 def subreddit @subreddit end |
#subreddit_id ⇒ Object (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 |
#ups ⇒ Object (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_distinguish ⇒ Object
79 80 81 |
# File 'lib/ruby_reddit_api/comment.rb', line 79 def admin_distinguish add_distinction "admin" end |
#approve ⇒ true, false
Approve a comment
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 |
#comments ⇒ Array<Reddit::Comment>
Fetch comments
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
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
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 |
#hide ⇒ true, false
Hide a comment
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 |
#indistinguish ⇒ Object
83 84 85 |
# File 'lib/ruby_reddit_api/comment.rb', line 83 def indistinguish add_distinction "no" end |
#inspect ⇒ Object
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_distinguish ⇒ Object
75 76 77 |
# File 'lib/ruby_reddit_api/comment.rb', line 75 def moderator_distinguish add_distinction "yes" end |
#remove ⇒ true, false
Remove a comment
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
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_body ⇒ String
Trimmed comment body suitable for #inspect
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_s ⇒ String
32 33 34 |
# File 'lib/ruby_reddit_api/comment.rb', line 32 def to_s body end |