class Redd::Models::User

A reddit user.

Public Class Methods

from_id(client, id) click to toggle source

Create a User from their name. @param client [APIClient] the api client to initialize the object with @param id [String] the username @return [User]

# File lib/redd/models/user.rb, line 16
def self.from_id(client, id)
  new(client, name: id)
end

Public Instance Methods

friend(note = nil) click to toggle source

Add the user as a friend. @param note [String] a note for the friend

# 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) click to toggle source

Gift a redditor reddit gold. @param months [Integer] the number of months of gold to gift

# 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
listing(type, **params) click to toggle source

Get the appropriate listing. @param type [:overview, :submitted, :comments, :liked, :disliked, :hidden, :saved, :gilded]

the type of listing to request

@param params [Hash] a list of params to send with the request @option params [:hot, :new, :top, :controversial] :sort the order of the listing @option params [String] :after return results after the given fullname @option params [String] :before return results before the given fullname @option params [Integer] :count the number of items already seen in the listing @option params [1..100] :limit the maximum number of things to return @option params [:hour, :day, :week, :month, :year, :all] :time the time period to consider

when sorting

@option params [:given] :show whether to show the gildings given

@note The option :time only applies to the top and controversial sorts. @return [Listing<Submission>]

# 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
send_message(subject:, text:, from: nil) click to toggle source

Compose a message to the moderators of a subreddit.

@param subject [String] the subject of the message @param text [String] the message text @param from [Subreddit, nil] the subreddit to send the message on behalf of

Calls superclass method Redd::Models::Messageable#send_message
# File lib/redd/models/user.rb, line 33
def send_message(subject:, text:, from: nil)
  super(to: get_attribute(:name), subject: subject, text: text, from: from)
end
unblock(me: nil) click to toggle source

Unblock a previously blocked user. @param me [User] (optional) the person doing the unblocking

# 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() click to toggle source

Unfriend the user.

# 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

Private Instance Methods

default_loader() click to toggle source
# File lib/redd/models/user.rb, line 93
def default_loader
  @client.get("/user/#{@attributes.fetch(:name)}/about").body[:data]
end