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