Skip to content

Commit

Permalink
Lists based on DRAFT API
Browse files Browse the repository at this point in the history
  • Loading branch information
Wynn Netherland committed Oct 31, 2009
1 parent f6a77fd commit be4bffd
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 3 deletions.
69 changes: 68 additions & 1 deletion lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Twitter
class Base
extend Forwardable

def_delegators :client, :get, :post
def_delegators :client, :get, :post, :put, :delete

attr_reader :client

Expand Down Expand Up @@ -153,6 +153,65 @@ def help
perform_get('/help/test.json')
end

def list_create(list_owner_username, options)
perform_post("/#{list_owner_username}/lists.json", :body => {:user => list_owner_username}.merge(options))
end

def list_update(list_owner_username, slug, options)
perform_put("/#{list_owner_username}/lists/#{slug}.json", :body => options)
end

def list_delete(list_owner_username, slug)
perform_delete("/#{list_owner_username}/lists/#{slug}.json")
end

def lists(list_owner_username=nil)
path = "http://api.twitter.com/1"
path += "/#{list_owner_username}" if list_owner_username
path += "/lists.json"
perform_get(path)
end

def list(list_owner_username, slug)
perform_get("/#{list_owner_username}/lists/#{slug}.json")
end

def list_timeline(list_owner_username, slug)
perform_get("/#{list_owner_username}/lists/#{slug}/statuses.json")
end

def memberships(list_owner_username)
perform_get("/#{list_owner_username}/lists/memberships.json")
end

def list_members(list_owner_username, slug)
perform_get("/#{list_owner_username}/#{slug}/members.json")
end

def list_add_member(list_owner_username, slug, new_id)
perform_post("/#{list_owner_username}/#{slug}/members.json", :body => {:id => new_id})
end

def list_remove_member(list_owner_username, slug, id)
perform_delete("/#{list_owner_username}/#{slug}/members.json", :body => {:id => id})
end

def is_list_member?(list_owner_username, slug, id)
perform_get("/#{list_owner_username}/#{slug}/members/#{id}.json").error.nil?
end

def list_subscribers(list_owner_username, slug)
perform_get("/#{list_owner_username}/#{slug}/subscribers.json")
end

def list_subscribe(list_owner_username, slug)
perform_post("/#{list_owner_username}/#{slug}/subscribers.json")
end

def list_unsubscribe(list_owner_username, slug)
perform_delete("/#{list_owner_username}/#{slug}/subscribers.json")
end

private
def perform_get(path, options={})
Twitter::Request.get(self, path, options)
Expand All @@ -161,5 +220,13 @@ def perform_get(path, options={})
def perform_post(path, options={})
Twitter::Request.post(self, path, options)
end

def perform_put(path, options={})
Twitter::Request.put(self, path, options)
end

def perform_delete(path, options={})
Twitter::Request.delete(self, path, options)
end
end
end
8 changes: 8 additions & 0 deletions lib/twitter/httpauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def post(uri, body={}, headers={})
self.class.post(uri, :body => body, :headers => headers, :basic_auth => basic_auth)
end

def put(uri, body={}, headers={})
self.class.put(uri, :body => body, :headers => headers, :basic_auth => basic_auth)
end

def delete(uri, body={}, headers={})
self.class.delete(uri, :body => body, :headers => headers, :basic_auth => basic_auth)
end

private
def basic_auth
@basic_auth ||= {:username => @username, :password => @password}
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/oauth.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Twitter
class OAuth
extend Forwardable
def_delegators :access_token, :get, :post
def_delegators :access_token, :get, :post, :put, :delete

attr_reader :ctoken, :csecret, :consumer_options

Expand Down
18 changes: 17 additions & 1 deletion lib/twitter/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ def self.post(client, path, options={})
new(client, :post, path, options).perform
end

def self.put(client, path, options={})
new(client, :put, path, options).perform
end

def self.delete(client, path, options={})
new(client, :delete, path, options).perform
end

attr_reader :client, :method, :path, :options

def_delegators :client, :get, :post
def_delegators :client, :get, :post, :put, :delete

def initialize(client, method, path, options={})
@client, @method, @path, @options = client, method, path, {:mash => true}.merge(options)
Expand Down Expand Up @@ -43,6 +51,14 @@ def perform_post
send(:post, uri, options[:body], options[:headers])
end

def perform_put
send(:put, uri, options[:body], options[:headers])
end

def perform_delete
send(:delete, uri, options[:headers])
end

