dambalah-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.rb +31 -0
  6. data/lib/twitter/client.rb +21 -0
  7. data/lib/twitter/client/account.rb +24 -0
  8. data/lib/twitter/client/auth.rb +27 -0
  9. data/lib/twitter/client/base.rb +87 -0
  10. data/lib/twitter/client/blocks.rb +35 -0
  11. data/lib/twitter/client/favorites.rb +53 -0
  12. data/lib/twitter/client/friendship.rb +47 -0
  13. data/lib/twitter/client/messaging.rb +79 -0
  14. data/lib/twitter/client/status.rb +46 -0
  15. data/lib/twitter/client/timeline.rb +72 -0
  16. data/lib/twitter/client/user.rb +87 -0
  17. data/lib/twitter/config.rb +71 -0
  18. data/lib/twitter/console.rb +28 -0
  19. data/lib/twitter/core.rb +137 -0
  20. data/lib/twitter/ext.rb +2 -0
  21. data/lib/twitter/ext/stdlib.rb +51 -0
  22. data/lib/twitter/extras.rb +39 -0
  23. data/lib/twitter/meta.rb +56 -0
  24. data/lib/twitter/model.rb +344 -0
  25. data/lib/twitter/rails.rb +92 -0
  26. data/lib/twitter/version.rb +19 -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 +109 -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.
@@ -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,31 @@
1
+ #
2
+
3
+ module Twitter; end
4
+
5
+ def require_local(suffix)
6
+ require(File.expand_path(File.join(File.dirname(__FILE__), suffix)))
7
+ end
8
+
9
+ # For better unicode support
10
+ $KCODE = 'u'
11
+ require 'jcode'
12
+
13
+ # External requires
14
+ require('yaml')
15
+ require('date')
16
+ require('time')
17
+ require('net/https')
18
+ require('uri')
19
+ require('cgi')
20
+ require('json')
21
+ require('yaml')
22
+
23
+ # Ordering matters...pay attention here!
24
+ require_local('twitter/ext')
25
+ require_local('twitter/version')
26
+ require_local('twitter/meta')
27
+ require_local('twitter/core')
28
+ require_local('twitter/model')
29
+ require_local('twitter/config')
30
+ require_local('twitter/client')
31
+ require_local('twitter/console')
@@ -0,0 +1,21 @@
1
+ # client.rb contains the classes, methods and extends <tt>Twitter4R</tt>
2
+ # features to define client calls to the Twitter REST API.
3
+ #
4
+ # See:
5
+ # * <tt>Twitter::Client</tt>
6
+
7
+ # Used to query or post to the Twitter REST API to simplify code.
8
+ class Twitter::Client
9
+ include Twitter::ClassUtilMixin
10
+ end
11
+
12
+ require('twitter/client/base.rb')
13
+ require('twitter/client/timeline.rb')
14
+ require('twitter/client/status.rb')
15
+ require('twitter/client/friendship.rb')
16
+ require('twitter/client/messaging.rb')
17
+ require('twitter/client/user.rb')
18
+ require('twitter/client/auth.rb')
19
+ require('twitter/client/favorites.rb')
20
+ require('twitter/client/blocks.rb')
21
+ require('twitter/client/account.rb')
@@ -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