contacthrk-twitter_oauth 0.1.20

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile ADDED
@@ -0,0 +1,72 @@
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
41
+
42
+ request_token.authorize_url
43
+ => http://twitter.com/oauth/authorize?oauth_token=TOKEN
44
+ </code></pre>
45
+
46
+ In your application your user would be redirected to Twitter to authorize the application at this point. You'll need to store
47
+ the request token (usually in the session) for later. The code continues below assuming the user has authorized your application.
48
+
49
+ <pre><code>access_token = client.authorize(
50
+ request_token.token,
51
+ request_token.secret
52
+ )
53
+
54
+ client.authorized?
55
+ => true
56
+
57
+ client.update('checking out the twitter_oauth library') # sends a twitter status update
58
+ </code></pre>
59
+
60
+ 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.
61
+
62
+ <pre><code>access_token = @user.access_token # assuming @user
63
+ client = TwitterOAuth::Client.new(
64
+ :consumer_key => 'YOUR_CONSUMER_KEY',
65
+ :consumer_secret => 'YOUR-CONSUMER-SECRET',
66
+ :token => access_token.token,
67
+ :secret => access_token.secret
68
+ )
69
+
70
+ client.authorized?
71
+ => true
72
+ </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,72 @@
1
+ require 'twitter_oauth/account'
2
+ require 'twitter_oauth/statuses'
3
+ require 'twitter_oauth/direct_messages'
4
+ require 'twitter_oauth/search'
5
+ require 'twitter_oauth/notifications'
6
+ require 'twitter_oauth/blocks'
7
+ require 'twitter_oauth/friendships'
8
+ require 'twitter_oauth/user'
9
+ require 'twitter_oauth/favorites'
10
+ require 'twitter_oauth/utils'
11
+
12
+ module TwitterOAuth
13
+ class Client
14
+
15
+ def initialize(options = {})
16
+ @consumer_key = options[:consumer_key]
17
+ @consumer_secret = options[:consumer_secret]
18
+ @token = options[:token]
19
+ @secret = options[:secret]
20
+ end
21
+
22
+ def authorize(token, secret)
23
+ request_token = OAuth::RequestToken.new(
24
+ consumer, token, secret
25
+ )
26
+ @access_token = request_token.get_access_token
27
+ @token = @access_token.token
28
+ @secret = @access_token.secret
29
+ @access_token
30
+ end
31
+
32
+ def authentication_request_token
33
+ consumer.options[:authorize_path] = '/oauth/authenticate'
34
+ request_token
35
+ end
36
+
37
+ def show(username)
38
+ oauth_response = access_token.get("/users/show/#{username}.json")
39
+ JSON.parse(oauth_response.body)
40
+ end
41
+
42
+ # Added by HK: to get latest tweets from any user
43
+ def get_tweets_from(username)
44
+ user = show(username)
45
+ if user['error']
46
+ return user
47
+ else
48
+ url = "/statuses/user_timeline/#{user['id']}.json"
49
+ oauth_response = access_token.get(url)
50
+ JSON.parse(oauth_response.body)
51
+ end
52
+ end
53
+
54
+ def request_token
55
+ consumer.get_request_token
56
+ end
57
+
58
+ private
59
+ def consumer
60
+ @consumer ||= OAuth::Consumer.new(
61
+ @consumer_key,
62
+ @consumer_secret,
63
+ { :site=>"http://twitter.com" }
64
+ )
65
+ end
66
+
67
+ def access_token
68
+ @access_token ||= OAuth::AccessToken.new(consumer, @token, @secret)
69
+ end
70
+ end
71
+ end
72
+
@@ -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,33 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ def friends_ids
5
+ oauth_response = access_token.get("/friends/ids.json")
6
+ JSON.parse(oauth_response.body)
7
+ end
8
+
9
+ def followers_ids
10
+ oauth_response = access_token.get("/followers/ids.json")
11
+ JSON.parse(oauth_response.body)
12
+ end
13
+
14
+ # friend this user.
15
+ def friend(id)
16
+ oauth_response = access_token.post("/friendships/create/#{id}.json")
17
+ JSON.parse(oauth_response.body)
18
+ end
19
+
20
+ # unfriend.
21
+ def unfriend(id)
22
+ oauth_response = access_token.post("/friendships/destroy/#{id}.json")
23
+ JSON.parse(oauth_response.body)
24
+ end
25
+
26
+ # exists?.
27
+ def exists?(a, b)
28
+ oauth_response = access_token.get("/friendships/exists.json?user_a=#{a}&user_b=#{b}")
29
+ oauth_response.body.strip == 'true'
30
+ end
31
+
32
+ end
33
+ 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,16 @@
1
+ require 'open-uri'
2
+ require 'ostruct'
3
+
4
+ module TwitterOAuth
5
+ class Client
6
+
7
+ def search(q, page = 1, per_page = 20)
8
+ response = open("http://search.twitter.com/search.json?q=#{URI.escape(q)}&page=#{page}&rpp=#{per_page}")
9
+ search_result = JSON.parse(response.read)
10
+ search_result = OpenStruct.new(search_result)
11
+ search_result.results = search_result.results.collect{|x| OpenStruct.new(x)}
12
+ search_result
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,52 @@
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
6
+ oauth_response = access_token.get('/statuses/public_timeline.json')
7
+ JSON.parse(oauth_response.body)
8
+ end
9
+
10
+ # Returns the 20 most recent statuses posted by the authenticating user and that user's friends.
11
+ def friends_timeline(rpp=20, page=1)
12
+ oauth_response = access_token.get("/statuses/friends_timeline.json?count=#{rpp}&page=#{page}")
13
+ JSON.parse(oauth_response.body)
14
+ end
15
+
16
+ # Returns the 20 most recent statuses posted from the authenticating user.
17
+ def user(page=1)
18
+ oauth_response = access_token.get("/statuses/user_timeline.json?page=#{page}")
19
+ JSON.parse(oauth_response.body)
20
+ end
21
+
22
+ # Returns a single status, specified by the id parameter below.
23
+ def status(id)
24
+ oauth_response = access_token.get("/statuses/show/#{id}.json")
25
+ JSON.parse(oauth_response.body)
26
+ end
27
+
28
+ # Updates the authenticating user's status.
29
+ def update(message)
30
+ oauth_response = access_token.post('/statuses/update.json', :status => message)
31
+ JSON.parse(oauth_response.body)
32
+ end
33
+
34
+ # Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
35
+ def replies(page=1)
36
+ oauth_response = access_token.get("/statuses/mentions.json?page=#{page}")
37
+ JSON.parse(oauth_response.body)
38
+ end
39
+
40
+ # alias
41
+ def mentions
42
+ replies
43
+ end
44
+
45
+ # Destroys the status specified by the required ID parameter
46
+ def status_destroy(id)
47
+ oauth_response = access_token.post("/statuses/destroy/#{id}.json")
48
+ JSON.parse(oauth_response.body)
49
+ end
50
+
51
+ end
52
+ 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,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: contacthrk-twitter_oauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.20
5
+ platform: ruby
6
+ authors:
7
+ - Richard Taylor
8
+ - Hemant Khemani
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-03-20 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: oauth
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.3.1
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ type: :runtime
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 1.1.2
35
+ version:
36
+ - !ruby/object:Gem::Dependency
37
+ name: mime-types
38
+ type: :runtime
39
+ version_requirement:
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "1.15"
45
+ version:
46
+ description: twitter_oauth is a Ruby library for talking to twitter using the new oauth method.
47
+ email: contacthrk@gmail.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files: []
53
+
54
+ files:
55
+ - README.textile
56
+ - lib/twitter_oauth
57
+ - lib/twitter_oauth.rb
58
+ - lib/twitter_oauth/client.rb
59
+ - lib/twitter_oauth/account.rb
60
+ - lib/twitter_oauth/statuses.rb
61
+ - lib/twitter_oauth/direct_messages.rb
62
+ - lib/twitter_oauth/search.rb
63
+ - lib/twitter_oauth/blocks.rb
64
+ - lib/twitter_oauth/friendships.rb
65
+ - lib/twitter_oauth/notifications.rb
66
+ - lib/twitter_oauth/user.rb
67
+ - lib/twitter_oauth/favorites.rb
68
+ - lib/twitter_oauth/utils.rb
69
+ has_rdoc: false
70
+ homepage: http://github.com/contacthrk/twitter_oauth
71
+ post_install_message:
72
+ rdoc_options:
73
+ - --inline-source
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project: twitter_oauth
92
+ rubygems_version: 1.2.0
93
+ signing_key:
94
+ specification_version: 2
95
+ summary: twitter_oauth is a Ruby library for talking to twitter using the new oauth method.
96
+ test_files: []
97
+