From 31df5737ad6a177b256a9d771e8492aa5b34a7e0 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Tue, 1 Mar 2011 23:36:33 +0000 Subject: [PATCH 01/10] added rspec, yard, and console task to Rakefile --- Rakefile | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Rakefile b/Rakefile index 14cfe0b..ab78217 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 Redistat pre-loaded." +task :console do + exec "irb -r spec/spec_helper" +end +task :c => :console From a9cffbfde7e7f87095d605b15258d2c909d817a5 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Tue, 1 Mar 2011 23:36:50 +0000 Subject: [PATCH 02/10] specify dependencies --- twhois.gemspec | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/twhois.gemspec b/twhois.gemspec index f37a67f..fa9f1ed 100644 --- a/twhois.gemspec +++ b/twhois.gemspec @@ -18,4 +18,10 @@ Gem::Specification.new do |s| 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.add_runtime_dependency 'json_pure', '>= 1.0.0' + s.add_runtime_dependency 'hashie', '>= 1.0.0' + + s.add_development_dependency 'rspec', '>= 2.5.0' + s.add_development_dependency 'yard', '>= 0.6.4' end From b7e0f9b5d7f890a9b404efa1a5742a0284285b90 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Tue, 1 Mar 2011 23:36:56 +0000 Subject: [PATCH 03/10] added .rvmrc file --- .rvmrc | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .rvmrc 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 + From 192c88db8e6919093ef2ccd3fd8d10d8517b6417 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Tue, 1 Mar 2011 23:37:06 +0000 Subject: [PATCH 04/10] added spec_helper.rb --- spec/spec_helper.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 spec/spec_helper.rb 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' + From aba833af8daf959022b216199e17798e15818154 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Tue, 1 Mar 2011 23:37:48 +0000 Subject: [PATCH 05/10] core of gem done, spec pass --- lib/twhois.rb | 23 ++++++++++++++++++++++- lib/twhois/user.rb | 7 +++++++ lib/twhois/version.rb | 2 ++ spec/twhois_spec.rb | 25 +++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 lib/twhois/user.rb create mode 100644 spec/twhois_spec.rb diff --git a/lib/twhois.rb b/lib/twhois.rb index f0da803..d0d8533 100644 --- a/lib/twhois.rb +++ b/lib/twhois.rb @@ -1,3 +1,24 @@ +# encoding: utf-8 + +require 'rubygems' +require 'net/http' +require 'json' +require 'hashie' + +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..ec96c14 --- /dev/null +++ b/lib/twhois/user.rb @@ -0,0 +1,7 @@ +# encoding: utf-8 + +module Twhois + class User < ::Hashie::Mash + + 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/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 From 0a34194965d16af97b59b5ff2d27da2aa157c7d3 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Tue, 1 Mar 2011 23:45:06 +0000 Subject: [PATCH 06/10] removed dependency on hashie --- lib/twhois.rb | 1 - lib/twhois/user.rb | 10 +++++++++- twhois.gemspec | 1 - 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/twhois.rb b/lib/twhois.rb index d0d8533..5e4db43 100644 --- a/lib/twhois.rb +++ b/lib/twhois.rb @@ -3,7 +3,6 @@ require 'rubygems' require 'net/http' require 'json' -require 'hashie' require 'twhois/user' require 'twhois/version' diff --git a/lib/twhois/user.rb b/lib/twhois/user.rb index ec96c14..9cda763 100644 --- a/lib/twhois/user.rb +++ b/lib/twhois/user.rb @@ -1,7 +1,15 @@ # encoding: utf-8 module Twhois - class User < ::Hashie::Mash + # 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/twhois.gemspec b/twhois.gemspec index fa9f1ed..21f834e 100644 --- a/twhois.gemspec +++ b/twhois.gemspec @@ -20,7 +20,6 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] s.add_runtime_dependency 'json_pure', '>= 1.0.0' - s.add_runtime_dependency 'hashie', '>= 1.0.0' s.add_development_dependency 'rspec', '>= 2.5.0' s.add_development_dependency 'yard', '>= 0.6.4' From 4458a0973239e4ecabe3a45568f9ecefb14c2d93 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Tue, 1 Mar 2011 23:47:44 +0000 Subject: [PATCH 07/10] updated gemspec with correct details --- twhois.gemspec | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/twhois.gemspec b/twhois.gemspec index 21f834e..c40abb8 100644 --- a/twhois.gemspec +++ b/twhois.gemspec @@ -1,23 +1,23 @@ # -*- 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' From 69f2cb6d4e08dd490195db275369d71d7e9e9053 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Tue, 1 Mar 2011 23:48:36 +0000 Subject: [PATCH 08/10] fixed a paste-fail --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index ab78217..d6888b6 100644 --- a/Rakefile +++ b/Rakefile @@ -31,7 +31,7 @@ end # Misc. # -desc "Start an irb console with Redistat pre-loaded." +desc "Start an irb console with Twhois pre-loaded." task :console do exec "irb -r spec/spec_helper" end From 3a18d0bb06263ef1680a1afc57ae75317196c2b7 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 2 Mar 2011 00:16:54 +0000 Subject: [PATCH 09/10] added twhois executable --- bin/twhois | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 bin/twhois 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 From 60ad83f4d9a73d1f4158041067961920dc0ad3a6 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 2 Mar 2011 00:17:04 +0000 Subject: [PATCH 10/10] added readme file --- README.md | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 README.md 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.