Class: Redd::Models::Subreddit

Inherits:
Model
  • Object
show all
Includes:
Messageable, Searchable
Defined in:
lib/redd/models/subreddit.rb

Overview

A subreddit.

Constant Summary collapse

SETTINGS_MAP =

A mapping from keys returned by #settings to keys required by #modify_settings

{
  subreddit_type: :type,
  language: :lang,
  content_options: :link_type,
  default_set: :allow_top,
  header_hover_text: :'header-title'
}

Instance Attribute Summary collapse

Attributes inherited from Model

#client

Listings collapse

Moderator Listings collapse

Relationship Listings collapse

Instance Method Summary collapse

Methods inherited from Model

#initialize

Constructor Details

This class inherits a constructor from Redd::Models::Model

Instance Attribute Details

#accounts_activeInteger (readonly)

Returns the number of active accounts.

Returns:

  • (Integer)

    the number of active accounts


532
# File 'lib/redd/models/subreddit.rb', line 532

property :accounts_active

#accounts_active_is_fuzzedBoolean (readonly)

Returns whether active accounts is fuzzed.

Returns:

  • (Boolean)

    whether active accounts is fuzzed


605
# File 'lib/redd/models/subreddit.rb', line 605

property :accounts_active_is_fuzzed?, from: :accounts_active_is_fuzzed

#active_user_countInteger (readonly)

Returns the number of active users.

Returns:

  • (Integer)

    the number of active users


528
# File 'lib/redd/models/subreddit.rb', line 528

property :active_user_count

#advertiser_categoryString (readonly)

Returns the advertiser category.

Returns:

  • (String)

    the advertiser category


609
# File 'lib/redd/models/subreddit.rb', line 609

property :advertiser_category

#allow_images?Boolean (readonly)

Returns whether images are allowed.

Returns:

  • (Boolean)

    whether images are allowed


621
# File 'lib/redd/models/subreddit.rb', line 621

property :allow_images?, from: :allow_images

#audience_targetString (readonly)

Returns no clue what this means.

Returns:

  • (String)

    no clue what this means


520
# File 'lib/redd/models/subreddit.rb', line 520

property :audience_target

Returns the url to the subreddit's banner image.

Returns:

  • (String)

    the url to the subreddit's banner image


424
# File 'lib/redd/models/subreddit.rb', line 424

property :banner_image, from: :banner_img

Returns the banner dimensions.

Returns:

  • (Array<Integer>)

    the banner dimensions


428
# File 'lib/redd/models/subreddit.rb', line 428

property :banner_size

#can_set_flair?Boolean (readonly)

Returns whether the user can set the flair in the subreddit.

Returns:

  • (Boolean)

    whether the user can set the flair in the subreddit


480
# File 'lib/redd/models/subreddit.rb', line 480

property :can_set_flair?, from: :user_can_flair_in_sr

#collapse_deleted_comments?Boolean (readonly)

Returns whether deleted comments are collapsed.

Returns:

  • (Boolean)

    whether deleted comments are collapsed


492
# File 'lib/redd/models/subreddit.rb', line 492

property :collapse_deleted_comments?, from: :collapse_deleted_comments

#comment_score_hide_minsInteger (readonly)

Returns the number of minutes the comment score is hidden.

Returns:

  • (Integer)

    the number of minutes the comment score is hidden


629
# File 'lib/redd/models/subreddit.rb', line 629

property :comment_score_hide_mins

#created_atTime (readonly)

Returns the time the subreddit was created.

Returns:

  • (Time)

    the time the subreddit was created


601
# File 'lib/redd/models/subreddit.rb', line 601

property :created_at, from: :created_utc, with: ->(t) { Time.at(t) }

#descriptionString (readonly)

Returns the subreddit description.

Returns:

  • (String)

    the subreddit description


464
# File 'lib/redd/models/subreddit.rb', line 464

property :description

#description_htmlString (readonly)

Returns the html-rendered version of the subreddit description.

Returns:

  • (String)

    the html-rendered version of the subreddit description


468
# File 'lib/redd/models/subreddit.rb', line 468

property :description_html

#display_nameString (readonly)

Returns the subreddit's name.

Returns:

  • (String)

    the subreddit's name


404
# File 'lib/redd/models/subreddit.rb', line 404

property :display_name, :required

#display_name_prefixedString (readonly)

Deprecated.

not really deprecated, but prefer just using the display_name directly

Returns the display name, prefixed with a “r/”.

Returns:

  • (String)

    the display name, prefixed with a “r/”.


549
# File 'lib/redd/models/subreddit.rb', line 549

