Skip to content

Commit

Permalink
Added Twitter API versioning support for unauthenticated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Mar 21, 2010
1 parent 9555f4a commit fb895cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
16 changes: 9 additions & 7 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,40 @@ class Unavailable < StandardError; end
class InformTwitter < StandardError; end
class NotFound < StandardError; end

API_VERSION = 1

def self.firehose
response = HTTParty.get("http://api.twitter.com/statuses/public_timeline.json", :format => :json)
response = HTTParty.get("http://api.twitter.com/#{API_VERSION}/statuses/public_timeline.json", :format => :json)
response.map { |tweet| Hashie::Mash.new(tweet) }
end

def self.user(id)
response = HTTParty.get("http://api.twitter.com/users/show/#{id}.json", :format => :json)
response = HTTParty.get("http://api.twitter.com/#{API_VERSION}/users/show/#{id}.json", :format => :json)
Hashie::Mash.new(response)
end

def self.status(id)
response = HTTParty.get("http://api.twitter.com/statuses/show/#{id}.json", :format => :json)
response = HTTParty.get("http://api.twitter.com/#{API_VERSION}/statuses/show/#{id}.json", :format => :json)
Hashie::Mash.new(response)
end

def self.friend_ids(id)
HTTParty.get("http://api.twitter.com/friends/ids/#{id}.json", :format => :json)
HTTParty.get("http://api.twitter.com/#{API_VERSION}/friends/ids/#{id}.json", :format => :json)
end

def self.follower_ids(id)
HTTParty.get("http://api.twitter.com/followers/ids/#{id}.json", :format => :json)
HTTParty.get("http://api.twitter.com/#{API_VERSION}/followers/ids/#{id}.json", :format => :json)
end

def self.timeline(id, options={})
response = HTTParty.get("http://api.twitter.com/statuses/user_timeline/#{id}.json", :query => options, :format => :json)
response = HTTParty.get("http://api.twitter.com/#{API_VERSION}/statuses/user_timeline/#{id}.json", :query => options, :format => :json)
response.map{|tweet| Hashie::Mash.new tweet}
end

# :per_page = max number of statues to get at once
# :page = which page of tweets you wish to get
def self.list_timeline(list_owner_username, slug, query = {})
response = HTTParty.get("http://api.twitter.com/1/#{list_owner_username}/lists/#{slug}/statuses.json", :query => query, :format => :json)
response = HTTParty.get("http://api.twitter.com/#{API_VERSION}/#{list_owner_username}/lists/#{slug}/statuses.json", :query => query, :format => :json)
response.map{|tweet| Hashie::Mash.new tweet}
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/twitter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class TwitterTest < Test::Unit::TestCase
should "have firehose method for public timeline" do
stub_get('http://api.twitter.com:80/statuses/public_timeline.json', 'firehose.json')
stub_get('http://api.twitter.com:80/1/statuses/public_timeline.json', 'firehose.json')
hose = Twitter.firehose
hose.size.should == 20
first = hose.first
Expand All @@ -11,35 +11,35 @@ class TwitterTest < Test::Unit::TestCase
end

should "have user method for unauthenticated calls to get a user's information" do
stub_get('http://api.twitter.com:80/users/show/jnunemaker.json', 'user.json')
stub_get('http://api.twitter.com:80/1/users/show/jnunemaker.json', 'user.json')
user = Twitter.user('jnunemaker')
user.name.should == 'John Nunemaker'
user.description.should == 'Loves his wife, ruby, notre dame football and iu basketball'
end

should "have status method for unauthenticated calls to get a status" do
stub_get('http://api.twitter.com:80/statuses/show/1533815199.json', 'status_show.json')
stub_get('http://api.twitter.com:80/1/statuses/show/1533815199.json', 'status_show.json')
status = Twitter.status(1533815199)
status.id.should == 1533815199
status.text.should == 'Eating some oatmeal and butterscotch cookies with a cold glass of milk for breakfast. Tasty!'
end

should "have a timeline method for unauthenticated calls to get a user's timeline" do
stub_get('http://api.twitter.com:80/statuses/user_timeline/jnunemaker.json', 'user_timeline.json')
stub_get('http://api.twitter.com:80/1/statuses/user_timeline/jnunemaker.json', 'user_timeline.json')
statuses = Twitter.timeline('jnunemaker')
statuses.first.id.should == 1445986256
statuses.first.user.screen_name.should == 'jnunemaker'

end

should "have friend_ids method" do
stub_get('http://api.twitter.com:80/friends/ids/jnunemaker.json', 'friend_ids.json')
stub_get('http://api.twitter.com:80/1/friends/ids/jnunemaker.json', 'friend_ids.json')
ids = Twitter.friend_ids('jnunemaker')
ids.size.should == 161
end

should "have follower_ids method" do
stub_get('http://api.twitter.com:80/followers/ids/jnunemaker.json', 'follower_ids.json')
stub_get('http://api.twitter.com:80/1/followers/ids/jnunemaker.json', 'follower_ids.json')
ids = Twitter.follower_ids('jnunemaker')
ids.size.should == 1252
end
Expand Down

0 comments on commit fb895cc

Please sign in to comment.