Permalink
Browse files

check if api_key is valid in Backtweet and raise BackTweet::Unauthent…

…ication if api_key is invalid
  • Loading branch information...
shingara committed Mar 3, 2010
1 parent 696e8b1 commit bca279292464d03be4b069289c341e853c6c9dd9
@@ -1,22 +1,24 @@
module Twitterland
+
class BackTweets
include HTTParty
base_uri 'backtweets.com'
format :json
-
+
# Return tweet referencing a URL
# Get your api_key at http://www.backtype.com/developers
- #
+ #
# Twitterland::BackTweets.search('http://squeejee.com', 'OU812')
def self.search(q, api_key, options={})
options['itemsperpage'] = options.delete(:items_per_page) if options[:items_per_page]
rubyize_response(Mash.new(get("/search.json", :query => {:q => q, :key => api_key}.merge(options))))
end
-
-
+
+
# Scrubs the response from Back Tweets to rubyize keys
def self.rubyize_response(response)
results = Mash.new
+ raise BackTweets::Unauthenticated.new if response.has_key?('error')
results.total_results = response['totalresults'].to_i
results.start_index = response['startindex']
results.items_per_page = response['itemsperpage']
@@ -31,4 +33,7 @@ def self.rubyize_response(response)
end
private_class_method :rubyize_response
end
-end
+
+ class BackTweets::Unauthenticated < Exception
+ end
+end
@@ -0,0 +1 @@
+{"error":{"errorCode":"unauthenticated"}}
@@ -2,7 +2,7 @@
class BackTweetsTest < Test::Unit::TestCase
include Twitterland
-
+
context "Hitting the BackTweets API" do
should "return tweets referencing a URL" do
stub_get 'http://backtweets.com/search.json?q=http%3A%2F%2Fsqueejee.com&key=OU812', 'backtweets.json'
@@ -14,7 +14,14 @@ class BackTweetsTest < Test::Unit::TestCase
last_tweet.id = 1642929098
last_tweet.from_user_id = 383935
last_tweet.from_user = 'Curvezilla'
-
+
+ end
+
+ should "raise Invalid key if bad key" do
+ stub_get 'http://backtweets.com/search.json?q=http%3A%2F%2Fsqueejee.com&key=OU812', 'backtweets_unauthenticated.json'
+ assert_raise Twitterland::BackTweets::Unauthenticated do
+ Twitterland::BackTweets.search('http://squeejee.com', 'OU812')
+ end
end
end
-end
+end

0 comments on commit bca2792

Please sign in to comment.