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