Module: RedditKit::Client::Account

Included in:
RedditKit::Client
Defined in:
lib/redditkit/client/account.rb

Overview

Methods which operate on a user’s account.

Instance Method Summary collapse

Instance Method Details

#sign_in(username, password) ⇒ Object

Sign in to a reddit account.

Parameters:

  • username (String)

    The reddit account’s username.

  • password (String)

    The reddit account’s password.

See Also:



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/redditkit/client/account.rb', line 12

def (username, password)
  response = https_post 'api/login', { :user => username, :passwd => password, :api_type => :json }
  body = response[:body]

  data = body[:json][:data]

  @modhash = data[:modhash]
  @cookie = data[:cookie]
  @username = username

  @current_user = user @username
  @username = @current_user.username
end

#sign_outObject

Sign the current user out.



34
35
36
37
38
39
40
# File 'lib/redditkit/client/account.rb', line 34

def sign_out
  @username = nil
  @current_user = nil

  @modhash = nil
  @cookie = nil
end

#signed_in?Boolean

Check is a user is currently signed in.

Returns:

  • (Boolean)


29
30
31
# File 'lib/redditkit/client/account.rb', line 29

def signed_in?
  !!@modhash and !!@cookie
end

#update_account(options) ⇒ Object

Updates the current user’s email or password.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • current_password (String)

    The user’s current password.

  • new_password (String)

    The user’s new password.

  • email (String)

    The user’s new email address.

See Also:



48
49
50
51
52
53
# File 'lib/redditkit/client/account.rb', line 48

def (options)
  password = options[:new_password]
  parameters = { :curpass => options[:current_password], :email => options[:email], :newpass => password, :verpass => password, :api_type => :json }

  post('api/update', parameters)
end

#update_session(password) ⇒ String

Invalidates all session cookies and updates the current one.

Parameters:

  • password (String)

    The authenticated user’s current password.

Returns:

  • (String)

    The new cookie value.

See Also:



60
61
62
63
64
65
66
67
68
69
# File 'lib/redditkit/client/account.rb', line 60

def update_session(password)
  response = post('api/clear_sessions', :dest => api_endpoint, :curpass => password, :api_type => 'json')

  response_headers = response[:response_headers]
  full_cookie_string = response_headers['set-cookie']
  cookie = full_cookie_string[/reddit_session=(.*?);/m, 1]

  @cookie = cookie
  cookie
end