jugyo-twitter_oauth 0.5.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2613a1a8e813111ff42327cf22471949e516fa9d
4
+ data.tar.gz: d5cc2bd9e5bf09fbd87cec2d5a932ae8d8f24720
5
+ SHA512:
6
+ metadata.gz: 80fe3be97ea3a0ce64a5531e3e90bc1825fde379cfa4292d27412152b67d07e078480e760dfff7026e9b5c2118b7c60e4a724454e757fdbd8753c93162685ee2
7
+ data.tar.gz: 4afbdf8f7bfff81e93bdeb86233fc435023cac8bb61b1bd51a05c2d4fa02a661d0a89fced06689e257db9f1418d84180484adb172b3c8ae2e21f17f4164386cc
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ mytest.rb
2
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) Richard Taylor
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
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,159 @@
1
+ h1. Twitter OAuth REST API client library for Ruby
2
+
3
+ h2. Install the gem
4
+
5
+ sudo gem install twitter_oauth # (via gemcutter)
6
+
7
+ h2. Using the gem
8
+
9
+ To make authorized requests with the client library you'll need to "create a Twitter OAuth Application":http://twitter.com/oauth_clients/new.
10
+
11
+ See "sinitter":http://github.com/moomerman/sinitter/tree/master for a full website integration example.
12
+
13
+ h2. Unauthorized request example
14
+
15
+ The Twitter API can be called to make public requests without needing any client credentials.
16
+ Most methods will not work in this mode but some of them do. An example to retrieve the public details
17
+ about a Twitter user is below.
18
+
19
+ <pre><code>client = TwitterOAuth::Client.new
20
+
21
+ puts client.show('twitter')
22
+ => => {"status"=>{"truncated"=>false, "favorited"=>false, "text"=>"Update on service issues http://tinyurl.com/ca872j", "id"=>1357776683, "in_reply_to_user_id"=>nil, "in_reply_to_status_id"=>nil, "source"=>"<a href=\"http://twitterfeed.com\">twitterfeed</a>", "created_at"=>"Fri Mar 20 01:17:35 +0000 2009"}, "name"=>"Twitter", "profile_sidebar_fill_color"=>"CDFFFF", "profile_sidebar_border_color"=>"8a6447", "profile_background_tile"=>false, "profile_link_color"=>"0000ff", "url"=>"http://twitter.com", "favourites_count"=>0, "id"=>783214, "description"=>"Always wondering what everyone's doing.", "profile_text_color"=>"000000", "protected"=>false, "utc_offset"=>-28800, "screen_name"=>"twitter", "profile_background_color"=>"9ae4e8", "time_zone"=>"Pacific Time (US & Canada)", "followers_count"=>469150, "profile_background_image_url"=>"http://static.twitter.com/images/themes/theme1/bg.gif", "friends_count"=>30, "statuses_count"=>290, "location"=>"San Francisco, CA", "profile_image_url"=>"http://s3.amazonaws.com/twitter_production/profile_images/75075164/twitter_bird_profile_normal.png", "created_at"=>"Tue Feb 20 14:35:54 +0000 2007"}
23
+ </code></pre>
24
+
25
+ You can also access to the search API which is available in either authorized or unauthorized modes.
26
+
27
+ <pre><code>search = client.search('twitter')
28
+ search.results.size => 20
29
+ search.results.first.from_user => "josephpred"
30
+ search.results.first.text
31
+ => "Useful public service Twitter account for those of you hitting Tahoe or just needing to cross the pass to Reno: @i80chains"
32
+ </code></pre>
33
+
34
+ h2. Authorized request example
35
+
36
+ To use the full power of the Twitter API you need to authorize your application and a valid Twitter user via OAuth.
37
+ An example showing how to update the status of an authorized user is below.
38
+
39
+ Firstly we need to create an instance of the client with your application client credentials you have been given by Twitter
40
+ when you set up your application.
41
+
42
+ <pre><code>client = TwitterOAuth::Client.new(
43
+ :consumer_key => 'YOUR_APP_CONSUMER_KEY',
44
+ :consumer_secret => 'YOUR_APP_CONSUMER_SECRET'
45
+ )
46
+ request_token = client.request_token(:oauth_callback => oauth_confirm_url)
47
+ #:oauth_callback required for web apps, since oauth gem by default force PIN-based flow
48
+ #( see http://groups.google.com/group/twitter-development-talk/browse_thread/thread/472500cfe9e7cdb9/848f834227d3e64d )
49
+
50
+
51
+ request_token.authorize_url
52
+ => http://twitter.com/oauth/authorize?oauth_token=TOKEN
53
+ </code></pre>
54
+
55
+ In your application your user would be redirected to Twitter to authorize the application at this point. You'll need to store
56
+ the request token (usually in the session) for later. The code continues below assuming the user has authorized your application.
57
+
58
+ NOTE: Above we called the <code>client.request_token(...)</code> method, this authorizes the application on every call. You can also use the <code>client.authentication_request_token(...)</code> method which automatically redirects back to your application if the user has previously authorized the app.
59
+
60
+ <pre><code>access_token = client.authorize(
61
+ request_token.token,
62
+ request_token.secret,
63
+ :oauth_verifier => params[:oauth_verifier]
64
+ )
65
+
66
+ client.authorized?
67
+ => true
68
+
69
+ client.update('checking out the twitter_oauth library') # sends a twitter status update
70
+ </code></pre>
71
+
72
+ Now if you keep hold of the access_token (usually in the database) for this user you won't need to re-authorize them next time. When you create an instance of the client you can just pass in the access token and secret that you have stored.
73
+
74
+ <pre><code>access_token = @user.access_token # assuming @user
75
+ client = TwitterOAuth::Client.new(
76
+ :consumer_key => 'YOUR_CONSUMER_KEY',
77
+ :consumer_secret => 'YOUR_APP_CONSUMER_SECRET',
78
+ :token => access_token.token,
79
+ :secret => access_token.secret
80
+ )
81
+
82
+ client.authorized?
83
+ => true
84
+ </code></pre>
85
+
86
+ h2. PIN-based flow
87
+
88
+ If you're writing a command line application or desktop app, you will probably want to use the PIN-based authorization method rather than the website redirect method.
89
+
90
+ <pre><code>client = TwitterOAuth::Client.new(
91
+ :consumer_key => 'YOUR_CONSUMER_KEY',
92
+ :consumer_secret => 'YOUR_APP_CONSUMER_SECRET'
93
+ )
94
+
95
+ request_token = client.authentication_request_token(
96
+ :oauth_callback => 'oob'
97
+ )
98
+
99
+ puts request_token.authorize_url
100
+
101
+ print 'Please visit the URL and enter the code: '
102
+ code = gets.strip
103
+
104
+ access_token = client.authorize(
105
+ request_token.token,
106
+ request_token.secret,
107
+ :oauth_verifier => code
108
+ )
109
+
110
+ client.authorized?
111
+ => true
112
+ </code></pre>
113
+
114
+ The special oauth callback value of <code>oob</code> tells Twitter you want to do the PIN-based authentication. The user goes to the authorization URL to get their unique code and they paste that into your application. Finally we authorize the user with this code.
115
+
116
+ h2. Working with a Proxy
117
+
118
+ Services such as "Apigee Analytics and API Management":http://apigee.com/ require you to proxy your API requests through their servers. The workflow is as follows.
119
+
120
+ First you need to authorize the Twitter user via OAuth directly via the Twitter API (this part cannot be proxied)
121
+
122
+ <pre><code>client = TwitterOAuth::Client.new(
123
+ :consumer_key => 'YOUR_APP_CONSUMER_KEY',
124
+ :consumer_secret => 'YOURA_APP_CONSUMER_SECRET'
125
+ )
126
+ request_token = client.request_token(:oauth_callback => 'YOUR_CALLBACK_URL')
127
+
128
+ request_token.authorize_url
129
+ => http://twitter.com/oauth/authorize?oauth_token=TOKEN
130
+ </code></pre>
131
+
132
+ The user is sent to Twitter to allow the application to access their account and then you need to obtain the access token for the user
133
+
134
+ <pre><code>access_token = client.authorize(
135
+ request_token.token,
136
+ request_token.secret,
137
+ :oauth_verifier => params[:oauth_verifier]
138
+ )
139
+
140
+ client.authorized?
141
+ => true
142
+ </code></pre>
143
+
144
+ Now you can make all further API calls through the proxy of your choice:
145
+
146
+ <pre><code>access_token = @user.access_token # assuming @user
147
+ client = TwitterOAuth::Client.new(
148
+ :proxy => 'http://XXX.YYY.apigee.com',
149
+ :consumer_key => 'YOUR_CONSUMER_KEY',
150
+ :consumer_secret => 'YOUR-CONSUMER-SECRET',
151
+ :token => access_token.token,
152
+ :secret => access_token.secret
153
+ )
154
+
155
+ client.authorized?
156
+ => true
157
+
158
+ client.update('Proxy via Apigee is working')
159
+ </code></pre>
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rake/testtask'
2
+ require 'shoulda/tasks'
3
+ require "bundler/gem_tasks"
4
+
5
+
6
+ task :default => ["test:units"]
7
+
8
+ desc "Run basic tests"
9
+ Rake::TestTask.new("test:units") { |t|
10
+ t.pattern = 'test/**/*_test.rb'
11
+ t.verbose = true
12
+ t.warning = true
13
+ }
@@ -0,0 +1,5 @@
1
+ require 'oauth'
2
+ require 'json'
3
+ require 'mime/types'
4
+
5
+ require 'twitter_oauth/client'
@@ -0,0 +1,58 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful;
5
+ # returns a 401 status code and an error message if not.
6
+ def authorized?
7
+ oauth_response = access_token.get("/#{@api_version}/account/verify_credentials.json")
8
+ return oauth_response.class == Net::HTTPOK
9
+ end
10
+
11
+ # Returns client info
12
+ def info
13
+ get('/account/verify_credentials.json')
14
+ end
15
+
16
+ # Returns totals info (followers, updates, friends, favorites)
17
+ def totals
18
+ get('/account/totals.json')
19
+ end
20
+
21
+ # Returns the remaining number of API requests available to the requesting user before the API limit is reached for the current hour.
22
+ def rate_limit_status
23
+ get('/account/rate_limit_status.json')
24
+ end
25
+
26
+ # Updates profile background image. Takes a File object and optional tile argument.
27
+ # Returns extended user info object.
28
+ def update_profile_background_image(image, tile = false)
29
+ body, headers = http_multipart_data({:image => image, :tile => tile})
30
+ post('/account/update_profile_background_image.json', body, headers)
31
+ end
32
+
33
+ # Updates profile avatar image. Takes a File object which should be an image.
34
+ # Returns extended user info object.
35
+ def update_profile_image(image)
36
+ body, headers = http_multipart_data({:image => image})
37
+ post('/account/update_profile_image.json', body, headers)
38
+ end
39
+
40
+ # colors hash must contain at least one or more of the following keys :profile_background_color, :profile_text_color, :profile_link_color, :profile_sidebar_fill_color, :profile_sidebar_border_color
41
+ # returns extended user info object.
42
+ def update_profile_colors(colors)
43
+ post('/account/update_profile_colors.json', colors)
44
+ end
45
+
46
+ # Sets values that users are able to set under the "Account" tab of their settings page.
47
+ # Valid parameters are:
48
+ # :name Full name associated with the profile. Maximum of 20 characters.
49
+ # :url URL associated with the profile. Will be prepended with "http://" if not present. Maximum of 100 characters.
50
+ # :location The city or country describing where the user of the account is located. The contents are not normalized
51
+ # or geocoded in any way. Maximum of 30 characters.
52
+ # :description A description of the user owning the account. Maximum of 160 characters.
53
+ def update_profile(params)
54
+ post('/account/update_profile', params)
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,33 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Blocks the user specified in the ID parameter as the authenticating user.
5
+ # Destroys a friendship to the blocked user if it exists.
6
+ # Returns the blocked user in the requested format when successful.
7
+ def block(id)
8
+ post("/blocks/create/#{id}.json")
9
+ end
10
+
11
+ # Un-blocks the user specified in the ID parameter for the authenticating user.
12
+ # Returns the un-blocked user in the requested format when successful.
13
+ def unblock(id)
14
+ post("/blocks/destroy/#{id}.json")
15
+ end
16
+
17
+ # Returns if the authenticating user is blocking a target user.
18
+ def blocked?(id)
19
+ get("/blocks/exists/#{id}.json")
20
+ end
21
+
22
+ # Returns an array of user objects that the authenticating user is blocking.
23
+ def blocking
24
+ get("/blocks/blocking.json")
25
+ end
26
+
27
+ # Returns an array of numeric user ids the authenticating user is blocking.
28
+ def blocking_ids
29
+ get("/blocks/blocking/ids.json")
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,105 @@
1
+ require 'twitter_oauth/timeline'
2
+ require 'twitter_oauth/status'
3
+ require 'twitter_oauth/account'
4
+ require 'twitter_oauth/direct_messages'
5
+ require 'twitter_oauth/search'
6
+ require 'twitter_oauth/notifications'
7
+ require 'twitter_oauth/blocks'
8
+ require 'twitter_oauth/friendships'
9
+ require 'twitter_oauth/user'
10
+ require 'twitter_oauth/favorites'
11
+ require 'twitter_oauth/utils'
12
+ require 'twitter_oauth/trends'
13
+ require 'twitter_oauth/lists'
14
+ require 'twitter_oauth/saved_searches'
15
+ require 'twitter_oauth/spam'
16
+ require 'twitter_oauth/geo'
17
+ require 'twitter_oauth/version'
18
+
19
+ module TwitterOAuth
20
+ class Client
21
+
22
+ def initialize(options = {})
23
+ @consumer_key = options[:consumer_key]
24
+ @consumer_secret = options[:consumer_secret]
25
+ @token = options[:token]
26
+ @secret = options[:secret]
27
+ @proxy = options[:proxy]
28
+ @debug = options[:debug]
29
+ @api_version = options[:api_version] || '1'
30
+ @api_host = options[:api_host] || 'api.twitter.com'
31
+ @search_host = options[:search_host] || 'search.twitter.com'
32
+ end
33
+
34
+ def authorize(token, secret, options = {})
35
+ request_token = OAuth::RequestToken.new(
36
+ consumer(:secure => true), token, secret
37
+ )
38
+ @access_token = request_token.get_access_token(options)
39
+ @token = @access_token.token
40
+ @secret = @access_token.secret
41
+ @access_token
42
+ end
43
+
44
+ def show(username)
45
+ get("/users/show/#{username}.json")
46
+ end
47
+
48
+ # Returns the string "ok" in the requested format with a 200 OK HTTP status code.
49
+ def test
50
+ get("/help/test.json")
51
+ end
52
+
53
+ def request_token(options={})
54
+ consumer(:secure => true).get_request_token(options)
55
+ end
56
+
57
+ def authentication_request_token(options={})
58
+ consumer(:secure => true).options[:authorize_path] = '/oauth/authenticate'
59
+ request_token(options)
60
+ end
61
+
62
+ private
63
+
64
+ def consumer(options={})
65
+ options[:secure] ||= false
66
+ protocol = options[:secure] ? 'https' : 'http'
67
+ @consumer ||= OAuth::Consumer.new(
68
+ @consumer_key,
69
+ @consumer_secret,
70
+ { :site => "#{protocol}://#{@api_host}", :request_endpoint => @proxy }
71
+ )
72
+ end
73
+
74
+ def access_token
75
+ @access_token ||= OAuth::AccessToken.new(consumer, @token, @secret)
76
+ end
77
+
78
+ def get(path, headers={})
79
+ headers.merge!("User-Agent" => "twitter_oauth gem v#{TwitterOAuth::VERSION}")
80
+ oauth_response = access_token.get("/#{@api_version}#{path}", headers)
81
+ parse(oauth_response.body)
82
+ end
83
+
84
+ def post(path, body='', headers={})
85
+ headers.merge!("User-Agent" => "twitter_oauth gem v#{TwitterOAuth::VERSION}")
86
+ oauth_response = access_token.post("/#{@api_version}#{path}", body, headers)
87
+ parse(oauth_response.body)
88
+ end
89
+
90
+ def delete(path, headers={})
91
+ headers.merge!("User-Agent" => "twitter_oauth gem v#{TwitterOAuth::VERSION}")
92
+ oauth_response = access_token.delete("/#{@api_version}#{path}", headers)
93
+ parse(oauth_response.body)
94
+ end
95
+
96
+ def parse(response_body)
97
+ begin
98
+ JSON.parse(response_body)
99
+ rescue JSON::ParserError
100
+ {:response => response_body}.to_json
101
+ end
102
+ end
103
+ end
104
+ end
105
+
@@ -0,0 +1,29 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Return the most recent direct messages sent to the authenticating user.
5
+ # By default, returns the last 20. See http://apiwiki.twitter.com/Twitter-REST-API-Method:-direct_messages
6
+ # for other options
7
+ def messages(options={})
8
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
9
+ get("/direct_messages.json?#{args}")
10
+ end
11
+
12
+ # By default, returns a list of the 20 most recent direct messages sent by the authenticating user.
13
+ def sent_messages(options={})
14
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
15
+ get("/direct_messages/sent.json?#{args}")
16
+ end
17
+
18
+ # Sends a new direct message to the specified user from the authenticating user.
19
+ def message(user, text)
20
+ post('/direct_messages/new.json', :user => user, :text => text)
21
+ end
22
+
23
+ # Destroys the direct message specified in the required ID parameter.
24
+ def message_destroy(id)
25
+ post("/direct_messages/destroy/#{id}.json")
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Returns the 20 most recent favorite statuses for the authenticating user or user specified by the ID parameter.
5
+ def favorites(page=1)
6
+ get("/favorites.json?page=#{page}")
7
+ end
8
+
9
+ # Favorites the status specified in the ID parameter as the authenticating user.
10
+ # Returns the favorite status when successful.
11
+ def favorite(id)
12
+ post("/favorites/create/#{id}.json")
13
+ end
14
+
15
+ # Un-favorites the status specified in the ID parameter as the authenticating user.
16
+ # Returns the un-favorited status when successful.
17
+ def unfavorite(id)
18
+ post("/favorites/destroy/#{id}.json")
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Returns an array of numeric IDs for every user the specified user is following.
5
+ def friends_ids(options={})
6
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
7
+ get("/friends/ids.json?#{args}")
8
+ end
9
+
10
+ # Returns an array of numeric IDs for every user following the specified user.
11
+ def followers_ids(options={})
12
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
13
+ get("/followers/ids.json?#{args}")
14
+ end
15
+
16
+ # Allows the authenticating user to follow the specified user. Returns the befriended user when successful.
17
+ def friend(id)
18
+ post("/friendships/create/#{id}.json")
19
+ end
20
+
21
+ # Allows the authenticating users to unfollow the specified user. Returns the unfollowed user when successful.
22
+ def unfriend(id)
23
+ post("/friendships/destroy/#{id}.json")
24
+ end
25
+
26
+ # Tests for the existence of friendship between two users. Will return true if user_a follows user_b, otherwise will return false.
27
+ # You are better off using get_friendship as it returns more extended information.
28
+ def friends?(a, b)
29
+ oauth_response = access_token.get("/friendships/exists.json?user_a=#{a}&user_b=#{b}")
30
+ oauth_response.body.strip == 'true'
31
+ end
32
+
33
+ # Returns detailed information about the relationship between two users.
34
+ def get_friendship(a, b)
35
+ get("/friendships/show.json?source_screen_name=#{a}&target_screen_name=#{b}")
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Search for places (cities and neighborhoods) that can be attached to a statuses/update.
5
+ # Given a latitude and a longitude, return a list of all the valid places that can be used as a place_id when updating a status
6
+ def reverse_geocode(lat, lng, options={})
7
+ options[:lat] = lat; options[:lng] = lng
8
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
9
+ get "/geo/reverse_geocode.json?#{args}"
10
+ end
11
+
12
+ # Search for places (cities and neighborhoods) that can be attached to a statuses/update.
13
+ # Given a latitude and a longitude pair, or an IP address, return a list of all the valid cities
14
+ # and neighborhoods that can be used as a place_id when updating a status.
15
+ def geo_search(options={})
16
+ options[:query] = URI.escape(options[:query]) if options[:query]
17
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
18
+ get "/geo/search.json?#{args}"
19
+ end
20
+
21
+ # Find out more details of a place that was returned from the geo/reverse_geocode method.
22
+ def geo(id)
23
+ get "/geo/id/#{id}.json"
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,100 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ #
5
+ # List methods
6
+ #
7
+
8
+ # Creates a new list for the authenticated user. Accounts are limited to 20 lists.
9
+ def create_list(user, list, options={})
10
+ post("/#{user}/lists.json", options.merge(:name => list))
11
+ end
12
+
13
+ # Updates the specified list.
14
+ def update_list(user, list, options={})
15
+ post("/#{user}/lists/#{list}.json", options)
16
+ end
17
+
18
+ # List the lists of the specified user.
19
+ # Private lists will be included if the authenticated user is the same as the user whose lists are being returned.
20
+ def get_lists(user)
21
+ get("/#{user}/lists.json")
22
+ end
23
+
24
+ # Show the specified list. Private lists will only be shown if the authenticated user owns the specified list.
25
+ def get_list(user, list)
26
+ get("/#{user}/lists/#{list}.json")
27
+ end
28
+
29
+ # Deletes the specified list. Must be owned by the authenticated user.
30
+ def delete_list(user, list)
31
+ delete("/#{user}/lists/#{list}.json")
32
+ end
33
+
34
+ # Show tweet timeline for members of the specified list.
35
+ def list_statuses(user, list)
36
+ get("/#{user}/lists/#{list}/statuses.json")
37
+ end
38
+
39
+ # List the lists the specified user has been added to.
40
+ def list_memberships(user)
41
+ get("/#{user}/lists/memberships.json")
42
+ end
43
+
44
+ # List the lists the specified user follows.
45
+ def list_subscriptions(user)
46
+ get("/#{user}/lists/subscriptions.json")
47
+ end
48
+
49
+ #
50
+ # List Members Methods
51
+ #
52
+
53
+ # Returns the members of the specified list.
54
+ def list_members(user, list)
55
+ get("/#{user}/#{list}/members.json")
56
+ end
57
+
58
+ # Add a member to a list. The authenticated user must own the list to be able to add members to it.
59
+ # Lists are limited to having 500 members.
60
+ def add_member_to_list(user, list, member_id, options={})
61
+ post("/#{user}/#{list}/members.json", options.merge(:id => member_id))
62
+ end
63
+
64
+ # Removes the specified member from the list.
65
+ # The authenticated user must be the list's owner to remove members from the list.
66
+ def remove_member_from_list(user, list, member_id)
67
+ delete("/#{user}/#{list}/members.json?id=#{member_id}")
68
+ end
69
+
70
+ # Check if a user is a member of the specified list.
71
+ def get_member_of_list(user, list, member_id)
72
+ get("/#{user}/#{list}/members/#{member_id}.json")
73
+ end
74
+
75
+ #
76
+ # List Subscribers Methods
77
+ #
78
+
79
+ # Returns the subscribers of the specified list.
80
+ def list_subscribers(user, list)
81
+ get("/#{user}/#{list}/subscribers.json")
82
+ end
83
+
84
+ # Make the authenticated user follow the specified list.
85
+ def subscribe_to_list(user, list, options={})
86
+ post("/#{user}/#{list}/subscribers.json")
87
+ end
88
+
89
+ # Unsubscribes the authenticated user form the specified list.
90
+ def unsubscribe_from_list(user, list)
91
+ delete("/#{user}/#{list}/subscribers.json")
92
+ end
93
+
94
+ # Check if the specified user is a subscriber of the specified list.
95
+ def get_subscriber_of_list(user, list, subscriber_id)
96
+ get("/#{user}/#{list}/subscribers/#{subscriber_id}.json")
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,17 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Enables device notifications for updates from the specified user.
5
+ # Returns the specified user when successful.
6
+ def follow(id)
7
+ post("/notifications/follow/#{id}.json")
8
+ end
9
+
10
+ # Disables notifications for updates from the specified user to the authenticating user.
11
+ # Returns the specified user when successful.
12
+ def leave(id)
13
+ post("/notifications/leave/#{id}.json")
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Returns the authenticated user's saved search queries.
5
+ def saved_searches
6
+ get("/saved_searches.json")
7
+ end
8
+
9
+ # Retrieve the data for a saved search owned by the authenticating user specified by the given id.
10
+ def get_saved_search(search_id)
11
+ get("/saved_searches/show/#{search_id}.json")
12
+ end
13
+
14
+ # Creates a saved search for the authenticated user.
15
+ def create_saved_search(query)
16
+ post("/saved_searches/create.json", :query => query)
17
+ end
18
+
19
+ def delete_saved_search(search_id)
20
+ post("/saved_searches/destroy/#{search_id}.json")
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,35 @@
1
+ require 'open-uri'
2
+
3
+ module TwitterOAuth
4
+ class Client
5
+
6
+ def search(q, options={})
7
+ options[:page] ||= 1
8
+ options[:rpp] ||= 20
9
+ options[:q] = URI.escape(q)
10
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
11
+ search_get("/search.json?#{args}")
12
+ end
13
+
14
+ # Returns the current top 10 trending topics on Twitter.
15
+ def current_trends
16
+ search_get("/trends/current.json")
17
+ end
18
+
19
+ # Returns the top 20 trending topics for each hour in a given day.
20
+ def daily_trends
21
+ search_get("/trends/daily.json")
22
+ end
23
+
24
+ # Returns the top 30 trending topics for each day in a given week.
25
+ def weekly_trends
26
+ search_get("/trends/weekly.json")
27
+ end
28
+
29
+ private
30
+ def search_get(path)
31
+ response = open("http://#{@search_host}" + path, 'User-Agent' => "twitter_oauth gem v#{TwitterOAuth::VERSION}")
32
+ JSON.parse(response.read)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # The user specified in the id is blocked by the authenticated user and reported as a spammer.
5
+ def report_spam(user)
6
+ post("/report_spam.json", :id => user)
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,30 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Returns a single status, specified by the id parameter below.
5
+ def status(id)
6
+ get("/statuses/show/#{id}.json")
7
+ end
8
+
9
+ # Updates the authenticating user's status.
10
+ def update(message, options={})
11
+ post('/statuses/update.json', options.merge(:status => message))
12
+ end
13
+
14
+ # Destroys the status specified by the required ID parameter
15
+ def status_destroy(id)
16
+ post("/statuses/destroy/#{id}.json")
17
+ end
18
+
19
+ # Retweets the tweet specified by the id parameter. Returns the original tweet with retweet details embedded.
20
+ def retweet(id)
21
+ post("/statuses/retweet/#{id}.json")
22
+ end
23
+
24
+ # Returns the 100 most recent retweets of the tweet.
25
+ def retweets(id)
26
+ get("/statuses/retweets/#{id}.json")
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,56 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Returns the 20 most recent statuses from non-protected users who have set a custom user icon.
5
+ def public_timeline(options={})
6
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
7
+ get("/statuses/public_timeline.json?#{args}")
8
+ end
9
+
10
+ # Returns the 20 most recent statuses, including retweets, posted by the authenticating user and that user's friends.
11
+ # This is the equivalent of /timeline/home on the Web.
12
+ def home_timeline(options={})
13
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
14
+ get("/statuses/home_timeline.json?#{args}")
15
+ end
16
+
17
+ # Returns the 20 most recent statuses posted by the authenticating user and that user's friends.
18
+ def friends_timeline(options={})
19
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
20
+ get("/statuses/friends_timeline.json?#{args}")
21
+ end
22
+
23
+ # Returns the 20 most recent statuses posted from the authenticating user.
24
+ def user_timeline(options={})
25
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
26
+ get("/statuses/user_timeline.json?#{args}")
27
+ end
28
+ alias :user :user_timeline
29
+
30
+ # Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
31
+ def mentions(options={})
32
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
33
+ get("/statuses/mentions.json?#{args}")
34
+ end
35
+ alias :replies :mentions
36
+
37
+ # Returns the 20 most recent retweets posted by the authenticating user
38
+ def retweeted_by_me(options={})
39
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
40
+ get("/statuses/retweeted_by_me.json?#{args}")
41
+ end
42
+
43
+ # Returns the 20 most recent retweets posted by the authenticating user's friends.
44
+ def retweeted_to_me(options={})
45
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
46
+ get("/statuses/retweeted_to_me.json?#{args}")
47
+ end
48
+
49
+ # Returns the 20 most recent tweets of the authenticated user that have been retweeted by others.
50
+ def retweets_of_me(options={})
51
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
52
+ get("/statuses/retweets_of_me.json?#{args}")
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,25 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Returns the top ten topics that are currently trending on Twitter.
5
+ def trends
6
+ get("/trends.json")
7
+ end
8
+
9
+ # Returns the locations that Twitter has trending topic information for.
10
+ # The response is an array of "locations" that encode the location's WOEID (a Yahoo! Where On Earth ID)
11
+ # and some other human-readable information such as a canonical name and country the location belongs in.
12
+ def trends_available
13
+ get("/trends/available.json")
14
+ end
15
+
16
+ # Returns the top 10 trending topics for a specific location Twitter has trending topic information for.
17
+ # The response is an array of "trend" objects that encode the name of the trending topic, the query
18
+ # parameter that can be used to search for the topic on Search, and the direct URL that can be issued against Search.
19
+ # This information is cached for five minutes, and therefore users are discouraged from querying these endpoints
20
+ # faster than once every five minutes. Global trends information is also available from this API by using a WOEID of 1.
21
+ def trends_for_location(woeid)
22
+ get("/trends/woeid.json")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,60 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Returns the 100 last friends
5
+ # The page parameter is implemented for legacy reasons, but use of this is slow
6
+ # as passing page is no longer supported by the Twitter API as the use of cursors
7
+ # is now obligitory. It is recommended that you use all_friends instead
8
+ def friends(page=1)
9
+ return get("/statuses/friends.json?page=#{page}") if page == 1
10
+ users = []
11
+ cursor = "-1"
12
+ page.times do
13
+ return [] if cursor == 0
14
+ json = get("/statuses/friends.json?cursor=#{cursor}")
15
+ cursor = json["next_cursor"]
16
+ users = json["users"]
17
+ end
18
+ users
19
+ end
20
+
21
+ # Returns all pages of friends
22
+ def all_friends
23
+ users = []
24
+ cursor = "-1"
25
+ while cursor != 0 do
26
+ json = get("/statuses/friends.json?cursor=#{cursor}")
27
+ cursor = json["next_cursor"]
28
+ users += json["users"]
29
+ end
30
+ users
31
+ end
32
+
33
+ # Returns the 100 last followers
34
+ def followers(page=1)
35
+ return get("/statuses/followers.json?page=#{page}") if page == 1
36
+ users = []
37
+ cursor = "-1"
38
+ page.times do
39
+ return [] if cursor == 0
40
+ json = get("/statuses/followers.json?cursor=#{cursor}")
41
+ cursor = json["next_cursor"]
42
+ users = json["users"]
43
+ end
44
+ users
45
+ end
46
+
47
+ # Returns all pages of followers
48
+ def all_followers
49
+ users = []
50
+ cursor = "-1"
51
+ while cursor != 0 do
52
+ json = get("/statuses/followers.json?cursor=#{cursor}")
53
+ cursor = json["next_cursor"]
54
+ users += json["users"]
55
+ end
56
+ users
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,34 @@
1
+ module TwitterOAuth
2
+ class Client
3
+ CRLF = "\r\n"
4
+
5
+ private
6
+ # Properly encodes images in form/multipart specification for upload via OAuth.
7
+ def http_multipart_data(params)
8
+ body = ""
9
+ headers = {}
10
+
11
+ boundary = Time.now.to_i.to_s(16)
12
+
13
+ headers["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
14
+ params.each do |key,value|
15
+ esc_key = OAuth::Helper.escape(key.to_s)
16
+ body << "--#{boundary}#{CRLF}"
17
+
18
+ if value.respond_to?(:read)
19
+ mime_type = MIME::Types.type_for(value.path)[0] || MIME::Types["application/octet-stream"][0]
20
+ body << "Content-Disposition: form-data; name=\"#{esc_key}\"; filename=\"#{File.basename(value.path)}\"#{CRLF}"
21
+ body << "Content-Type: #{mime_type.simplified}#{CRLF*2}"
22
+ body << value.read
23
+ else
24
+ body << "Content-Disposition: form-data; name=\"#{esc_key}\"#{CRLF*2}#{value}"
25
+ end
26
+ end
27
+
28
+ body << "--#{boundary}--#{CRLF*2}"
29
+ headers["Content-Length"] = body.size.to_s
30
+
31
+ return [ body, headers ]
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module TwitterOAuth
2
+ VERSION = '0.5.0.pre2'
3
+ end
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__), "/test_helper")
2
+
3
+ class ClientTest < Test::Unit::TestCase
4
+ include TwitterOAuth
5
+
6
+ context "A client instance" do
7
+
8
+ setup do
9
+ @client = Client.new
10
+ end
11
+
12
+ should "be kind of TwitterOAuth" do
13
+ assert_kind_of TwitterOAuth::Client, @client
14
+ end
15
+
16
+ context "authentication_request_token" do
17
+ setup do
18
+ @consumer = stub("oauth consumer", :options => {})
19
+ @client.stubs(:request_token)
20
+ @client.stubs(:consumer).returns(@consumer)
21
+ end
22
+
23
+ should "sets consumers authorize path" do
24
+ @client.authentication_request_token
25
+ assert_equal @client.consumer.options[:authorize_path], '/oauth/authenticate'
26
+ end
27
+ end
28
+
29
+ context "when authorizing" do
30
+ setup do
31
+ @request_token = stub('a request token')
32
+ @access_token = stub_everything('access token')
33
+ @request_token.stubs(:get_access_token).returns(@access_token)
34
+ OAuth::RequestToken.stubs(:new).returns(@request_token)
35
+ end
36
+
37
+ should "return an access token" do
38
+ assert_equal @client.authorize("haha","haha"), @access_token
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ lib_files = File.join(File.dirname(__FILE__), "..", "lib")
7
+
8
+ Dir.glob(File.join(lib_files, "**")).each do |file|
9
+ require file
10
+ end
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require "twitter_oauth/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "jugyo-twitter_oauth"
8
+ s.version = TwitterOAuth::VERSION
9
+ s.authors = ["Richard Taylor", "jugyo"]
10
+ s.email = ["moomerman@gmail.com", "jugyo.org@gmail.com"]
11
+ s.homepage = "http://github.com/jugyo/twitter_oauth"
12
+ s.summary = %q{twitter_oauth is a Ruby client for the Twitter API using OAuth.}
13
+ s.description = %q{twitter_oauth is a Ruby client for the Twitter API using OAuth.}
14
+
15
+ s.rubyforge_project = "twitter_oauth"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_runtime_dependency(%q<oauth>, [">= 0.4.1"])
23
+ s.add_runtime_dependency(%q<json>, [">= 1.1.9"])
24
+ s.add_runtime_dependency(%q<mime-types>, [">= 1.16"])
25
+ s.add_development_dependency(%q<shoulda>, ["~> 2.0"])
26
+ s.add_development_dependency(%q<mocha>)
27
+ s.add_development_dependency(%q<rake>)
28
+
29
+ # specify any dependencies here; for example:
30
+ # s.add_development_dependency "rspec"
31
+ # s.add_runtime_dependency "rest-client"
32
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jugyo-twitter_oauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0.pre2
5
+ platform: ruby
6
+ authors:
7
+ - Richard Taylor
8
+ - jugyo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: oauth
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: 0.4.1
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: 0.4.1
28
+ - !ruby/object:Gem::Dependency
29
+ name: json
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: 1.1.9
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: 1.1.9
42
+ - !ruby/object:Gem::Dependency
43
+ name: mime-types
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '1.16'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '1.16'
56
+ - !ruby/object:Gem::Dependency
57
+ name: shoulda
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '2.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: mocha
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rake
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: twitter_oauth is a Ruby client for the Twitter API using OAuth.
99
+ email:
100
+ - moomerman@gmail.com
101
+ - jugyo.org@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - .gitignore
107
+ - Gemfile
108
+ - LICENSE
109
+ - README.textile
110
+ - Rakefile
111
+ - lib/twitter_oauth.rb
112
+ - lib/twitter_oauth/account.rb
113
+ - lib/twitter_oauth/blocks.rb
114
+ - lib/twitter_oauth/client.rb
115
+ - lib/twitter_oauth/direct_messages.rb
116
+ - lib/twitter_oauth/favorites.rb
117
+ - lib/twitter_oauth/friendships.rb
118
+ - lib/twitter_oauth/geo.rb
119
+ - lib/twitter_oauth/lists.rb
120
+ - lib/twitter_oauth/notifications.rb
121
+ - lib/twitter_oauth/saved_searches.rb
122
+ - lib/twitter_oauth/search.rb
123
+ - lib/twitter_oauth/spam.rb
124
+ - lib/twitter_oauth/status.rb
125
+ - lib/twitter_oauth/timeline.rb
126
+ - lib/twitter_oauth/trends.rb
127
+ - lib/twitter_oauth/user.rb
128
+ - lib/twitter_oauth/utils.rb
129
+ - lib/twitter_oauth/version.rb
130
+ - test/client_test.rb
131
+ - test/test_helper.rb
132
+ - twitter_oauth.gemspec
133
+ homepage: http://github.com/jugyo/twitter_oauth
134
+ licenses: []
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - '>'
148
+ - !ruby/object:Gem::Version
149
+ version: 1.3.1
150
+ requirements: []
151
+ rubyforge_project: twitter_oauth
152
+ rubygems_version: 2.0.0
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: twitter_oauth is a Ruby client for the Twitter API using OAuth.
156
+ test_files:
157
+ - test/client_test.rb
158
+ - test/test_helper.rb