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.
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 |
#recommended_subreddits(subreddits) ⇒ Array<String>
Gets an array of recommended subreddits.
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.
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.
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.
16 17 18 19 20 21 22 |
# File 'lib/redditkit/client/subreddits.rb', line 16 def subreddits( = {}) 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.
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.
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.
31 32 33 34 35 36 37 |
# File 'lib/redditkit/client/subreddits.rb', line 31 def subscribed_subreddits( = {}) category = [:category] or 'subscriber' path = "subreddits/mine/#{category}.json" .delete :category objects_from_response(:get, path, ) end |
#unsubscribe(subreddit) ⇒ Object
Unsubscribes from a 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 |