rorra-twitter_oauth 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
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.
@@ -0,0 +1,127 @@
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 => 'YOURA_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
+ <pre><code>access_token = client.authorize(
59
+ request_token.token,
60
+ request_token.secret,
61
+ :oauth_verifier => params[:oauth_verifier]
62
+ )
63
+
64
+ client.authorized?
65
+ => true
66
+
67
+ client.update('checking out the twitter_oauth library') # sends a twitter status update
68
+ </code></pre>
69
+
70
+ 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.
71
+
72
+ <pre><code>access_token = @user.access_token # assuming @user
73
+ client = TwitterOAuth::Client.new(
74
+ :consumer_key => 'YOUR_CONSUMER_KEY',
75
+ :consumer_secret => 'YOUR-CONSUMER-SECRET',
76
+ :token => access_token.token,
77
+ :secret => access_token.secret
78
+ )
79
+
80
+ client.authorized?
81
+ => true
82
+ </code></pre>
83
+
84
+ h2. Working with a Proxy
85
+
86
+ 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.
87
+
88
+ First you need to authorize the Twitter user via OAuth directly via the Twitter API (this part cannot be proxied)
89
+
90
+ <pre><code>client = TwitterOAuth::Client.new(
91
+ :consumer_key => 'YOUR_APP_CONSUMER_KEY',
92
+ :consumer_secret => 'YOURA_APP_CONSUMER_SECRET'
93
+ )
94
+ request_token = client.request_token(:oauth_callback => 'YOUR_CALLBACK_URL')
95
+
96
+ request_token.authorize_url
97
+ => http://twitter.com/oauth/authorize?oauth_token=TOKEN
98
+ </code></pre>
99
+
100
+ 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
101
+
102
+ <pre><code>access_token = client.authorize(
103
+ request_token.token,
104
+ request_token.secret,
105
+ :oauth_verifier => params[:oauth_verifier]
106
+ )
107
+
108
+ client.authorized?
109
+ => true
110
+ </code></pre>
111
+
112
+ Now you can make all further API calls through the proxy of your choice:
113
+
114
+ <pre><code>access_token = @user.access_token # assuming @user
115
+ client = TwitterOAuth::Client.new(
116
+ :proxy => 'http://XXX.YYY.apigee.com',
117
+ :consumer_key => 'YOUR_CONSUMER_KEY',
118
+ :consumer_secret => 'YOUR-CONSUMER-SECRET',
119
+ :token => access_token.token,
120
+ :secret => access_token.secret
121
+ )
122
+
123
+ client.authorized?
124
+ => true
125
+
126
+ client.update('Proxy via Apigee is working')
127
+ </code></pre>
@@ -0,0 +1,9 @@
1
+ require 'oauth'
2
+ require 'json'
3
+ require 'mime/types'
4
+
5
+ require 'twitter_oauth/client'
6
+
7
+ module TwitterOAuth
8
+ VERSION = '0.4.3'
9
+ end
@@ -0,0 +1,53 @@
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('/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 the remaining number of API requests available to the requesting user before the API limit is reached for the current hour.
17
+ def rate_limit_status
18
+ get('/account/rate_limit_status.json')
19
+ end
20
+
21
+ # Updates profile background image. Takes a File object and optional tile argument.
22
+ # Returns extended user info object.
23
+ def update_profile_background_image(image, tile = false)
24
+ body, headers = http_multipart_data({:image => image, :tile => tile})
25
+ post('/account/update_profile_background_image.json', body, headers)
26
+ end
27
+
28
+ # Updates profile avatar image. Takes a File object which should be an image.
29
+ # Returns extended user info object.
30
+ def update_profile_image(image)
31
+ body, headers = http_multipart_data({:image => image})
32
+ post('/account/update_profile_image.json', body, headers)
33
+ end
34
+
35
+ # 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
36
+ # returns extended user info object.
37
+ def update_profile_colors(colors)
38
+ post('/account/update_profile_colors.json', colors)
39
+ end
40
+
41
+ # Sets values that users are able to set under the "Account" tab of their settings page.
42
+ # Valid parameters are:
43
+ # :name Full name associated with the profile. Maximum of 20 characters.
44
+ # :url URL associated with the profile. Will be prepended with "http://" if not present. Maximum of 100 characters.
45
+ # :location The city or country describing where the user of the account is located. The contents are not normalized
46
+ # or geocoded in any way. Maximum of 30 characters.
47
+ # :description A description of the user owning the account. Maximum of 160 characters.
48
+ def update_profile(params)
49
+ post('/account/update_profile', params)
50
+ end
51
+
52
+ end
53
+ 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,90 @@
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
+
18
+ module TwitterOAuth
19
+ class Client
20
+
21
+ def initialize(options = {})
22
+ @consumer_key = options[:consumer_key]
23
+ @consumer_secret = options[:consumer_secret]
24
+ @token = options[:token]
25
+ @secret = options[:secret]
26
+ @proxy = options[:proxy]
27
+ end
28
+
29
+ def authorize(token, secret, options = {})
30
+ request_token = OAuth::RequestToken.new(
31
+ consumer, token, secret
32
+ )
33
+ @access_token = request_token.get_access_token(options)
34
+ @token = @access_token.token
35
+ @secret = @access_token.secret
36
+ @access_token
37
+ end
38
+
39
+ def show(username)
40
+ get("/users/show/#{username}.json")
41
+ end
42
+
43
+ # Returns the string "ok" in the requested format with a 200 OK HTTP status code.
44
+ def test
45
+ get("/help/test.json")
46
+ end
47
+
48
+ def request_token(options={})
49
+ consumer.get_request_token(options)
50
+ end
51
+
52
+ def authentication_request_token(options={})
53
+ consumer.options[:authorize_path] = '/oauth/authenticate'
54
+ request_token(options)
55
+ end
56
+
57
+ private
58
+
59
+ def consumer
60
+ @consumer ||= OAuth::Consumer.new(
61
+ @consumer_key,
62
+ @consumer_secret,
63
+ { :site => 'http://api.twitter.com', :request_endpoint => @proxy }
64
+ )
65
+ end
66
+
67
+ def access_token
68
+ @access_token ||= OAuth::AccessToken.new(consumer, @token, @secret)
69
+ end
70
+
71
+ def get(path, headers={})
72
+ headers.merge!("User-Agent" => "twitter_oauth gem v#{TwitterOAuth::VERSION}")
73
+ oauth_response = access_token.get("/1#{path}", headers)
74
+ JSON.parse(oauth_response.body)
75
+ end
76
+
77
+ def post(path, body='', headers={})
78
+ headers.merge!("User-Agent" => "twitter_oauth gem v#{TwitterOAuth::VERSION}")
79
+ oauth_response = access_token.post("/1#{path}", body, headers)
80
+ JSON.parse(oauth_response.body)
81
+ end
82
+
83
+ def delete(path, headers={})
84
+ headers.merge!("User-Agent" => "twitter_oauth gem v#{TwitterOAuth::VERSION}")
85
+ oauth_response = access_token.delete("/1#{path}", headers)
86
+ JSON.parse(oauth_response.body)
87
+ end
88
+ end
89
+ end
90
+
@@ -0,0 +1,32 @@
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
+ body = {:text => text}
21
+ body[:screen_name] = user["screen_name"] if user.has_key?("screen_name")
22
+ body[:user_id] = user["id"] if user.has_key?("id")
23
+ post('/direct_messages/new.json', body)
24
+ end
25
+
26
+ # Destroys the direct message specified in the required ID parameter.
27
+ def message_destroy(id)
28
+ post("/direct_messages/destroy/#{id}.json")
29
+ end
30
+
31
+ end
32
+ 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.twitter.com' + path, 'User-Agent' => 'github.com/moomerman/twitter_outh')
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
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rorra-twitter_oauth
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 3
10
+ version: 0.4.3
11
+ platform: ruby
12
+ authors:
13
+ - Richard Taylor
14
+ - Rodrigo Dominguez
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-08-11 00:00:00 -03:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: oauth
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 13
31
+ segments:
32
+ - 0
33
+ - 4
34
+ - 1
35
+ version: 0.4.1
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: json
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 1
47
+ segments:
48
+ - 1
49
+ - 1
50
+ - 9
51
+ version: 1.1.9
52
+ type: :runtime
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: mime-types
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 47
63
+ segments:
64
+ - 1
65
+ - 16
66
+ version: "1.16"
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: shoulda
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ name: mocha
85
+ prerelease: false
86
+ requirement: &id005 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ type: :development
96
+ version_requirements: *id005
97
+ description: twitter_oauth is a Ruby client for the Twitter API using OAuth.
98
+ email: rorra@rorra.com.ar
99
+ executables: []
100
+
101
+ extensions: []
102
+
103
+ extra_rdoc_files: []
104
+
105
+ files:
106
+ - LICENSE
107
+ - README.textile
108
+ - lib/twitter_oauth.rb
109
+ - lib/twitter_oauth/account.rb
110
+ - lib/twitter_oauth/blocks.rb
111
+ - lib/twitter_oauth/client.rb
112
+ - lib/twitter_oauth/direct_messages.rb
113
+ - lib/twitter_oauth/favorites.rb
114
+ - lib/twitter_oauth/friendships.rb
115
+ - lib/twitter_oauth/geo.rb
116
+ - lib/twitter_oauth/lists.rb
117
+ - lib/twitter_oauth/notifications.rb
118
+ - lib/twitter_oauth/saved_searches.rb
119
+ - lib/twitter_oauth/search.rb
120
+ - lib/twitter_oauth/spam.rb
121
+ - lib/twitter_oauth/status.rb
122
+ - lib/twitter_oauth/timeline.rb
123
+ - lib/twitter_oauth/trends.rb
124
+ - lib/twitter_oauth/user.rb
125
+ - lib/twitter_oauth/utils.rb
126
+ has_rdoc: true
127
+ homepage: http://github.com/rorra/twitter_oauth
128
+ licenses: []
129
+
130
+ post_install_message:
131
+ rdoc_options:
132
+ - --inline-source
133
+ - --charset=UTF-8
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
144
+ version: "0"
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ hash: 3
151
+ segments:
152
+ - 0
153
+ version: "0"
154
+ requirements: []
155
+
156
+ rubyforge_project: twitter_oauth
157
+ rubygems_version: 1.3.7
158
+ signing_key:
159
+ specification_version: 2
160
+ summary: twitter_oauth is a Ruby client for the Twitter API using OAuth.
161
+ test_files: []
162
+