Class: Net::Twitter::Models::User

Inherits:
Object
  • Object
show all
Defined in:
lib/net/twitter/models/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ User

Returns a new instance of User.



10
11
12
13
# File 'lib/net/twitter/models/user.rb', line 10

def initialize(attrs = {})
  attrs.each{|k, v| instance_variable_set("@#{k}", v) unless v.nil?}
  @follower_count = attrs['followers_count']
end

Instance Attribute Details

#follower_countObject (readonly)

Returns the value of attribute follower_count.



8
9
10
# File 'lib/net/twitter/models/user.rb', line 8

def follower_count
  @follower_count
end

#screen_nameObject (readonly)

Returns the value of attribute screen_name.



8
9
10
# File 'lib/net/twitter/models/user.rb', line 8

def screen_name
  @screen_name
end

Class Method Details

.find_by(params = {}) ⇒ Net::Twitter::Models::User?

Returns the existing Twitter user matching the provided attributes or nil when the user is not found.

Parameters:

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

    the attributes to find a user by.

Options Hash (params):

  • :screen_name (String)

    The Twitter user’s screen name (case-insensitive).

Returns:



23
24
25
26
27
# File 'lib/net/twitter/models/user.rb', line 23

def self.find_by(params = {})
  find_by! params
rescue Errors::UnknownUser, Errors::SuspendedUser
  nil
end

.find_by!(params = {}) ⇒ Net::Twitter::Models::User

Returns the existing Twitter user matching the provided attributes or raises an error when the user is not found or suspended.

Parameters:

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

    the attributes to find a user by.

Options Hash (params):

  • :screen_name (String)

    The Twitter user’s screen name (case-insensitive).

Returns:

Raises:

  • (Net::Errors::UnknownUser)

    if the user cannot be found.

  • (Net::Errors::SuspendedUser)

    if the user account is suspended.



38
39
40
41
42
43
44
45
46
47
# File 'lib/net/twitter/models/user.rb', line 38

def self.find_by!(params = {})
  request = Api::Request.new endpoint: 'users/show', params: params
  user_data = request.run
  new user_data
rescue Errors::ResponseError => error
  case error.response
    when Net::HTTPNotFound then raise Errors::UnknownUser
    when Net::HTTPForbidden then raise Errors::SuspendedUser
  end
end

.where(conditions = {}) ⇒ Array<Net::Twitter::Models::User>

Returns the Twitter users.

Parameters:

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

    The attributes to find users by.

Options Hash (conditions):

  • screen_name (Array<String>)

    The Twitter user’s screen names (case-insensitive).

Returns:

Raises:

  • (Net::Errors::TooManyUsers)

    when more than 100 Twitter users match the provided attributes.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/net/twitter/models/user.rb', line 59

def self.where(conditions = {})
  params = to_where_params conditions
  request = Api::Request.new endpoint: 'users/lookup', params: params
  users_data = request.run
  users_data.map{|user_data| new user_data}
rescue Errors::ResponseError => error
  case error.response
    when Net::HTTPNotFound then []
    when Net::HTTPForbidden then raise Errors::TooManyUsers
  end
end