dferranti-twitter4r 0.4.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/CHANGES +129 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +37 -0
  4. data/TODO +7 -0
  5. data/bin/t4rsh +80 -0
  6. data/lib/twitter.rb +34 -0
  7. data/lib/twitter/client.rb +24 -0
  8. data/lib/twitter/client/account.rb +24 -0
  9. data/lib/twitter/client/auth.rb +27 -0
  10. data/lib/twitter/client/base.rb +96 -0
  11. data/lib/twitter/client/blocks.rb +35 -0
  12. data/lib/twitter/client/favorites.rb +53 -0
  13. data/lib/twitter/client/friendship.rb +35 -0
  14. data/lib/twitter/client/graph.rb +37 -0
  15. data/lib/twitter/client/messaging.rb +79 -0
  16. data/lib/twitter/client/profile.rb +29 -0
  17. data/lib/twitter/client/search.rb +27 -0
  18. data/lib/twitter/client/status.rb +51 -0
  19. data/lib/twitter/client/timeline.rb +72 -0
  20. data/lib/twitter/client/user.rb +65 -0
  21. data/lib/twitter/config.rb +77 -0
  22. data/lib/twitter/console.rb +31 -0
  23. data/lib/twitter/core.rb +137 -0
  24. data/lib/twitter/ext.rb +2 -0
  25. data/lib/twitter/ext/stdlib.rb +52 -0
  26. data/lib/twitter/extras.rb +39 -0
  27. data/lib/twitter/meta.rb +56 -0
  28. data/lib/twitter/model.rb +360 -0
  29. data/lib/twitter/version.rb +19 -0
  30. data/spec/twitter/client/account_spec.rb +28 -0
  31. data/spec/twitter/client/auth_spec.rb +34 -0
  32. data/spec/twitter/client/base_spec.rb +242 -0
  33. data/spec/twitter/client/blocks_spec.rb +76 -0
  34. data/spec/twitter/client/favorites_spec.rb +183 -0
  35. data/spec/twitter/client/friendship_spec.rb +76 -0
  36. data/spec/twitter/client/graph_spec.rb +67 -0
  37. data/spec/twitter/client/messaging_spec.rb +135 -0
  38. data/spec/twitter/client/profile_spec.rb +91 -0
  39. data/spec/twitter/client/search_spec.rb +68 -0
  40. data/spec/twitter/client/status_spec.rb +119 -0
  41. data/spec/twitter/client/timeline_spec.rb +79 -0
  42. data/spec/twitter/client/user_spec.rb +203 -0
  43. data/spec/twitter/client_spec.rb +2 -0
  44. data/spec/twitter/config_spec.rb +86 -0
  45. data/spec/twitter/console_spec.rb +15 -0
  46. data/spec/twitter/core_spec.rb +127 -0
  47. data/spec/twitter/ext/stdlib_spec.rb +59 -0
  48. data/spec/twitter/extras_spec.rb +46 -0
  49. data/spec/twitter/meta_spec.rb +90 -0
  50. data/spec/twitter/model_spec.rb +508 -0
  51. data/spec/twitter/version_spec.rb +19 -0
  52. metadata +115 -0
