diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a02b2b8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2011 Jim Myhrberg. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 6d47cb0..d7b5d8b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Twhois # -Whois-like Ruby Gem for Twitter users +Whois-like command-line tool and Ruby Gem for Twitter users ## Installation ## @@ -36,6 +36,14 @@ Whois-like Ruby Gem for Twitter users user.nil? #=> true +## Why? ## + + @sxtxixtxcxh: make me a gem + @jimeh: no + @sxtxixtxcxh: sudo make me a gem + @jimeh: ok + + ## Note on Patches/Pull Requests * Fork the project. @@ -49,7 +57,7 @@ Whois-like Ruby Gem for Twitter users ## License and Copyright -Copyright (c) 2010 Jim Myhrberg. +Copyright (c) 2011 Jim Myhrberg. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/bin/twhois b/bin/twhois index b142510..dcc6735 100755 --- a/bin/twhois +++ b/bin/twhois @@ -4,25 +4,32 @@ require 'twhois' if ARGV.size > 0 ARGV.each do |username| - user = Twhois.lookup(username) - if user - puts "@#{user.screen_name}:" - puts " Name: #{user.name}" - puts " URL: #{user.url}" if user.url - puts " Location: #{user.location}" if user.location - puts " Description: #{user.description}" if user.description - puts " Followers: #{user.followers_count}" - puts " Following: #{user.friends_count}" - puts " Tweets: #{user.statuses_count}" - puts " Last Tweet (#{user.status['created_at']}):" - puts " #{user.status['text']}" - puts " Timezone: #{user.time_zone}" if user.time_zone - puts " Joined On: #{user.created_at}" - puts " Profile Picture: #{user.profile_image_url}" - puts " Private Account: #{user.protected ? "Yes" : "No"}" - else - puts "@#{username}:" - puts " Not Found" + begin + user = Twhois.lookup(username) + if user + puts "@#{user.screen_name}:" + puts " Name: #{user.name}" + puts " URL: #{user.url}" if user.respond_to?(:url) + puts " Profile: http://twitter.com/#{user.screen_name}" + puts " Location: #{user.location}" if user.respond_to?(:location) + puts " Description: #{user.description}" if user.respond_to?(:description) + puts " Followers: #{user.followers_count}" + puts " Following: #{user.friends_count}" + puts " Tweets: #{user.statuses_count}" + if user.respond_to?(:status) + puts " Last Tweet (#{user.status['created_at']}):" + puts " #{user.status['text']}" + end + puts " Timezone: #{user.time_zone}" if user.respond_to?(:time_zone) + puts " Joined On: #{user.created_at}" + puts " Profile Picture: #{user.profile_image_url}" + puts " Private Account: #{user.protected ? "Yes" : "No"}" + else + puts "@#{username}:" + puts " Not Found" + end + rescue Twhois::InvalidUsername => e + puts "\"#{username}\" is not a valid Twitter username" end end else diff --git a/lib/twhois.rb b/lib/twhois.rb index 5e4db43..c6007b0 100644 --- a/lib/twhois.rb +++ b/lib/twhois.rb @@ -12,12 +12,20 @@ module Twhois LOOKUP_HOST = "api.twitter.com" LOOKUP_PATH = "/1/users/show.json?screen_name=" + class InvalidUsername < StandardError; end + # Lookup a Twitter user by their username. def self.lookup(username) + raise InvalidUsername, "Username is invalid" unless valid_username?(username) res = Net::HTTP.start(LOOKUP_HOST) { |http| http.get(LOOKUP_PATH + username) } if res.code == '200' User.new(JSON.parse(res.body)) end end + def self.valid_username?(username) + return false if username.match(/^[a-zA-Z0-9_]{1,15}$/).nil? + return true + end + end diff --git a/lib/twhois/version.rb b/lib/twhois/version.rb index 7160115..6e32a10 100644 --- a/lib/twhois/version.rb +++ b/lib/twhois/version.rb @@ -1,5 +1,5 @@ # encoding: utf-8 module Twhois - VERSION = "0.0.2" + VERSION = "0.0.3" end diff --git a/spec/twhois_spec.rb b/spec/twhois_spec.rb index 5a562d6..a456f19 100644 --- a/spec/twhois_spec.rb +++ b/spec/twhois_spec.rb @@ -18,8 +18,18 @@ describe Twhois do end it "should return error on unknown user" do - user = Twhois.lookup('jimehoawhefoahelfhasdf') + user = Twhois.lookup('akjsdfkjasdfasd') user.should be_nil end + it "should raise an exception on invalid usernames" do + lambda { # invalid characters + user = Twhois.lookup("abc/damn") + }.should raise_error(Twhois::InvalidUsername) + + lambda { # longer than 15 characters + user = Twhois.lookup("abcasdjfakajsdfasdfasdfa") + }.should raise_error(Twhois::InvalidUsername) + end + end \ No newline at end of file