Skip to content
Browse files

Added method and tests for bulk user lookup

  • Loading branch information...
1 parent 69d4df5 commit 5723b60f042d98b630040fa076ac86e9b735dee8 @sferik committed
Showing with 28 additions and 0 deletions.
  1. +15 −0 lib/twitter/base.rb
  2. +1 −0 test/fixtures/users.json
  3. +12 −0 test/twitter/base_test.rb
View
15 lib/twitter/base.rb
@@ -91,6 +91,21 @@ def user(id, query={})
perform_get("/#{API_VERSION}/users/show/#{id}.json", :query => query)
end
+ def users(*ids_or_usernames)
+ ids, usernames = [], []
+ ids_or_usernames.each do |id_or_username|
+ if id_or_username.is_a?(Integer)
+ ids << id_or_username
+ elsif id_or_username.is_a?(String)
+ usernames << id_or_username
+ end
+ end
+ query = {}
+ query[:user_id] = ids.join(",") unless ids.empty?
+ query[:screen_name] = usernames.join(",") unless usernames.empty?
+ perform_get("/#{API_VERSION}/users/lookup.json", :query => query)
+ end
+
# Options: page, per_page
def user_search(q, query={})
q = URI.escape(q)
View
1 test/fixtures/users.json
@@ -0,0 +1 @@
+[{"created_at":"Mon Jul 16 12:59:01 +0000 2007","friends_count":50,"description":"Captain","statuses_count":1948,"profile_text_color":"333333","screen_name":"sferik","status":{"created_at":"Thu Mar 18 18:13:19 +0000 2010","truncated":false,"in_reply_to_status_id":null,"source":"<a href=\"http://www.atebits.com/\" rel=\"nofollow\">Tweetie</a>","in_reply_to_screen_name":null,"favorited":false,"in_reply_to_user_id":null,"id":10682865729,"text":"If the earth was square, what shape would web browser icons be? #rhetorical"},"following":false,"profile_background_image_url":"http://a3.twimg.com/profile_background_images/58220609/gem.png","favourites_count":494,"contributors_enabled":false,"profile_link_color":"BF1238","url":null,"geo_enabled":true,"profile_background_tile":false,"profile_background_color":"000000","location":"California","verified":false,"profile_sidebar_fill_color":"EFEFEF","protected":true,"name":"Erik Michaels-Ober","notifications":false,"time_zone":"Pacific Time (US & Canada)","profile_sidebar_border_color":"FFFFFF","followers_count":799,"id":7505382,"lang":"en","utc_offset":-28800,"profile_image_url":"http://a1.twimg.com/profile_images/323331048/me_normal.jpg"},{"created_at":"Tue Dec 12 06:39:24 +0000 2006","friends_count":39,"description":"Full-time inventor.\r\nI don't do drugs. I AM drugs.","statuses_count":2157,"profile_text_color":"333333","screen_name":"jm3","status":{"created_at":"Thu Mar 18 21:32:31 +0000 2010","in_reply_to_status_id":null,"truncated":false,"source":"<a href=\"http://foursquare.com\" rel=\"nofollow\">foursquare</a>","in_reply_to_screen_name":null,"in_reply_to_user_id":null,"id":10690325085,"favorited":false,"text":"\"Social media is the new porn.\" Not sure I agree. (@ OMMA Global SF w/ 21 others) http://4sq.com/aURoO4"},"following":true,"profile_background_image_url":"http://a1.twimg.com/profile_background_images/67143062/drucker.jpg","favourites_count":4179,"contributors_enabled":false,"profile_link_color":"038543","url":"http://jm3.net","geo_enabled":true,"profile_background_tile":true,"profile_background_color":"ACDED6","location":"SF Yay Area","verified":false,"profile_sidebar_fill_color":"F6F6F6","protected":false,"name":"John Manoogian III","notifications":true,"time_zone":"Pacific Time (US & Canada)","profile_sidebar_border_color":"EEEEEE","followers_count":477,"id":59593,"lang":"en","utc_offset":-28800,"profile_image_url":"http://a3.twimg.com/profile_images/689856229/jm3-stripes_normal.jpg"},{"created_at":"Thu Feb 15 16:28:04 +0000 2007","friends_count":223,"description":"Internet researcher & open source hacker. I created Know Your Meme and taught the Internet Famous class","contributors_enabled":false,"profile_text_color":"000000","screen_name":"jamiew","status":{"created_at":"Tue Mar 16 02:00:21 +0000 2010","favorited":false,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"source":"web","in_reply_to_screen_name":null,"id":10547290021,"truncated":false,"text":"Chat Roulette Piano Improv http://www.youtube.com/watch?v=32vpgNiAH60 A++, I'm in love"},"following":true,"profile_background_image_url":"http://a1.twimg.com/profile_background_images/83556904/fat_NBC_diagram_cooper_40.jpg","favourites_count":496,"profile_link_color":"061e6f","url":"http://jamiedubs.com","geo_enabled":false,"profile_background_tile":true,"profile_background_color":"ffffff","location":"San Francisco","verified":false,"profile_sidebar_fill_color":"ededed","protected":false,"name":"Jamie Wilkinson","notifications":false,"time_zone":"Pacific Time (US & Canada)","profile_sidebar_border_color":"e3c4c9","followers_count":1135,"id":774010,"lang":"en","statuses_count":918,"utc_offset":-28800,"profile_image_url":"http://a1.twimg.com/profile_images/698063756/jamie_beards-big_normal.jpg"}]
View
12 test/twitter/base_test.rb
@@ -153,6 +153,18 @@ class BaseTest < Test::Unit::TestCase
@twitter.friendship_show(:source_screen_name => 'dcrec1', :target_screen_name => 'pengwynn').relationship.target.followed_by == false
end
+ should "be able to lookup a user" do
+ stub_get("/1/users/show/4243.json", "user.json")
+ user = @twitter.user(4243)
+ user.screen_name.should == "jnunemaker"
+ end
+
+ should "be able to lookup users in bulk" do
+ stub_get("/1/users/lookup.json?screen_name=sferik&user_id=59593,774010", "users.json")
+ users = @twitter.users("sferik", 59593, 774010)
+ users.first.screen_name.should == "sferik"
+ end
+
should "be able to search people" do
stub_get("/1/users/search.json?q=Wynn%20Netherland", 'people_search.json')
people = @twitter.user_search('Wynn Netherland')

0 comments on commit 5723b60

Please sign in to comment.
Something went wrong with that request. Please try again.