Class: Redd::Models::User

Inherits:
LazyModel show all
Includes:
Messageable
Defined in:
lib/redd/models/user.rb

Overview

A reddit user.

Instance Attribute Summary

Attributes inherited from BasicModel

#client

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • client (APIClient)

    the api client to initialize the object with

  • id (String)

    the username

Returns:


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

See Also:


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

See Also:


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.

Parameters:

  • note (String) (defaults to: nil)

    a note for the 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.

Parameters:

  • months (Integer) (defaults to: 1)

    the number of months of gold to gift


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

See Also:


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

See Also:


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

See Also:


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>

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

  • params (Hash)

    a list of params to send with the request

Options Hash (**params):

  • :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:


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

See Also:


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

See Also:


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.

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


33
34
35
# 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

#submitted(**params) ⇒ Object

See Also:


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.

Parameters:

  • me (User) (defaults to: nil)

    (optional) the person doing the unblocking


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

#unfriendObject

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