Class: Redd::Models::User

Inherits:
Model
  • Object
show all
Includes:
Messageable
Defined in:
lib/redd/models/user.rb

Overview

A reddit user.

Direct Known Subclasses

Self

Instance Attribute Summary collapse

Attributes inherited from Model

#client

Instance Method Summary collapse

Methods inherited from Model

#initialize

Constructor Details

This class inherits a constructor from Redd::Models::Model

Instance Attribute Details

#comment_karmaInteger (readonly)

Returns the user's comment karma.

Returns:

  • (Integer)

    the user's comment karma


220
# File 'lib/redd/models/user.rb', line 220

property :comment_karma

#created_atTime (readonly)

Returns the time the user signed up.

Returns:

  • (Time)

    the time the user signed up


208
# File 'lib/redd/models/user.rb', line 208

property :created_at, from: :created_utc, with: ->(epoch) { Time.at(epoch) }

#employee?Boolean (readonly)

Returns whether the user is a reddit employee.

Returns:

  • (Boolean)

    whether the user is a reddit employee


103
# File 'lib/redd/models/user.rb', line 103

property :employee?, from: :is_employee

#featuresHash (readonly)

Returns a hash of features.

Returns:

  • (Hash)

    a hash of features


107
# File 'lib/redd/models/user.rb', line 107

property :features

#friend?Boolean (readonly)

Returns whether the user is your friend.

Returns:

  • (Boolean)

    whether the user is your friend


111
# File 'lib/redd/models/user.rb', line 111

property :friend?, from: :is_friend

#geopopularString (readonly)

Returns:

  • (String)

123
# File 'lib/redd/models/user.rb', line 123

property :geopopular, from: :pref_geopopular

#gold?Boolean (readonly)

Returns whether the user currently has gold.

Returns:

  • (Boolean)

    whether the user currently has gold


168
# File 'lib/redd/models/user.rb', line 168

property :gold?, from: :is_gold

#gold_credditsInteger (readonly)

Returns the number of gold creddits the user has.

Returns:

  • (Integer)

    the number of gold creddits the user has


212
# File 'lib/redd/models/user.rb', line 212

property :gold_creddits

#gold_expirationTime? (readonly)

Returns the time when the user's gold expires.

Returns:

  • (Time, nil)

    the time when the user's gold expires


135
# File 'lib/redd/models/user.rb', line 135

property :gold_expiration, with: ->(epoch) { Time.at(epoch) if epoch }

#has_mail?Boolean (readonly)

Returns whether the user has new messages.

Returns:

  • (Boolean)

    whether the user has new messages


200
# File 'lib/redd/models/user.rb', line 200

property :has_mail?, from: :has_mail

#has_mod_mail?Boolean (readonly)

Returns whether the user has old-style mod mail.

Returns:

  • (Boolean)

    whether the user has old-style mod mail


180
# File 'lib/redd/models/user.rb', line 180

property :has_mod_mail?, from: :has_mod_mail

#has_subscribed?Boolean (readonly)

Returns:

  • (Boolean)

224
# File 'lib/redd/models/user.rb', line 224

property :has_subscribed?, from: :has_subscribed

#has_verified_email?Boolean (readonly)

Returns whether the user's email has been verified.

Returns:

  • (Boolean)

    whether the user's email has been verified


176
# File 'lib/redd/models/user.rb', line 176

property :has_verified_email?, from: :has_verified_email

#hidden_from_robots?Boolean (readonly)

Returns whether the user chose to hide from Google.

Returns:

  • (Boolean)

    whether the user chose to hide from Google


184
# File 'lib/redd/models/user.rb', line 184

property :hidden_from_robots?, from: :hide_from_robots

#idString (readonly)

Returns the user's base36 id.

Returns:

  • (String)

    the user's base36 id


139
# File 'lib/redd/models/user.rb', line 139

property :id

#in_beta?Boolean (readonly)

Returns whether the user is in beta.

Returns:

  • (Boolean)

    whether the user is in beta


216
# File 'lib/redd/models/user.rb', line 216

property :in_beta?, from: :in_beta

#inbox_countInteger (readonly)

Returns the number of messages in the user's inbox.

Returns:

  • (Integer)

    the number of messages in the user's inbox


