Added v 0.3.1 snapshot.
[twitter4r-core.git] / lib / twitter / client / favorites.rb
blob2c3799652127fdcc8b178599507a30159f96235d
1 class Twitter::Client
2   # Why Twitter.com developers can't correctly document their API, I do not know!
3   @@FAVORITES_URIS = {
4     :add => '/favourings/create',
5     :remove => '/favourings/destroy',
6   }
8   # Provides access to the Twitter list favorites API.
9   # 
10   # You can access the authenticated [Twitter] user's favorites list using this method.
11   # 
12   # By default you will receive the last twenty statuses added to your favorites list.
13   # To get a previous page you can provide options to this method.  For example,
14   #  statuses = client.favorites(:page => 2)
15   # The above one-liner will get the second page of favorites for the authenticated user.
16   def favorites(options = nil)
17     def uri_suffix(opts); opts && opts[:page] ? "?page=#{opts[:page]}" : ""; end
18     uri = '/favorites.json' + uri_suffix(options)
19     response = http_connect {|conn|     create_http_get_request(uri) }
20     bless_models(Twitter::Status.unmarshal(response.body))
21   end
22         
23   # Provides access to the Twitter add/remove favorite API.
24   # 
25   # You can add and remove favorite status using this method.
26   # 
27   # <tt>action</tt> can be any of the following values:
28   # * <tt>:add</tt> - to add a status to your favorites, you would use this <tt>action</tt> value
29   # * <tt>:remove</tt> - to remove an status from your existing favorites list use this.
30   # 
31   # The <tt>value</tt> must be either the status object to add or remove or 
32   # the integer unique status ID.
33   # 
34   # Examples:
35   #  id = 126006103423
36   #  client.favorite(:add, id)
37   #  client.favorite(:remove, id)
38   #  status = Twitter::Status.find(id, client)
39   #  client.favorite(:add, status)
40   #  client.favorite(:remove, status)
41   def favorite(action, value)
42     raise ArgumentError, "Invalid favorite action provided: #{action}" unless @@FAVORITES_URIS.keys.member?(action)
43     value = value.to_i.to_s unless value.is_a?(String)
44     uri = "#{@@FAVORITES_URIS[action]}/#{value}.json"
45     case action
46     when :add
47       response = http_connect {|conn| create_http_post_request(uri) }
48     when :remove
49       response = http_connect {|conn| create_http_delete_request(uri) }
50     end
51     bless_model(Twitter::Status.unmarshal(response.body))
52   end
53 end