property :display_name_prefixed, default: ->() { "r/#{read_attribute(:display_name)}" }

#enrolled_in_new_modmail?Boolean (readonly)

Returns whether the subreddit is enrolled in the new modmail.

Returns:

  • (Boolean)

    whether the subreddit is enrolled in the new modmail


581
# File 'lib/redd/models/subreddit.rb', line 581

property :enrolled_in_new_modmail?, from: :is_enrolled_in_new_modmail

#header_imgString (readonly)

Returns the url to the header image.

Returns:

  • (String)

    the url to the header image


484
# File 'lib/redd/models/subreddit.rb', line 484

property :header_image, from: :header_img

#header_sizeArray<Integer> (readonly)

Returns the dimensions of the header image.

Returns:

  • (Array<Integer>)

    the dimensions of the header image


488
# File 'lib/redd/models/subreddit.rb', line 488

property :header_size

#header_titleString (readonly)

Returns the header's “title” attribute (i.e. mouseover text).

Returns:

  • (String)

    the header's “title” attribute (i.e. mouseover text)


544
# File 'lib/redd/models/subreddit.rb', line 544

property :header_title

#hide_ads?Boolean (readonly)

Returns whether ads are hidden?.

Returns:

  • (Boolean)

    whether ads are hidden?


597
# File 'lib/redd/models/subreddit.rb', line 597

property :hide_ads?, from: :hide_ads

#icon_imageString (readonly)

Returns the url to the icon image.

Returns:

  • (String)

    the url to the icon image


540
# File 'lib/redd/models/subreddit.rb', line 540

property :icon_image, from: :icon_img

#icon_sizeArray<Integer> (readonly)

Returns the subreddit icon size.

Returns:

  • (Array<Integer>)

    the subreddit icon size


516
# File 'lib/redd/models/subreddit.rb', line 516

property :icon_size

#idString (readonly)

Returns the subreddit's base36 id.

Returns:

  • (String)

    the subreddit's base36 id.


408
# File 'lib/redd/models/subreddit.rb', line 408

property :id

#key_colorString (readonly)

Returns a hex color code, not sure what this does.

Returns:

  • (String)

    a hex color code, not sure what this does


565
# File 'lib/redd/models/subreddit.rb', line 565

property :key_color

#languageString (readonly)

Returns the subreddit's language code.

Returns:

  • (String)

    the subreddit's language code


577
# File 'lib/redd/models/subreddit.rb', line 577

property :language, from: :lang

Returns whether link flairs are enabled.

Returns:

  • (Boolean)

    whether link flairs are enabled


617
# File 'lib/redd/models/subreddit.rb', line 617

property :link_flair_enabled?, from: :link_flair_enabled

#nameString (readonly)

Returns the subreddit's t5_ fullname.

Returns:

  • (String)

    the subreddit's t5_ fullname.


412
# File 'lib/redd/models/subreddit.rb', line 412

property :name

#over_18?Boolean (readonly)

Returns whether the user is marked as over 18.

Returns:

  • (Boolean)

    whether the user is marked as over 18


508
# File 'lib/redd/models/subreddit.rb', line 508

property :over_18?, from: :over18

#public_descriptionString (readonly)

Returns the public description.

Returns:

  • (String)

    the public description


500
# File 'lib/redd/models/subreddit.rb', line 500

property :public_description

#public_description_htmlString (readonly)

Returns the html-rendered version of the public description.

Returns:

  • (String)

    the html-rendered version of the public description


504
# File 'lib/redd/models/subreddit.rb', line 504

property :public_description_html

#public_trafficBoolean (readonly)

Returns whether the traffic page is public.

Returns:

  • (Boolean)

    whether the traffic page is public


561
# File 'lib/redd/models/subreddit.rb', line 561

property :public_traffic?, from: :public_traffic

#quarantined?Boolean (readonly)

Returns whether the subreddit is quarantined.

Returns:

  • (Boolean)

    whether the subreddit is quarantined


593
# File 'lib/redd/models/subreddit.rb', line 593

property :quarantined?, from: :quarantine

#show_media?Boolean (readonly)

Returns whether media is shown.

Returns:

  • (Boolean)

    whether media is shown


460
# File 'lib/redd/models/subreddit.rb', line 460

property :show_media?, from: :show_media

#show_media_previewBoolean (readonly)

Returns whether media previews are shown.

Returns:

  • (Boolean)

    whether media previews are shown


625
# File 'lib/redd/models/subreddit.rb', line 625

property :show_media_preview?, from: :show_media_preview