data/CHANGES ADDED
@@ -0,0 +1,129 @@
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
+ *
7
+ * Added specs for Twitter::Client#profile (:info, :colors and :device cases)
8
+ * Added Twitter4R shell
9
+ * Improved code coverage for twitter/ext/stdlib code and removed hard coded extension in GET request path for retrieving account information
10
+ * Changed Twitter::RESTError super class to be RuntimeError instead of Exception
11
+ * Added URI.encode => CGI.escape fix
12
+ * Added block methods
13
+ * Added Twitter::Client#inspect method to XXXX out passwords
14
+ * Minor refactoring of spec/spec_helper.rb
15
+ * Added support for :page, :lite and :since options for Twitter::User#followers, Twitter::Client#my([:friends|:followers]...) and Twitter::Client#user([:friends|:followers]...)calls
16
+ * Added Twitter::Client.status(:replies) support
17
+
18
+
19
+ == 0.3.0 Changes
20
+ * Bunch of changes, but forgot to track them and too lazy to look through the SVN/Git logs. Sorry.
21
+
22
+ == 0.2.5 Changes
23
+
24
+ === 2007-09-23
25
+ * Added paging support as per Sergio Santos' request (#36).
26
+
27
+ == 0.2.4 Changes
28
+
29
+ === 2007-07-24
30
+ * Fixed ActiveSupport +Time#to_s+ conflict such that integration with Rails is much less painful (#34)
31
+
32
+ == 0.2.3 Changes
33
+
34
+ === 2007-07-22
35
+ * Fixed defect #31 such that passing string screen name as for user argument is handled correctly.
36
+ * Fixed #30 typo: respond_to -> respond_to?
37
+ * Added relevant exception handling for #message(:post, ...) case (#32)
38
+ * Add ability to pass in Twitter::User object to Twitter::Client#user(...) #33
39
+ * Added stats Rake task
40
+ * 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.
41
+
42
+ == 0.2.2 Changes
43
+
44
+ === 2007-07-18
45
+ * Fixed URI paths for user, messaging and friendship APIs (#25)
46
+ * Added action checks for Twitter::Client methods: #user, #my, #message, #messages, #status, #timeline, #friend (#26)
47
+ * Added 'source' configuration documentation.
48
+ * Added missing attributes for Twitter::User (#28)
49
+
50
+ == 0.2.1 Changes
51
+
52
+ === 2007-07-17
53
+ * Added 'source' feature and configurability.
54
+
55
+ == 0.2.0 Changes
56
+
57
+ === 2007-07-08
58
+ * Added featured users API as an "extra" (#19).
59
+ * Productionized website for publishing.
60
+ * Published Ruby Gem on Rubyforge.
61
+
62
+ === 2007-07-07
63
+ * Refactored Twitter4R API to be more consistent, by grouping APIs (#6):
64
+ - Messaging APIs: direct_messages, new, destroy, replies
65
+ - Friendship APIs: create, destroy
66
+ * Added following features (#7):
67
+ - Retrieving direct messages
68
+ - User APIs: friends, followers, show
69
+ * Updated documentation and example code.
70
+
71
+ === 2007-07-06
72
+ * Refactored Twitter4R API to be more consistent, by grouping APIs (#6):
73
+ - Status APIs: show, update, destroy
74
+ - User APIs: friends, followers, show
75
+ * Added X-Twitter-Client HTTP headers and Twitter::Config options (#16)
76
+ * Removed redundant feature (#8):
77
+ - Followers timeline
78
+ * Refactored HTTP request/response code to DRY up code.
79
+ * Fix REST error handling to use #is_a?(HTTPSuccess) instead of code in ['200', '201'] to determine REST error (#15).
80
+
81
+ === 2007-06-25
82
+ * Updated example documentation (#14)
83
+ * Refactored marshaling unmarshaling code (#13)
84
+
85
+ === 2007-06-20
86
+ * Added proxy user/pass support. Tested only via endo-testing. (No system/integration testing behind real proxy as I do not have that environment).
87
+
88
+
89
+ === 2007-06-17
90
+ * Refactored Twitter4R API to be more consistent, by grouping APIs (#6):
91
+ - Timeline APIs: public, friends, user
92
+
93
+ === 2007-06-13
94
+ * Added RSpec Autotest integration
95
+ * Fixed Twitter::Meta generation of spec for hash values
96
+ * Added HTTP header to each request including generated User-Agent header
97
+ * Added RCovMorpher and template to restyle RCov output upon release
98
+ * Added Gemspec dependencies and requirements
99
+ * Added default tidy YAML configuration file for RCovMorpher
100
+ * Added Contributors list and updated external dependencies list to README
101
+ * Removed shebang from examples
102
+
103
+ === 2007-06-12
104
+ * Added proxy support as per Kaiichi Matsunaga submitted patch (#11).
105
+ * Added SSL support (#12)
106
+
107
+ === 2007-05-19
108
+ * Translated RSpec specifications from 0.8.2 compliant to 1.0.0 (#10)
109
+
110
+ == 0.1.1 Changes
111
+
112
+ === 2007-06-25
113
+ * Added SSL support (#12)
114
+ * Added Proxy support (#11)
115
+
116
+ == 0.1.0 Changes
117
+
118
+ === 2007-05-08
119
+ * Added Google Analytics Javascript code to website pages (#5)
120
+
121
+ === 2007-05-07
122
+ * Fixed errors in online sample code documentation and redeployed website (#2 and #3)
123
+ * Created more consistent RDoc theme to go more with website home page (#4)
124
+
125
+ === 2007-05-06
126
+ * Initial revision of codebase commited; includes:
127
+ - Achieved 80% Twitter API feature-completeness
128
+ - Attained 100% RSpec C0 code coverage
129
+ - 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,37 @@
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
+ Code:
11
+ * Kaiichi Matsunaga <ma2 at lifemedia dot co dot jp> - proxy code suggestion
12
+ * Sergio Santos <> - message paging code suggestion
13
+ * Adam Stiles <adam at stilesoft dot com> - URI.encode => CGI.escape fix
14
+ * Carl Crawley <cwcrawley at gmail dot com> - Friendship get => post fix
15
+ * Christian Johansen <christian at cjohansen dot no> - in_reply_to attributes in Twitter::Status
16
+
17
+ Design Suggestions:
18
+ * Bosco So <rubymeetup at boscoso dot com> - making Twitter::Error a RuntimeError instead of an Exception to prevent irb from crashing out.
19
+
20
+ == Description
21
+ 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.
22
+
23
+ == External Dependencies
24
+ * Ruby 1.8 (tested with 1.8.6)
25
+ * RSpec gem 1.0.0+ (tested with 1.1.3)
26
+ * JSON gem 0.4.3+ (tested with versions: 1.1.1 and 1.1.2)
27
+ * jcode (for unicode support)
28
+
29
+ == Usage Examples
30
+ Twitter4R starting with version 0.1.1 and above is organized into seven parts:
31
+ * {Configuration API}[link:files/examples/configure_rb.html]
32
+ * {Friendship API}[link:files/examples/friendship_rb.html]
33
+ * {Messaging API}[link:files/examples/messaging_rb.html]
34
+ * {Model API}[link:files/examples/model_rb.html]
35
+ * {Status API}[link:files/examples/status_rb.html]
36
+ * {Timeline API}[link:files/examples/timeline_rb.html]
37
+ * {User API}[link:files/examples/user_rb.html]
data/TODO ADDED
@@ -0,0 +1,7 @@
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 :replies support in Twitter::Client#status(...) and Twitter::Client#timeline_for(...)
5
+ * Add RDoc for :replies support
6
+ * Add better RDoc for Twitter::Client.account_info(:rate_limit_status)
7
+ * Add specs for Twitter::Client.account_info(:rate_limit_status)
data/bin/t4rsh ADDED
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require("irb")
4
+ require("irb/completion")
5
+ require("rubygems")
6
+
7
+ begin
8
+ gem('twitter4r', '>0.3.0')
9
+ require("twitter")
10
+ require("twitter/console")
11
+ rescue Gem::LoadError
12
+ begin
13
+ gem("mbbx6spp-twitter4r", '>=0.3.1')
14
+ require("twitter")
15
+ require("twitter/console")
16
+ rescue Gem::LoadError
17
+ abort("Error: You must install either twitter4r gem from Rubyforge with version 0.3.1 or greater or the mbbx6spp-twitter4r gem from GitHub's servers with version 0.3.1 or greater (and make sure it is a recent version of the gem).")
18
+ end
19
+ end
20
+
21
+ module Twitter
22
+ class Console
23
+ class << self
24
+ @@OPTIONS = { :debugger => false, :config => "~/.twitter4r/accounts.yml" }
25
+ def parse_options
26
+ OptionParser.new do |opt|
27
+ opt.banner = "Usage: t4rsh [environment] [options]"
28
+ opt.on("--config=[~/.twitter4r/accounts.yml]", 'Use a specific config file.') { |v| @@OPTIONS[:config] = v }
29
+ opt.parse!(ARGV)
30
+ end
31
+ end
32
+
33
+ def config_file
34
+ result = ENV["T4R_CONFIG"]
35
+ file_name = File.expand_path('twitter.yml')
36
+ result ||= file_name if File.exists?(file_name)
37
+ file_name = File.expand_path('twitter.yml', 'config')
38
+ result ||= file_name if File.exists?(file_name)
39
+ file_name = File.expand_path('~/.twitter.yml')
40
+ result ||= file_name if File.exists?(file_name)
41
+ result
42
+ end
43
+
44
+ def account
45
+ ENV["T4R_ENV"] || ENV["MERB_ENV"] || ENV["RAILS_ENV"]
46
+ end
47
+
48
+ def run(file)
49
+ IRB.init_config(nil)
50
+ # configuration...
51
+ IRB.conf[:IRB_NAME] = "t4rsh"
52
+ IRB.conf[:VERSION] = Twitter::Version.to_version
53
+ IRB.conf[:USE_READLINE] = true
54
+ IRB.conf[:PROMPT_MODE] = :T4RSH
55
+ IRB.conf[:PROMPT][:T4RSH] = {
56
+ :PROMPT_I => "%N[%3n:%i]> ", # top level prompt
57
+ :PROMPT_C => "%N[%3n:%i]* ", # after conditional like "if"
58
+ :PROMPT_S => "%N[%3n:%i]* ", # during continuing string
59
+ :RETURN => "=> %s\n", # return value
60
+ }
61
+ IRB.start(file)
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ #if __FILE__ == $0
68
+ @twitter = nil
69
+ config_file = Twitter::Console.config_file
70
+ account = Twitter::Console.account
71
+
72
+ if config_file && account
73
+ @twitter = Twitter::Client.from_config(config_file, account)
74
+ puts "Used #{config_file} to create client for #{account} account."
75
+ puts "Access @twitter for instantiated client."
76
+ Twitter::Console.run(__FILE__)
77
+ else
78
+ abort("Please make sure #{config_file} exists and contains your Twitter credentials (separated by account/environment) and that you specify the account/environment to use, e.g. if you have a 'test' section in your configuration file that you want to use set/export T4R_ENV=test as an environment variable or RAILS_ENV=test or MERB_ENV=test")
79
+ end
80
+ #end
data/lib/twitter.rb ADDED
@@ -0,0 +1,34 @@
1
+ #
2
+ require('rubygems')
3
+
4
+ module Twitter; end
5
+
6
+ def require_local(suffix)
7
+ require(File.expand_path(File.join(File.dirname(__FILE__), suffix)))
8
+ end
9
+
10
+ # For better unicode support in 1.8
11
+ if RUBY_VERSION < '1.9'
12
+ $KCODE = 'u'
13
+ require 'jcode'
14
+ end
15
+
16
+ # External requires
17
+ require('yaml')
18
+ require('date')
19
+ require('time')
20
+ require('net/https')
21
+ require('uri')
22
+ require('cgi')
23
+ require('json')
24
+ require('yaml')
25
+
26
+ # Ordering matters...pay attention here!
27
+ require_local('twitter/ext')
28
+ require_local('twitter/version')
29
+ require_local('twitter/meta')
30
+ require_local('twitter/core')
31
+ require_local('twitter/model')
32
+ require_local('twitter/config')
33
+ require_local('twitter/client')
34
+ require_local('twitter/console')
@@ -0,0 +1,24 @@
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')
13
+ require('twitter/client/timeline')
14
+ require('twitter/client/status')
15
+ require('twitter/client/friendship')
16
+ require('twitter/client/messaging')
17
+ require('twitter/client/user')
18
+ require('twitter/client/auth')
19
+ require('twitter/client/favorites')
20
+ require('twitter/client/blocks')
21
+ require('twitter/client/account')
22
+ require('twitter/client/graph')
23
+ require('twitter/client/profile')
24
+ require('twitter/client/search')
@@ -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])
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,96 @@
1
+ class Twitter::Client
2
+ alias :old_inspect :inspect
3
+
4
+ def inspect
5
+ s = old_inspect
6
+ s.gsub!(/@password=".*?"/, '@password="XXXX"')
7
+ end
8
+
9
+ protected
10
+ attr_accessor :login, :password
11
+
12
+ # Returns the response of the HTTP connection.
13
+ def http_connect(body = nil, require_auth = true, service = :rest, &block)
14
+ require_block(block_given?)
15
+ connection = create_http_connection(service)
16
+ connection.start do |connection|
17
+ request = yield connection if block_given?
18
+ request.basic_auth(@login, @password) if require_auth
19
+ response = connection.request(request, body)
20
+ handle_rest_response(response)
21
+ response
22
+ end
23
+ end
24
+
25
+ # "Blesses" model object with client information
26
+ def bless_model(model)
27
+ model.bless(self) if model
28
+ end
29
+
30
+ def bless_models(list)
31
+ return bless_model(list) if list.respond_to?(:client=)
32
+ list.collect { |model| bless_model(model) } if list.respond_to?(:collect)
33
+ end
34
+
35
+ private
36
+ @@http_header = nil
37
+
38
+ def raise_rest_error(response, uri = nil)
39
+ map = JSON.parse(response.body)
40
+ raise Twitter::RESTError.new(:code => response.code,
41
+ :message => response.message,
42
+ :error => map["error"],
43
+ :uri => uri)
44
+ end
45
+
46
+ def handle_rest_response(response, uri = nil)
47
+ unless response.is_a?(Net::HTTPSuccess)
48
+ raise_rest_error(response, uri)
49
+ end
50
+ end
51
+
52
+ def create_http_connection(service = :rest)
53
+ case service
54
+ when :rest
55
+ protocol, host, port = @@config.protocol, @@config.host, @@config.port
56
+ when :search
57
+ protocol, host, port = @@config.search_protocol, @@config.search_host, @@config.search_port
58
+ end
59
+ conn = Net::HTTP.new(host, port,
60
+ @@config.proxy_host, @@config.proxy_port,
61
+ @@config.proxy_user, @@config.proxy_pass)
62
+ if protocol == :ssl
63
+ conn.use_ssl = true
64
+ conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
65
+ end
66
+ conn
67
+ end
68
+
69
+ def http_header
70
+ # can cache this in class variable since all "variables" used to
71
+ # create the contents of the HTTP header are determined by other
72
+ # class variables that are not designed to change after instantiation.
73
+ @@http_header ||= {
74
+ 'User-Agent' => "Twitter4R v#{Twitter::Version.to_version} [#{@@config.user_agent}]",
75
+ 'Accept' => 'text/x-json',
76
+ 'X-Twitter-Client' => @@config.application_name,
77
+ 'X-Twitter-Client-Version' => @@config.application_version,
78
+ 'X-Twitter-Client-URL' => @@config.application_url,
79
+ }
80
+ @@http_header
81
+ end
82
+
83
+ def create_http_get_request(uri, params = {})
84
+ path = (params.size > 0) ? "#{uri}?#{params.to_http_str}" : uri
85
+ Net::HTTP::Get.new(path, http_header)
86
+ end
87
+
88
+ def create_http_post_request(uri)
89
+ Net::HTTP::Post.new(uri, http_header)
90
+ end
91
+
92
+ def create_http_delete_request(uri, params = {})
93
+ path = (params.size > 0) ? "#{uri}?#{params.to_http_str}" : uri
94
+ Net::HTTP::Delete.new(path, http_header)
95
+ end
96
+ end