There might be something like this already out there but I just wanted a quick and dirty ruby class to get some relevant user information given a twitter username.
Feel free to use it as you see fit - just don’t blame me if you have an issue. You can download from here.
# Copyright (c) 2008 by Jeff Haynie. All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of Jeff Haynie nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
require 'open-uri'
require 'rubygems'
require 'json/pure'
class Twitter
def Twitter.status(un)
u = URI.parse "http://twitter.com/statuses/user_timeline.json?id=#{un}&count=1"
j = nil
begin
j = JSON.parse(u.read).first
rescue OpenURI::HTTPError=>e
case e.to_s
when /^404/
raise 'Not Found'
when /^304/
raise 'No Info'
end
end
o = {}
user = j['user']
fullname = user['name']
if fullname == un
o[:firstname]=nil
o[:lastname]=nil
else
o[:firstname],o[:lastname] = Twitter.parse_name(fullname)
end
o[:url] = user['url']
o[:location] = user['location']
o[:image] = user['profile_image_url']
o[:bio] = user['description']
o[:status] = j['text']
o[:date] = j['created_at']
o
end
private
def Twitter.parse_name(fullname)
idx = fullname.index ' '
if idx
return fullname[0..idx].strip,fullname[idx..-1].strip
end
return '',fullname.strip
end
end
if __FILE__ == $0
o = Twitter.status ARGV.first || 'jhaynie'
puts "Firstname: #{o[:firstname]}, Lastname: #{o[:lastname]}"
puts "URL: #{o[:url]}"
puts "Location: #{o[:location]}"
puts "Status: #{o[:status]}"
puts "Date: #{o[:date]}"
puts "Bio: #{o[:bio]}"
end
You can run from command line to test it out:
> ruby twitter.rb ev
Replace twitter.rb with the name of the file you saved the code as and the 2nd argument (ev) with the twitter username.