Module: RedditKit::Client::Subreddits

Included in:
RedditKit::Client
Defined in:
lib/redditkit/client/subreddits.rb

Overview

Methods for interacting with subreddits.

Instance Method Summary collapse

Instance Method Details

#random_subredditRedditKit::Subreddit

Gets a random subreddit.



75
76
77
78
79
80
81
82
# File 'lib/redditkit/client/subreddits.rb', line 75

def random_subreddit
  response = get('r/random', nil)
  headers = response[:response_headers]
  location = headers[:location]

  subreddit_name = location[/\/r\/(.*)\//, 1]
  subreddit subreddit_name
end

Gets an array of recommended subreddits.

Examples:

RedditKit.recommended_subreddits %w(ruby programming)

Parameters:

  • subreddits (Array<String>)

    An array of subreddit names.

Returns:

  • (Array<String>)

    An array of recommended subreddit names.



112
113
114
115
116
117
118
119
120
# File 'lib/redditkit/client/subreddits.rb', line 112

def recommended_subreddits(subreddits)
  names = subreddits.join(',')
  parameters = { :srnames => names }

  response = get('api/subreddit_recommendations.json', parameters)
  body =  response[:body]

  body.map { |subreddit| subreddit[:sr_name] }
end

#search_subreddits_by_name(name) ⇒ RedditKit::PaginatedResponse

Searches for subreddits with a specific name.

Parameters:

  • name (String)

    The name to search for.

Returns:



88
89
90
91
# File 'lib/redditkit/client/subreddits.rb', line 88

def search_subreddits_by_name(name)
  parameters = { :q => name }
  objects_from_response :get, 'subreddits/search.json', parameters
end

#subreddit(subreddit_name) ⇒ RedditKit::Subreddit

Gets a subreddit object.

Examples:

client.subreddit “programming”

Parameters:

  • subreddit_name (String)

    A subreddit’s display name.

Returns:



48
49
50
# File 'lib/redditkit/client/subreddits.rb', line 48

def subreddit(subreddit_name)
  object_from_response(:get, "r/#{subreddit_name}/about.json", nil)
end

#subreddits(options = {}) ⇒ RedditKit::PaginatedResponse

Gets subreddits from a specified category.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • category (new, popular, banned)

    The category of subreddits. Defaults to popular.

  • limit (1..100)

    The number of subreddits to return.

  • before (String)

    Only return subreddits before this id.

  • after (String)

    Only return subreddits after this id.

Returns:



16
17
18
19
20
21
22
23
24
# File 'lib/redditkit/client/subreddits.rb', line 16

def subreddits(options = {})
  options = options.clone

  category = options[:category] or 'popular'
  path = "reddits/#{category}.json"
  options.delete :category

  objects_from_response(:get, path, options)
end

#subreddits_by_topic(topic) ⇒ Array<String>

Gets an array of subreddit names by topic.

Examples:

RedditKit.subreddits_by_topic ‘programming’

Parameters:

  • topic (String)

    The desired topic.

Returns:

  • (Array<String>)

    An array of subreddit names.



98
99
100
101
102
103
104
105
# File 'lib/redditkit/client/subreddits.rb', line 98

def subreddits_by_topic(topic)
  parameters = { :query => topic }

  response = get('api/subreddits_by_topic.json', parameters)
  body =  response[:body]

  body.map { |subreddit| subreddit[:name] }
end

#subscribe(subreddit) ⇒ Object

Subscribes to a subreddit.

Parameters:

  • subreddit (String, RedditKit::Subreddit)

    A subreddit’s full name, or a RedditKit::Subreddit.



55
56
57
58
59
60
# File 'lib/redditkit/client/subreddits.rb', line 55

def subscribe(subreddit)
  full_name = extract_full_name subreddit
  parameters = { :action => 'sub', :sr => full_name }

  post('api/subscribe', parameters)
end

#subscribed_subreddits(options = {}) ⇒ RedditKit::PaginatedResponse

Gets the current user’s subscribed subreddits.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • category (subscriber, contributor, moderator)

    The category from which to return subreddits. Defaults to subscriber.

  • limit (1..100)

    The number of subreddits to return.

  • before (String)

    Only return subreddits before this id.

  • after (String)

    Only return subreddits after this id.

Returns:



33
34
35
36
37
38
39
40
41
# File 'lib/redditkit/client/subreddits.rb', line 33

def subscribed_subreddits(options = {})
  options = options.clone

  category = options[:category] or 'subscriber'
  path = "subreddits/mine/#{category}.json"
  options.delete :category

  objects_from_response(:get, path, options)
end

#unsubscribe(subreddit) ⇒ Object

Unsubscribes from a subreddit.

Parameters:

  • subreddit (String, RedditKit::Subreddit)

    A subreddit’s full name, or a RedditKit::Subreddit.



65
66
67
68
69
70
# File 'lib/redditkit/client/subreddits.rb', line 65

def unsubscribe(subreddit)
  full_name = extract_full_name subreddit
  parameters = { :action => 'unsub', :sr => full_name }

  post('api/subscribe', parameters)
end