Class: Redd::Models::User
- Inherits:
-
LazyModel
- Object
- BasicModel
- LazyModel
- Redd::Models::User
- Includes:
- Messageable
- Defined in:
- lib/redd/models/user.rb
Overview
A reddit user.
Instance Attribute Summary
Attributes inherited from BasicModel
Class Method Summary collapse
-
.from_id(client, id) ⇒ User
Create a User from their name.
Instance Method Summary collapse
- #comments(**params) ⇒ Object
- #disliked(**params) ⇒ Object
-
#friend(note = nil) ⇒ Object
Add the user as a friend.
-
#gift_gold(months: 1) ⇒ Object
Gift a redditor reddit gold.
- #gilded(**params) ⇒ Object
- #hidden(**params) ⇒ Object
- #liked(**params) ⇒ Object
-
#listing(type, **params) ⇒ Listing<Submission>
Get the appropriate listing.
- #overview(**params) ⇒ Object
- #saved(**params) ⇒ Object
-
#send_message(subject:, text:, from: nil) ⇒ Object
Compose a message to the moderators of a subreddit.
- #submitted(**params) ⇒ Object
-
#unblock(me: nil) ⇒ Object
Unblock a previously blocked user.
-
#unfriend ⇒ Object
Unfriend the user.
Methods inherited from LazyModel
#force_load, #initialize, #method_missing, #respond_to_missing?, #to_h
Methods inherited from BasicModel
#initialize, #inspect, #method_missing, #respond_to_missing?, #to_ary, #to_h
Constructor Details
This class inherits a constructor from Redd::Models::LazyModel
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Redd::Models::LazyModel
Class Method Details
.from_id(client, id) ⇒ User
Create a User from their name.
16 17 18 |
# File 'lib/redd/models/user.rb', line 16 def self.from_id(client, id) new(client, name: id) end |
Instance Method Details
#comments(**params) ⇒ Object
81 82 83 |
# File 'lib/redd/models/user.rb', line 81 %i(overview submitted comments liked disliked hidden saved gilded).each do |type| define_method(type) { |**params| listing(type, **params) } end |
#disliked(**params) ⇒ Object
81 82 83 |
# File 'lib/redd/models/user.rb', line 81 %i(overview submitted comments liked disliked hidden saved gilded).each do |type| define_method(type) { |**params| listing(type, **params) } end |
#friend(note = nil) ⇒ Object
Add the user as a friend.
39 40 41 42 43 |
# File 'lib/redd/models/user.rb', line 39 def friend(note = nil) name = get_attribute(:name) body = JSON.generate(note ? { name: name, note: note } : { name: name }) @client.request(:put, "/api/v1/me/friends/#{name}", body: body) end |
#gift_gold(months: 1) ⇒ Object
Gift a redditor reddit gold.
87 88 89 |
# File 'lib/redd/models/user.rb', line 87 def gift_gold(months: 1) @client.post("/api/v1/gold/give/#{get_attribute(:name)}", months: months) end |
#gilded(**params) ⇒ Object
81 82 83 |
# File 'lib/redd/models/user.rb', line 81 %i(overview submitted comments liked disliked hidden saved gilded).each do |type| define_method(type) { |**params| listing(type, **params) } end |
#hidden(**params) ⇒ Object
81 82 83 |
# File 'lib/redd/models/user.rb', line 81 %i(overview submitted comments liked disliked hidden saved gilded).each do |type| define_method(type) { |**params| listing(type, **params) } end |
#liked(**params) ⇒ Object
81 82 83 |
# File 'lib/redd/models/user.rb', line 81 %i(overview submitted comments liked disliked hidden saved gilded).each do |type| define_method(type) { |**params| listing(type, **params) } end |
#listing(type, **params) ⇒ Listing<Submission>
The option :time only applies to the top and controversial sorts.
Get the appropriate listing.
66 67 68 69 |
# File 'lib/redd/models/user.rb', line 66 def listing(type, **params) params[:t] = params.delete(:time) if params.key?(:time) @client.model(:get, "/user/#{get_attribute(:name)}/#{type}.json", params) end |
#overview(**params) ⇒ Object
81 82 83 |
# File 'lib/redd/models/user.rb', line 81 %i(overview submitted comments liked disliked hidden saved gilded).each do |type| define_method(type) { |**params| listing(type, **params) } end |
#saved(**params) ⇒ Object
81 82 83 |
# File 'lib/redd/models/user.rb', line 81 %i(overview submitted comments liked disliked hidden saved gilded).each do |type| define_method(type) { |**params| listing(type, **params) } end |
#send_message(subject:, text:, from: nil) ⇒ Object
Compose a message to the moderators of a subreddit.
33 34 35 |
# File 'lib/redd/models/user.rb', line 33 def (subject:, text:, from: nil) super(to: get_attribute(:name), subject: subject, text: text, from: from) end |
#submitted(**params) ⇒ Object
81 82 83 |
# File 'lib/redd/models/user.rb', line 81 %i(overview submitted comments liked disliked hidden saved gilded).each do |type| define_method(type) { |**params| listing(type, **params) } end |
#unblock(me: nil) ⇒ Object
Unblock a previously blocked user.
22 23 24 25 26 |
# File 'lib/redd/models/user.rb', line 22 def unblock(me: nil) my_id = 't2_' + (me.is_a?(User) ? user.id : @client.get('/api/v1/me').body[:id]) # Talk about an unintuitive endpoint @client.post('/api/unfriend', container: my_id, name: get_attribute(:name), type: 'enemy') end |
#unfriend ⇒ Object
Unfriend the user.
46 47 48 49 |
# File 'lib/redd/models/user.rb', line 46 def unfriend name = get_attribute(:name) @client.request(:delete, "/api/v1/me/friends/#{name}", raw: true, form: { id: name }) end |