Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Made new class for search by twitter username

  • Loading branch information...
commit 9e3c58eddab59ec919cfe5527cdaa806b03f8d4e 1 parent 46c96a5
@jtadeulopes jtadeulopes authored
View
1  lib/twittercounter.rb
@@ -1,6 +1,7 @@
require "httparty"
require "twittercounter/request"
require "twittercounter/client"
+require "twittercounter/twitter"
module Twittercounter
end
View
2  lib/twittercounter/client.rb
@@ -13,7 +13,7 @@ def initialize(options={})
raise "twitter_id or twitter_username is required" if options[:twitter_id].blank? && options[:twitter_username].blank?
if options.has_key?(:twitter_username)
- twitter_id = Request.get("http://api.twitter.com/users/show.json", :query => {:screen_name => options.delete(:twitter_username)})["id"]
+ twitter_id = Twitter.user(options.delete(:twitter_username)).id
options.merge!(:twitter_id => twitter_id)
end
View
22 lib/twittercounter/twitter.rb
@@ -0,0 +1,22 @@
+module Twittercounter
+
+ class Twitter
+
+ attr_accessor :id
+
+ def initialize(response)
+ self.id = response["id"]
+ end
+
+ def self.user(username)
+ response = Request.get("http://api.twitter.com/users/show.json", :query => {:screen_name => username})
+ if response.code == 200
+ Twitter.new(response)
+ else
+ raise "#{response.code} #{response.message}"
+ end
+ end
+
+ end
+
+end
View
21 spec/twittercounter/twitter_spec.rb
@@ -0,0 +1,21 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe Twittercounter::Twitter do
+
+ context "search information about a twitter username" do
+ use_vcr_cassette "twitter_username_zigotto"
+
+ let(:user) do
+ Twittercounter::Twitter.user("zigotto")
+ end
+
+ it { user.id.should == 18770991 }
+ end
+
+ context "when use a invalid twitter username" do
+ use_vcr_cassette "invalid_twitter_username"
+ it { expect { Twittercounter::Twitter.user("123invalid456ID")}.to raise_error(/404 Not Found/) }
+ it { expect { Twittercounter::Twitter.user }.to raise_error }
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.