twitter_oauth 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,76 @@
1
+ h1. Twitter OAuth REST API client library for Ruby
2
+
3
+ To make authorized requests with the client library you'll need to [create a Twitter OAuth Application](http://twitter.com/oauth_clients/new).
4
+
5
+ See [sinitter](http://github.com/moomerman/sinitter/tree/master) for a full website integration example.
6
+
7
+ h2. Unauthorized request example
8
+
9
+ The Twitter API can be called to make public requests without needing any client credentials.
10
+ Most methods will not work in this mode but some of them do. An example to retrieve the public details
11
+ about a Twitter user is below.
12
+
13
+ <pre><code>client = TwitterOAuth::Client.new
14
+
15
+ puts client.show('twitter')
16
+ => => {"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"}
17
+ </code></pre>
18
+
19
+ You can also access to the search API which is available in either authorized or unauthorized modes.
20
+
21
+ <pre><code>search = client.search('twitter')
22
+ search.results.size => 20
23
+ search.results.first.from_user => "josephpred"
24
+ search.results.first.text
25
+ => "Useful public service Twitter account for those of you hitting Tahoe or just needing to cross the pass to Reno: @i80chains"
26
+ </code></pre>
27
+
28
+ h2. Authorized request example
29
+
30
+ To use the full power of the Twitter API you need to authorize your application and a valid Twitter user via OAuth.
31
+ An example showing how to update the status of an authorized user is below.
32
+
33
+ Firstly we need to create an instance of the client with your application client credentials you have been given by Twitter
34
+ when you set up your application.
35
+
36
+ <pre><code>client = TwitterOAuth::Client.new(
37
+ :consumer_key => 'YOUR_APP_CONSUMER_KEY',
38
+ :consumer_secret => 'YOURA_APP_CONSUMER_SECRET'
39
+ )
40
+ request_token = client.request_token(:oauth_callback => oauth_confirm_url)
41
+ #:oauth_callback required for web apps, since oauth gem by default forse PIN-based flow
42
+ #( see http://groups.google.com/group/twitter-development-talk/browse_thread/thread/472500cfe9e7cdb9/848f834227d3e64d )
43
+
44
+
45
+ request_token.authorize_url
46
+ => http://twitter.com/oauth/authorize?oauth_token=TOKEN
47
+ </code></pre>
48
+
49
+ In your application your user would be redirected to Twitter to authorize the application at this point. You'll need to store
50
+ the request token (usually in the session) for later. The code continues below assuming the user has authorized your application.
51
+
52
+ <pre><code>access_token = client.authorize(
53
+ request_token.token,
54
+ request_token.secret,
55
+ :oauth_verifier => params[:oauth_verifier]
56
+ )
57
+
58
+ client.authorized?
59
+ => true
60
+
61
+ client.update('checking out the twitter_oauth library') # sends a twitter status update
62
+ </code></pre>
63
+
64
+ 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.
65
+
66
+ <pre><code>access_token = @user.access_token # assuming @user
67
+ client = TwitterOAuth::Client.new(
68
+ :consumer_key => 'YOUR_CONSUMER_KEY',
69
+ :consumer_secret => 'YOUR-CONSUMER-SECRET',
70
+ :token => access_token.token,
71
+ :secret => access_token.secret
72
+ )
73
+
74
+ client.authorized?
75
+ => true
76
+ </code></pre>
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'oauth'
3
+ require 'json'
4
+ require 'mime/types'
5
+
6
+ require 'twitter_oauth/client'
7
+
8
+ module TwitterOAuth
9
+ end
@@ -0,0 +1,45 @@
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
+ oauth_response = access_token.get('/account/verify_credentials.json')
14
+ JSON.parse(oauth_response.body)
15
+ end
16
+
17
+ # Returns the remaining number of API requests available to the requesting user before the API limit is reached for the current hour.
18
+ def rate_limit_status
19
+ oauth_response = access_token.get('/account/rate_limit_status.json')
20
+ JSON.parse(oauth_response.body)
21
+ end
22
+
23
+ # Updates profile background image. Takes a File object and optional tile argument.
24
+ # Returns extended user info object.
25
+ def update_profile_background_image(image, tile = false)
26
+ body, headers = http_multipart_data({:image => image, :tile => tile})
27
+ oauth_response = access_token.post('/account/update_profile_background_image.json', body, headers)
28
+ end
29
+
30
+ # Updates profile avatar image. Takes a File object which should be an image.
31
+ # Returns extended user info object.
32
+ def update_profile_image(image)
33
+ body, headers = http_multipart_data({:image => image})
34
+ oauth_response = access_token.post('/account/update_profile_image.json', body, headers)
35
+ end
36
+
37
+ # 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
38
+ # returns extended user info object.
39
+ def update_profile_colors(colors)
40
+ oauth_response = access_token.post('/account/update_profile_colors.json', colors)
41
+ JSON.parse(oauth_response.body)
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,17 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # unblock this user.
5
+ def block(id)
6
+ oauth_response = access_token.post("/blocks/create/#{id}.json")
7
+ JSON.parse(oauth_response.body)
8
+ end
9
+
10
+ # block this user.
11
+ def unblock(id)
12
+ oauth_response = access_token.post("/blocks/destroy/#{id}.json")
13
+ JSON.parse(oauth_response.body)
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,61 @@
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
+
13
+ module TwitterOAuth
14
+ class Client
15
+
16
+ def initialize(options = {})
17
+ @consumer_key = options[:consumer_key]
18
+ @consumer_secret = options[:consumer_secret]
19
+ @token = options[:token]
20
+ @secret = options[:secret]
21
+ end
22
+
23
+ def authorize(token, secret, options = {})
24
+ request_token = OAuth::RequestToken.new(
25
+ consumer, token, secret
26
+ )
27
+ @access_token = request_token.get_access_token(options)
28
+ @token = @access_token.token
29
+ @secret = @access_token.secret
30
+ @access_token
31
+ end
32
+
33
+ def show(username)
34
+ oauth_response = access_token.get("/users/show/#{username}.json")
35
+ JSON.parse(oauth_response.body)
36
+ end
37
+
38
+ def request_token(options={})
39
+ consumer.get_request_token(options)
40
+ end
41
+
42
+ def authentication_request_token(options={})
43
+ consumer.options[:authorize_path] = '/oauth/authenticate'
44
+ request_token(options)
45
+ end
46
+
47
+ private
48
+ def consumer
49
+ @consumer ||= OAuth::Consumer.new(
50
+ @consumer_key,
51
+ @consumer_secret,
52
+ { :site=>"http://twitter.com" }
53
+ )
54
+ end
55
+
56
+ def access_token
57
+ @access_token ||= OAuth::AccessToken.new(consumer, @token, @secret)
58
+ end
59
+ end
60
+ end
61
+
@@ -0,0 +1,29 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Returns a list of the 20 most recent direct messages sent to the authenticating user.
5
+ def messages(page=1)
6
+ oauth_response = access_token.get("/direct_messages.json?page=#{page}")
7
+ JSON.parse(oauth_response.body)
8
+ end
9
+
10
+ # Returns a list of the 20 most recent direct messages sent by the authenticating user.
11
+ def sent_messages
12
+ oauth_response = access_token.get('/direct_messages/sent.json')
13
+ JSON.parse(oauth_response.body)
14
+ end
15
+
16
+ # Sends a new direct message to the specified user from the authenticating user.
17
+ def message(user, text)
18
+ oauth_response = access_token.post('/direct_messages/new.json', :user => user, :text => text)
19
+ JSON.parse(oauth_response.body)
20
+ end
21
+
22
+ # Destroys the direct message specified in the required ID parameter.
23
+ def message_destroy(id)
24
+ oauth_response = access_token.post("/direct_messages/destroy/#{id}.json")
25
+ JSON.parse(oauth_response.body)
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ def favorites(page=1)
5
+ oauth_response = access_token.get("/favorites.json?page=#{page}")
6
+ JSON.parse(oauth_response.body)
7
+ end
8
+
9
+ def favorite
10
+ oauth_response = access_token.post("/favorites/create/#{id}.json")
11
+ JSON.parse(oauth_response.body)
12
+ end
13
+
14
+ def unfavorite
15
+ oauth_response = access_token.post("/favorites/destroy/#{id}.json")
16
+ JSON.parse(oauth_response.body)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,35 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ def friends_ids(options={})
5
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
6
+ oauth_response = access_token.get("/friends/ids.json?#{args}")
7
+ JSON.parse(oauth_response.body)
8
+ end
9
+
10
+ def followers_ids(options={})
11
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
12
+ oauth_response = access_token.get("/followers/ids.json?#{args}")
13
+ JSON.parse(oauth_response.body)
14
+ end
15
+
16
+ # friend this user.
17
+ def friend(id)
18
+ oauth_response = access_token.post("/friendships/create/#{id}.json")
19
+ JSON.parse(oauth_response.body)
20
+ end
21
+
22
+ # unfriend.
23
+ def unfriend(id)
24
+ oauth_response = access_token.post("/friendships/destroy/#{id}.json")
25
+ JSON.parse(oauth_response.body)
26
+ end
27
+
28
+ # exists?.
29
+ def exists?(a, b)
30
+ oauth_response = access_token.get("/friendships/exists.json?user_a=#{a}&user_b=#{b}")
31
+ oauth_response.body.strip == 'true'
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # follow this user.
5
+ def follow(id)
6
+ oauth_response = access_token.post("/notifications/follow/#{id}.json")
7
+ JSON.parse(oauth_response.body)
8
+ end
9
+
10
+ # unfollow.
11
+ def leave(id)
12
+ oauth_response = access_token.post("/notifications/leave/#{id}.json")
13
+ JSON.parse(oauth_response.body)
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ require 'open-uri'
2
+ require 'ostruct'
3
+
4
+ module TwitterOAuth
5
+ class Client
6
+
7
+ def search(q, options={})
8
+ options[:page] ||= 1
9
+ options[:per_page] ||= 20
10
+ response = open("http://search.twitter.com/search.json?q=#{URI.escape(q)}&page=#{options[:page]}&rpp=#{options[:per_page]}&since_id=#{options[:since_id]}")
11
+ search_result = JSON.parse(response.read)
12
+ search_result = OpenStruct.new(search_result)
13
+ search_result.results = search_result.results.collect{|x| OpenStruct.new(x)}
14
+ search_result
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Returns a single status, specified by the id parameter below.
5
+ def status(id)
6
+ oauth_response = access_token.get("/statuses/show/#{id}.json")
7
+ JSON.parse(oauth_response.body)
8
+ end
9
+
10
+ # Updates the authenticating user's status.
11
+ def update(message, options={})
12
+ oauth_response = access_token.post('/statuses/update.json', options.merge(:status => message))
13
+ JSON.parse(oauth_response.body)
14
+ end
15
+
16
+ # Destroys the status specified by the required ID parameter
17
+ def status_destroy(id)
18
+ oauth_response = access_token.post("/statuses/destroy/#{id}.json")
19
+ JSON.parse(oauth_response.body)
20
+ end
21
+
22
+ # Retweets the tweet specified by the id parameter. Returns the original tweet with retweet details embedded.
23
+ def retweet(id)
24
+ oauth_response = access_token.post("/statuses/retweet/#{id}.json")
25
+ JSON.parse(oauth_response.body)
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,64 @@
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
+ oauth_response = access_token.get("/statuses/public_timeline.json?#{args}")
8
+ JSON.parse(oauth_response.body)
9
+ end
10
+
11
+ # Returns the 20 most recent statuses, including retweets, posted by the authenticating user and that user's friends.
12
+ # This is the equivalent of /timeline/home on the Web.
13
+ def home_timeline(options={})
14
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
15
+ oauth_response = access_token.get("/statuses/home_timeline.json?#{args}")
16
+ JSON.parse(oauth_response.body)
17
+ end
18
+
19
+ # Returns the 20 most recent statuses posted by the authenticating user and that user's friends.
20
+ def friends_timeline(options={})
21
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
22
+ oauth_response = access_token.get("/statuses/friends_timeline.json?#{args}")
23
+ JSON.parse(oauth_response.body)
24
+ end
25
+
26
+ # Returns the 20 most recent statuses posted from the authenticating user.
27
+ def user_timeline(options={})
28
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
29
+ oauth_response = access_token.get("/statuses/user_timeline.json?#{args}")
30
+ JSON.parse(oauth_response.body)
31
+ end
32
+ alias :user :user_timeline
33
+
34
+ # Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
35
+ def mentions(options={})
36
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
37
+ oauth_response = access_token.get("/statuses/mentions.json?#{args}")
38
+ JSON.parse(oauth_response.body)
39
+ end
40
+ alias :replies :mentions
41
+
42
+ # Returns the 20 most recent retweets posted by the authenticating user
43
+ def retweeted_by_me(options={})
44
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
45
+ oauth_response = access_token.get("/statuses/retweeted_by_me.json?#{args}")
46
+ JSON.parse(oauth_response.body)
47
+ end
48
+
49
+ # Returns the 20 most recent retweets posted by the authenticating user's friends.
50
+ def retweeted_to_me(options={})
51
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
52
+ oauth_response = access_token.get("/statuses/retweeted_to_me.json?#{args}")
53
+ JSON.parse(oauth_response.body)
54
+ end
55
+
56
+ # Returns the 20 most recent tweets of the authenticated user that have been retweeted by others.
57
+ def retweets_of_me(options={})
58
+ args = options.map{|k,v| "#{k}=#{v}"}.join('&')
59
+ oauth_response = access_token.get("/statuses/retweets_of_me.json?#{args}")
60
+ JSON.parse(oauth_response.body)
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,17 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ # Returns the 100 last friends
5
+ def friends(page=1)
6
+ oauth_response = access_token.get("/statuses/friends.json?page=#{page}")
7
+ JSON.parse(oauth_response.body)
8
+ end
9
+
10
+ # Returns the 100 last followers
11
+ def followers(page=1)
12
+ oauth_response = access_token.get("/statuses/followers.json?page=#{page}")
13
+ JSON.parse(oauth_response.body)
14
+ end
15
+
16
+ end
17
+ 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,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter_oauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Richard Taylor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-20 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: oauth
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.3.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.2
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: mime-types
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "1.15"
44
+ version:
45
+ description: twitter_oauth is a Ruby library for talking to twitter using the new oauth method.
46
+ email: moomerman@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files: []
52
+
53
+ files:
54
+ - README.textile
55
+ - lib/twitter_oauth.rb
56
+ - lib/twitter_oauth/client.rb
57
+ - lib/twitter_oauth/timeline.rb
58
+ - lib/twitter_oauth/account.rb
59
+ - lib/twitter_oauth/status.rb
60
+ - lib/twitter_oauth/direct_messages.rb
61
+ - lib/twitter_oauth/search.rb
62
+ - lib/twitter_oauth/blocks.rb
63
+ - lib/twitter_oauth/friendships.rb
64
+ - lib/twitter_oauth/notifications.rb
65
+ - lib/twitter_oauth/user.rb
66
+ - lib/twitter_oauth/favorites.rb
67
+ - lib/twitter_oauth/utils.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/moomerman/twitter_oauth
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --inline-source
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project: twitter_oauth
93
+ rubygems_version: 1.3.5
94
+ signing_key:
95
+ specification_version: 2
96
+ summary: twitter_oauth is a Ruby library for talking to twitter using the new oauth method.
97
+ test_files: []
98
+