#spoilers_enabled?Boolean (readonly)

Returns whether the subreddit has spoilers enabled.

Returns:

  • (Boolean)

    whether the subreddit has spoilers enabled


512
# File 'lib/redd/models/subreddit.rb', line 512

property :spoilers_enabled?, from: :spoilers_enabled

#submission_typeString (readonly)

Returns the allowed submission type (?).

Returns:

  • (String)

    the allowed submission type (?)


637
# File 'lib/redd/models/subreddit.rb', line 637

property :submission_type

Returns the label text on the submit link button.

Returns:

  • (String)

    the label text on the submit link button


553
# File 'lib/redd/models/subreddit.rb', line 553

property :submit_link_label

#submit_textString (readonly)

Returns the submit text.

Returns:

  • (String)

    the submit text


472
# File 'lib/redd/models/subreddit.rb', line 472

property :submit_text

#submit_text_htmlString (readonly)

Returns the submit text html.

Returns:

  • (String)

    the submit text html


476
# File 'lib/redd/models/subreddit.rb', line 476

property :submit_text_html

#submit_text_labelString (readonly)

Returns the label text on the submit text button.

Returns:

  • (String)

    the label text on the submit text button


557
# File 'lib/redd/models/subreddit.rb', line 557

property :submit_text_label

#subreddit_theme_enabled?Boolean (readonly)

Returns whether the subreddit theme is enabled.

Returns:

  • (Boolean)

    whether the subreddit theme is enabled


613
# File 'lib/redd/models/subreddit.rb', line 613

property :subreddit_theme_enabled?, from: :user_sr_theme_enabled

#subreddit_typeString (readonly)

Returns whether it's a public, private, or gold-restricted subreddit.

Returns:

  • (String)

    whether it's a public, private, or gold-restricted subreddit


633
# File 'lib/redd/models/subreddit.rb', line 633

property :subreddit_type

#subscribersInteger (readonly)

Returns the subreddit's subscriber count.

Returns:

  • (Integer)

    the subreddit's subscriber count


536
# File 'lib/redd/models/subreddit.rb', line 536

property :subscribers

#suggested_comment_sortString (readonly)

Returns the suggested comment sort.

Returns:

  • (String)

    the suggested comment sort


524
# File 'lib/redd/models/subreddit.rb', line 524

property :suggested_comment_sort

#titleString (readonly)

Returns the subreddit's page title text.

Returns:

  • (String)

    the subreddit's page title text.


416
# File 'lib/redd/models/subreddit.rb', line 416

property :title

#urlString (readonly)

Returns the subreddit's *relative* url (e.g. /r/Redd/).

Returns:

  • (String)

    the subreddit's *relative* url (e.g. /r/Redd/)


589
# File 'lib/redd/models/subreddit.rb', line 589

property :url, default: ->() { "/r/#{read_attribute(:display_name)}/" }

#user_flair_css_classString (readonly)

Returns the css class for the user's flair.

Returns:

  • (String)

    the css class for the user's flair


436
# File 'lib/redd/models/subreddit.rb', line 436

property :user_flair_css_class

#user_flair_enabled?Boolean (readonly)

Returns whether the subreddit allows setting user flairs.

Returns:

  • (Boolean)

    whether the subreddit allows setting user flairs


573
# File 'lib/redd/models/subreddit.rb', line 573

property :user_flair_enabled?, from: :user_flair_enabled_in_sr

#user_flair_textString (readonly)

Returns the logged-in user's flair text.

Returns:

  • (String)

    the logged-in user's flair text


432
# File 'lib/redd/models/subreddit.rb', line 432

property :user_flair_text

#user_flair_visible?Boolean (readonly)

Returns whether the user's flair is shown to others.

Returns:

  • (Boolean)

    whether the user's flair is shown to others


569
# File 'lib/redd/models/subreddit.rb', line 569

property :user_flair_visible?, from: :user_sr_flair_enabled

#user_has_favorited?Boolean (readonly)

Returns whether the user has favourited the subreddit.

Returns:

  • (Boolean)

    whether the user has favourited the subreddit


496
# File 'lib/redd/models/subreddit.rb', line 496

property :user_has_favorited?, from: :user_has_favorited

#user_is_bannedBoolean (readonly)

Returns whether the logged-in user is banned from this subreddit.

Returns:

  • (Boolean)

    whether the logged-in user is banned from this subreddit


440
# File 'lib/redd/models/subreddit.rb', line 440

property :user_is_banned?, from: :user_is_banned

#user_is_contributor?Boolean (readonly)

Returns whether the logged-in user is the subreddit's contributor.

