Class: Redd::Objects::Subreddit

Inherits:
Thing
  • Object
show all
Includes:
Thing::Messageable, Thing::Refreshable
Defined in:
lib/redd/objects/subreddit.rb

Overview

TODO:

#subscribe! and #unsubscribe!

A comment made on links.

Instance Attribute Summary

Attributes inherited from Base

#client

Submissions collapse

Stylesheets collapse

Invites collapse

Flairs collapse

Listings collapse

Moderation collapse

Methods included from Thing::Refreshable

#refresh!

Methods included from Thing::Messageable

#send_message

Methods inherited from Thing

#==, #fullname

Methods inherited from Base

alias_property, #initialize

Constructor Details

This class inherits a constructor from Redd::Objects::Base

Instance Method Details

#accept_moderator_invite!Object

Accept a moderator invite from a subreddit.



101
102
103
# File 'lib/redd/objects/subreddit.rb', line 101

def accept_moderator_invite!
  post("/r/#{display_name}/api/accept_moderator_invite")
end

#add_comment(text) ⇒ Objects::Comment

Add a comment to the submission.

Parameters:

  • text (String)

    The text to comment.

Returns:



65
66
67
# File 'lib/redd/objects/subreddit.rb', line 65

def add_comment(text)
  client.add_comment(self, text)
end

#admin_aboutObjects::Base

Returns The current settings of a subreddit.

Returns:



264
265
266
# File 'lib/redd/objects/subreddit.rb', line 264

def admin_about
  client.request_object(:get, "/r/#{display_name}/about/edit.json")
end

#admin_edit(attributes) ⇒ Object

Note:

This method may make additional requests if not all of the required attributes are provided. Take a look at the source for the required attributes required to avoid making the additional request.

Edit the subreddit’s settings

Parameters:

  • attributes (Hash)

    The subreddit’s new settings.

See Also:



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/redd/objects/subreddit.rb', line 274

def admin_edit(attributes)
  params = {
    # Subreddit name
    sr: fullname,
    # Apparently useless options
    show_cname_sidebar: true,
    :"header-title" => title
  }

  required = %i(
    allow_top collapse_deleted_comments comment_score_hide_mins
    css_on_cname description exclude_banned_modqueue lang link_type name
    over_18 public_description public_traffic show_media spam_comments
    spam_links spam_selfposts submit_link_label submit_text
    submit_text_label title type wiki_edit_age wiki_edit_karma wikimode
  )

  if required.all? { |key| attributes.key?(key) }
    params.merge!(attributes)
  else
    about = admin_about
    final = about
            .select { |k, _| required.include?(k) }
            .merge(
              name: display_name,
              type: about[:subreddit_type],
              lang: about[:language],
              link_type: about[:content_options],
              allow_top: true,
              css_on_cname: true
            )
            .merge(attributes)
    params.merge!(final)
  end

  post('/api/site_admin', params)
end

#edit_stylesheet(contents, reason = nil) ⇒ Object

Note:

www.reddit.com/r/naut/about/stylesheet/ is a good place to test if you have an error.

Edit the subreddit’s stylesheet

Parameters:

  • contents (String)

    The new CSS.

  • reason (String) (defaults to: nil)

    Why you modified the stylesheet.

Author:

  • Takashi M (@beatak) and Avinash Dwarapu (@avidw)



90
91
92
93
94
# File 'lib/redd/objects/subreddit.rb', line 90

def edit_stylesheet(contents, reason = nil)
  params = { op: 'save', stylesheet_contents: contents }
  params[:reason] = reason if reason
  post("/r/#{display_name}/api/subreddit_stylesheet", params)
end

#get_comments(**params) ⇒ Objects::Listing<Objects::Thing>

Note:

The option :t only applies to the top and controversial sorts.

Get the appropriate listing.

Parameters:

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • String (String :before Return results before the given fullname.)

    :before Return results before the given fullname.

  • :count (Integer) — default: 0

    The number of items already seen in the listing.

  • :limit (1..100) — default: 25

    The maximum number of things to return.

  • :t (:hour, :day, :week, :month, :year, :all)

    The time period to consider when sorting.

Returns:



