precision-twitter_oauth 0.1.9.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,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,8 @@
1
+ require 'rubygems'
2
+ require 'oauth'
3
+ require 'json'
4
+
5
+ require 'twitter_oauth/client'
6
+
7
+ module TwitterOAuth
8
+ end
@@ -0,0 +1,18 @@
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 the remaining number of API requests available to the requesting user before the API limit is reached for the current hour.
12
+ def rate_limit_status
13
+ oauth_response = access_token.get('/account/rate_limit_status.json')
14
+ JSON.parse(oauth_response.body)
15
+ end
16
+
17
+ end
18
+ 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,52 @@
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
+
9
+ module TwitterOAuth
10
+ class Client
11
+
12
+ def initialize(options = {})
13
+ @consumer_key = options[:consumer_key]
14
+ @consumer_secret = options[:consumer_secret]
15
+ @token = options[:token]
16
+ @secret = options[:secret]
17
+ end
18
+
19
+ def authorize(token, secret)
20
+ request_token = OAuth::RequestToken.new(
21
+ consumer, token, secret
22
+ )
23
+ @access_token = request_token.get_access_token
24
+ @token = @access_token.token
25
+ @secret = @access_token.secret
26
+ @access_token
27
+ end
28
+
29
+ def show(username)
30
+ oauth_response = access_token.get("/users/show/#{username}.json")
31
+ JSON.parse(oauth_response.body)
32
+ end
33
+
34
+ def request_token
35
+ consumer.get_request_token
36
+ end
37
+
38
+ private
39
+ def consumer
40
+ @consumer ||= OAuth::Consumer.new(
41
+ @consumer_key,
42
+ @consumer_secret,
43
+ { :site=>"http://twitter.com" }
44
+ )
45
+ end
46
+
47
+ def access_token
48
+ @access_token ||= OAuth::AccessToken.new(consumer, @token, @secret)
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,23 @@
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
6
+ oauth_response = access_token.get('/direct_messages.json')
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
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ module TwitterOAuth
2
+ class Client
3
+
4
+ def friends
5
+ oauth_response = access_token.post("friends/ids.json")
6
+ JSON.parse(oauth_response.body)
7
+ end
8
+
9
+ # friend this user.
10
+ def friend(id)
11
+ oauth_response = access_token.post("/friendships/create/#{id}.json")
12
+ JSON.parse(oauth_response.body)
13
+ end
14
+
15
+ # unfriend.
16
+ def unfriend(id)
17
+ oauth_response = access_token.post("/friendships/destroy/#{id}.json")
18
+ JSON.parse(oauth_response.body)
19
+ end
20
+
21
+ # exists?.
22
+ def exists?(a, b)
23
+ oauth_response = access_token.get("/friendships/exists.json?user_a=#{a}&user_b=#{b}")
24
+ JSON.parse(oauth_response.body)
25
+ end
26
+
27
+ end
28
+ 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,41 @@
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
12
+ oauth_response = access_token.get('/statuses/friends_timeline.json')
13
+ JSON.parse(oauth_response.body)
14
+ end
15
+
16
+ # Returns the 20 most recent statuses posted from the authenticating user.
17
+ def user
18
+ oauth_response = access_token.get('/statuses/user_timeline.json')
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
36
+ oauth_response = access_token.get('/statuses/replies.json')
37
+ JSON.parse(oauth_response.body)
38
+ end
39
+
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: precision-twitter_oauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.9.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
+ description: twitter_oauth is a Ruby library for talking to twitter using the new oauth method.
36
+ email: moomerman@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - README.textile
45
+ - lib/twitter_oauth
46
+ - lib/twitter_oauth.rb
47
+ - lib/twitter_oauth/client.rb
48
+ - lib/twitter_oauth/account.rb
49
+ - lib/twitter_oauth/statuses.rb
50
+ - lib/twitter_oauth/direct_messages.rb
51
+ - lib/twitter_oauth/search.rb
52
+ - lib/twitter_oauth/blocks.rb
53
+ - lib/twitter_oauth/friendships.rb
54
+ has_rdoc: false
55
+ homepage: http://github.com/moomerman/twitter_oauth
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --inline-source
59
+ - --charset=UTF-8
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project: twitter_oauth
77
+ rubygems_version: 1.2.0
78
+ signing_key:
79
+ specification_version: 2
80
+ summary: twitter_oauth is a Ruby library for talking to twitter using the new oauth method.
81
+ test_files: []
82
+