Class: Reddit::Thing

Inherits:
Base
  • Object
show all
Includes:
JsonListing
Defined in:
lib/ruby_reddit_api/thing.rb

Overview

Author:

  • James Cook

Direct Known Subclasses

Comment, Message, MessageGroup, Submission

Instance Attribute Summary

Attributes inherited from Base

#debug, #last_action

Instance Method Summary collapse

Methods included from JsonListing

included

Methods inherited from Base

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

Constructor Details

#initialize(data) ⇒ Thing

Returns a new instance of Thing.



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

def initialize(data)
  parse(data)
  @debug    = StringIO.new
end

Instance Method Details

#authorReddit::User

The author of the entity. The data is lazy-loaded and cached on the object

Returns:



19
20
21
22
# File 'lib/ruby_reddit_api/thing.rb', line 19

def author
  response = read("/user/#{@author}/about.json", :handler => "User") if @author
  @author_data ||= response[0] if response
end

#idString

The reddit ID of this entity

Returns:

  • (String)


13
14
15
# File 'lib/ruby_reddit_api/thing.rb', line 13

def id
  "#{kind}_#{@id}"
end

#reporttrue, false

Report thing

Returns:

  • (true, false)


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

def report
  toggle :report
end

#toggle(which) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/ruby_reddit_api/thing.rb', line 30

def toggle(which)
  return false unless logged_in?
  mapping = {:save => "save", :unsave => "unsave", :hide => "hidden", :unhide => "unhidden", :report => "report"}
  mode = mapping[which]
  resp = self.class.post("/api/#{which}", {:body => {:id => id, :uh => modhash, :r => subreddit, :executed => mode }, :headers => base_headers, :debug_output => @debug })
  resp.code == 200
end