Twitter4R v0.2.0: User API

Written on 6:33:00 PM by S. Potter

The User API provides an easy Ruby API that maps onto a less consistent Twitter REST API, which is good news for Twitter4R users:) The following example code snippet shows how you would use the Twitter4R User API:


require('rubygems')
gem('twitter4r', '0.2.0')
require('twitter')

# I have a 'twitter.yml' file in subdir 'config' that I will load credential from.
# This feature of Twitter::Client is in the Extras API and you need to require
# twitter/console to access it.
require 'twitter/console'
config_file = File.join('config', 'twitter.yml')
twitter = Twitter::Client.from_config(config_file)

# Gets the Twitter::User object for user with screen name otherlogin.
user = twitter.user('otherlogin')
# Gets an Array of Twitter::User objects that represent the friends of user 
# with screen name otherlogin.
friends = twitter.user('otherlogin', :friends)

# Gets the Twitter::User object for the user we used to authenticate above
# using the Twitter::Client.from_config method in the Extras API above.
me = twitter.my(:info)

# Gets the Array of Twitter::User objects that represent the followers of 
# the user whose credentials were used to authenticate with the Twitter service.
myfollowers = twitter.my(:followers)
# The same can be done for friends, i.e.
myfriends = twitter.my(:friends)

# Alternative we can use the equivalent Model APIs like so....
myfollowers = me.followers
myfriends = me.friends
Hope you find this helpful. If you want to learn more about the User API you should consult the Twitter4R v0.2.0 RDoc.

If you enjoyed this post Subscribe to our feed

6 Comments

  1. Nando Vieira |

    Is there any method that returns me a list of followers that I'm not following?

    In practice, is something like FOLLOWERS - FRIENDS.

     
  2. S. Potter |

    I did a quick sanity check to see if the Twitter REST API exposes such a method, but I couldn't see that they did.

    Unfortunately at present going through the official API, Twitter only provides getting followers and friends independently (which is 2 API calls, no matter which binding library you use like Twitter4R) and then take the difference yourself.

    You could suggest it to the Twitter developers to include such a method in their API.

    If there is a development I do not know about and Twitter has documented this API not on the usual wiki page, then please let me know.

     
  3. Anonymous |

    I have 200 friends in twitter.

    myfriends = twitter.my(:friends)
    This code returns first 100 friends.

    How to get next 100 friends?

     
  4. S. Potter |

    It depends which version you are using? If you install the latest development release (currently 0.3.1) from the GitHub gem server you can do the following:

    page1 = twitter.my(:friends)
    page2 = twitter.my(:friends, :page => 2)
    # etc.

    Each page will have 100 friends.

    To install the development release please refer to the wiki:
    http://github.com/mbbx6spp/twitter4r/wikis/howto-install-github-development-releases

    Thanks!

     
  5. Anonymous |

    Thanks for quick response!

    I used Twitter4R version 0.3.0.
    I installed 0.3.1 and tried new code, then got the next 100 friends.

    I will develop my code using Twitter4R.
    Thanks a lot.

     
  6. S. Potter |

    I am glad that helped you. In future, if you have problems with Twitter4R you may find the twitter4r-users Google Group to be a good place to search and to submit comments or discuss things with other users:
    http://groups.google.com/group/twitter4r-users

     

Post a Comment