szimek-twitter 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/History +174 -0
  2. data/License +20 -0
  3. data/Notes +33 -0
  4. data/README.rdoc +13 -0
  5. data/Rakefile +103 -0
  6. data/VERSION.yml +4 -0
  7. data/examples/connect.rb +30 -0
  8. data/examples/friendship_existance.rb +13 -0
  9. data/examples/helpers/config_store.rb +38 -0
  10. data/examples/httpauth.rb +11 -0
  11. data/examples/search.rb +15 -0
  12. data/examples/timeline.rb +19 -0
  13. data/examples/update.rb +11 -0
  14. data/examples/user.rb +5 -0
  15. data/lib/twitter.rb +56 -0
  16. data/lib/twitter/base.rb +165 -0
  17. data/lib/twitter/httpauth.rb +26 -0
  18. data/lib/twitter/no_auth.rb +14 -0
  19. data/lib/twitter/oauth.rb +34 -0
  20. data/lib/twitter/rails/controller.rb +49 -0
  21. data/lib/twitter/rails/helpers.rb +17 -0
  22. data/lib/twitter/rails/initializer.rb +33 -0
  23. data/lib/twitter/request.rb +101 -0
  24. data/lib/twitter/search.rb +106 -0
  25. data/test/fixtures/firehose.json +1 -0
  26. data/test/fixtures/friends_timeline.json +1 -0
  27. data/test/fixtures/rate_limit_exceeded.json +1 -0
  28. data/test/fixtures/replies.json +1 -0
  29. data/test/fixtures/search.json +1 -0
  30. data/test/fixtures/search_from_jnunemaker.json +1 -0
  31. data/test/fixtures/status.json +1 -0
  32. data/test/fixtures/user.json +1 -0
  33. data/test/fixtures/user_timeline.json +1 -0
  34. data/test/test_helper.rb +36 -0
  35. data/test/twitter/base_test.rb +118 -0
  36. data/test/twitter/httpauth_test.rb +50 -0
  37. data/test/twitter/no_auth_test.rb +25 -0
  38. data/test/twitter/oauth_test.rb +71 -0
  39. data/test/twitter/request_test.rb +203 -0
  40. data/test/twitter/search_test.rb +144 -0
  41. data/test/twitter_test.rb +19 -0
  42. metadata +187 -0
