twhois 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jim Myhrberg.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Twhois #
2
2
 
3
- Whois-like Ruby Gem for Twitter users
3
+ Whois-like command-line tool and Ruby Gem for Twitter users
4
4
 
5
5
 
6
6
  ## Installation ##
@@ -36,6 +36,14 @@ Whois-like Ruby Gem for Twitter users
36
36
  user.nil? #=> true
37
37
 
38
38
 
39
+ ## Why? ##
40
+
41
+ @sxtxixtxcxh: make me a gem
42
+ @jimeh: no
43
+ @sxtxixtxcxh: sudo make me a gem
44
+ @jimeh: ok
45
+
46
+
39
47
  ## Note on Patches/Pull Requests
40
48
 
41
49
  * Fork the project.
@@ -49,7 +57,7 @@ Whois-like Ruby Gem for Twitter users
49
57
 
50
58
  ## License and Copyright
51
59
 
52
- Copyright (c) 2010 Jim Myhrberg.
60
+ Copyright (c) 2011 Jim Myhrberg.
53
61
 
54
62
  Permission is hereby granted, free of charge, to any person obtaining
55
63
  a copy of this software and associated documentation files (the
data/bin/twhois CHANGED
@@ -4,25 +4,32 @@ require 'twhois'
4
4
 
5
5
  if ARGV.size > 0
6
6
  ARGV.each do |username|
7
- user = Twhois.lookup(username)
8
- if user
9
- puts "@#{user.screen_name}:"
10
- puts " Name: #{user.name}"
11
- puts " URL: #{user.url}" if user.url
12
- puts " Location: #{user.location}" if user.location
13
- puts " Description: #{user.description}" if user.description
14
- puts " Followers: #{user.followers_count}"
15
- puts " Following: #{user.friends_count}"
16
- puts " Tweets: #{user.statuses_count}"
17
- puts " Last Tweet (#{user.status['created_at']}):"
18
- puts " #{user.status['text']}"
19
- puts " Timezone: #{user.time_zone}" if user.time_zone
20
- puts " Joined On: #{user.created_at}"
21
- puts " Profile Picture: #{user.profile_image_url}"
22
- puts " Private Account: #{user.protected ? "Yes" : "No"}"
23
- else
24
- puts "@#{username}:"
25
- puts " Not Found"
7
+ begin
8
+ user = Twhois.lookup(username)
9
+ if user
10
+ puts "@#{user.screen_name}:"
11
+ puts " Name: #{user.name}"
12
+ puts " URL: #{user.url}" if user.respond_to?(:url)
13
+ puts " Profile: http://twitter.com/#{user.screen_name}"
14
+ puts " Location: #{user.location}" if user.respond_to?(:location)
15
+ puts " Description: #{user.description}" if user.respond_to?(:description)
16
+ puts " Followers: #{user.followers_count}"
17
+ puts " Following: #{user.friends_count}"
18
+ puts " Tweets: #{user.statuses_count}"
19
+ if user.respond_to?(:status)
20
+ puts " Last Tweet (#{user.status['created_at']}):"
21
+ puts " #{user.status['text']}"
22
+ end
23
+ puts " Timezone: #{user.time_zone}" if user.respond_to?(:time_zone)
24
+ puts " Joined On: #{user.created_at}"
25
+ puts " Profile Picture: #{user.profile_image_url}"
26
+ puts " Private Account: #{user.protected ? "Yes" : "No"}"
27
+ else
28
+ puts "@#{username}:"
29
+ puts " Not Found"
30
+ end
31
+ rescue Twhois::InvalidUsername => e
32
+ puts "\"#{username}\" is not a valid Twitter username"
26
33
  end
27
34
  end
28
35
  else
data/lib/twhois.rb CHANGED
@@ -12,12 +12,20 @@ module Twhois
12
12
  LOOKUP_HOST = "api.twitter.com"
13
13
  LOOKUP_PATH = "/1/users/show.json?screen_name="
14
14
 
15
+ class InvalidUsername < StandardError; end
16
+
15
17
  # Lookup a Twitter user by their username.
16
18
  def self.lookup(username)
19
+ raise InvalidUsername, "Username is invalid" unless valid_username?(username)
17
20
  res = Net::HTTP.start(LOOKUP_HOST) { |http| http.get(LOOKUP_PATH + username) }
18
21
  if res.code == '200'
19
22
  User.new(JSON.parse(res.body))
20
23
  end
21
24
  end
22
25
 
26
+ def self.valid_username?(username)
27
+ return false if username.match(/^[a-zA-Z0-9_]{1,15}$/).nil?
28
+ return true
29
+ end
30
+
23
31
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Twhois
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
data/spec/twhois_spec.rb CHANGED
@@ -18,8 +18,18 @@ describe Twhois do
18
18
  end
19
19
 
20
20
  it "should return error on unknown user" do
21
- user = Twhois.lookup('jimehoawhefoahelfhasdf')
21
+ user = Twhois.lookup('akjsdfkjasdfasd')
22
22
  user.should be_nil
23
23
  end
24
24
 
25
+ it "should raise an exception on invalid usernames" do
26
+ lambda { # invalid characters
27
+ user = Twhois.lookup("abc/damn")
28
+ }.should raise_error(Twhois::InvalidUsername)
29
+
30
+ lambda { # longer than 15 characters
31
+ user = Twhois.lookup("abcasdjfakajsdfasdfasdfa")
32
+ }.should raise_error(Twhois::InvalidUsername)
33
+ end
34
+
25
35
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twhois
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jim Myhrberg
@@ -79,6 +79,7 @@ files:
79
79
  - .gitignore
80
80
  - .rvmrc
81
81
  - Gemfile
82
+ - LICENSE
82
83
  - README.md
83
84
  - Rakefile
84
85
  - bin/twhois