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
-
#random_subreddit ⇒ RedditKit::Subreddit
Gets a random subreddit.
-
#recommended_subreddits(subreddits) ⇒ Array<String>
Gets an array of recommended subreddits.
-
#search_subreddits_by_name(name) ⇒ RedditKit::PaginatedResponse
Searches for subreddits with a specific name.
-
#subreddit(subreddit_name) ⇒ RedditKit::Subreddit
Gets a subreddit object.
-
#subreddits(options = {}) ⇒ RedditKit::PaginatedResponse
Gets subreddits from a specified category.
-
#subreddits_by_topic(topic) ⇒ Array<String>
Gets an array of subreddit names by topic.
-
#subscribe(subreddit) ⇒ Object
Subscribes to a subreddit.
-
#subscribed_subreddits(options = {}) ⇒ RedditKit::PaginatedResponse
Gets the current user’s subscribed subreddits.
-
#unsubscribe(subreddit) ⇒ Object
Unsubscribes from a subreddit.
Instance Method Details
#random_subreddit ⇒ RedditKit::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 |
#recommended_subreddits(subreddits) ⇒ Array<String>
Gets an array of recommended subreddits.
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.
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.
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.
16 17 18 19 20 21 22 23 24 |
# File 'lib/redditkit/client/subreddits.rb', line 16 def subreddits( = {}) = .clone category = [:category] or 'popular' path = "reddits/#{category}.json" .delete :category objects_from_response(:get, path, ) end |
#subreddits_by_topic(topic) ⇒ Array<String>
Gets an array of subreddit names by topic.
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.
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.
33 34 35 36 37 38 39 40 41 |
# File 'lib/redditkit/client/subreddits.rb', line 33 def subscribed_subreddits( = {}) = .clone category = [:category] or 'subscriber' path = "subreddits/mine/#{category}.json" .delete :category objects_from_response(:get, path, ) end |
#unsubscribe(subreddit) ⇒ Object
Unsubscribes from a 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 |