Class: Redd::Models::Session

Inherits:
BasicModel show all
Includes:
Searchable
Defined in:
lib/redd/models/session.rb

Overview

The starter class.

Instance Attribute Summary

Attributes inherited from BasicModel

#client

Instance Method Summary collapse

Methods included from Searchable

#search

Methods inherited from BasicModel

from_id, #initialize, #inspect, #method_missing, #respond_to_missing?, #to_ary, #to_h

Constructor Details

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Redd::Models::BasicModel

Instance Method Details

#blockedArray<User>

Returns users blocked by the logged-in user.

Returns:

  • (Array<User>)

    users blocked by the logged-in user



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

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:



106
107
108
109
110
111
112
# File 'lib/redd/models/session.rb', line 106

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



115
116
117
118
119
# File 'lib/redd/models/session.rb', line 115

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_ids(fullnames) ⇒ Listing<Submission, Comment>

Get submissions or comments by their fullnames.

Parameters:

  • fullnames (String, Array<String>)

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

Returns:



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

def from_ids(fullnames)
  # XXX: Could we use better methods for t1_ and t3_?
  @client.model(:get, '/api/info', id: Array(fullnames).join(','))
end

#from_url(url) ⇒ Submission, ...

Get submissions or comments by their fullnames.

Parameters:

  • url (String)

    the object’s url

Returns:



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

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

#front_pageFrontPage

Returns the user’s front page.

Returns:



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

def front_page
  FrontPage.new(@client)
end

#karma_breakdownHash

Returns a breakdown of karma over subreddits.

Returns:

  • (Hash)

    a breakdown of karma over subreddits



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

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

#live_thread(id) ⇒ LiveThread

Returns the live thread.

Returns:



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

def live_thread(id)
  LiveThread.from_id(client, id)
end

#meUser

Returns the logged-in user.

Returns:

  • (User)

    the logged-in user



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

def me
  User.new(@client) { |client| client.get('/api/v1/me').body }
end

#mod_mailModMail

Returns the new modmail.

Returns:



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

def mod_mail
  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:



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

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



88
89
90
# File 'lib/redd/models/session.rb', line 88

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



52
53
54
# File 'lib/redd/models/session.rb', line 52

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



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

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:



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

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

#read_all_messagesObject

Mark all messages as read.



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

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



136
137
138
# File 'lib/redd/models/session.rb', line 136

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:



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

def subreddit(display_name)
  Subreddit.from_id(@client, display_name)
end

#trustedArray<User>

Returns users blocked by the logged-in user.

Returns:

  • (Array<User>)

    users blocked by the logged-in user



129
130
131
132
133
# File 'lib/redd/models/session.rb', line 129

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.from_id(@client, name)
end