class Redd::Models::Session
The starter class.
Public Instance Methods
@return [Array<User>] users blocked by the logged-in user
# File lib/redd/models/session.rb, line 122 def blocked @client.get('/prefs/blocked').body[:data][:children].map do |h| User.new(@client, name: h[:name], id: h[:id].sub('t2_', ''), since: h[:date]) end end
Edit the user's preferences. @param new_prefs [Hash] the changed preferences @return [Hash] the new preferences @see my_preferences
# File lib/redd/models/session.rb, line 106 def edit_preferences(new_prefs = {}) @client.request( :patch, '/api/v1/me/prefs', headers: { 'Content-Type' => 'application/json' }, body: JSON.generate(new_prefs) ).body end
@return [Array<User>] the logged-in user's friends
# File lib/redd/models/session.rb, line 115 def friends @client.get('/api/v1/me/friends').body[:data][:children].map do |h| User.new(@client, name: h[:name], id: h[:id].sub('t2_', ''), since: h[:date]) end end
Get submissions or comments by their fullnames. @param fullnames [String, Array<String>] one or an array of fullnames (e.g. t3_abc1234) @return [Listing<Submission, Comment>]
# File lib/redd/models/session.rb, line 66 def from_ids(fullnames) # XXX: Could we use better methods for t1_ and t3_? @client.model(:get, '/api/info', id: Array(fullnames).join(',')) end
Get submissions or comments by their fullnames. @param url [String] the object's url @return [Submission, Comment
, nil] the object, or nil if not found
# File lib/redd/models/session.rb, line 74 def from_url(url) @client.model(:get, '/api/info', url: url).first end
@return [FrontPage] the user's front page
# File lib/redd/models/session.rb, line 23 def front_page FrontPage.new(@client) end
@return [Hash] a breakdown of karma over subreddits
# File lib/redd/models/session.rb, line 28 def karma_breakdown @client.get('/api/v1/me/karma').body[:data] end
@return [LiveThread] the live thread
# File lib/redd/models/session.rb, line 18 def live_thread(id) LiveThread.from_id(client, id) end
@return [User] the logged-in user
# File lib/redd/models/session.rb, line 33 def me User.new(@client) { |client| client.get('/api/v1/me').body } end
@return [ModMail] the new modmail
# File lib/redd/models/session.rb, line 13 def mod_mail ModMail.new(@client) end
Get a (lazily loaded) multi by its path. @param path [String] the multi's path, surrounded by a leading and trailing / @return [Multireddit]
# File lib/redd/models/session.rb, line 59 def multi(path) Multireddit.from_id(@client, path) end
Return a listing of the user's inbox (including comment replies and private messages).
@param category ['inbox', 'unread', 'sent', 'moderator'] the category of messages to view @param mark [Boolean] whether to remove the orangered from the user's inbox @param params [Hash] a list of optional params to send with the request @option params [String] :after return results after the given fullname @option params [String] :before return results before the given fullname @option params [Integer] :count (0) the number of items already seen in the listing @option params [1..100] :limit (25) the maximum number of things to return @return [Listing<Comment, PrivateMessage>]
# File lib/redd/models/session.rb, line 88 def my_messages(category: 'inbox', mark: false, **params) @client.model(:get, "/message/#{category}.json", params.merge(mark: mark)) end
@return [Array<Multireddit>] array of multireddits belonging to the user
# File lib/redd/models/session.rb, line 52 def my_multis @client.get('/api/multi/mine').body.map { |m| @client.unmarshal(m) } end
@return [Hash] the user's preferences
# File lib/redd/models/session.rb, line 98 def my_preferences @client.get('/api/v1/me/prefs').body end
Return a listing of the user's subreddits.
@param type ['subscriber', 'contributor', 'moderator'] the type of subreddits @param params [Hash] a list of optional params to send with the request @option params [String] :after return results after the given fullname @option params [String] :before return results before the given fullname @option params [Integer] :count (0) the number of items already seen in the listing @option params [1..100] :limit (25) the maximum number of things to return @return [Listing<Subreddit>]
# File lib/redd/models/session.rb, line 149 def my_subreddits(type, **params) @client.model(:get, "/subreddits/mine/#{type}", params) end
Mark all messages as read.
# File lib/redd/models/session.rb, line 93 def read_all_messages @client.post('/api/read_all_messages') end
@return [Array<String>] a list of categories the user's items are saved in
# File lib/redd/models/session.rb, line 136 def saved_categories @client.get('/api/saved_categories').body end
Get a (lazily loaded) subreddit by its name. @param display_name [String] the subreddit's display name @return [Subreddit]
# File lib/redd/models/session.rb, line 47 def subreddit(display_name) Subreddit.from_id(@client, display_name) end
@return [Array<User>] users blocked by the logged-in user
# File lib/redd/models/session.rb, line 129 def trusted @client.get('/prefs/trusted').body[:data][:children].map do |h| User.new(@client, name: h[:name], id: h[:id].sub('t2_', ''), since: h[:date]) end end
Get a (lazily loaded) reddit user by their name. @param name [String] the username @return [User]
# File lib/redd/models/session.rb, line 40 def user(name) User.from_id(@client, name) end