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>


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.

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>


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.


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.


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


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

def front_page
  FrontPage.new(@client)
end

#karma_breakdownHash


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


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

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

#meUser


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


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.


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

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


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>


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


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.

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


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>


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.


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

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

#trustedArray<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.


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

def user(name)
  User.from_id(@client, name)
end