dschn-twitter_oauth 0.1.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.textile ADDED
@@ -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,60 @@
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, options = {})
23
+ request_token = OAuth::RequestToken.new(
24
+ consumer, token, secret
25
+ )
26
+ @access_token = request_token.get_access_token(options)
27
+ @token = @access_token.token
28
+ @secret = @access_token.secret
29
+ @access_token
30
+ end
31
+
32
+ def authentication_request_token(options = {})
33
+ consumer.options[:authorize_path] = '/oauth/authenticate'
34
+ request_token(options)
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
+ def request_token(options={})
43
+ consumer.get_request_token(options)
44
+ end
45
+
46
+ private
47
+ def consumer
48
+ @consumer ||= OAuth::Consumer.new(
49
+ @consumer_key,
50
+ @consumer_secret,
51
+ { :site=>"http://twitter.com" }
52
+ )
53
+ end
54
+
55
+ def access_token
56
+ @access_token ||= OAuth::AccessToken.new(consumer, @token, @secret)
57
+ end
58
+ end
59
+ end
60
+
@@ -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, options = {})
30
+ oauth_response = access_token.post('/statuses/update.json', options.merge(: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,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dschn-twitter_oauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.20.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 -07: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
56
+ - lib/twitter_oauth.rb
57
+ - lib/twitter_oauth/client.rb
58
+ - lib/twitter_oauth/account.rb
59
+ - lib/twitter_oauth/statuses.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: false
69
+ homepage: http://github.com/moomerman/twitter_oauth
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --inline-source
73
+ - --charset=UTF-8
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ version:
88
+ requirements: []
89
+
90
+ rubyforge_project: twitter_oauth
91
+ rubygems_version: 1.2.0
92
+ signing_key:
93
+ specification_version: 2
94
+ summary: twitter_oauth is a Ruby library for talking to twitter using the new oauth method.
95
+ test_files: []
96
+