Returns:

  • (Boolean)

    whether the logged-in user is the subreddit's contributor


420
# File 'lib/redd/models/subreddit.rb', line 420

property :user_is_contributor?, from: :user_is_contributor

#user_is_moderator?Boolean (readonly)

Returns whether the logged-in user is a moderator of the subreddit.

Returns:

  • (Boolean)

    whether the logged-in user is a moderator of the subreddit


444
# File 'lib/redd/models/subreddit.rb', line 444

property :user_is_moderator?, from: :user_is_moderator

#user_is_muted?Boolean (readonly)

Returns whether the logged-in user is muted from the subreddit.

Returns:

  • (Boolean)

    whether the logged-in user is muted from the subreddit


448
# File 'lib/redd/models/subreddit.rb', line 448

property :user_is_muted?, from: :user_is_muted

#user_is_subscriberBoolean (readonly)

Returns whether the logged-in user is a subscriber to the subreddit.

Returns:

  • (Boolean)

    whether the logged-in user is a subscriber to the subreddit


452
# File 'lib/redd/models/subreddit.rb', line 452

property :user_is_subscriber?, from: :user_is_subscriber

#whitelist_statusString (readonly)

Returns not sure what this does, something to do with ads?.

Returns:

  • (String)

    not sure what this does, something to do with ads?


585
# File 'lib/redd/models/subreddit.rb', line 585

property :whitelist_status

#wiki_enabled?Boolean (readonly)

Returns whether the wiki is enabled for this subreddit.

Returns:

  • (Boolean)

    whether the wiki is enabled for this subreddit


456
# File 'lib/redd/models/subreddit.rb', line 456

property :wiki_enabled?, from: :wiki_enabled

Instance Method Details

#accept_moderator_inviteObject

Accept an invite to become a moderator of this subreddit.


300
301
302
# File 'lib/redd/models/subreddit.rb', line 300

def accept_moderator_invite
  client.post("/r/#{read_attribute(:display_name)}/api/accept_moderator_invite")
end

#add_contributor(user) ⇒ Object

Add a contributor to the subreddit.

Parameters:

  • user (User)

    the user to add


317
318
319
# File 'lib/redd/models/subreddit.rb', line 317

def add_contributor(user)
  add_relationship(type: 'contributor', name: user.name)
end

#add_wiki_contributor(user) ⇒ Object

Allow a user to contribute to the wiki.

Parameters:

  • user (User)

    the user to add


351
352
353
# File 'lib/redd/models/subreddit.rb', line 351

def add_wiki_contributor(user)
  add_relationship(type: 'wikicontributor', name: user.name)
end

#ban(user, **params) ⇒ Object

Ban a user from a subreddit.

Parameters:

  • user (User)

    the user to ban

  • params (Hash)

    additional options to supply with the request

Options Hash (**params):

  • :ban_reason (String)

    the reason for the ban

  • :ban_message (String)

    a message sent to the banned user

  • :note (String)

    a note that only moderators can see

  • :duration (Integer)

    the number of days to ban the user (if temporary)


339
340
341
# File 'lib/redd/models/subreddit.rb', line 339

def ban(user, **params)
  add_relationship(type: 'banned', name: user.name, **params)
end

#ban_wiki_contributor(user, **params) ⇒ Object

Ban a user from contributing to the wiki.

Parameters:

  • user (User)

    the user to ban

  • params (Hash)

    additional options to supply with the request

Options Hash (**params):

  • :ban_reason (String)

    the reason for the ban (not sure this matters)

  • :note (String)

    a note that only moderators can see

  • :duration (Integer)

    the number of days to ban the user (if temporary)


367
368
369
# File 'lib/redd/models/subreddit.rb', line 367

def ban_wiki_contributor(user, **params)
  add_relationship(type: 'wikibanned', name: user.name, **params)
end

#banned(**params) ⇒ Object


116
117
118
# File 'lib/redd/models/subreddit.rb', line 116

%i[banned muted wikibanned contributors wikicontributors moderators].each do |type|
  define_method(type) { |**params| relationship_listing(type, **params) }
end

#comments(**options) ⇒ Object

See Also:


55
56
57
# File 'lib/redd/models/subreddit.rb', line 55

%i[hot new top controversial comments rising gilded].each do |sort|
  define_method(sort) { |**options| listing(sort, **options) }
end

#contributors(**params) ⇒ Object


116
117
118
# File 'lib/redd/models/subreddit.rb', line 116

%i[banned muted wikibanned contributors wikicontributors moderators].each do |type|
  define_method(type) { |**params| relationship_listing(type, **params) }
