Module: Snoo::Subreddit

Defined in:
lib/snoo/subreddits.rb

Overview

Methods for administering a subreddit, as well as looking up subreddits (subreddit search)

Author:

Instance Method Summary collapse

Instance Method Details

#accept_moderator(subreddit) ⇒ Object

Accept a moderatorship

Parameters:

  • subreddit (String)

    The subreddit to accept in. You must have been invited



239
240
241
242
# File 'lib/snoo/subreddits.rb', line 239

def accept_moderator subreddit
  logged_in?
  post('/api/accept_moderator_invite', body: {r: subreddit, uh: @modhash, api_type: 'json'})
end

#add_contributor(container, user, subreddit) ⇒ Object

Add a contributor to the subreddit

Parameters:

  • container (String)

    The subreddit id. Must be a subreddit id (begins with t5_)

  • user (String)

    The user

  • subreddit (String)

    The subreddit targeted



174
175
176
# File 'lib/snoo/subreddits.rb', line 174

def add_contributor container, user, subreddit
  friend_wrapper container: container, name: user, r: subreddit, type: "contributor"
end

#add_moderator(container, user, subreddit) ⇒ Object

Add a moderator to the subreddit

Parameters:

  • container (String)

    The subreddit id. Must be a subreddit id (begins with t5_)

  • user (String)

    The user

  • subreddit (String)

    The subreddit targeted



166
167
168
# File 'lib/snoo/subreddits.rb', line 166

def add_moderator container, user, subreddit
  friend_wrapper container: container, name: user, r: subreddit, type: "moderator"
end

#ban_user(container, user, subreddit) ⇒ Object

Ban a user from a subreddit

Parameters:

  • container (String)

    The subreddit id. Must be a subreddit id (begins with t5_)

  • user (String)

    The user

  • subreddit (String)

    The subreddit targeted



182
183
184
# File 'lib/snoo/subreddits.rb', line 182

def ban_user container, user, subreddit
  friend_wrapper container: container, name: user, r: subreddit, type: "banned"
end

#delete_header(subreddit) ⇒ Object

Deletes the header image of a subreddit

Parameters:

  • subreddit (String)

    The subreddit targeted



11
12
13
14
# File 'lib/snoo/subreddits.rb', line 11

def delete_header subreddit
  logged_in?
  post('/api/delete_sr_header', body: {r: subreddit, uh: @modhash, api_type: 'json'})
end

#delete_image(subreddit, image_name) ⇒ Object

Deletes an image from a subreddit. This is for css, not removing posts

Parameters:

  • image_name (String)

    the image to delete from the subreddit. Can be obtained via #get_stylesheet

  • subreddit (String)

    The subreddit targeted



21
22
23
24
# File 'lib/snoo/subreddits.rb', line 21

def delete_image subreddit, image_name
  logged_in?
  post('/api/delete_sr_image', body: {r: subreddit, img_name: image_name, uh: @modhash, api_type: 'json'})
end

#get_banned_users(subreddit) ⇒ Object

List banned users of a subreddit

Parameters:

  • subreddit (String)

    The subreddit targeted



231
232
233
234
# File 'lib/snoo/subreddits.rb', line 231

def get_banned_users subreddit
  logged_in?
  get("/r/#{subreddit}/about/banned.json")
end

#get_contributors(subreddit) ⇒ Object

List contributors of a subreddit

Parameters:

  • subreddit (String)

    The subreddit targeted



222
223
224
225
# File 'lib/snoo/subreddits.rb', line 222

def get_contributors subreddit
  logged_in?
  get("/r/#{subreddit}/about/contributors.json")
end

#get_moderators(subreddit) ⇒ Object

List moderators of a subreddit

Parameters:

  • subreddit (String)

    The subreddit targeted



214
215
216
# File 'lib/snoo/subreddits.rb', line 214

def get_moderators subreddit
  get("/r/#{subreddit}/about/moderators.json")
end

#get_reddits(opts = {}) ⇒ Object

Get a list of subreddits

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

  • opts (Hash) (defaults to: {})

    An options hash

Options Hash (opts):

  • :condition (popular, new, banned)

    The type of subreddits to return

  • :limit (1..100)

    The number of results to return

  • :after (String)

    Return subreddits after this id.

  • :before (String)

    Return subreddits before this id.



138
139
140
141
142
143
144
# File 'lib/snoo/subreddits.rb', line 138

def get_reddits opts = {}
  url = "/reddits/%s.json" % (opts[:condition] if opts[:condition])
  opts.delete :condition
  query = opts

  get(url, query: query)
end

#get_stylesheet(subreddit) ⇒ Object

Get subreddit stylesheet and images

Parameters:

  • subreddit (String)

    The subreddit targeted



109
110
111
112
# File 'lib/snoo/subreddits.rb', line 109

def get_stylesheet subreddit
  logged_in?
  get("/r/#{subreddit}/about/stylesheet.json")
end

#get_subreddit_settings(subreddit) ⇒ Object

Gets a hash of the subreddit settings Returns a webserver error (404) if you don't have moderator permissions on said subreddit

Parameters:

  • subreddit (String)

    the subreddit to fetch data from



30
31
32
33
# File 'lib/snoo/subreddits.rb', line 30

def get_subreddit_settings subreddit
  logged_in?
  get("/r/#{subreddit}/about/edit/.json")
end

#my_reddits(opts = {}) ⇒ Object

Get subreddits I have

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

  • opts (Hash) (defaults to: {})

    An options hash

