3 :received => '/direct_messages.json',
4 :sent => '/direct_messages/sent.json',
5 :post => '/direct_messages/new.json',
6 :delete => '/direct_messages/destroy.json',
9 # Provides access to Twitter's Messaging API for received and
10 # sent direct messages.
13 # received_messages = @twitter.messages(:received)
15 # An <tt>ArgumentError</tt> will be raised if an invalid <tt>action</tt>
16 # is given. Valid actions are:
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))
26 # Provides access to Twitter's Messaging API for sending and deleting
27 # direct messages to other users.
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>.
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
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.
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)
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.
57 # An <tt>ArgumentError</tt> will be raised if an invalid <tt>action</tt>
58 # is given. Valid actions are:
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)
71 response = rest_oauth_connect(:post, uri, {:text => value, :user => user, :source => self.class.config.source})
73 response = rest_oauth_connect(:delete, uri, :id => value.to_i)
75 message = Twitter::Message.unmarshal(response.body)