end

#controversial(**options) ⇒ Object

See Also:


55
56
57
# File 'lib/redd/models/subreddit.rb', line 55

%i[hot new top controversial comments rising gilded].each do |sort|
  define_method(sort) { |**options| listing(sort, **options) }
end

#delete_flair(user) ⇒ Object

Remove the flair from a user

Parameters:

  • thing (User, String)

    a User from which to remove flair


210
211
212
213
# File 'lib/redd/models/subreddit.rb', line 210

def delete_flair(user)
  name = user.is_a?(User) ? user.name : user
  client.post("/r/#{read_attribute(:display_name)}/api/deleteflair", name: name)
end

#delete_image(upload_type:, image_name: nil) ⇒ Object

Delete a subreddit-specific image.

Parameters:

  • upload_type ('img', 'header', 'icon', 'banner')

    the image to delete

  • image_name (String) (defaults to: nil)

    the image name (if upload_type is 'img')


393
394
395
396
397
398
399
400
# File 'lib/redd/models/subreddit.rb', line 393

def delete_image(upload_type:, image_name: nil)
  unless %w[img header icon banner].include?(upload_type)
    raise ArgumentError, 'unknown upload_type'
  end
  params = {}
  params[:name] = image_name if upload_type.to_s == 'img'
  client.post("/r/#{read_attribute(:display_name)}/api/delete_sr_#{upload_type}", params)
end

#edited(**params) ⇒ Object

See Also:


83
84
85
# File 'lib/redd/models/subreddit.rb', line 83

%i[reports spam modqueue unmoderated edited].each do |type|
  define_method(type) { |**params| moderator_listing(type, **params) }
end

#flair_listing(**params) ⇒ Listing<Hash<Symbol, String>>

Get a listing of all user flairs.

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

  • :name (String)

    prefer #get_flair

  • :only (:links, :comments)

    the type of objects required

Returns:


193
194
195
196
# File 'lib/redd/models/subreddit.rb', line 193

def flair_listing(**params)
  res = client.get("/r/#{read_attribute(:display_name)}/api/flairlist", params).body
  Listing.new(client, children: res[:users], before: res[:prev], after: res[:next])
end

#get_flair(user) ⇒ Hash?

Get the user's flair data.

Parameters:

  • user (User)

    the user whose flair to fetch

Returns:

  • (Hash, nil)

201
202
203
204
205
206
# File 'lib/redd/models/subreddit.rb', line 201

def get_flair(user)
  # We have to do this because reddit returns all flairs if given a nonexistent user
  flair = flair_listing(name: user.name).first
  return flair if flair && flair[:user].casecmp(user.name).zero?
  nil
end

#gilded(**options) ⇒ Object

See Also:


55
56
57
# File 'lib/redd/models/subreddit.rb', line 55

%i[hot new top controversial comments rising gilded].each do |sort|
  define_method(sort) { |**options| listing(sort, **options) }
end

#hot(**options) ⇒ Object

See Also:


55
56
57
# File 'lib/redd/models/subreddit.rb', line 55

%i[hot new top controversial comments rising gilded].each do |sort|
  define_method(sort) { |**options| listing(sort, **options) }
end

#invite_moderator(user, permissions: '+all') ⇒ Object

Invite a user to moderate this subreddit.

Parameters:

  • user (User)

    the user to invite

  • permissions (String) (defaults to: '+all')

    the permission string to invite the user with


289
290
291
# File 'lib/redd/models/subreddit.rb', line 289

def invite_moderator(user, permissions: '+all')
  add_relationship(type: 'moderator_invite', name: user.name, permissions: permissions)
end

#leave_contributorObject

Leave from being a contributor on a subreddit.


328
329
330
# File 'lib/redd/models/subreddit.rb', line 328

def leave_contributor
  client.post('/api/leavecontributor', id: read_attribute(:name))
end

#leave_moderatorObject

Leave from being a moderator on a subreddit.


311
312
313
# File 'lib/redd/models/subreddit.rb', line 311

def leave_moderator
  client.post('/api/leavemoderator', id: read_attribute(:name))
end

#listing(sort, **options) ⇒ Listing<Submission, Comment>

Note:

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

Get the appropriate listing.

Parameters:

  • sort (:hot, :new, :top, :controversial, :comments, :rising, :gilded)

    the type of listing

  • options (Hash)

    a list of options to send with the request

Options Hash (**options):

  • :after (String)

    return results after the given fullname

  • :before (String)

    return results before the given fullname

  • :limit (Integer, nil)

    maximum number of items to return (nil for no limit)

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

    the time period to consider when sorting

