class SocialNet::Twitter::Models::User

Attributes

follower_count[R]
screen_name[R]

Public Class Methods

find_by(params = {}) click to toggle source

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

@return [SocialNet::Twitter::Models::User] when the user is found. @return [nil] when the user is not found or suspended. @param [Hash] params the attributes to find a user by. @option params [String] :screen_name The Twitter user’s screen name

(case-insensitive).
# File lib/social_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 = {}) click to toggle source

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

@return [SocialNet::Twitter::Models::User] the Twitter user. @param [Hash] params the attributes to find a user by. @option params [String] :screen_name The Twitter user’s screen name

(case-insensitive).

@raise [SocialNet::Errors::UnknownUser] if the user cannot be found. @raise [SocialNet::Errors::SuspendedUser] if the user account is suspended.

# File lib/social_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
new(attrs = {}) click to toggle source
# File lib/social_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
where(conditions = {}) click to toggle source

@return [Array<SocialNet::Twitter::Models::User>] the Twitter users. @param [Hash] conditions The attributes to find users by. @option conditions [Array<String>] screen_name The Twitter user's

screen names (case-insensitive).

@raise [SocialNet::Errors::TooManyUsers] when more than 100 Twitter users

match the provided attributes.
# File lib/social_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

Private Class Methods

to_where_params(conditions = {}) click to toggle source
# File lib/social_net/twitter/models/user.rb, line 73
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