Skip to content

Commit

Permalink
Allow users to pass in screen names with leading '@'
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Nov 3, 2010
1 parent c7ca0d0 commit fc3af84
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
8 changes: 8 additions & 0 deletions lib/twitter/client/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ module Twitter
class Client
module List
def list_create(screen_name, name, options={})
clean_screen_name!(screen_name)
response = post("#{screen_name}/lists", options.merge(:name => name))
format.to_s.downcase == 'xml' ? response['list'] : response
end

def list_update(screen_name, name, options={})
clean_screen_name!(screen_name)
response = put("#{screen_name}/lists/#{name}", options)
format.to_s.downcase == 'xml' ? response['list'] : response
end
Expand All @@ -15,6 +17,7 @@ def lists(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
screen_name = args.first
if screen_name
clean_screen_name!(screen_name)
response = get("#{screen_name}/lists", options)
else
response = get('lists', options)
Expand All @@ -23,26 +26,31 @@ def lists(*args)
end

def list(screen_name, name, options={})
clean_screen_name!(screen_name)
response = get("#{screen_name}/lists/#{name}", options)
format.to_s.downcase == 'xml' ? response['list'] : response
end

def list_delete(screen_name, name, options={})
clean_screen_name!(screen_name)
response = delete("#{screen_name}/lists/#{name}", options)
format.to_s.downcase == 'xml' ? response['list'] : response
end

def list_timeline(screen_name, name, options={})
clean_screen_name!(screen_name)
response = get("#{screen_name}/lists/#{name}/statuses", options)
format.to_s.downcase == 'xml' ? response['statuses'] : response
end

def memberships(screen_name, options={})
clean_screen_name!(screen_name)
response = get("#{screen_name}/lists/memberships", options)
format.to_s.downcase == 'xml' ? response['lists_list'] : response
end

def subscriptions(screen_name, options={})
clean_screen_name!(screen_name)
response = get("#{screen_name}/lists/subscriptions", options)
format.to_s.downcase == 'xml' ? response['lists_list'] : response
end
Expand Down
5 changes: 5 additions & 0 deletions lib/twitter/client/list_members.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@ module Twitter
class Client
module ListMembers
def list_members(screen_name, slug, options={})
clean_screen_name!(screen_name)
response = get("#{screen_name}/#{slug}/members", options)
format.to_s.downcase == 'xml' ? response['users_list'] : response
end

def list_add_member(screen_name, slug, user_id, options={})
clean_screen_name!(screen_name)
response = post("#{screen_name}/#{slug}/members", options.merge(:id => user_id))
format.to_s.downcase == 'xml' ? response['list'] : response
end

def list_add_members(screen_name, slug, users, options={})
clean_screen_name!(screen_name)
merge_users_into_options!(Array(users), options)
response = post("#{screen_name}/#{slug}/create_all", options)
format.to_s.downcase == 'xml' ? response['list'] : response
end

def list_remove_member(screen_name, slug, user_id, options={})
clean_screen_name!(screen_name)
response = delete("#{screen_name}/#{slug}/members", options.merge(:id => user_id))
format.to_s.downcase == 'xml' ? response['list'] : response
end

def is_list_member?(screen_name, slug, user_id, options={})
clean_screen_name!(screen_name)
begin
get("#{screen_name}/#{slug}/members/#{user_id}", options)
true
Expand Down
4 changes: 4 additions & 0 deletions lib/twitter/client/list_subscribers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ module Twitter
class Client
module ListSubscribers
def list_subscribers(screen_name, slug, options={})
clean_screen_name!(screen_name)
response = get("#{screen_name}/#{slug}/subscribers", options)
format.to_s.downcase == 'xml' ? response['users_list'] : response
end

def list_subscribe(screen_name, slug, options={})
clean_screen_name!(screen_name)
response = post("#{screen_name}/#{slug}/subscribers", options)
format.to_s.downcase == 'xml' ? response['list'] : response
end

def list_unsubscribe(screen_name, slug, options={})
clean_screen_name!(screen_name)
response = delete("#{screen_name}/#{slug}/subscribers", options)
format.to_s.downcase == 'xml' ? response['list'] : response
end

def is_subscriber?(screen_name, slug, user_id, options={})
clean_screen_name!(screen_name)
begin
get("#{screen_name}/#{slug}/subscribers/#{user_id}", options)
true
Expand Down
48 changes: 44 additions & 4 deletions lib/twitter/client/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def user(user, options={})
# Returns extended information of a given user
#
# @format :json, :xml
# @authenticated false
# @authenticated true
# @rate_limited true
# @param user [String, Integer] A Twitter user ID or screen name.
# @param options [Hash] A customizable set of options.
Expand All @@ -42,20 +42,60 @@ def users(*args)
format.to_s.downcase == 'xml' ? response['users'] : response
end

# Returns users that match the given query
#
# @format :json, :xml
# @authenticated true
# @rate_limited true
# @param query [String] The search query to run against people search.
# @param options [Hash] A customizable set of options.
# @option options [Integer] :per_page The number of people to retrieve. Maxiumum of 20 allowed per page.
# @option options [Integer] :page Specifies the page of results to retrieve.
# @option options [String] :include_entities Include {http://dev.twitter.com/pages/tweet_entities Tweet Entities} when set to true, 't' or 1.
# @return [Array]
# @see http://dev.twitter.com/doc/get/users/search
# @example Return users that match "Erik Michaels-Ober"
# Twitter.user_search("Erik Michaels-Ober")
def user_search(query, options={})
response = get('users/search', options.merge(:q => query))
format.to_s.downcase == 'xml' ? response['users'] : response
end

# Returns the list of suggested user categories or users in a given category
#
# @format :json, :xml
# @authenticated false
# @rate_limited true
# @param slug [String] The short name of list or a category.
# @param options [Hash] A customizable set of options.
# @return [Array]
# @see http://dev.twitter.com/doc/get/users/suggestions
# @see http://dev.twitter.com/doc/get/users/suggestions/:slug
# @example
# Twitter.suggestions # Return the list of suggested user categories
# Twitter.suggestions("art-design") # Return the users in the Art & Design category
def suggestions(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
category = args.first
response = get(['users/suggestions', category].compact.join('/'), options)
xml_key = category ? 'category' : 'suggestions'
slug = args.first
response = get(['users/suggestions', slug].compact.join('/'), options)
xml_key = slug ? 'category' : 'suggestions'
format.to_s.downcase == 'xml' ? response[xml_key] : response
end

# Access the profile image in various sizes for the user with the indicated screen name
#
# @format :json, :xml
# @authenticated false
# @rate_limited false
# @param screen_name [String] The screen name of the user for whom to return results for.
# @param options [Hash] A customizable set of options.
# @option options [String] :size ('normal') Specifies the size of image to fetch. Valid options include: 'bigger' (73px by 73px), 'normal' (48px by 48px), and 'mini' (24px by 24px).
# @return [String] The URL for the requested user's profile image.
# @see http://dev.twitter.com/doc/get/users/profile_image/:screen_name
# @example Return the URL for the 24px by 24px version of @sferik's profile image
# Twitter.profile_image("sferik", :size => 'mini')
def profile_image(screen_name, options={})
clean_screen_name!(screen_name)
get("users/profile_image/#{screen_name}", options, true).headers['location']
end

Expand Down
22 changes: 14 additions & 8 deletions lib/twitter/client/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ class Client
module Utils
private

def clean_screen_name!(screen_name)
screen_name.gsub!(/[@@]/, '') if screen_name

This comment has been minimized.

Copy link
@gaustin

gaustin Nov 5, 2010

The second "at" symbol in the regular expression in this line blows up on my Mac and Linux machines.

Namely: in `require': /home/gaustin/.rvm/gems/ruby-1.9.2-p0/gems/twitter-1.0.0.rc.6/lib/twitter/client/utils.rb:9: invalid multibyte char (US-ASCII) (SyntaxError)
/home/gaustin/.rvm/gems/ruby-1.9.2-p0/gems/twitter-1.0.0.rc.6/lib/twitter/client/utils.rb:9: invalid multibyte char (US-ASCII)
/home/gaustin/.rvm/gems/ruby-1.9.2-p0/gems/twitter-1.0.0.rc.6/lib/twitter/client/utils.rb:9: syntax error, unexpected $end, expecting ')'

This comment has been minimized.

Copy link
@sferik

sferik Nov 5, 2010

Author Owner

Thanks for reporting this bug. Please let me know if this fixes it: 4a62f18.

This comment has been minimized.

Copy link
@gaustin

gaustin Nov 5, 2010

That fixed my issue. Thanks.

end

def merge_user_into_options!(user_id_or_screen_name, options={})
case user_id_or_screen_name
when Fixnum
options[:user_id] = user_id_or_screen_name
when String
options[:screen_name] = user_id_or_screen_name.gsub('@', '')
when Fixnum
options[:user_id] = user_id_or_screen_name
when String
clean_screen_name!(user_id_or_screen_name)
options[:screen_name] = user_id_or_screen_name
end
options
end
Expand All @@ -18,10 +23,11 @@ def merge_users_into_options!(user_ids_or_screen_names, options={})
user_ids, screen_names = [], []
user_ids_or_screen_names.flatten.each do |user_id_or_screen_name|
case user_id_or_screen_name
when Fixnum
user_ids << user_id_or_screen_name
when String
screen_names << user_id_or_screen_name.gsub('@', '')
when Fixnum
user_ids << user_id_or_screen_name
when String
clean_screen_name!(user_id_or_screen_name)
screen_names << user_id_or_screen_name
end
end
options[:user_id] = user_ids.join(',') unless user_ids.empty?
Expand Down

0 comments on commit fc3af84

Please sign in to comment.