Merge branch 'release/v0.0.1'

This commit is contained in:
2011-03-02 00:18:44 +00:00
10 changed files with 222 additions and 11 deletions

2
.rvmrc Normal file
View File

@@ -0,0 +1,2 @@
rvm gemset use twhois

71
README.md Normal file
View File

@@ -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.

View File

@@ -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

26
bin/twhois Executable file
View File

@@ -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

View File

@@ -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

15
lib/twhois/user.rb Normal file
View File

@@ -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

View File

@@ -1,3 +1,5 @@
# encoding: utf-8
module Twhois
VERSION = "0.0.1"
end

9
spec/spec_helper.rb Normal file
View File

@@ -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'

25
spec/twhois_spec.rb Normal file
View File

@@ -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

View File

@@ -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