Returns:


37
38
39
40
41
42
43
44
# File 'lib/redd/models/subreddit.rb', line 37

def listing(sort, **options)
  options[:t] = options.delete(:time) if options.key?(:time)
  PaginatedListing.new(client, options) do |**req_options|
    client.model(
      :get, "/r/#{read_attribute(:display_name)}/#{sort}", options.merge(req_options)
    )
  end
end

#mod_log(**params) ⇒ Listing<ModAction>

Get the moderation log.

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

  • :type (String)

    filter events to a specific type

Returns:


282
283
284
# File 'lib/redd/models/subreddit.rb', line 282

def mod_log(**params)
  client.model(:get, "/r/#{read_attribute(:display_name)}/about/log", params)
end

#moderator_listing(type, **params) ⇒ Listing<Submission, Comment>

Get the appropriate moderator listing.

Parameters:

  • type (:reports, :spam, :modqueue, :unmoderated, :edited)

    the type of listing

  • 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

  • :only (:links, :comments)

    the type of objects required

Returns:


72
73
74
# File 'lib/redd/models/subreddit.rb', line 72

def moderator_listing(type, **params)
  client.model(:get, "/r/#{read_attribute(:display_name)}/about/#{type}", params)
end

#moderators(**params) ⇒ Object


116
117
118
# File 'lib/redd/models/subreddit.rb', line 116

%i[banned muted wikibanned contributors wikicontributors moderators].each do |type|
  define_method(type) { |**params| relationship_listing(type, **params) }
end

#modify_settings(**params) ⇒ Object

Modify the subreddit's settings.

Parameters:

  • params (Hash)

    the settings to change

See Also:


266
267
268
269
270
271
# File 'lib/redd/models/subreddit.rb', line 266

def modify_settings(**params)
  full_params = settings.merge(params)
  full_params[:sr] = read_attribute(:name)
  SETTINGS_MAP.each { |src, dest| full_params[dest] = full_params.delete(src) }
  client.post('/api/site_admin', full_params)
end

#modqueue(**params) ⇒ Object

See Also:


83
84
85
# File 'lib/redd/models/subreddit.rb', line 83

%i[reports spam modqueue unmoderated edited].each do |type|
  define_method(type) { |**params| moderator_listing(type, **params) }
end

#muted(**params) ⇒ Object


116
117
118
# File 'lib/redd/models/subreddit.rb', line 116

%i[banned muted wikibanned contributors wikicontributors moderators].each do |type|
  define_method(type) { |**params| relationship_listing(type, **params) }
end

#new(**options) ⇒ Object

See Also:


55
56
57
# File 'lib/redd/models/subreddit.rb', line 55

%i[hot new top controversial comments rising gilded].each do |sort|
  define_method(sort) { |**options| listing(sort, **options) }
end

#relationship_listing(type, **params) ⇒ Array<Hash>

Get the appropriate relationship listing.

Parameters:

  • type (:banned, :muted, :wikibanned, :contributors, :wikicontributors, :moderators)

    the type of listing

  • 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

  • :user (String)

    find a specific user

Returns:

  • (Array<Hash>)

101
102
103
104
105
106
# File 'lib/redd/models/subreddit.rb', line 101

def relationship_listing(type, **params)
  # TODO: add methods to determine if a certain user was banned/muted/etc
  # TODO: return User types?
  user_list = client.get("/r/#{read_attribute(:display_name)}/about/#{type}", params).body
  user_list[:data][:children]
end

#remove_contributor(user) ⇒ Object

Remove a contributor from the subreddit.

Parameters:

  • user (User)

    the user to remove


323
324
325
# File 'lib/redd/models/subreddit.rb', line 323

def remove_contributor(user)
  remove_relationship(type: 'contributor', name: user.name)
end

#remove_moderator(user) ⇒ Object

Dethrone a moderator.

Parameters:

  • user (User)

    the user to remove


306
307
308
# File 'lib/redd/models/subreddit.rb', line 306

def remove_moderator(user)
  remove_relationship(type: 'moderator', name: user.name)
end

#remove_wiki_contributor(user) ⇒ Object

No longer allow a user to contribute to the wiki.

Parameters:

  • user (User)

    the user to remove


357
358
359
# File 'lib/redd/models/subreddit.rb', line 357

def remove_wiki_contributor(user)
  remove_relationship(type: 'wikicontributor', name: user.name)
end

#reports(**params) ⇒ Object

See Also:


83
84
85
# File 'lib/redd/models/subreddit.rb', line 83

