diff --git a/.rvmrc b/.rvmrc new file mode 100644 index 0000000..50112eb --- /dev/null +++ b/.rvmrc @@ -0,0 +1,2 @@ +rvm gemset use twhois + diff --git a/README.md b/README.md new file mode 100644 index 0000000..6d47cb0 --- /dev/null +++ b/README.md @@ -0,0 +1,71 @@ +# Twhois # + +Whois-like Ruby Gem for Twitter users + + +## Installation ## + + gem install twhois + + +## Usage ## + +### Command line ### + + $ twhois jimeh darthvader + @jimeh: + Name: Jim Myhrberg + URL: http://jimeh.me/ + [...] + @darthvader: + Name: Darth Vader + Location: Empire, CO + [...] + +### Ruby + + require 'twhois' + + # Lookup a user + user = Twhois.lookup('jimeh') + user.screen_name #=> jimeh + user.name #=> Jim Myhrberg + + # Lookup a user that doesn't exist + user = Twhois.lookup('hadsfasdfauwhgiuahwg') + user.nil? #=> true + + +## Note on Patches/Pull Requests + +* Fork the project. +* Make your feature addition or bug fix. +* Add tests for it. This is important so I don't break it in a + future version unintentionally. +* Commit, do not mess with rakefile, version, or history. + (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull) +* Send me a pull request. Bonus points for topic branches. + + +## License and Copyright + +Copyright (c) 2010 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. diff --git a/Rakefile b/Rakefile index 14cfe0b..d6888b6 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,38 @@ require 'bundler' Bundler::GemHelper.install_tasks + +# +# Rspec +# + +require 'rspec/core/rake_task' +RSpec::Core::RakeTask.new(:spec) do |spec| + spec.pattern = 'spec/**/*_spec.rb' +end + +task :default => [:start, :spec, :stop] + + +# +# Yard +# + +begin + require 'yard' + YARD::Rake::YardocTask.new +rescue LoadError + task :yardoc do + abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard" + end +end + + +# +# Misc. +# + +desc "Start an irb console with Twhois pre-loaded." +task :console do + exec "irb -r spec/spec_helper" +end +task :c => :console diff --git a/bin/twhois b/bin/twhois new file mode 100755 index 0000000..947be11 --- /dev/null +++ b/bin/twhois @@ -0,0 +1,26 @@ +#! /usr/bin/env ruby +require 'rubygems' +require 'twhois' + +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" + end +end diff --git a/lib/twhois.rb b/lib/twhois.rb index f0da803..5e4db43 100644 --- a/lib/twhois.rb +++ b/lib/twhois.rb @@ -1,3 +1,23 @@ +# encoding: utf-8 + +require 'rubygems' +require 'net/http' +require 'json' + +require 'twhois/user' +require 'twhois/version' + module Twhois - # Your code goes here... + + LOOKUP_HOST = "api.twitter.com" + LOOKUP_PATH = "/1/users/show.json?screen_name=" + + # Lookup a Twitter user by their username. + def self.lookup(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 + end diff --git a/lib/twhois/user.rb b/lib/twhois/user.rb new file mode 100644 index 0000000..9cda763 --- /dev/null +++ b/lib/twhois/user.rb @@ -0,0 +1,15 @@ +# encoding: utf-8 + +module Twhois + # Kinda acts like the Hashie gem + class User + + def initialize(hash) + hash.keys.each do |key| + self.class.send(:attr_accessor, key.to_sym) + self.send("#{key}=", hash[key]) + end + end + + end +end diff --git a/lib/twhois/version.rb b/lib/twhois/version.rb index 834d455..6b5386f 100644 --- a/lib/twhois/version.rb +++ b/lib/twhois/version.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + module Twhois VERSION = "0.0.1" end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..140a7f7 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,9 @@ +# add project-relative load paths +$LOAD_PATH.unshift(File.dirname(__FILE__)) +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) + +# require stuff +require 'twhois' +require 'rspec' +require 'rspec/autorun' + diff --git a/spec/twhois_spec.rb b/spec/twhois_spec.rb new file mode 100644 index 0000000..5a562d6 --- /dev/null +++ b/spec/twhois_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe Twhois do + + it "should find author's twitter account" do + user = Twhois.lookup('jimeh') + user.screen_name.should == "jimeh" + user.lang.should == "en" + user.profile_image_url.should_not be_nil + user.name.should_not be_nil + user.location.should_not be_nil + user.url.should_not be_nil + user.followers_count.should_not be_nil + user.description.should_not be_nil + user.time_zone.should_not be_nil + user.profile_background_image_url.should_not be_nil + # ...that should be enough + end + + it "should return error on unknown user" do + user = Twhois.lookup('jimehoawhefoahelfhasdf') + user.should be_nil + end + +end \ No newline at end of file diff --git a/twhois.gemspec b/twhois.gemspec index f37a67f..c40abb8 100644 --- a/twhois.gemspec +++ b/twhois.gemspec @@ -1,21 +1,26 @@ # -*- encoding: utf-8 -*- -$:.push File.expand_path("../lib", __FILE__) -require "twhois/version" +$:.push File.expand_path('../lib', __FILE__) +require 'twhois/version' Gem::Specification.new do |s| - s.name = "twhois" + s.name = 'twhois' s.version = Twhois::VERSION s.platform = Gem::Platform::RUBY - s.authors = ["TODO: Write your name"] - s.email = ["TODO: Write your email address"] - s.homepage = "" - s.summary = %q{TODO: Write a gem summary} - s.description = %q{TODO: Write a gem description} + s.authors = ['Jim Myhrberg'] + s.email = ['contact@jimeh.me'] + s.homepage = 'https://github.com/jimeh/twhois' + s.summary = 'Whois-like Ruby Gem for Twitter users' + s.description = 'Whois-like Ruby Gem for Twitter users' - s.rubyforge_project = "twhois" + s.rubyforge_project = 'twhois' s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } - s.require_paths = ["lib"] + s.require_paths = ['lib'] + + s.add_runtime_dependency 'json_pure', '>= 1.0.0' + + s.add_development_dependency 'rspec', '>= 2.5.0' + s.add_development_dependency 'yard', '>= 0.6.4' end