Options Hash (opts):

  • :condition (subscriber, contributor, moderator)

    The permission level to return subreddits from

  • :limit (1..100)

    The number of results to return

  • :after (String)

    Return subreddits after this id

  • :before (String)

    Return subreddits before this id



122
123
124
125
126
127
128
# File 'lib/snoo/subreddits.rb', line 122

def my_reddits opts = {}
  logged_in?
  url = "/reddits/mine/%s.json" % (opts[:condition] if opts[:condition])
  opts.delete :condition
  query = opts
  get(url, query: query)
end

#remove_contributor(container, user, subreddit) ⇒ Object

Remove a contributor from a subreddit

Parameters:

  • container (String)

    The subreddit id. Must be a subreddit id (begins with t5_)

  • user (String)

    The user

  • subreddit (String)

    The subreddit targeted



198
199
200
# File 'lib/snoo/subreddits.rb', line 198

def remove_contributor container, user, subreddit
  unfriend_wrapper container: container, name: user, r: subreddit, type: "contributor"
end

#remove_moderator(container, user, subreddit) ⇒ Object

Remove a moderator from a subreddit

Parameters:

  • container (String)

    The subreddit id. Must be a subreddit id (begins with t5_)

  • user (String)

    The user

  • subreddit (String)

    The subreddit targeted



190
191
192
# File 'lib/snoo/subreddits.rb', line 190

def remove_moderator container, user, subreddit
  unfriend_wrapper container: container, name: user, r: subreddit, type: "moderator"
end

#search_reddits(q, opts = {}) ⇒ Object

Search subreddits

Parameters:

  • q (String)

    The search query

  • opts (Hash) (defaults to: {})

    a customizable set of options

  • opts (Hash) (defaults to: {})

    An options hash

Options Hash (opts):

  • :limit (1..100)

    The number of results to return

  • :after (String)

    Return subreddits after this id.

  • :before (String)

    Return subreddits before this id.



154
155
156
157
158
# File 'lib/snoo/subreddits.rb', line 154

def search_reddits q, opts = {}
  query = {q: q}
  query.merge! opts
  get('/reddits/search.json', query: query)
end

#set_stylesheet(stylesheet, subreddit) ⇒ Object

Set the subreddit stylesheet

Parameters:

  • stylesheet (String)

    The stylesheet for the subreddit. Overwrites the current one

  • subreddit (String)

    The subreddit targeted



72
73
74
75
# File 'lib/snoo/subreddits.rb', line 72

def set_stylesheet stylesheet, subreddit
  logged_in?
  post('/api/subreddit_stylesheet', body: {op: 'save', r: subreddit, stylesheet_contents: stylesheet, uh: @modhash, api_type: 'json'})
end

#subreddit_info(subreddit) ⇒ Object

Get subreddit info

Parameters:

  • subreddit (String)

    The subreddit targeted



101
102
103
# File 'lib/snoo/subreddits.rb', line 101

def subreddit_info subreddit
  get("/r/#{subreddit}/about.json")
end

#subreddit_settings(subreddit, opts = {}) ⇒ Object

TODO:

test if every param is actually required

Sets subreddit settings.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

  • subreddit (String)

    The subreddit targeted

  • opts (Hash) (defaults to: {})

    An options hash

Options Hash (opts):

  • :title (String)

    The subreddit's title

  • :public_description (String)

    The subreddit's public description

  • :description (String)

    The subreddit's sidebar

  • :lang (String) — default: en

    The default language. ISO language code

  • :type (public, private, restricted) — default: public

    The subreddits type

  • :link_type (any, link, self) — default: any

    The type of posts allowed on this subreddit

  • :allow_top (true, false) — default: true

    Allow this subreddit to appear on the front page

  • :show_media (true, false) — default: true

    show thumbnails and media embeds

  • :header-title (String)

    The header mouse-over text

  • :over_18 (true, false) — default: false

    If the subreddit requires over 18 access



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/snoo/subreddits.rb', line 50

def subreddit_settings subreddit, opts = {}
  logged_in?
  params = {
    type: 'public',
    link_type: 'any',
    lang: 'en',
    r: subreddit,
    uh: @modhash,
    allow_top: true,
    show_media: true,
    over_18: false,
    api_type: 'json'
  }
  params.merge! opts
  post('/api/site_admin', body: params)
end

#subscribe(subreddit, action = "sub") ⇒ Object

Subscribe to a subreddit

Parameters:

  • action (sub, unsub) (defaults to: "sub")

    Subscribe or unsubscribe

  • subreddit (String)

    The subreddit targeted



82
83
84
85
# File 'lib/snoo/subreddits.rb', line 82

def subscribe subreddit, action = "sub"
  logged_in?
  post('/api/subscribe', body: {action: action, sr: subreddit, uh: @modhash, api_type: 'json'})
end

#unban_user(container, user, subreddit) ⇒ Object

Unban a user from a subreddit

Parameters:

  • container (String)

    The subreddit id. Must be a subreddit id (begins with t5_)

  • user (String)

    The user

  • subreddit (String)

    The subreddit targeted



206
207
208
# File 'lib/snoo/subreddits.rb', line 206

def unban_user container, user, subreddit
  unfriend_wrapper container: container, name: user, r: subreddit, type: "banned"
end

#unsubscribe(subreddit) ⇒ Object

Unsubscribe from a subreddit This is an alias for subscribe "unsub"

Parameters:

  • subreddit (String)

    The subreddit targeted



92
93
94
# File 'lib/snoo/subreddits.rb', line 92

def unsubscribe subreddit
  subscribe("unsub", subreddit)
end