mbbx6spp-twitter4r 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/CHANGES +124 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +32 -0
  4. data/TODO +9 -0
  5. data/lib/twitter/client/account.rb +24 -0
  6. data/lib/twitter/client/auth.rb +27 -0
  7. data/lib/twitter/client/base.rb +87 -0
  8. data/lib/twitter/client/blocks.rb +35 -0
  9. data/lib/twitter/client/favorites.rb +53 -0
  10. data/lib/twitter/client/friendship.rb +35 -0
  11. data/lib/twitter/client/messaging.rb +79 -0
  12. data/lib/twitter/client/status.rb +46 -0
  13. data/lib/twitter/client/timeline.rb +72 -0
  14. data/lib/twitter/client/user.rb +65 -0
  15. data/lib/twitter/client.rb +21 -0
  16. data/lib/twitter/config.rb +71 -0
  17. data/lib/twitter/console.rb +28 -0
  18. data/lib/twitter/core.rb +137 -0
  19. data/lib/twitter/ext/stdlib.rb +51 -0
  20. data/lib/twitter/ext.rb +2 -0
  21. data/lib/twitter/extras.rb +39 -0
  22. data/lib/twitter/meta.rb +56 -0
  23. data/lib/twitter/model.rb +348 -0
  24. data/lib/twitter/rails.rb +92 -0
  25. data/lib/twitter/version.rb +19 -0
  26. data/lib/twitter.rb +31 -0
  27. data/spec/twitter/client/auth_spec.rb +34 -0
  28. data/spec/twitter/client/base_spec.rb +242 -0
  29. data/spec/twitter/client/blocks_spec.rb +76 -0
  30. data/spec/twitter/client/favorites_spec.rb +183 -0
  31. data/spec/twitter/client/friendship_spec.rb +76 -0
  32. data/spec/twitter/client/messaging_spec.rb +135 -0
  33. data/spec/twitter/client/status_spec.rb +92 -0
  34. data/spec/twitter/client/timeline_spec.rb +79 -0
  35. data/spec/twitter/client/user_spec.rb +203 -0
  36. data/spec/twitter/client_spec.rb +2 -0
  37. data/spec/twitter/config_spec.rb +86 -0
  38. data/spec/twitter/console_spec.rb +15 -0
  39. data/spec/twitter/core_spec.rb +127 -0
  40. data/spec/twitter/ext/stdlib_spec.rb +42 -0
  41. data/spec/twitter/extras_spec.rb +46 -0
  42. data/spec/twitter/meta_spec.rb +90 -0
  43. data/spec/twitter/model_spec.rb +464 -0
  44. data/spec/twitter/rails_spec.rb +110 -0
  45. data/spec/twitter/version_spec.rb +19 -0
  46. metadata +108 -0