@@ -0,0 +1,15 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'twitter')
2
+ require 'pp'
3
+
4
+ search = Twitter::Search.new.from('jnunemaker')
5
+
6
+ puts '*'*50, 'First Run', '*'*50
7
+ search.each { |result| pp result }
8
+
9
+ puts '*'*50, 'Second Run', '*'*50
10
+ search.each { |result| pp result }
11
+
12
+ puts '*'*50, 'Parameter Check', '*'*50
13
+ pp Twitter::Search.new('#austineats').fetch().results.first
14
+ pp Twitter::Search.new('#austineats').page(2).fetch().results.first
15
+ pp Twitter::Search.new('#austineats').since(1412737343).fetch().results.first
@@ -0,0 +1,19 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'twitter')
2
+ require File.join(File.dirname(__FILE__), 'helpers', 'config_store')
3
+ require 'pp'
4
+
5
+ config = ConfigStore.new("#{ENV['HOME']}/.twitter")
6
+
7
+ oauth = Twitter::OAuth.new(config['token'], config['secret'])
8
+ oauth.authorize_from_access(config['atoken'], config['asecret'])
9
+
10
+ client = Twitter::Base.new(oauth)
11
+
12
+ pp client.friends_timeline
13
+ puts '*'*50
14
+
15
+ pp client.user_timeline
16
+ puts '*'*50
17
+
18
+ pp client.replies
19
+ puts '*'*50
@@ -0,0 +1,11 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'twitter')
2
+ require File.join(File.dirname(__FILE__), 'helpers', 'config_store')
3
+ require 'pp'
4
+
5
+ config = ConfigStore.new("#{ENV['HOME']}/.twitter")
6
+
7
+ oauth = Twitter::OAuth.new(config['token'], config['secret'])
8
+ oauth.authorize_from_access(config['atoken'], config['asecret'])
9
+
10
+ client = Twitter::Base.new(oauth)
11
+ pp client.update('This is an update from the twitter gem')
@@ -0,0 +1,5 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'twitter')
2
+ require 'pp'
3
+
4
+ pp Twitter.user('jnunemaker')
5
+ pp Twitter.user('snitch_test')
@@ -0,0 +1,56 @@
1
+ require 'forwardable'
2
+ require 'rubygems'
3
+
4
+ gem 'oauth', '0.3.2'
5
+ require 'oauth'
6
+
7
+ gem 'mash', '0.0.3'
8
+ require 'mash'
9
+
10
+ gem 'httparty', '0.4.2'
11
+ require 'httparty'
12
+
13
+ module Twitter
14
+ class TwitterError < StandardError
15
+ attr_reader :data
16
+
17
+ def initialize(data)
18
+ @data = data
19
+ super
20
+ end
21
+ end
22
+
23
+ class RateLimitExceeded < TwitterError; end
24
+ class Unauthorized < TwitterError; end
25
+ class General < TwitterError; end
26
+
27
+ class Unavailable < StandardError; end
28
+ class InformTwitter < StandardError; end
29
+ class NotFound < StandardError; end
30
+
31
+
32
+ def self.firehose
33
+ response = HTTParty.get('http://twitter.com/statuses/public_timeline.json', :format => :json)
34
+ response.map { |tweet| Mash.new(tweet) }
35
+ end
36
+
37
+ def self.user(id)
38
+ response = HTTParty.get("http://twitter.com/users/show/#{id}.json", :format => :json)
39
+ Mash.new(response)
40
+ end
41
+ end
42
+
43
+ directory = File.dirname(__FILE__)
44
+ $:.unshift(directory) unless $:.include?(directory)
45
+
46
+ require 'twitter/no_auth'
47
+ require 'twitter/oauth'
48
+ require 'twitter/httpauth'
49
+ require 'twitter/request'
50
+ require 'twitter/base'
51
+ require 'twitter/search'
52
+
53
+ if defined? Rails
54
+ require 'twitter/rails/controller'
55
+ require 'twitter/rails/initializer'
56
+ end
@@ -0,0 +1,165 @@
1
+ module Twitter
2
+ class Base
3
+ extend Forwardable
4
+
5
+ def_delegators :client, :get, :post
6
+
7
+ attr_reader :client
8
+
9
+ def initialize(client=Twitter::NoAuth.new)
10
+ @client = client
11
+ end
12
+
13
+ # Options: since_id, max_id, count, page, since
14
+ def friends_timeline(query={})
15
+ perform_get('/statuses/friends_timeline.json', :query => query)
16
+ end
17
+
18
+ # Options: id, user_id, screen_name, since_id, max_id, page, since
19
+ def user_timeline(query={})
20
+ perform_get('/statuses/user_timeline.json', :query => query)
21
+ end
22
+
23
+ def status(id)
24
+ perform_get("/statuses/show/#{id}.json")
25
+ end
26
+
27
+ # Options: in_reply_to_status_id
28
+ def update(status, query={})
29
+ perform_post("/statuses/update.json", :body => {:status => status}.merge(query))
30
+ end
31
+
32
+ # Options: since_id, max_id, since, page
33
+ def replies(query={})
34
+ perform_get('/statuses/replies.json', :query => query)
35
+ end
36
+
37
+ def status_destroy(id)
38
+ perform_post("/statuses/destroy/#{id}.json")
39
+ end
40
+
41
+ # Options: id, user_id, screen_name, page
42
+ def friends(query={})
43
+ perform_get('/statuses/friends.json', :query => query)
44
+ end
45
+
46
+ # Options: id, user_id, screen_name, page
47
+ def followers(query={})
48
+ perform_get('/statuses/followers.json', :query => query)
49
+ end
50
+
51
+ def user(id=nil)
52
+ id ? perform_get("/users/show/#{id}.json") : verify_credentials
53
+ end
54
+
55
+ # Options: since, since_id, page
56
+ def direct_messages(query={})
57
+ perform_get("/direct_messages.json", :query => query)
58
+ end
59
+
60
+ # Options: since, since_id, page
61
+ def direct_messages_sent(query={})
62
+ perform_get("/direct_messages/sent.json", :query => query)
63
+ end
64
+
65
+ def direct_message_create(user, text)
66
+ perform_post("/direct_messages/new.json", :body => {:user => user, :text => text})
67
+ end
68
+
69
+ def direct_message_destroy(id)
70
+ perform_post("/direct_messages/destroy/#{id}.json")
71
+ end
72
+
73
+ def friendship_create(id, follow=false)
74
+ body = {}
75
+ body.merge!(:follow => follow) if follow
76
+ perform_post("/friendships/create/#{id}.json", :body => body)
77
+ end
78
+
79
+ def friendship_destroy(id)
80
+ perform_post("/friendships/destroy/#{id}.json")
81
+ end
82
+
83
+ def friendship_exists?(a, b)
84
+ perform_get("/friendships/exists.json", :query => {:user_a => a, :user_b => b})
85
+ end
86
+
87
+ # Options: id, user_id, screen_name
88
+ def friend_ids(query={})
89
+ perform_get("/friends/ids.json", :query => query)
90
+ end
91
+
92
+ # Options: id, user_id, screen_name
93
+ def follower_ids(query={})
94
+ perform_get("/followers/ids.json", :query => query)
95
+ end
96
+
97
+ def verify_credentials
98
+ perform_get("/account/verify_credentials.json")
99
+ end
100
+
101
+ # Device must be sms, im or none
102
+ def update_delivery_device(device)
103
+ perform_post('/account/update_delivery_device.json', :body => {:device => device})
104
+ end
105
+
106
+ # One or more of the following must be present:
107
+ # profile_background_color, profile_text_color, profile_link_color,
108
+ # profile_sidebar_fill_color, profile_sidebar_border_color
109
+ def update_profile_colors(colors={})
110
+ perform_post('/account/update_profile_colors.json', :body => colors)
111
+ end
112
+
113
+ def rate_limit_status
114
+ perform_get('/account/rate_limit_status.json')
115
+ end
116
+
117
+ # One or more of the following must be present:
118
+ # name, email, url, location, description
119
+ def update_profile(body={})
120
+ perform_post('/account/update_profile.json', :body => body)
121
+ end
122
+
123
+ # Options: id, page
124
+ def favorites(query={})
125
+ perform_get('/favorites.json', :query => query)
126
+ end
127
+
128
+ def favorite_create(id)
129
+ perform_post("/favorites/create/#{id}.json")
130
+ end
131
+
132
+ def favorite_destroy(id)
133
+ perform_post("/favorites/destroy/#{id}.json")
134
+ end
135
+
136
+ def enable_notifications(id)
137
+ perform_post("/notifications/follow/#{id}.json")
138
+ end
139
+
140
+ def disable_notifications(id)
141
+ perform_post("/notifications/leave/#{id}.json")
142
+ end
143
+
144
+ def block(id)
145
+ perform_post("/blocks/create/#{id}.json")
146
+ end
147
+
148
+ def unblock(id)
149
+ perform_post("/blocks/destroy/#{id}.json")
150
+ end
151
+
152
+ def help
153
+ perform_get('/help/test.json')
154
+ end
155
+
156
+ private
157
+ def perform_get(path, options={})
158
+ Twitter::Request.get(self, path, options)
159
+ end
160
+
161
+ def perform_post(path, options={})
162
+ Twitter::Request.post(self, path, options)
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,26 @@
1
+ module Twitter
2
+ class HTTPAuth
3
+ include HTTParty
4
+ base_uri 'http://twitter.com'
5
+ format :plain
6
+
7
+ attr_reader :username, :password
8
+
9
+ def initialize(username, password)
10
+ @username, @password = username, password
11
+ end
12
+
13
+ def get(uri, headers={})
14
+ self.class.get(uri, :headers => headers, :basic_auth => basic_auth)
15
+ end
16
+
17
+ def post(uri, body={}, headers={})
18
+ self.class.post(uri, :body => body, :headers => headers, :basic_auth => basic_auth)
19
+ end
20
+
21
+ private
22
+ def basic_auth
23
+ @basic_auth ||= {:username => @username, :password => @password}
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ module Twitter
2
+ class NoAuth
3
+ include HTTParty
4
+ base_uri 'http://twitter.com'
5
+
6
+ def get(uri, headers={})
7
+ self.class.get(uri, :headers => headers)
8
+ end
9
+
10
+ def post(uri, body={}, headers={})
11
+ self.class.post(uri, :body => body, :headers => headers)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ module Twitter
2
+ class OAuth
3
+ extend Forwardable
4
+ def_delegators :access_token, :get, :post
5
+
6
+ attr_reader :ctoken, :csecret
7
+
8
+ def initialize(ctoken, csecret)
9
+ @ctoken, @csecret = ctoken, csecret
10
+ end
11
+
12
+ def consumer
13
+ @consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, {:site => 'http://twitter.com'})
14
+ end
15
+
16
+ def request_token
17
+ @request_token ||= consumer.get_request_token
18
+ end
19
+
20
+ def authorize_from_request(rtoken, rsecret)
21
+ request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret)
22
+ access_token = request_token.get_access_token
23
+ @atoken, @asecret = access_token.token, access_token.secret
24
+ end
25
+
26
+ def access_token
27
+ @access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret)
28
+ end
29
+
30
+ def authorize_from_access(atoken, asecret)
31
+ @atoken, @asecret = atoken, asecret
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,49 @@
1
+ module Twitter
2
+ module Rails
3
+ module Controller
4
+ def self.included?(controller)
5
+ #controller.helper_method :twitter_session, :logged_in_via_twitter?
6
+ end
7
+
8
+ protected
9
+ def twitter_session
10
+ @twitter_session
11
+ end
12
+
13
+ def create_twitter_session
14
+ request_access_token if request_token_available?
15
+
16
+ if access_token_available?
17
+ twitter_oauth.authorize_from_access(session['twitter_atoken'], session['twitter_asecret'])
18
+ @twitter_session = Twitter::Base.new(twitter_oauth)
19
+ end
20
+ end
21
+
22
+ def logged_in_via_twitter?
23
+ !!twitter_session
24
+ end
25
+
26
+ def request_access_token
27
+ oauth = self.twitter_oauth
28
+ oauth.authorize_from_request(session['twitter_rtoken'], session['twitter_rsecret'])
29
+
30
+ session['twitter_rtoken'] = nil
31
+ session['twitter_rsecret'] = nil
32
+ session['twitter_atoken'] = oauth.access_token.token
33
+ session['twitter_asecret'] = oauth.access_token.secret
34
+ end
35
+
36
+ def request_token_available?
37
+ session['twitter_rtoken'] && session['twitter_rsecret']
38
+ end
39
+
40
+ def access_token_available?
41
+ session['twitter_atoken'] && session['twitter_asecret']
42
+ end
43
+
44
+ def twitter_oauth
45
+ @twitter_oauth ||= Twitter::OAuth.new(ENV['TWITTER_CONSUMER_TOKEN'], ENV['TWITTER_CONSUMER_SECRET'])
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,17 @@
1
+ module Twitter
2
+ module Rails
3
+ module Helpers
4
+
5
+ def twitter_profile_pic(user, options={})
6
+ id = user.respond_to?(:twitter_id) ? user.twitter_id : user
7
+ begin
8
+ image_url = Twitter::Base.new.user(id).profile_image_url
9
+ rescue
10
+ image_url = "http://static.twitter.com/images/default_profile_normal.png"
11
+ end
12
+ image_tag(image_url, options)
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ module Twitter
2
+ module Rails
3
+ class << self
4
+ def load_configuration(twitter_yaml_file)
5
+ twitter_yaml_file = ::Rails.root + 'config' + 'twitter.yml'
6
+
7
+ if File.exists?(twitter_yaml_file)
8
+ twitter_config = YAML.load(File.read(twitter_yaml_file))[RAILS_ENV]
9
+ ENV['TWITTER_CONSUMER_TOKEN'] = twitter_config['token']
10
+ ENV['TWITTER_CONSUMER_SECRET'] = twitter_config['secret']
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ class ActionController::Base
18
+ class << self
19
+ def inherited_with_twitter(subclass)
20
+ inherited_without_twitter(subclass)
21
+ if subclass.to_s == "ApplicationController"
22
+ subclass.class_eval do
23
+ include Twitter::Rails::Controller
24
+ helper Twitter::Rails::Helpers
25
+ end
26
+ end
27
+ end
28
+ alias_method_chain :inherited, :twitter
29
+ end
30
+ end
31
+
32
+ twitter_config = Rails.root + 'config' + 'twitter.yml'
33
+ Twitter::Rails.load_configuration(twitter_config)