200
201
202
203
204
# File 'lib/redd/objects/subreddit.rb', line 200

%w(hot new top controversial comments).each do |sort|
  define_method :"get_#{sort}" do |**params|
    client.send(:"get_#{sort}", self, **params)
  end
end

#get_controversial(**params) ⇒ Objects::Listing<Objects::Thing>

Note:

The option :t only applies to the top and controversial sorts.

Get the appropriate listing.

Parameters:

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • String (String :before Return results before the given fullname.)

    :before Return results before the given fullname.

  • :count (Integer) — default: 0

    The number of items already seen in the listing.

  • :limit (1..100) — default: 25

    The maximum number of things to return.

  • :t (:hour, :day, :week, :month, :year, :all)

    The time period to consider when sorting.

Returns:



200
201
202
203
204
# File 'lib/redd/objects/subreddit.rb', line 200

%w(hot new top controversial comments).each do |sort|
  define_method :"get_#{sort}" do |**params|
    client.send(:"get_#{sort}", self, **params)
  end
end

#get_edited(**params) ⇒ Objects::Listing<Objects::Thing>

Get the appropriate moderator listing.

Parameters:

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • :before (String)

    Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..100)

    The maximum number of things to return.

  • :location (Object)

    No idea what this does.

  • :only (:links, :comments)

    The type of things to show.

Returns:

See Also:



255
256
257
258
259
260
261
# File 'lib/redd/objects/subreddit.rb', line 255

%w(reports spam modqueue unmoderated edited).each do |sort|
  define_method :"get_#{sort}" do |**params|
    client.request_object(
      :get, "/r/#{display_name}/about/#{sort}", params
    )
  end
end

#get_flair(user) ⇒ Hash?

Get the flair of a user.

Parameters:

Returns:

  • (Hash, nil)

    Flair info about the user or nil if nobody was found.



150
151
152
153
154
# File 'lib/redd/objects/subreddit.rb', line 150

def get_flair(user)
  username = client.property(user, :name)
  flair = get_flairlist(user: username).first
  flair if flair[:user].casecmp(username) == 0
end

#get_flairlist(**params) ⇒ Objects::Listing<Hash>

Get a list of everbody on the subreddit with a user flair.

Parameters:

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • :before (String)

    Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..1000)

    The maximum number of things to return.

  • :name (String)

    The username when getting the flair of just one user.

Returns:



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/redd/objects/subreddit.rb', line 133

def get_flairlist(**params)
  body = get("/r/#{display_name}/api/flairlist.json", params).body
  client.object_from_body(
    kind: 'Listing',
    data: {
      children: body[:users],
      before: body[:prev],
      after: body[:next]
    }
  )
end

#get_hot(**params) ⇒ Objects::Listing<Objects::Thing>

Note:

The option :t only applies to the top and controversial sorts.

Get the appropriate listing.

Parameters:

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • String (String :before Return results before the given fullname.)

    :before Return results before the given fullname.

  • :count (Integer) — default: 0

    The number of items already seen in the listing.

  • :limit (1..100) — default: 25

    The maximum number of things to return.

  • :t (:hour, :day, :week, :month, :year, :all)

    The time period to consider when sorting.

Returns:



200
201
202
203
204
# File 'lib/redd/objects/subreddit.rb', line 200

%w(hot new top controversial comments).each do |sort|
  define_method :"get_#{sort}" do |**params|
    client.send(:"get_#{sort}", self, **params)
  end
end

#get_modqueue(**params) ⇒ Objects::Listing<Objects::Thing>

Get the appropriate moderator listing.

Parameters:

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • :before (String)

    Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..100)

    The maximum number of things to return.

  • :location (Object)

    No idea what this does.

  • :only (:links, :comments)

    The type of things to show.

Returns:

See Also:



255
256
257
258
259
260
261
# File 'lib/redd/objects/subreddit.rb', line 255

%w(reports spam modqueue unmoderated edited).each do |sort|
  define_method :"get_#{sort}" do |**params|
    client.request_object(
      :get, "/r/#{display_name}/about/#{sort}", params
    )
  end
end

#get_new(**params) ⇒ Objects::Listing<Objects::Thing>