192
# File 'lib/redd/models/user.rb', line 192

property :inbox_count

Returns the user's link karma.

Returns:

  • (Integer)

    the user's link karma


188
# File 'lib/redd/models/user.rb', line 188

property :link_karma

#mod?Boolean (readonly)

Returns whether the user is a moderator.

Returns:

  • (Boolean)

    whether the user is a moderator


172
# File 'lib/redd/models/user.rb', line 172

property :mod?, from: :is_mod

#nameString (readonly)

Returns the user's username.

Returns:

  • (String)

    the user's username


99
# File 'lib/redd/models/user.rb', line 99

property :name

#new_modmail_exists?Boolean (readonly)

Returns whether the user has mail in the new modmail.

Returns:

  • (Boolean)

    whether the user has mail in the new modmail


160
# File 'lib/redd/models/user.rb', line 160

property :new_modmail_exists?, from: :new_modmail_exists

#no_profanity?Boolean (readonly)

Returns whether the user chooses to filter profanity.

Returns:

  • (Boolean)

    whether the user chooses to filter profanity


115
# File 'lib/redd/models/user.rb', line 115

property :no_profanity?, from: :pref_no_profanity

#over_18?Boolean (readonly)

Returns whether the user has indicated they're over 18.

Returns:

  • (Boolean)

    whether the user has indicated they're over 18


147
# File 'lib/redd/models/user.rb', line 147

property :over_18?, from: :profile_over_18

#profile_imageString (readonly)

Returns a link to the user's profile image.

Returns:

  • (String)

    a link to the user's profile image


143
# File 'lib/redd/models/user.rb', line 143

property :profile_image, from: :profile_img

#show_snoovatar?Boolean (readonly)

Returns whether the user's snoovatar is shown.

Returns:

  • (Boolean)

    whether the user's snoovatar is shown


204
# File 'lib/redd/models/user.rb', line 204

property :show_snoovatar?, from: :pref_show_snoovatar

#show_top_karma_subreddits?Boolean (readonly)

Returns whether top karma subreddits are shown on the user's page.

Returns:

  • (Boolean)

    whether top karma subreddits are shown on the user's page


196
# File 'lib/redd/models/user.rb', line 196

property :show_top_karma_subreddits?, from: :pref_top_karma_subreddits

#sponsor?Boolean (readonly)

Returns:

  • (Boolean)

131
# File 'lib/redd/models/user.rb', line 131

property :sponsor?, from: :is_sponsor

#subredditSubreddit (readonly)

Returns the user's personal “subreddit”.

Returns:

  • (Subreddit)

    the user's personal “subreddit”


127
# File 'lib/redd/models/user.rb', line 127

property :subreddit, with: ->(name) { Subreddit.new(client, display_name: name) if name }

#suspended?Boolean (readonly)

Returns whether the user is suspended.

Returns:

  • (Boolean)

    whether the user is suspended


119
# File 'lib/redd/models/user.rb', line 119

property :suspended?, from: :is_suspended

#suspension_expirationTime? (readonly)

Returns the time when the user's suspension expires.

Returns:

  • (Time, nil)

    the time when the user's suspension expires


151
152
# File 'lib/redd/models/user.rb', line 151

property :suspension_expiration, from: :suspension_expiration_utc,
with: ->(epoch) { Time.at(epoch) if epoch }

#verified?Boolean (readonly)

Returns whether the user is verified (?).

Returns:

  • (Boolean)

    whether the user is verified (?)


156
# File 'lib/redd/models/user.rb', line 156

property :verified?, from: :verified

Instance Method Details

#blockObject

Block this user.


58
59
60
# File 'lib/redd/models/user.rb', line 58

def block
  client.post('/api/block_user', account_id: read_attribute(:id))
end

#comments(**params) ⇒ Object

See Also:


44
45
46
# File 'lib/redd/models/user.rb', line 44

%i[overview submitted comments liked disliked hidden saved gilded].each do |type|
  define_method(type) { |**params| listing(type, **params) }
end

#disliked(**params) ⇒ Object

See Also:


44
45
46
# File 'lib/redd/models/user.rb', line 44

%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.

Parameters:

  • note (String) (defaults to: nil)

    a note for the friend


79
80
81
82
83
# File 'lib/redd/models/user.rb', line 79