%i[reports spam modqueue unmoderated edited].each do |type|
  define_method(type) { |**params| moderator_listing(type, **params) }
end

#rising(**options) ⇒ Object

See Also:


55
56
57
# File 'lib/redd/models/subreddit.rb', line 55

%i[hot new top controversial comments rising gilded].each do |sort|
  define_method(sort) { |**options| listing(sort, **options) }
end

#search(query, **params) ⇒ Object

Search a subreddit.

Parameters:

See Also:


138
139
140
141
# File 'lib/redd/models/subreddit.rb', line 138

def search(query, **params)
  restricted_params = { restrict_to: read_attribute(:display_name) }.merge(params)
  super(query, restricted_params)
end

#send_message(subject:, text:, from: nil) ⇒ Object

Compose a message to the moderators of a subreddit.

Parameters:

  • subject (String)

    the subject of the message

  • text (String)

    the message text

  • from (Subreddit, nil) (defaults to: nil)

    the subreddit to send the message on behalf of


169
170
171
# File 'lib/redd/models/subreddit.rb', line 169

def send_message(subject:, text:, from: nil)
  super(to: "/r/#{read_attribute(:display_name)}", subject: subject, text: text, from: from)
end

#set_flair(thing, text, css_class: nil) ⇒ Object

Set the flair for a link or a user for this subreddit.

Parameters:

  • thing (User, Submission)

    the object whose flair to edit

  • text (String)

    a string no longer than 64 characters

  • css_class (String) (defaults to: nil)

    the css class to assign to the flair


177
178
179
180
181
182
# File 'lib/redd/models/subreddit.rb', line 177

def set_flair(thing, text, css_class: nil)
  key = thing.is_a?(User) ? :name : :link
  params = { :text => text, key => thing.name }
  params[:css_class] = css_class if css_class
  client.post("/r/#{read_attribute(:display_name)}/api/flair", params)
end

#set_flair_template(thing, template_id, text: nil) ⇒ Object

Set a Submission's or User's flair based on a flair template id.

Parameters:

  • thing (User, Submission)

    an object to assign a template to

  • template_id (String)

    the UUID of the flair template to assign

  • text (String) (defaults to: nil)

    optional text for the flair


219
220
221
222
223
# File 'lib/redd/models/subreddit.rb', line 219

def set_flair_template(thing, template_id, text: nil)
  key = thing.is_a?(User) ? :name : :link
  params = { key => thing.name, flair_template_id: template_id, text: text }
  client.post("/r/#{read_attribute(:display_name)}/api/selectflair", params)
end

#settingsHash

Returns the subreddit's settings.

Returns:

  • (Hash)

    the subreddit's settings


259
260
261
# File 'lib/redd/models/subreddit.rb', line 259

def settings
  client.get("/r/#{read_attribute(:display_name)}/about/edit").body[:data]
end

#spam(**params) ⇒ Object

See Also:


83
84
85
# File 'lib/redd/models/subreddit.rb', line 83

%i[reports spam modqueue unmoderated edited].each do |type|
  define_method(type) { |**params| moderator_listing(type, **params) }
end

#stylesheetString?

Get the subreddit's CSS.

Returns:

  • (String, nil)

    the stylesheet or nil if no stylesheet exists


242
243
244
245
246
247
# File 'lib/redd/models/subreddit.rb', line 242

def stylesheet
  url = client.get("/r/#{read_attribute(:display_name)}/stylesheet").headers['location']
  HTTP.get(url).body.to_s
rescue Errors::NotFound
  nil
end

#submit(title, text: nil, url: nil, resubmit: false, sendreplies: true) ⇒ Submission

Note:

If both text and url are provided, url takes precedence.

Submit a link or a text post to the subreddit.

Parameters:

  • title (String)

    the title of the submission

  • 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:

  • (Submission)

    The returned object (url, id and name)


153
154
155
156
157
158
159
160
161
162
# File 'lib/redd/models/subreddit.rb', line 153

def submit(title, text: nil, url: nil, resubmit: false, sendreplies: true)
  params = {
    title: title, sr: read_attribute(:display_name),
    resubmit: resubmit, sendreplies: sendreplies
  }
  params[:kind] = url ? 'link' : 'self'
  params[:url]  = url  if url
  params[:text] = text if text
  Submission.new(client, client.post('/api/submit', params).body[:json][:data])
end

#subscribe(action: :sub, skip_initial_defaults: false) ⇒ Object

Add the subreddit to the user's subscribed subreddits.


226
227
228
229
230
231
232
233
# File 'lib/redd/models/subreddit.rb', line 226

