Class: Redd::Models::Session

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

Overview

The starter class.

Instance Attribute Summary

Attributes inherited from Model

#client

Instance Method Summary collapse

Methods included from Searchable

#search

Methods inherited from Model

#initialize

Constructor Details

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

Instance Method Details

#blockedArray<User>

Returns users blocked by the logged-in user.

Returns:

  • (Array<User>)

    users blocked by the logged-in user


147
148
149
150
151
# File 'lib/redd/models/session.rb', line 147

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.

Parameters:

  • new_prefs (Hash) (defaults to: {})

    the changed preferences

Returns:

  • (Hash)

    the new preferences

See Also:


131
132
133
134
135
136
137
# File 'lib/redd/models/session.rb', line 131

def edit_preferences(new_prefs = {})
  client.request(
    :patch, '/api/v1/me/prefs',
    headers: { 'Content-Type' => 'application/json' },
    body: JSON.generate(new_prefs)
  ).body
end

#friendsArray<User>

Returns the logged-in user's friends.

Returns:

  • (Array<User>)

    the logged-in user's friends


140
141
142
143
144
# File 'lib/redd/models/session.rb', line 140

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_fullnames(*fullnames) ⇒ Array<Submission, Comment, User, Subreddit>

Create lazily-loaded objects from their fullnames (e.g. t1_abc123).

Parameters:

  • fullnames (String)

    fullname for a submission, comment, or subreddit.

Returns:


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/redd/models/session.rb', line 81

def from_fullnames(*fullnames)
  fullnames.map do |name|
    if name.start_with?('t1_')
      Comment.new(client, name: name)
    elsif name.start_with?('t3_')
      Submission.new(client, name: name)
    elsif name.start_with?('t5_')
      Subreddit.new(client, name: name)
    else
      raise "unknown fullname #{name}"
    end
  end
end

#from_ids(*fullnames) ⇒ Listing<Submission, Comment>

Deprecated.

Try the lazier #from_fullnames instead.

Get submissions or comments by their fullnames.

Parameters:

  • fullnames (String, Array<String>)

    one or an array of fullnames (e.g. t3_abc1234)

Returns:


74
75
76
# File 'lib/redd/models/session.rb', line 74

def from_ids(*fullnames)
  client.model(:get, '/api/info', id: fullnames.join(','))
end

#from_url(url) ⇒ Submission, ...

Get submissions or comments by their fullnames.

Parameters:

  • url (String)

    the object's url

Returns:


98
99
100
# File 'lib/redd/models/session.rb', line 98

def from_url(url)
  client.model(:get, '/api/info', url: url).first
end

#front_pageFrontPage

Returns the user's front page.

Returns:


18
19
20
# File 'lib/redd/models/session.rb', line 18

def front_page
  @front_page ||= FrontPage.new(client)
end

#karma_breakdownHash

Returns a breakdown of karma over subreddits.

Returns:

  • (Hash)

    a breakdown of karma over subreddits


33
34
35
# File 'lib/redd/models/session.rb', line 33

def karma_breakdown
  client.get('/api/v1/me/karma').body[:data]
end

#live_thread(id) ⇒ LiveThread

Returns the live thread.

Returns:


28
29
30
# File 'lib/redd/models/session.rb', line 28

def live_thread(id)
  LiveThread.new(client, id: id)
end

#meUser

Returns the logged-in user.

Returns:

  • (User)

    the logged-in user


13
14
15
# File 'lib/redd/models/session.rb', line 13

def me
  @me ||= Self.new(client)
end

#modmailModmail

Returns the new modmail.

Returns:


23
24
25
# File 'lib/redd/models/session.rb', line 23

def modmail
  @modmail ||= Modmail.new(client)
end

#multi(path) ⇒ Multireddit

Get a (lazily loaded) multi by its path.

Parameters:

  • path (String)

    the multi's path, surrounded by a leading and trailing /

Returns:


66
67
68
# File 'lib/redd/models/session.rb', line 66

def multi(path)
  Multireddit.new(client, path: 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).