Note:

The option :t only applies to the top and controversial sorts.

Get the appropriate listing.

Parameters:

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • String (String :before Return results before the given fullname.)

    :before Return results before the given fullname.

  • :count (Integer) — default: 0

    The number of items already seen in the listing.

  • :limit (1..100) — default: 25

    The maximum number of things to return.

  • :t (:hour, :day, :week, :month, :year, :all)

    The time period to consider when sorting.

Returns:



200
201
202
203
204
# File 'lib/redd/objects/subreddit.rb', line 200

%w(hot new top controversial comments).each do |sort|
  define_method :"get_#{sort}" do |**params|
    client.send(:"get_#{sort}", self, **params)
  end
end

#get_reports(**params) ⇒ Objects::Listing<Objects::Thing>

Get the appropriate moderator listing.

Parameters:

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • :before (String)

    Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..100)

    The maximum number of things to return.

  • :location (Object)

    No idea what this does.

  • :only (:links, :comments)

    The type of things to show.

Returns:

See Also:



255
256
257
258
259
260
261
# File 'lib/redd/objects/subreddit.rb', line 255

%w(reports spam modqueue unmoderated edited).each do |sort|
  define_method :"get_#{sort}" do |**params|
    client.request_object(
      :get, "/r/#{display_name}/about/#{sort}", params
    )
  end
end

#get_spam(**params) ⇒ Objects::Listing<Objects::Thing>

Get the appropriate moderator listing.

Parameters:

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • :before (String)

    Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..100)

    The maximum number of things to return.

  • :location (Object)

    No idea what this does.

  • :only (:links, :comments)

    The type of things to show.

Returns:

See Also:



255
256
257
258
259
260
261
# File 'lib/redd/objects/subreddit.rb', line 255

%w(reports spam modqueue unmoderated edited).each do |sort|
  define_method :"get_#{sort}" do |**params|
    client.request_object(
      :get, "/r/#{display_name}/about/#{sort}", params
    )
  end
end

#get_top(**params) ⇒ Objects::Listing<Objects::Thing>

Note:

The option :t only applies to the top and controversial sorts.

Get the appropriate listing.

Parameters:

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • String (String :before Return results before the given fullname.)

    :before Return results before the given fullname.

  • :count (Integer) — default: 0

    The number of items already seen in the listing.

  • :limit (1..100) — default: 25

    The maximum number of things to return.

  • :t (:hour, :day, :week, :month, :year, :all)

    The time period to consider when sorting.

Returns:



200
201
202
203
204
# File 'lib/redd/objects/subreddit.rb', line 200

%w(hot new top controversial comments).each do |sort|
  define_method :"get_#{sort}" do |**params|
    client.send(:"get_#{sort}", self, **params)
  end
end

#get_unmoderated(**params) ⇒ Objects::Listing<Objects::Thing>

Get the appropriate moderator listing.

Parameters:

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • :before (String)

    Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..100)

    The maximum number of things to return.

  • :location (Object)

    No idea what this does.

  • :only (:links, :comments)

    The type of things to show.

Returns:

See Also:



255
256
257
258
259
260
261
# File 'lib/redd/objects/subreddit.rb', line 255

%w(reports spam modqueue unmoderated edited).each do |sort|
  define_method :"get_#{sort}" do |**params|
    client.request_object(
      :get, "/r/#{display_name}/about/#{sort}", params
    )
  end
end

#leave_contributor_status!Object

Stop being a contributor of the subreddit.



106
107
108
# File 'lib/redd/objects/subreddit.rb', line 106

def leave_contributor_status!
  post('/api/leavecontributor', id: fullname)
end

#leave_moderator_status!Object

Stop being a moderator of the subreddit.



111
112
113
# File 'lib/redd/objects/subreddit.rb', line 111

def leave_moderator_status!
  post('/api/leavemoderator', id: fullname)
end

#search(query, **params) ⇒ Objects::Listing<Objects::Thing>

Note:

The option :t only applies to the top and controversial sorts.

Search.