def friend(note = nil)
  name = read_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.

Parameters:

  • months (Integer) (defaults to: 1)

    the number of months of gold to gift


93
94
95
# File 'lib/redd/models/user.rb', line 93

def gift_gold(months: 1)
  client.post("/api/v1/gold/give/#{read_attribute(:name)}", months: months)
end

#gilded(**params) ⇒ Object

See Also:


44
45
46
# File 'lib/redd/models/user.rb', line 44

%i[overview submitted comments liked disliked hidden saved gilded].each do |type|
  define_method(type) { |**params| listing(type, **params) }
end

#hidden(**params) ⇒ Object

See Also:


44
45
46
# File 'lib/redd/models/user.rb', line 44

%i[overview submitted comments liked disliked hidden saved gilded].each do |type|
  define_method(type) { |**params| listing(type, **params) }
end

#liked(**params) ⇒ Object

See Also:


44
45
46
# File 'lib/redd/models/user.rb', line 44

%i[overview submitted comments liked disliked hidden saved gilded].each do |type|
  define_method(type) { |**params| listing(type, **params) }
end

#listing(type, **options) ⇒ Listing<Submission>

Note:

The option :time only applies to the top and controversial sorts.

Get the appropriate listing.

Parameters:

  • type (:overview, :submitted, :comments, :liked, :disliked, :hidden, :saved, :gilded)

    the type of listing to request

  • options (Hash)

    a list of options to send with the request

Options Hash (**options):

  • :sort (:hot, :new, :top, :controversial)

    the order of the listing

  • :after (String)

    return results after the given fullname

  • :before (String)

    return results before the given fullname

  • :count (Integer)

    the number of items already seen in the listing

  • :limit (1..100)

    the maximum number of things to return

  • :time (:hour, :day, :week, :month, :year, :all)

    the time period to consider when sorting

  • :show (:given)

    whether to show the gildings given

Returns:


27
28
29
30
31
32
# File 'lib/redd/models/user.rb', line 27

def listing(type, **options)
  options[:t] = options.delete(:time) if options.key?(:time)
  PaginatedListing.new(client, options) do |**req_opts|
    client.model(:get, "/user/#{read_attribute(:name)}/#{type}.json", options.merge(req_opts))
  end
end

#overview(**params) ⇒ Object

See Also:


44
45
46
# File 'lib/redd/models/user.rb', line 44

%i[overview submitted comments liked disliked hidden saved gilded].each do |type|
  define_method(type) { |**params| listing(type, **params) }
end

#saved(**params) ⇒ Object

See Also:


44
45
46
# File 'lib/redd/models/user.rb', line 44

%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.

Parameters:

  • subject (String)

    the subject of the message

  • text (String)

    the message text

  • from (Subreddit, nil) (defaults to: nil)

    the subreddit to send the message on behalf of


53
54
55
# File 'lib/redd/models/user.rb', line 53

def send_message(subject:, text:, from: nil)
  super(to: read_attribute(:name), subject: subject, text: text, from: from)
end

#submitted(**params) ⇒ Object

See Also:


44
45
46
# File 'lib/redd/models/user.rb', line 44

%i[overview submitted comments liked disliked hidden saved gilded].each do |type|
  define_method(type) { |**params| listing(type, **params) }
end

#trophiesArray<Trophy>

Returns this user's trophies.

Returns:

  • (Array<Trophy>)

    this user's trophies


63
64
65
66
67
# File 'lib/redd/models/user.rb', line 63

def trophies
  client.get("/api/v1/user/#{read_attribute(:name)}/trophies")
        .body[:data][:trophies]
        .map { |t| client.unmarshal(t) }
end

#unblock(me: nil) ⇒ Object

Unblock a previously blocked user.

Parameters:

  • me (User) (defaults to: nil)

    (optional) the person doing the unblocking


71
72
73
74
75
# File 'lib/redd/models/user.rb', line 71

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: read_attribute(:name), type: 'enemy')
end

#unfriendObject

Unfriend the user.


86
87
88
89
# File 'lib/redd/models/user.rb', line 86

def unfriend
  name = read_attribute(:name)
  client.request(:delete, "/api/v1/me/friends/#{name}", raw: true, form: { id: name })
end