Parameters:

  • category ('inbox', 'unread', 'sent', 'moderator', 'messages', 'comments', 'selfreply', 'mentions') (defaults to: 'inbox')

    the category of messages to view

  • mark (Boolean) (defaults to: false)

    whether to remove the orangered from the user's inbox

  • params (Hash)

    a list of optional params to send with the request

Options Hash (**params):

  • :after (String)

    return results after the given fullname

  • :before (String)

    return results before the given fullname

  • :count (Integer) — default: 0

    the number of items already seen in the listing

  • :limit (1..100) — default: 25

    the maximum number of things to return

Returns:


113
114
115
# File 'lib/redd/models/session.rb', line 113

def my_messages(category: 'inbox', mark: false, **params)
  client.model(:get, "/message/#{category}.json", params.merge(mark: mark))
end

#my_multisArray<Multireddit>

Returns array of multireddits belonging to the user.

Returns:

  • (Array<Multireddit>)

    array of multireddits belonging to the user


59
60
61
# File 'lib/redd/models/session.rb', line 59

def my_multis
  client.get('/api/multi/mine').body.map { |m| client.unmarshal(m) }
end

#my_preferencesHash

Returns the user's preferences.

Returns:

  • (Hash)

    the user's preferences


123
124
125
# File 'lib/redd/models/session.rb', line 123

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.

Parameters:

  • type ('subscriber', 'contributor', 'moderator')

    the type of subreddits

  • params (Hash)

    a list of optional params to send with the request

Options Hash (**params):

  • :after (String)

    return results after the given fullname

  • :before (String)

    return results before the given fullname

  • :count (Integer) — default: 0

    the number of items already seen in the listing

  • :limit (1..100) — default: 25

    the maximum number of things to return

Returns:


174
175
176
# File 'lib/redd/models/session.rb', line 174

def my_subreddits(type, **params)
  client.model(:get, "/subreddits/mine/#{type}", params)
end

#read_all_messagesObject

Mark all messages as read.


118
119
120
# File 'lib/redd/models/session.rb', line 118

def read_all_messages
  client.post('/api/read_all_messages')
end

#saved_categoriesArray<String>

Returns a list of categories the user's items are saved in.

Returns:

  • (Array<String>)

    a list of categories the user's items are saved in


161
162
163
# File 'lib/redd/models/session.rb', line 161

def saved_categories
  client.get('/api/saved_categories').body
end

#subreddit(display_name) ⇒ Subreddit

Get a (lazily loaded) subreddit by its name.

Parameters:

  • display_name (String)

    the subreddit's display name

Returns:


54
55
56
# File 'lib/redd/models/session.rb', line 54

def subreddit(display_name)
  Subreddit.new(client, display_name: display_name)
end

Return trending subreddits.

Examples:

session.trending_subreddits
=> {
  "subreddit_names": [
    "AskLibertarians",
    "OpTicGaming",
    "Cuphead",
    "AlmostParkour",
    "TheGoodPlace"
  ],
  "comment_count": 176,
  "comment_url": "/r/trendingsubreddits/comments/73dkin/trending_subreddits_for_..."
}

Returns:

  • (Hash)

193
194
195
# File 'lib/redd/models/session.rb', line 193

def trending_subreddits
  client.get('/api/trending_subreddits').body
end

#trustedArray<User>

Returns users trusted by the logged-in user.

Returns:

  • (Array<User>)

    users trusted by the logged-in user


154
155
156
157
158
# File 'lib/redd/models/session.rb', line 154

def trusted
  client.get('/prefs/trusted').body[:data][:children].map do |h|
    User.new(client, name: h[:name], id: h[:id].sub('t2_', ''), since: h[:date])
  end
end

#user(name) ⇒ User

Get a (lazily loaded) reddit user by their name.

Parameters:

  • name (String)

    the username

Returns:


40
41
42
# File 'lib/redd/models/session.rb', line 40

def user(name)
  User.new(client, name: name)
end

#username_available?(username) ⇒ Boolean

Returns whether a username is available.

Parameters:

  • username (String)

    the username to check

Returns:

  • (Boolean)

    whether the username is available


47
48
49
# File 'lib/redd/models/session.rb', line 47

def username_available?(username)
  client.get('/api/username_available', user: username).body
end