Class: Redd::Models::Session
- Inherits:
-
BasicModel
- Object
- BasicModel
- Redd::Models::Session
- Includes:
- Searchable
- Defined in:
- lib/redd/models/session.rb
Overview
The starter class.
Instance Attribute Summary
Attributes inherited from BasicModel
Instance Method Summary collapse
-
#blocked ⇒ Array<User>
Users blocked by the logged-in user.
-
#edit_preferences(new_prefs = {}) ⇒ Hash
Edit the user’s preferences.
-
#friends ⇒ Array<User>
The logged-in user’s friends.
-
#from_ids(fullnames) ⇒ Listing<Submission, Comment>
Get submissions or comments by their fullnames.
-
#from_url(url) ⇒ Submission, ...
Get submissions or comments by their fullnames.
-
#front_page ⇒ FrontPage
The user’s front page.
-
#karma_breakdown ⇒ Hash
A breakdown of karma over subreddits.
-
#live_thread(id) ⇒ LiveThread
The live thread.
-
#me ⇒ User
The logged-in user.
-
#mod_mail ⇒ ModMail
The new modmail.
-
#multi(path) ⇒ Multireddit
Get a (lazily loaded) multi by its path.
-
#my_messages(category: 'inbox', mark: false, **params) ⇒ Listing<Comment, PrivateMessage>
Return a listing of the user’s inbox (including comment replies and private messages).
-
#my_multis ⇒ Array<Multireddit>
Array of multireddits belonging to the user.
-
#my_preferences ⇒ Hash
The user’s preferences.
-
#my_subreddits(type, **params) ⇒ Listing<Subreddit>
Return a listing of the user’s subreddits.
-
#read_all_messages ⇒ Object
Mark all messages as read.
-
#saved_categories ⇒ Array<String>
A list of categories the user’s items are saved in.
-
#subreddit(display_name) ⇒ Subreddit
Get a (lazily loaded) subreddit by its name.
-
#trusted ⇒ Array<User>
Users blocked by the logged-in user.
-
#user(name) ⇒ User
Get a (lazily loaded) reddit user by their name.
Methods included from Searchable
Methods inherited from BasicModel
from_id, #initialize, #inspect, #method_missing, #respond_to_missing?, #to_ary, #to_h
Constructor Details
This class inherits a constructor from Redd::Models::BasicModel
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Redd::Models::BasicModel
Instance Method Details
#blocked ⇒ Array<User>
Returns users blocked by the logged-in user.
122 123 124 125 126 |
# 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_preferences(new_prefs = {}) ⇒ Hash
Edit the user’s preferences.
106 107 108 109 110 111 112 |
# 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 |
#friends ⇒ Array<User>
Returns the logged-in user’s friends.
115 116 117 118 119 |
# 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 |
#from_ids(fullnames) ⇒ Listing<Submission, Comment>
Get submissions or comments by their fullnames.
66 67 68 69 |
# 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 |
#from_url(url) ⇒ Submission, ...
Get submissions or comments by their fullnames.
74 75 76 |
# File 'lib/redd/models/session.rb', line 74 def from_url(url) @client.model(:get, '/api/info', url: url).first end |
#front_page ⇒ FrontPage
Returns the user’s front page.
23 24 25 |
# File 'lib/redd/models/session.rb', line 23 def front_page FrontPage.new(@client) end |
#karma_breakdown ⇒ Hash
Returns a breakdown of karma over subreddits.
28 29 30 |
# File 'lib/redd/models/session.rb', line 28 def karma_breakdown @client.get('/api/v1/me/karma').body[:data] end |
#live_thread(id) ⇒ LiveThread
Returns the live thread.
18 19 20 |
# File 'lib/redd/models/session.rb', line 18 def live_thread(id) LiveThread.from_id(client, id) end |
#me ⇒ User
Returns the logged-in user.
33 34 35 |
# File 'lib/redd/models/session.rb', line 33 def me User.new(@client) { |client| client.get('/api/v1/me').body } end |
#mod_mail ⇒ ModMail
Returns the new modmail.
13 14 15 |
# File 'lib/redd/models/session.rb', line 13 def mod_mail ModMail.new(@client) end |
#multi(path) ⇒ Multireddit
Get a (lazily loaded) multi by its path.
59 60 61 |
# File 'lib/redd/models/session.rb', line 59 def multi(path) Multireddit.from_id(@client, path) end |
#my_messages(category: 'inbox', mark: false, **params) ⇒ Listing<Comment, PrivateMessage>
Return a listing of the user’s inbox (including comment replies and private messages).
88 89 90 |
# File 'lib/redd/models/session.rb', line 88 def (category: 'inbox', mark: false, **params) @client.model(:get, "/message/#{category}.json", params.merge(mark: mark)) end |
#my_multis ⇒ Array<Multireddit>
Returns array of multireddits belonging to the user.
52 53 54 |
# File 'lib/redd/models/session.rb', line 52 def my_multis @client.get('/api/multi/mine').body.map { |m| @client.unmarshal(m) } end |
#my_preferences ⇒ Hash
Returns the user’s preferences.
98 99 100 |
# File 'lib/redd/models/session.rb', line 98 def my_preferences @client.get('/api/v1/me/prefs').body end |
#my_subreddits(type, **params) ⇒ Listing<Subreddit>
Return a listing of the user’s subreddits.
149 150 151 |
# File 'lib/redd/models/session.rb', line 149 def my_subreddits(type, **params) @client.model(:get, "/subreddits/mine/#{type}", params) end |
#read_all_messages ⇒ Object
Mark all messages as read.
93 94 95 |
# File 'lib/redd/models/session.rb', line 93 def @client.post('/api/read_all_messages') end |
#saved_categories ⇒ Array<String>
Returns a list of categories the user’s items are saved in.
136 137 138 |
# File 'lib/redd/models/session.rb', line 136 def saved_categories @client.get('/api/saved_categories').body end |
#subreddit(display_name) ⇒ Subreddit
Get a (lazily loaded) subreddit by its name.
47 48 49 |
# File 'lib/redd/models/session.rb', line 47 def subreddit(display_name) Subreddit.from_id(@client, display_name) end |