def make_friendly(response)
raise_errors(response)
data = parse(response)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"mode":"public","uri":"/pengwynn/rubyists","member_count":2,"slug":"rubyists","subscriber_count":0,"full_name":"@pengwynn/rubyists","user":{"profile_image_url":"http://a1.twimg.com/profile_images/485575482/komikazee_normal.png","description":"Christian husband and father, Ruby developer and web designer. Co-founder of TweetCongress.org.","following":false,"statuses_count":1929,"time_zone":"Central Time (US & Canada)","profile_text_color":"666666","followers_count":2192,"screen_name":"pengwynn","profile_background_image_url":"http://a1.twimg.com/profile_background_images/47314868/twitter.png","friends_count":1916,"profile_link_color":"35abe9","url":"http://wynnnetherland.com","profile_background_tile":false,"created_at":"Sat Mar 08 16:34:22 +0000 2008","notifications":false,"favourites_count":18,"profile_background_color":"efefef","protected":false,"geo_enabled":false,"profile_sidebar_fill_color":"dddddd","location":"Dallas, TX","name":"Wynn Netherland","id":14100886,"verified":false,"utc_offset":-21600,"profile_sidebar_border_color":"cccccc"},"name":"Rubyists","id":1129440}
1 change: 1 addition & 0 deletions test/fixtures/list_statuses.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/fixtures/list_users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"users":[{"profile_image_url":"http://a3.twimg.com/profile_images/61024905/black250_normal.jpg","description":"I make things simple.","following":true,"statuses_count":6123,"time_zone":"Indiana (East)","status":{"in_reply_to_user_id":10293122,"in_reply_to_status_id":5273556367,"truncated":false,"favorited":false,"source":"<a href=\"http://birdfeedapp.com\" rel=\"nofollow\">Birdfeed</a>","created_at":"Fri Oct 30 01:36:40 +0000 2009","in_reply_to_screen_name":"joshuaclayton","id":5275756114,"text":"@joshuaclayton totally! It kills me."},"profile_text_color":"666666","followers_count":1659,"screen_name":"jnunemaker","profile_background_image_url":"http://s.twimg.com/a/1256778767/images/themes/theme9/bg.gif","friends_count":138,"profile_link_color":"2FC2EF","url":"http://railstips.org/about","profile_background_tile":false,"created_at":"Sun Aug 13 22:56:06 +0000 2006","notifications":false,"favourites_count":115,"profile_background_color":"1A1B1F","protected":false,"geo_enabled":false,"profile_sidebar_fill_color":"252429","location":"South Bend, IN","name":"John Nunemaker","id":4243,"verified":false,"utc_offset":-18000,"profile_sidebar_border_color":"181A1E"},{"time_zone":"Pacific Time (US & Canada)","profile_image_url":"http://a1.twimg.com/profile_images/427781590/yehuda_normal.jpg","description":"jQuery/Merb/DM FTW","following":true,"verified":false,"profile_text_color":"000000","status":{"source":"<a href=\"http://www.atebits.com/\" rel=\"nofollow\">Tweetie</a>","in_reply_to_user_id":713263,"in_reply_to_status_id":5262502756,"truncated":false,"created_at":"Thu Oct 29 16:35:31 +0000 2009","favorited":false,"in_reply_to_screen_name":"defunkt","id":5262909930,"text":"@defunkt curious: what's not working about the Ruby version? Or just interested in playing with Python?"},"profile_background_image_url":"http://a3.twimg.com/profile_background_images/12633131/Twit3.gif","screen_name":"wycats","profile_link_color":"0000ff","profile_background_tile":false,"followers_count":3137,"url":"http://www.yehudakatz.com","created_at":"Thu Aug 30 04:07:52 +0000 2007","profile_background_color":"333333","friends_count":119,"statuses_count":1511,"profile_sidebar_fill_color":"a8caa0","protected":false,"notifications":false,"favourites_count":3,"location":"iPhone: 37.786461,-122.394867","name":"wycats","profile_sidebar_border_color":"87bc44","id":8526432,"geo_enabled":false,"utc_offset":-28800}], "next_cursor":0, "previous_cursor":0 }
1 change: 1 addition & 0 deletions test/fixtures/lists.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"lists":[{"mode":"public","uri":"/pengwynn/rubyists","member_count":2,"slug":"rubyists","subscriber_count":0,"full_name":"@pengwynn/rubyists","user":{"profile_image_url":"http://a1.twimg.com/profile_images/485575482/komikazee_normal.png","description":"Christian husband and father, Ruby developer and web designer. Co-founder of TweetCongress.org.","following":false,"statuses_count":1929,"time_zone":"Central Time (US & Canada)","profile_text_color":"666666","followers_count":2192,"screen_name":"pengwynn","profile_background_image_url":"http://a1.twimg.com/profile_background_images/47314868/twitter.png","friends_count":1916,"profile_link_color":"35abe9","url":"http://wynnnetherland.com","profile_background_tile":false,"created_at":"Sat Mar 08 16:34:22 +0000 2008","notifications":false,"favourites_count":18,"profile_background_color":"efefef","protected":false,"geo_enabled":false,"profile_sidebar_fill_color":"dddddd","location":"Dallas, TX","name":"Wynn Netherland","id":14100886,"verified":false,"utc_offset":-21600,"profile_sidebar_border_color":"cccccc"},"name":"Rubyists","id":1129440}], "next_cursor":0, "previous_cursor":0 }
1 change: 1 addition & 0 deletions test/fixtures/memberships.json

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ def stub_get(url, filename, status=nil)
def stub_post(url, filename)
FakeWeb.register_uri(:post, twitter_url(url), :body => fixture_file(filename))
end