def subscribe(action: :sub, skip_initial_defaults: false)
  client.post(
    '/api/subscribe',
    sr_name: read_attribute(:display_name),
    action: action,
    skip_initial_defaults: skip_initial_defaults
  )
end

#top(**options) ⇒ Object

See Also:


55
56
57
# File 'lib/redd/models/subreddit.rb', line 55

%i[hot new top controversial comments rising gilded].each do |sort|
  define_method(sort) { |**options| listing(sort, **options) }
end

#unban(user) ⇒ Object

Remove a ban on a user.

Parameters:

  • user (User)

    the user to unban


345
346
347
# File 'lib/redd/models/subreddit.rb', line 345

def unban(user)
  remove_relationship(type: 'banned', name: user.name)
end

#unban_wiki_contributor(user) ⇒ Object

No longer ban a user from contributing to the wiki.

Parameters:

  • user (User)

    the user to unban


373
374
375
# File 'lib/redd/models/subreddit.rb', line 373

def unban_wiki_contributor(user)
  remove_relationship(type: 'wikibanned', name: user.name)
end

#uninvite_moderator(user) ⇒ Object

Take back a moderator request.

Parameters:

  • user (User)

    the requested user


295
296
297
# File 'lib/redd/models/subreddit.rb', line 295

def uninvite_moderator(user)
  remove_relationship(type: 'moderator_invite', name: user.name)
end

#unmoderated(**params) ⇒ Object

See Also:


83
84
85
# File 'lib/redd/models/subreddit.rb', line 83

%i[reports spam modqueue unmoderated edited].each do |type|
  define_method(type) { |**params| moderator_listing(type, **params) }
end

#unsubscribeObject

Remove the subreddit from the user's subscribed subreddits.


236
237
238
# File 'lib/redd/models/subreddit.rb', line 236

def unsubscribe
  subscribe(action: :unsub)
end

#update_stylesheet(text, reason: nil) ⇒ Object

Edit the subreddit's stylesheet.

Parameters:

  • text (String)

    the updated CSS

  • reason (String) (defaults to: nil)

    the reason for modifying the stylesheet


252
253
254
255
256
# File 'lib/redd/models/subreddit.rb', line 252

def update_stylesheet(text, reason: nil)
  params = { op: 'save', stylesheet_contents: text }
  params[:reason] = reason if reason
  client.post("/r/#{read_attribute(:display_name)}/api/subreddit_stylesheet", params)
end

#upload_image(file:, image_type:, upload_type:, image_name: nil) ⇒ String

Upload a subreddit-specific image.

Parameters:

  • file (String, IO)

    the image file to upload

  • image_type ('jpg', 'png')

    the image type

  • upload_type ('img', 'header', 'icon', 'banner')

    where to upload the image

  • image_name (String) (defaults to: nil)

    the name of the image (if upload_type is 'img')

Returns:

  • (String)

    the url of the uploaded file


383
384
385
386
387
388
# File 'lib/redd/models/subreddit.rb', line 383

def upload_image(file:, image_type:, upload_type:, image_name: nil)
  file_data = HTTP::FormData::File.new(file)
  params = { img_type: image_type, upload_type: upload_type, file: file_data }
  params[:name] = image_name if upload_type.to_s == 'img'
  client.post("/r/#{read_attribute(:display_name)}/api/upload_sr_img", params).body[:img_src]
end

#wiki_page(title) ⇒ WikiPage

Get a wiki page by its title.

Parameters:

  • title (String)

    the page's title

Returns:


130
131
132
# File 'lib/redd/models/subreddit.rb', line 130

def wiki_page(title)
  WikiPage.new(client, title: title, subreddit: self)
end

#wiki_pagesArray<String>

Returns the subreddit's wiki pages.

Returns:

  • (Array<String>)

    the subreddit's wiki pages


123
124
125
# File 'lib/redd/models/subreddit.rb', line 123

def wiki_pages
  client.get("/r/#{read_attribute(:display_name)}/wiki/pages").body[:data]
end

#wikibanned(**params) ⇒ Object


116
117
118
# File 'lib/redd/models/subreddit.rb', line 116

%i[banned muted wikibanned contributors wikicontributors moderators].each do |type|
  define_method(type) { |**params| relationship_listing(type, **params) }
end

#wikicontributors(**params) ⇒ Object


116
117
118
# File 'lib/redd/models/subreddit.rb', line 116

%i[banned muted wikibanned contributors wikicontributors moderators].each do |type|
  define_method(type) { |**params| relationship_listing(type, **params) }
end