Parameters:

  • query (String)

    The query string.

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • String (String :before Return results before the given fullname.)

    :before Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..100)

    The maximum number of things to return.

  • :syntax (:cloudsearch, :lucene, :plain)

    The type of syntax to use.

  • :sort (:relevance, :new, :hot, :top, :comments)

    The way to sort the results.

  • :t (:hour, :day, :week, :month, :year, :all)

    The time period to consider when sorting.

Returns:



226
227
228
# File 'lib/redd/objects/subreddit.rb', line 226

def search(query, **params)
  client.search(query, self, **params)
end

#set_flair(thing, type = nil, text = nil, css_class = nil) ⇒ Object

Set the flair of a user or link.

Parameters:

  • thing (Objects::Subreddit, Objects::User)

    The user or link to set the flair to.

  • type (:user, :link) (defaults to: nil)

    The type of thing.

  • text (String) (defaults to: nil)

    The text to set the flair to.

  • css_class (String) (defaults to: nil)

    The css_class of the flair.



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/redd/objects/subreddit.rb', line 162

def set_flair(thing, type = nil, text = nil, css_class = nil)
  params = { text: text, css_class: css_class }
  if thing.is_a?(Objects::User) || type == :user
    params[:name] = client.property(thing, :name)
  elsif thing.is_a?(Objects::Submission) || type == :link
    params[:link] = client.property(thing, :fullname)
  else
    fail 'You should provide a proper type.'
  end

  post("/r/#{display_name}/api/flair", params)
end

#stylesheetString

Returns The css for the subreddit.

Returns:

  • (String)

    The css for the subreddit.



79
80
81
# File 'lib/redd/objects/subreddit.rb', line 79

def stylesheet
  Faraday.get(stylesheet_url).body
end

#stylesheet_urlString

Returns The url for the subreddit’s stylesheet.

Returns:

  • (String)

    The url for the subreddit’s stylesheet.



74
75
76
# File 'lib/redd/objects/subreddit.rb', line 74

def stylesheet_url
  get("/r/#{display_name}/stylesheet").headers['location']
end

#submit(title, captcha = nil, identifier = nil, text: nil, url: nil, resubmit: false, sendreplies: true) ⇒ Objects::Thing

Submit a link or a text post to the subreddit.

Parameters:

  • title (String)

    The title of the submission.

  • captcha (String) (defaults to: nil)

    A possible captcha result to send if one is required.

  • identifier (String) (defaults to: nil)

    The identifier for the captcha if one is required.

  • text (String) (defaults to: nil)

    The text of the self-post.

  • url (String) (defaults to: nil)

    The URL of the link.

  • resubmit (Boolean) (defaults to: false)

    Whether to post a link to the subreddit despite it having been posted there before (you monster).

  • sendreplies (Boolean) (defaults to: true)

    Whether to send the replies to your inbox.

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/redd/objects/subreddit.rb', line 35

def submit(
  title, captcha = nil, identifier = nil, text: nil, url: nil,
  resubmit: false, sendreplies: true
)

  params = {
    extension: 'json', title: title, sr: display_name,
    resubmit: resubmit, sendreplies: sendreplies
  }

  if captcha
    params[:captcha] = captcha
    params[:iden] = identifier
  end
  if text
    params[:kind] = :self
    params[:text] = text
  end
  if url
    params[:kind] = :link
    params[:url] = url
  end

  response = post('/api/submit', params)
  Objects::Thing.new(self, response.body[:json][:data])
end

#upload_image(file, name = nil) ⇒ String

Add or replace the subreddit image or header logo.

Parameters:

  • file (String, IO)

    The path/url to the file or the file itself.

  • name (String) (defaults to: nil)

    The name of the uploaded file.

Returns:

  • (String)

    The url of the image on reddit’s CDN.



316
317
318
319
320
321
322
323
324
# File 'lib/redd/objects/subreddit.rb', line 316

def upload_image(file, name = nil)
  io = (file.is_a?(IO) ? file : File.open(file, 'r'))
  type = FastImage.type(io)
  payload = Faraday::UploadIO.new(io, "image/#{type}")

  params = { file: payload, header: (name ? 0 : 1), img_type: type }
  params[:name] = name if name
  post("/r/#{display_name}/api/upload_sr_img", params).body[:img_src]
end