def stub_put(url, filename)
FakeWeb.register_uri(:put, twitter_url(url), :body => fixture_file(filename))
end

def stub_delete(url, filename)
FakeWeb.register_uri(:delete, twitter_url(url), :body => fixture_file(filename))
end
105 changes: 105 additions & 0 deletions test/twitter/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,110 @@ class BaseTest < Test::Unit::TestCase
end
end

context "when using lists" do

should "be able to create a new list" do
stub_post('/pengwynn/lists.json', 'list.json')
list = @twitter.list_create('pengwynn', {:name => 'Rubyists'})
list.name.should == 'Rubyists'
list.slug.should == 'rubyists'
list.mode.should == 'public'
end

should "be able to update a list" do
stub_put('/pengwynn/lists/rubyists.json', 'list.json')
list = @twitter.list_update('pengwynn', 'rubyists', {:name => 'Rubyists'})
list.name.should == 'Rubyists'
list.slug.should == 'rubyists'
list.mode.should == 'public'
end

should "be able to delete a list" do
stub_delete('/pengwynn/lists/rubyists.json', 'list.json')
list = @twitter.list_delete('pengwynn', 'rubyists')
list.name.should == 'Rubyists'
list.slug.should == 'rubyists'
list.mode.should == 'public'
end

should "be able to view lists to which a user belongs" do
stub_get('/pengwynn/lists/memberships.json', 'memberships.json')
lists = @twitter.memberships('pengwynn').lists
lists.size.should == 16
lists.first.name.should == 'web-dev'
lists.first.member_count.should == 38
end

should "be able to view lists for the authenticated user" do
stub_get('http://api.twitter.com/1/pengwynn/lists.json', 'lists.json')
lists = @twitter.lists('pengwynn').lists
lists.size.should == 1
lists.first.name.should == 'Rubyists'
lists.first.slug.should == 'rubyists'
end

should "be able to view list details" do
stub_get('/pengwynn/lists/rubyists.json', 'list.json')
list = @twitter.list('pengwynn', 'rubyists')
list.name.should == 'Rubyists'
list.subscriber_count.should == 0
end

should "be able to view list timeline" do
stub_get('/pengwynn/lists/rubyists/statuses.json', 'list_statuses.json')
tweets = @twitter.list_timeline('pengwynn', 'rubyists')
tweets.size.should == 20
tweets.first.id.should == 5272535583
tweets.first.user.name.should == 'John Nunemaker'
end

should "be able to view list members" do
stub_get('/pengwynn/rubyists/members.json', 'list_users.json')
members = @twitter.list_members('pengwynn', 'rubyists').users
members.size.should == 2
members.first.name.should == 'John Nunemaker'
members.first.screen_name.should == 'jnunemaker'
end

should "be able to add a member to a list" do
stub_post('/pengwynn/rubyists/members.json', 'user.json')
user = @twitter.list_add_member('pengwynn', 'rubyists', 4243)
user.screen_name.should == 'jnunemaker'
end

should "be able to remove a member from a list" do
stub_delete('/pengwynn/rubyists/members.json', 'user.json')
user = @twitter.list_remove_member('pengwynn', 'rubyists', 4243)
user.screen_name.should == 'jnunemaker'
end

should "be able to check if a user is member of a list" do
stub_get('/pengwynn/rubyists/members/4243.json', 'user.json')
@twitter.is_list_member?('pengwynn', 'rubyists', 4243).should == true
end

should "be able to view list subscribers" do
stub_get('/pengwynn/rubyists/subscribers.json', 'list_users.json')
subscribers = @twitter.list_subscribers('pengwynn', 'rubyists').users
subscribers.size.should == 2
subscribers.first.name.should == 'John Nunemaker'
subscribers.first.screen_name.should == 'jnunemaker'
end

should "be able to subscribe to a list" do
stub_post('/pengwynn/rubyists/subscribers.json', 'user.json')
user = @twitter.list_subscribe('pengwynn', 'rubyists')
user.screen_name.should == 'jnunemaker'
end

should "be able to unsubscribe from a list" do
stub_delete('/pengwynn/rubyists/subscribers.json', 'user.json')
user = @twitter.list_unsubscribe('pengwynn', 'rubyists')
user.screen_name.should == 'jnunemaker'
end

end


end
end

1 comment on commit be4bffd

@jnunemaker
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet!

Please sign in to comment.