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
# File 'lib/net/twitter/models/user.rb', line 10

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

Instance Attribute Details

#followers_countObject (readonly)

Returns the value of attribute followers_count.



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

def followers_count
  @followers_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:



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

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.



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

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

.to_where_params(conditions = {}) ⇒ Object



72
73
74
75
76
# File 'lib/net/twitter/models/user.rb', line 72

def self.to_where_params(conditions = {})
  conditions.dup.tap do |params|
    params.each{|k,v| params[k] = v.join(',') if v.is_a?(Array)}
  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.



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

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