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.



71
72
73
74
75
76
77
78
# File 'lib/redditkit/client/subreddits.rb', line 71

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.



108
109
110
111
112
113
114
115
116
# File 'lib/redditkit/client/subreddits.rb', line 108

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

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

  body.collect { |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:



84
85
86
87
# File 'lib/redditkit/client/subreddits.rb', line 84

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:



44
45
46
# File 'lib/redditkit/client/subreddits.rb', line 44

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
# File 'lib/redditkit/client/subreddits.rb', line 16

def subreddits(options = {})
  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.



94
95
96
97
98
99
100
101
# File 'lib/redditkit/client/subreddits.rb', line 94

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

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

  body.collect { |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.



51
52
53
54
55
56
# File 'lib/redditkit/client/subreddits.rb', line 51

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:



31
32
33
34
35
36
37
# File 'lib/redditkit/client/subreddits.rb', line 31

def subscribed_subreddits(options = {})
  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.



61
62
63
64
65
66
# File 'lib/redditkit/client/subreddits.rb', line 61

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

  post("api/subscribe", parameters)
end