Fixed Twitter::Client#message(:delete, ...) calls to use correct URI.
[twitter4r-core.git] / lib / twitter / client / messaging.rb
blobd9520f90823dce420cd1871cae4d0c50fde4b41d
1 class Twitter::Client
2   @@MESSAGING_URIS = {
3     :received => '/direct_messages.json',
4     :sent => '/direct_messages/sent.json',
5     :post => '/direct_messages/new.json',
6     :delete => '/direct_messages/destroy.json',
7   }
9   # Provides access to Twitter's Messaging API for received and 
10   # sent direct messages.
11   # 
12   # Example:
13   #  received_messages = @twitter.messages(:received)
14   # 
15   # An <tt>ArgumentError</tt> will be raised if an invalid <tt>action</tt> 
16   # is given.  Valid actions are:
17   # * +:received+
18   # * +:sent+
19   def messages(action, options = {})
20     raise ArgumentError, "Invalid messaging action: #{action}" unless [:sent, :received].member?(action)
21     uri = @@MESSAGING_URIS[action]
22     response = rest_oauth_connect(:get, uri, options)
23     bless_models(Twitter::Message.unmarshal(response.body))
24   end
25   
26   # Provides access to Twitter's Messaging API for sending and deleting 
27   # direct messages to other users.
28   # 
29   # <tt>action</tt> can be:
30   # * <tt>:post</tt> - to send a new direct message, <tt>value</tt>, to <tt>user</tt> given.
31   # * <tt>:delete</tt> - to delete direct message with message ID <tt>value</tt>.
32   # 
33   # <tt>value</tt> should be:
34   # * <tt>String</tt> when action is <tt>:post</tt>.  Will be the message text sent to given <tt>user</tt>.
35   # * <tt>Integer</tt> or <tt>Twitter::Message</tt> object when action is <tt>:delete</tt>.  Will refer to the unique message ID to delete.  When passing in an instance of <tt>Twitter::Message</tt> that Status will be 
36   # 
37   # <tt>user</tt> should be:
38   # * <tt>Twitter::User</tt>, <tt>Integer</tt> or <tt>String</tt> object when <tt>action</tt> is <tt>:post</tt>.  The <tt>Integer</tt> must be the unique ID of the Twitter user you wish to send the direct message to and any <tt>String</tt>s passed in must be the screen name of the user you wish to send the direct message to.
39   # * totally ignore when <tt>action</tt> is <tt>:delete</tt>.  It has no purpose in this use case scenario.
40   # 
41   # Examples:
42   # The example below sends the message text 'Are you coming over at 6pm for the BBQ tonight?' to user with screen name 'myfriendslogin'...
43   #  @twitter.message(:post, 'Are you coming over at 6pm for the BBQ tonight?', 'myfriendslogin')
44   # The example below sends the same message text as above to user with unique integer ID of 1234567890...
45   # the example below sends the same message text as above to user represented by <tt>user</tt> object instance of <tt>Twitter::User</tt>...
46   #  @twitter.message(:post, 'Are you coming over at 6pm for the BBQ tonight?', user)
47   #  message = @twitter.message(:post, 'Are you coming over at 6pm for the BBQ tonight?', 1234567890)
48   # the example below delete's the message send directly above to user with unique ID 1234567890...
49   #  @twitter.message(:delete, message)
50   # Or the following can also be done...
51   #  @twitter.message(:delete, message.id)
52   # 
53   # In both scenarios (<tt>action</tt> is <tt>:post</tt> or 
54   # <tt>:delete</tt>) a blessed <tt>Twitter::Message</tt> object is 
55   # returned that represents the newly posted or newly deleted message.
56   # 
57   # An <tt>ArgumentError</tt> will be raised if an invalid <tt>action</tt> 
58   # is given.  Valid actions are:
59   # * +:post+
60   # * +:delete+
61   # 
62   # An <tt>ArgumentError</tt> is also raised when no user argument is 
63   # supplied when <tt>action</tt> is +:post+.
64   def message(action, value, user = nil)
65     raise ArgumentError, "Invalid messaging action: #{action}" unless [:post, :delete].member?(action)
66     raise ArgumentError, "User argument must be supplied for :post case" if action.eql?(:post) and user.nil?
67     uri = @@MESSAGING_URIS[action]
68     user = user.to_i if user and user.is_a?(Twitter::User)
69     case action
70     when :post
71       response = rest_oauth_connect(:post, uri, {:text => value, :user => user, :source => self.class.config.source})
72     when :delete
73       response = rest_oauth_connect(:delete, uri, :id => value.to_i)
74     end
75     message = Twitter::Message.unmarshal(response.body)
76     bless_model(message)
77   end
78 end