Module: Snoo::Account

Defined in:
lib/snoo/account.rb

Overview

Account related methods, such as logging in, deleting the current user, changing passwords, etc

Author:

Instance Method Summary collapse

Instance Method Details

#auth(modhash, cookies) ⇒ Object

Auth into reddit via modhash and cookie. This has the advantage of not throttling you if you call it a lot



27
28
29
30
31
32
33
# File 'lib/snoo/account.rb', line 27

def auth modhash, cookies
  set_cookies cookies
  @modhash = modhash
  meinfo = get("/api/me.json")
  @username = meinfo['data']['name']
  @userid = 't2_' + meinfo['data']['id']
end

#clear_sessions(password) ⇒ HTTParty::Response

Note:

This method provides no verification or checking, so use with care

Invalidates all other reddit session cookies, and updates the current one. This will log out all other reddit clients, as described in the reddit API



50
51
52
53
54
55
# File 'lib/snoo/account.rb', line 50

def clear_sessions password
  logged_in?
  clear = post('/api/clear_sessions', body: { curpass: password, dest: @baseurl, uh: @modhash, api_type: 'json' })
  set_cookies clear.headers['set-cookie']
  return clear
end

#delete_user(password, reason = "deleted by script command") ⇒ HTTParty::Response

Note:

This method provides no verification or checking, so use with care

Deletes the current user. This requires a password for security reasons.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/snoo/account.rb', line 63

def delete_user password, reason = "deleted by script command"
  logged_in?
  delete = post('/api/delete_user', body: {
    confirm: true,
    delete_message: reason,
    passwd: password,
    uh: @modhash,
    user: @username,
    api_type: 'json'
    })
  return delete
end

#log_in(username, password) ⇒ HTTParty::Response

Log into a reddit account. You need to do this to use any restricted or write APIs



12
13
14
15
16
17
18
19
20
21
# File 'lib/snoo/account.rb', line 12

def  username, password
   = post("/api/login", :body => {user: username, passwd: password, api_type: 'json'})
  errors = ['json']['errors']
  raise errors[0][1] unless errors.size == 0
  set_cookies .headers['set-cookie']
  @modhash = ['json']['data']['modhash']
  @username = username
  @userid = 't2_' + get('/api/me.json')['data']['id']
  return 
end

#log_outObject

Logs out of a reddit account. This is usually uneeded, you can just log_in as a new account to replace the current one. This just nils the cookies and modhash



37
38
39
40
41
42
# File 'lib/snoo/account.rb', line 37

def log_out
  set_cookies nil
  @modhash = nil
  @userid = nil
  @username = nil
end

#meHTTParty::Response

Gets info about the currently logged in user.



78
79
80
81
# File 'lib/snoo/account.rb', line 78

def me
  logged_in?
  get("/api/me.json")
end

#update_user(currentPass, newPass, email = nil) ⇒ HTTParty::Response

Note:

This method provides no verification or checking, so use with care

Changes the current user's password/email.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/snoo/account.rb', line 90

def update_user currentPass, newPass, email = nil
  logged_in?
  params = {
    curpass: currentPass,
    newpass: newPass,
    uh: @modhash,
    verify: true,
    verpass: newPass,
    api_type: 'json'
    }
  params[:email] = email if email
  update = post('/api/update', body: params )
  set_cookies update.headers['set-cookie']
  return update
end