From 06057b125b52d50e311a9b2ed0a693bb57fc3982 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 2 Mar 2011 00:30:08 +0000 Subject: [PATCH 1/5] updated description in readme file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6d47cb0..d9e8449 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 ## From c8817621027f32b5b595e60d5b7e512bcd99de06 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 2 Mar 2011 00:51:19 +0000 Subject: [PATCH 2/5] updated readme --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d9e8449..d7b5d8b 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,14 @@ Whois-like command-line tool and 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 command-line tool and 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 From 56984a0ecbd850f72460c443eaca77e9fd1d835b Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 2 Mar 2011 00:51:26 +0000 Subject: [PATCH 3/5] added license file --- LICENSE | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 LICENSE 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 From 5b661e026441d9fca9615c430e5589ceb28471a5 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 2 Mar 2011 00:54:04 +0000 Subject: [PATCH 4/5] validate username characters and length --- bin/twhois | 45 ++++++++++++++++++++++++++------------------- lib/twhois.rb | 8 ++++++++ spec/twhois_spec.rb | 12 +++++++++++- 3 files changed, 45 insertions(+), 20 deletions(-) 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/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 From 3c835eccb078488992c45cff1d7d170bdc7d5505 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 2 Mar 2011 00:54:45 +0000 Subject: [PATCH 5/5] Version bump to 0.0.3 --- lib/twhois/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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