data/CHANGES ADDED
@@ -0,0 +1,124 @@
1
+ = CHANGES
2
+
3
+ Catalog(ue) of changes for Twitter4R 0.1.x releases including Retrospectiva ticket cross-reference numbers. Refer to http://retro.tautology.net/projects/twitter4r/tickets for more information.
4
+
5
+ == 0.3.1 Changes
6
+ * Added URI.encode => CGI.escape fix
7
+ * Added block methods
8
+ * Added Twitter::Client#inspect method to XXXX out passwords
9
+ * Minor refactoring of spec/spec_helper.rb
10
+ * Added support for :page, :lite and :since options for Twitter::User#followers, Twitter::Client#my([:friends|:followers]...) and Twitter::Client#user([:friends|:followers]...)calls
11
+ * Added Twitter::Client.status(:replies) support
12
+
13
+
14
+ == 0.3.0 Changes
15
+ * Bunch of changes, but forgot to track them and too lazy to look through the SVN/Git logs. Sorry.
16
+
17
+ == 0.2.5 Changes
18
+
19
+ === 2007-09-23
20
+ * Added paging support as per Sergio Santos' request (#36).
21
+
22
+ == 0.2.4 Changes
23
+
24
+ === 2007-07-24
25
+ * Fixed ActiveSupport +Time#to_s+ conflict such that integration with Rails is much less painful (#34)
26
+
27
+ == 0.2.3 Changes
28
+
29
+ === 2007-07-22
30
+ * Fixed defect #31 such that passing string screen name as for user argument is handled correctly.
31
+ * Fixed #30 typo: respond_to -> respond_to?
32
+ * Added relevant exception handling for #message(:post, ...) case (#32)
33
+ * Add ability to pass in Twitter::User object to Twitter::Client#user(...) #33
34
+ * Added stats Rake task
35
+ * Updated RDoc for Twitter::Client#user to warn against using it to get followers of authenticated user and updated ArgumentError raising logic as per #29.
36
+
37
+ == 0.2.2 Changes
38
+
39
+ === 2007-07-18
40
+ * Fixed URI paths for user, messaging and friendship APIs (#25)
41
+ * Added action checks for Twitter::Client methods: #user, #my, #message, #messages, #status, #timeline, #friend (#26)
42
+ * Added 'source' configuration documentation.
43
+ * Added missing attributes for Twitter::User (#28)
44
+
45
+ == 0.2.1 Changes
46
+
47
+ === 2007-07-17
48
+ * Added 'source' feature and configurability.
49
+
50
+ == 0.2.0 Changes
51
+
52
+ === 2007-07-08
53
+ * Added featured users API as an "extra" (#19).
54
+ * Productionized website for publishing.
55
+ * Published Ruby Gem on Rubyforge.
56
+
57
+ === 2007-07-07
58
+ * Refactored Twitter4R API to be more consistent, by grouping APIs (#6):
59
+ - Messaging APIs: direct_messages, new, destroy, replies
60
+ - Friendship APIs: create, destroy
61
+ * Added following features (#7):
62
+ - Retrieving direct messages
63
+ - User APIs: friends, followers, show
64
+ * Updated documentation and example code.
65
+
66
+ === 2007-07-06
67
+ * Refactored Twitter4R API to be more consistent, by grouping APIs (#6):
68
+ - Status APIs: show, update, destroy
69
+ - User APIs: friends, followers, show
70
+ * Added X-Twitter-Client HTTP headers and Twitter::Config options (#16)
71
+ * Removed redundant feature (#8):
72
+ - Followers timeline
73
+ * Refactored HTTP request/response code to DRY up code.
74
+ * Fix REST error handling to use #is_a?(HTTPSuccess) instead of code in ['200', '201'] to determine REST error (#15).
75
+
76
+ === 2007-06-25
77
+ * Updated example documentation (#14)
78
+ * Refactored marshaling unmarshaling code (#13)
79
+
80
+ === 2007-06-20
81
+ * Added proxy user/pass support. Tested only via endo-testing. (No system/integration testing behind real proxy as I do not have that environment).
82
+
83
+
84
+ === 2007-06-17
85
+ * Refactored Twitter4R API to be more consistent, by grouping APIs (#6):
86
+ - Timeline APIs: public, friends, user
87
+
88
+ === 2007-06-13
89
+ * Added RSpec Autotest integration
90
+ * Fixed Twitter::Meta generation of spec for hash values
91
+ * Added HTTP header to each request including generated User-Agent header
92
+ * Added RCovMorpher and template to restyle RCov output upon release
93
+ * Added Gemspec dependencies and requirements
94
+ * Added default tidy YAML configuration file for RCovMorpher
95
+ * Added Contributors list and updated external dependencies list to README
96
+ * Removed shebang from examples
97
+
98
+ === 2007-06-12
99
+ * Added proxy support as per Kaiichi Matsunaga submitted patch (#11).
100
+ * Added SSL support (#12)
101
+
102
+ === 2007-05-19
103
+ * Translated RSpec specifications from 0.8.2 compliant to 1.0.0 (#10)
104
+
105
+ == 0.1.1 Changes
106
+
107
+ === 2007-06-25
108
+ * Added SSL support (#12)
109
+ * Added Proxy support (#11)
110
+
111
+ == 0.1.0 Changes
112
+
113
+ === 2007-05-08
114
+ * Added Google Analytics Javascript code to website pages (#5)
115
+
116
+ === 2007-05-07
117
+ * Fixed errors in online sample code documentation and redeployed website (#2 and #3)
118
+ * Created more consistent RDoc theme to go more with website home page (#4)
119
+
120
+ === 2007-05-06
121
+ * Initial revision of codebase commited; includes:
122
+ - Achieved 80% Twitter API feature-completeness
123
+ - Attained 100% RSpec C0 code coverage
124
+ - Rake tasks for: RSpec, RCov, RDoc, Gem, Rubyforge Publishing, etc.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006-2007 Susan Potter <me _at_ susanpotter _dot_ net>.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,32 @@
1
+ = Twitter4R
2
+
3
+ * Project Website - http://twitter4r.rubyforge.org
4
+ * Mailing List - http://groups.google.com/group/twitter4r-users
5
+
6
+ == Developers
7
+ * {Susan Potter}[http://SusanPotter.NET] <me at susanpotter dot net>
8
+
9
+ == Contributors
10
+ * Kaiichi Matsunaga <ma2 at lifemedia dot co dot jp> - proxy code suggestion
11
+ * Sergio Santos <> - message paging code suggestion
12
+ * Adam Stiles <adam at stilesoft dot com> - URI.encode => CGI.escape fix
13
+ * Carl Crawley <cwcrawley at gmail dot com> - Friendship get => post fix
14
+
15
+ == Description
16
+ Twitter4R provides an object based API to query or update your Twitter account via pure Ruby. It hides the ugly HTTP/REST code from your code.
17
+
18
+ == External Dependencies
19
+ * Ruby 1.8 (tested with 1.8.6)
20
+ * RSpec gem 1.0.0+ (tested with 1.1.3)
21
+ * JSON gem 0.4.3+ (tested with versions: 1.1.1 and 1.1.2)
22
+ * jcode (for unicode support)
23
+
24
+ == Usage Examples
25
+ Twitter4R starting with version 0.1.1 and above is organized into seven parts:
26
+ * {Configuration API}[link:files/examples/configure_rb.html]
27
+ * {Friendship API}[link:files/examples/friendship_rb.html]
28
+ * {Messaging API}[link:files/examples/messaging_rb.html]
29
+ * {Model API}[link:files/examples/model_rb.html]
30
+ * {Status API}[link:files/examples/status_rb.html]
31
+ * {Timeline API}[link:files/examples/timeline_rb.html]
32
+ * {User API}[link:files/examples/user_rb.html]
data/TODO ADDED
@@ -0,0 +1,9 @@
1
+
2
+ 0.3.1 TODO:
3
+ * Add specs for :page, :lite and :since options support in Twitter::Client#my(...), Twitter::Client#user(....) and Twitter::User#followers calls
4
+ * Add specs for Kernel#gem_present? extension
5
+ * Add spec to test that warning is given to Rails 2.0+ users requiring 'twitter/rails'
6
+ * Add specs for :replies support in Twitter::Client#status(...) and Twitter::Client#timeline_for(...)
7
+ * Add RDoc for :replies support
8
+ * Add better RDoc for Twitter::Client.account_info(:rate_limit_status)
9
+ * Add specs for Twitter::Client.account_info(:rate_limit_status)
@@ -0,0 +1,24 @@
1
+ class Twitter::Client
2
+ @@ACCOUNT_URIS = {
3
+ :rate_limit_status => '/account/rate_limit_status',
4
+ }
5
+
6
+ # Provides access to the Twitter rate limit status API.
7
+ #
8
+ # You can find out information about your account status. Currently the only
9
+ # supported type of account status is the <tt>:rate_limit_status</tt> which
10
+ # returns a <tt>Twitter::RateLimitStatus</tt> object.
11
+ #
12
+ # Example:
13
+ # account_status = client.account_info
14
+ # puts account_status.remaining_hits
15
+ def account_info(type = :rate_limit_status)
16
+ connection = create_http_connection
17
+ connection.start do |connection|
18
+ response = http_connect do |conn|
19
+ create_http_get_request("#{@@ACCOUNT_URIS[type]}.json")
20
+ end
21
+ bless_models(Twitter::RateLimitStatus.unmarshal(response.body))
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ class Twitter::Client
2
+ @@AUTHENTICATION_URIS = {
3
+ :verify => '/account/verify_credentials',
4
+ }
5
+
6
+ # Provides access to the Twitter verify credentials API.
7
+ #
8
+ # You can verify Twitter user credentials with minimal overhead using this method.
9
+ #
10
+ # Example:
11
+ # client.authenticate?("osxisforlightweights", "l30p@rd_s^cks!")
12
+ def authenticate?(login, password)
13
+ verify_credentials(login, password)
14
+ end
15
+
16
+ private
17
+ def verify_credentials(username, passwd)
18
+ connection = create_http_connection
19
+ connection.start do |connection|
20
+ request = create_http_get_request("#{@@AUTHENTICATION_URIS[:verify]}.json")
21
+ request.basic_auth(username, passwd)
22
+ response = connection.request(request)
23
+ response.is_a?(Net::HTTPSuccess) ? true : false
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,87 @@
1
+ class Twitter::Client
2
+ alias :old_inspect :inspect
3
+ def inspect
4
+ s = old_inspect
5
+ s.gsub!(/@password=".*?"/, '@password="XXXX"')
6
+ end
7
+ protected
8
+ attr_accessor :login, :password
9
+
10
+ # Returns the response of the HTTP connection.
11
+ def http_connect(body = nil, require_auth = true, &block)
12
+ require_block(block_given?)
13
+ connection = create_http_connection
14
+ connection.start do |connection|
15
+ request = yield connection if block_given?
16
+ request.basic_auth(@login, @password) if require_auth
17
+ response = connection.request(request, body)
18
+ handle_rest_response(response)
19
+ response
20
+ end
21
+ end
22
+
23
+ # "Blesses" model object with client information
24
+ def bless_model(model)
25
+ model.bless(self) if model
26
+ end
27
+
28
+ def bless_models(list)
29
+ return bless_model(list) if list.respond_to?(:client=)
30
+ list.collect { |model| bless_model(model) } if list.respond_to?(:collect)
31
+ end
32
+
33
+ private
34
+ @@http_header = nil
35
+
36
+ def raise_rest_error(response, uri = nil)
37
+ raise Twitter::RESTError.new(:code => response.code,
38
+ :message => response.message,
39
+ :uri => uri)
40
+ end
41
+
42
+ def handle_rest_response(response, uri = nil)
43
+ unless response.is_a?(Net::HTTPSuccess)
44
+ raise_rest_error(response, uri)
45
+ end
46
+ end
47
+
48
+ def create_http_connection
49
+ conn = Net::HTTP.new(@@config.host, @@config.port,
50
+ @@config.proxy_host, @@config.proxy_port,
51
+ @@config.proxy_user, @@config.proxy_pass)
52
+ if @@config.protocol == :ssl
53
+ conn.use_ssl = true
54
+ conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
55
+ end
56
+ conn
57
+ end
58
+
59
+ def http_header
60
+ # can cache this in class variable since all "variables" used to
61
+ # create the contents of the HTTP header are determined by other
62
+ # class variables that are not designed to change after instantiation.
63
+ @@http_header ||= {
64
+ 'User-Agent' => "Twitter4R v#{Twitter::Version.to_version} [#{@@config.user_agent}]",
65
+ 'Accept' => 'text/x-json',
66
+ 'X-Twitter-Client' => @@config.application_name,
67
+ 'X-Twitter-Client-Version' => @@config.application_version,
68
+ 'X-Twitter-Client-URL' => @@config.application_url,
69
+ }
70
+ @@http_header
71
+ end
72
+
73
+ def create_http_get_request(uri, params = {})
74
+ path = (params.size > 0) ? "#{uri}?#{params.to_http_str}" : uri
75
+ Net::HTTP::Get.new(path, http_header)
76
+ end
77
+
78
+ def create_http_post_request(uri)
79
+ Net::HTTP::Post.new(uri, http_header)
80
+ end
81
+
82
+ def create_http_delete_request(uri, params = {})
83
+ path = (params.size > 0) ? "#{uri}?#{params.to_http_str}" : uri
84
+ Net::HTTP::Delete.new(path, http_header)
85
+ end
86
+ end
87
+
@@ -0,0 +1,35 @@
1
+ class Twitter::Client
2
+ @@BLOCK_URIS = {
3
+ :add => '/blocks/create',
4
+ :remove => '/blocks/destroy',
5
+ }
6
+
7
+ # Provides access to the Twitter Block API.
8
+ #
9
+ # You can add and remove blocks to users using this method.
10
+ #
11
+ # <tt>action</tt> can be any of the following values:
12
+ # * <tt>:add</tt> - to add a block, you would use this <tt>action</tt> value
13
+ # * <tt>:remove</tt> - to remove a block use this.
14
+ #
15
+ # The <tt>value</tt> must be either the user screen name, integer unique user ID or Twitter::User
16
+ # object representation.
17
+ #
18
+ # Examples:
19
+ # screen_name = 'dictionary'
20
+ # client.block(:add, 'dictionary')
21
+ # client.block(:remove, 'dictionary')
22
+ # id = 1260061
23
+ # client.block(:add, id)
24
+ # client.block(:remove, id)
25
+ # user = Twitter::User.find(id, client)
26
+ # client.block(:add, user)
27
+ # client.block(:remove, user)
28
+ def block(action, value)
29
+ raise ArgumentError, "Invalid friend action provided: #{action}" unless @@BLOCK_URIS.keys.member?(action)
30
+ value = value.to_i unless value.is_a?(String)
31
+ uri = "#{@@BLOCK_URIS[action]}/#{value}.json"
32
+ response = http_connect {|conn| create_http_get_request(uri) }
33
+ bless_model(Twitter::User.unmarshal(response.body))
34
+ end
35
+ end
@@ -0,0 +1,53 @@
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
+ }
7
+
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
@@ -0,0 +1,35 @@
1
+ class Twitter::Client
2
+ @@FRIENDSHIP_URIS = {
3
+ :add => '/friendships/create',
4
+ :remove => '/friendships/destroy',
5
+ }
6
+
7
+ # Provides access to the Twitter Friendship API.
8
+ #
9
+ # You can add and remove friends using this method.
10
+ #
11
+ # <tt>action</tt> can be any of the following values:
12
+ # * <tt>:add</tt> - to add a friend, you would use this <tt>action</tt> value
13
+ # * <tt>:remove</tt> - to remove an existing friend from your friends list use this.
14
+ #
15
+ # The <tt>value</tt> must be either the user to befriend or defriend's
16
+ # screen name, integer unique user ID or Twitter::User object representation.
17
+ #
18
+ # Examples:
19
+ # screen_name = 'dictionary'
20
+ # client.friend(:add, 'dictionary')
21
+ # client.friend(:remove, 'dictionary')
22
+ # id = 1260061
23
+ # client.friend(:add, id)
24
+ # client.friend(:remove, id)
25
+ # user = Twitter::User.find(id, client)
26
+ # client.friend(:add, user)
27
+ # client.friend(:remove, user)
28
+ def friend(action, value)
29
+ raise ArgumentError, "Invalid friend action provided: #{action}" unless @@FRIENDSHIP_URIS.keys.member?(action)
30
+ value = value.to_i unless value.is_a?(String)
31
+ uri = "#{@@FRIENDSHIP_URIS[action]}/#{value}.json"
32
+ response = http_connect {|conn| create_http_post_request(uri) }
33
+ bless_model(Twitter::User.unmarshal(response.body))
34
+ end
35
+ end
@@ -0,0 +1,79 @@
1
+ class Twitter::Client
2
+
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
+ }
9
+
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, options = {})
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, options) }
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
@@ -0,0 +1,46 @@
1
+ class Twitter::Client
2
+ @@STATUS_URIS = {
3
+ :get => '/statuses/show.json',
4
+ :post => '/statuses/update.json',
5
+ :delete => '/statuses/destroy.json',
6
+ }
7
+
8
+ # Provides access to individual statuses via Twitter's Status APIs
9
+ #
10
+ # <tt>action</tt> can be of the following values:
11
+ # * <tt>:get</tt> to retrieve status content. Assumes <tt>value</tt> given responds to :to_i message in meaningful way to yield intended status id.
12
+ # * <tt>:post</tt> to publish a new status
13
+ # * <tt>:delete</tt> to remove an existing status. Assumes <tt>value</tt> given responds to :to_i message in meaningful way to yield intended status id.
14
+ #
15
+ # <tt>value</tt> should be set to:
16
+ # * the status identifier for <tt>:get</tt> case
17
+ # * the status text message for <tt>:post</tt> case
18
+ # * none necessary for <tt>:delete</tt> case
19
+ #
20
+ # Examples:
21
+ # twitter.status(:get, 107786772)
22
+ # twitter.status(:post, "New Ruby open source project Twitter4R version 0.2.0 released.")
23
+ # twitter.status(:delete, 107790712)
24
+ #
25
+ # An <tt>ArgumentError</tt> will be raised if an invalid <tt>action</tt>
26
+ # is given. Valid actions are:
27
+ # * +:get+
28
+ # * +:post+
29
+ # * +:delete+
30
+ def status(action, value = nil)
31
+ return self.timeline_for(action, value || {}) if :replies == action
32
+ raise ArgumentError, "Invalid status action: #{action}" unless @@STATUS_URIS.keys.member?(action)
33
+ return nil unless value
34
+ uri = @@STATUS_URIS[action]
35
+ response = nil
36
+ case action
37
+ when :get
38
+ response = http_connect {|conn| create_http_get_request(uri, :id => value.to_i) }
39
+ when :post
40
+ response = http_connect({:status => value, :source => @@config.source}.to_http_str) {|conn| create_http_post_request(uri) }
41
+ when :delete
42
+ response = http_connect {|conn| create_http_delete_request(uri, :id => value.to_i) }
43
+ end
44
+ bless_model(Twitter::Status.unmarshal(response.body))
45
+ end
46
+ end
@@ -0,0 +1,72 @@
1
+ class Twitter::Client
2
+ @@TIMELINE_URIS = {
3
+ :public => '/statuses/public_timeline.json',
4
+ :friends => '/statuses/friends_timeline.json',
5
+ :friend => '/statuses/friends_timeline.json',
6
+ :user => '/statuses/user_timeline.json',
7
+ :me => '/statuses/user_timeline.json',
8
+ :replies => '/statuses/replies.json',
9
+ }
10
+
11
+ # Provides access to Twitter's Timeline APIs
12
+ #
13
+ # Returns timeline for given <tt>type</tt>.
14
+ #
15
+ # <tt>type</tt> can take the following values:
16
+ # * <tt>public</tt>
17
+ # * <tt>friends</tt> or <tt>friend</tt>
18
+ # * <tt>user</tt> or <tt>me</tt>
19
+ #
20
+ # <tt>:id</tt> is on key applicable to be defined in </tt>options</tt>:
21
+ # * the id or screen name (aka login) for :friends
22
+ # * the id or screen name (aka login) for :user
23
+ # * meaningless for the :me case, since <tt>twitter.timeline_for(:user, 'mylogin')</tt> and <tt>twitter.timeline_for(:me)</tt> are the same assuming 'mylogin' is the authenticated user's screen name (aka login).
24
+ #
25
+ # Examples:
26
+ # # returns the public statuses since status with id of 6543210
27
+ # twitter.timeline_for(:public, id => 6543210)
28
+ # # returns the statuses for friend with user id 43210
29
+ # twitter.timeline_for(:friend, :id => 43210)
30
+ # # returns the statuses for friend with screen name (aka login) of 'otherlogin'
31
+ # twitter.timeline_for(:friend, :id => 'otherlogin')
32
+ # # returns the statuses for user with screen name (aka login) of 'otherlogin'
33
+ # twitter.timeline_for(:user, :id => 'otherlogin')
34
+ #
35
+ # <tt>options</tt> can also include the following keys:
36
+ # * <tt>:id</tt> is the user ID, screen name of Twitter::User representation of a <tt>Twitter</tt> user.
37
+ # * <tt>:since</tt> is a Time object specifying the date-time from which to return results for. Applicable for the :friend, :friends, :user and :me cases.
38
+ # * <tt>:count</tt> specifies the number of statuses to retrieve. Only applicable for the :user case.
39
+ # * <tt>since_id</tt> is the status id of the public timeline from which to retrieve statuses for <tt>:public</tt>. Only applicable for the :public case.
40
+ #
41
+ # You can also pass this method a block, which will iterate through the results
42
+ # of the requested timeline and apply the block logic for each status returned.
43
+ #
44
+ # Example:
45
+ # twitter.timeline_for(:public) do |status|
46
+ # puts status.user.screen_name, status.text
47
+ # end
48
+ #
49
+ # twitter.timeline_for(:friend, :id => 'myfriend', :since => 30.minutes.ago) do |status|
50
+ # puts status.user.screen_name, status.text
51
+ # end
52
+ #
53
+ # timeline = twitter.timeline_for(:me) do |status|
54
+ # puts status.text
55
+ # end
56
+ #
57
+ # An <tt>ArgumentError</tt> will be raised if an invalid <tt>type</tt>
58
+ # is given. Valid types are:
59
+ # * +:public+
60
+ # * +:friends+
61
+ # * +:friend+
62
+ # * +:user+
63
+ # * +:me+
64
+ def timeline_for(type, options = {}, &block)
65
+ raise ArgumentError, "Invalid timeline type: #{type}" unless @@TIMELINE_URIS.keys.member?(type)
66
+ uri = @@TIMELINE_URIS[type]
67
+ response = http_connect {|conn| create_http_get_request(uri, options) }
68
+ timeline = Twitter::Status.unmarshal(response.body)
69
+ timeline.each {|status| bless_model(status); yield status if block_given? }
70
+ timeline
71
+ end
72
+ end