Module: RedditKit::Client::Users

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

Overview

Methods for interacting with reddit users.

Instance Method Summary collapse

Instance Method Details

#friend(user) ⇒ Object

Adds a user to the current user’s friend list.

Parameters:

  • user (String, RedditKit::User)

    A user’s username, or a RedditKit::User.



72
73
74
75
# File 'lib/redditkit/client/users.rb', line 72

def friend(user)
  friend_name = extract_string(user, :username)
  friend_request 'friend', :container => current_user.full_name, :name => friend_name, :type => :friend
end

#friendsArray<OpenStruct>

Gets the current user’s friends.

Returns:

  • (Array<OpenStruct>)


61
62
63
64
65
66
67
# File 'lib/redditkit/client/users.rb', line 61

def friends
  response = request(:get, 'prefs/friends.json', nil, https_connection)
  body = response[:body]
  friends = body[0][:data][:children]

  friends.map { |friend| OpenStruct.new(friend) }
end

#my_content(options = {}) ⇒ RedditKit::PaginatedResponse

Gets links and comments for the current user.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :category (overview, comments, submitted, liked, disliked)

    The category from which to return links and comments. Defaults to overview.

  • :limit (1..100)

    The number of links and comments to return.

  • :before (String)

    Only return links and comments before this id.

  • :after (String)

    Only return links and comments after this id.

Returns:



30
31
32
33
34
35
36
37
38
# File 'lib/redditkit/client/users.rb', line 30

def my_content(options = {})
  options = options.clone

  category = options[:category] || :overview
  path = "user/#{@username}/#{category}.json"
  options.delete :category

  objects_from_response(:get, path, options)
end

#register(username, password, options = {}) ⇒ Object

Registers a new reddit account.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • username (String)

    The username to register.

  • password (String)

    The password for the account.

  • email (String)

    The optional email address for the account.

  • captcha_identifier (String)

    The identifier for the CAPTCHA challenge solved by the user.

  • captcha (String)

    The user’s response to the CAPTCHA challenge.

  • remember (Boolean)

    Whether to keep the user’s session cookie beyond the current session.



103
104
105
106
# File 'lib/redditkit/client/users.rb', line 103

def register(username, password, options = {})
  parameters = { :user => username, :passwd => password, :passwd2 => password, :email => options[:email], :captcha => options[:captcha], :iden => options[:captcha_identifier] }
  post('api/register', parameters)
end

#unfriend(user) ⇒ Object

Removes a user from the current user’s friend list.

Parameters:



80
81
82
83
# File 'lib/redditkit/client/users.rb', line 80

def unfriend(user)
  friend_name = extract_string(user, :username)
  friend_request 'unfriend', :container => current_user.full_name, :name => friend_name, :type => :friend
end

#user(username = nil) ⇒ RedditKit::User

Gets a user object.

Examples:

current_user = client.user

user = client.user ‘amberlynns’

Parameters:

  • username (String) (defaults to: nil)

    A reddit account’s username. Gets the current user if this is nil.

Returns:



15
16
17
18
19
20
21
# File 'lib/redditkit/client/users.rb', line 15

def user(username = nil)
  if username
    object_from_response(:get, "user/#{username}/about.json", nil)
  else
    object_from_response(:get, 'api/me.json', nil)
  end
end

#user_content(user, options = {}) ⇒ RedditKit::PaginatedResponse

Note:

Public access to the liked and disliked categories is disabled by default, so this will return an empty array for most users.

Gets links and comments for a user.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :category (overview, comments, submitted, liked, disliked)

    The category from which to return links and comments. Defaults to overview.

  • :limit (1..100)

    The number of links and comments to return.

  • :before (String)

    Only return links and comments before this id.

  • :after (String)

    Only return links and comments after this id.

Returns:



48
49
50
51
52
53
54
55
56
# File 'lib/redditkit/client/users.rb', line 48

def user_content(user, options = {})
  options = options.clone
  username = user

  path = "user/#{username}/%s.json" % (options[:category] if options[:category])
  options.delete :category

  objects_from_response(:get, path, options)
end

#username_available?(username) ⇒ Boolean

Checks whether a specific username is available.

Examples:

puts “Username is available” if client.username_available? ‘some_username’

Parameters:

  • username (String)

    A username for which to check availability.

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/redditkit/client/users.rb', line 90

def username_available?(username)
  response = get('api/username_available.json', :user => username)
  response[:body]
end