rorra-twitter 0.9.9
Sign up to get free protection for your applications and to get access to all the features.
- data/History +290 -0
- data/License +20 -0
- data/Notes +33 -0
- data/README.rdoc +19 -0
- data/Rakefile +36 -0
- data/VERSION.yml +5 -0
- data/examples/connect.rb +30 -0
- data/examples/friendship_existence.rb +13 -0
- data/examples/helpers/config_store.rb +38 -0
- data/examples/httpauth.rb +11 -0
- data/examples/ids.rb +13 -0
- data/examples/lists.rb +11 -0
- data/examples/oauth.rb +27 -0
- data/examples/search.rb +15 -0
- data/examples/timeline.rb +19 -0
- data/examples/tumblr.rb +9 -0
- data/examples/unauthorized.rb +16 -0
- data/examples/update.rb +11 -0
- data/examples/user.rb +5 -0
- data/lib/twitter.rb +156 -0
- data/lib/twitter/base.rb +393 -0
- data/lib/twitter/geo.rb +25 -0
- data/lib/twitter/httpauth.rb +53 -0
- data/lib/twitter/local_trends.rb +30 -0
- data/lib/twitter/oauth.rb +64 -0
- data/lib/twitter/request.rb +71 -0
- data/lib/twitter/search.rb +163 -0
- data/lib/twitter/trends.rb +55 -0
- data/test/fixtures/blocking.json +1632 -0
- data/test/fixtures/firehose.json +1 -0
- data/test/fixtures/follower_ids.json +1 -0
- data/test/fixtures/followers.json +1 -0
- data/test/fixtures/friend_ids.json +1 -0
- data/test/fixtures/friends_timeline.json +1 -0
- data/test/fixtures/friendship.json +1 -0
- data/test/fixtures/friendship_exists.json +1 -0
- data/test/fixtures/geo_place.json +1 -0
- data/test/fixtures/geo_reverse_geocode.json +1 -0
- data/test/fixtures/geo_reverse_geocode_granularity.json +1 -0
- data/test/fixtures/geo_reverse_geocode_limit.json +1 -0
- data/test/fixtures/geo_search.json +1 -0
- data/test/fixtures/geo_search_ip_address.json +1 -0
- data/test/fixtures/geo_search_query.json +1 -0
- data/test/fixtures/home_timeline.json +1 -0
- data/test/fixtures/ids.json +1 -0
- data/test/fixtures/list.json +1 -0
- data/test/fixtures/list_statuses.json +1 -0
- data/test/fixtures/list_statuses_1_1.json +1 -0
- data/test/fixtures/list_statuses_2_1.json +1 -0
- data/test/fixtures/list_subscriptions.json +1 -0
- data/test/fixtures/list_users.json +1 -0
- data/test/fixtures/lists.json +1 -0
- data/test/fixtures/memberships.json +1 -0
- data/test/fixtures/mentions.json +1 -0
- data/test/fixtures/not_found.json +1 -0
- data/test/fixtures/people_search.json +39 -0
- data/test/fixtures/rate_limit_exceeded.json +1 -0
- data/test/fixtures/report_spam.json +41 -0
- data/test/fixtures/retweet.json +1 -0
- data/test/fixtures/retweeted_by_me.json +1 -0
- data/test/fixtures/retweeted_to_me.json +1 -0
- data/test/fixtures/retweeters_of_tweet.json +166 -0
- data/test/fixtures/retweets.json +1 -0
- data/test/fixtures/retweets_of_me.json +1 -0
- data/test/fixtures/sample-image.png +0 -0
- data/test/fixtures/saved_search.json +7 -0
- data/test/fixtures/saved_searches.json +16 -0
- data/test/fixtures/search.json +1 -0
- data/test/fixtures/search_from_jnunemaker.json +1 -0
- data/test/fixtures/status.json +1 -0
- data/test/fixtures/status_show.json +1 -0
- data/test/fixtures/trends_available.json +253 -0
- data/test/fixtures/trends_current.json +1 -0
- data/test/fixtures/trends_current_exclude.json +1 -0
- data/test/fixtures/trends_daily.json +1925 -0
- data/test/fixtures/trends_daily_date.json +1 -0
- data/test/fixtures/trends_daily_exclude.json +1 -0
- data/test/fixtures/trends_location.json +57 -0
- data/test/fixtures/trends_weekly.json +1 -0
- data/test/fixtures/trends_weekly_date.json +1 -0
- data/test/fixtures/trends_weekly_exclude.json +1 -0
- data/test/fixtures/unauthorized.json +1 -0
- data/test/fixtures/update_profile_background_image.json +1 -0
- data/test/fixtures/update_profile_image.json +1 -0
- data/test/fixtures/user.json +1 -0
- data/test/fixtures/user_timeline.json +710 -0
- data/test/fixtures/users.json +1 -0
- data/test/test_helper.rb +47 -0
- data/test/twitter/base_test.rb +426 -0
- data/test/twitter/geo_test.rb +79 -0
- data/test/twitter/httpauth_test.rb +86 -0
- data/test/twitter/oauth_test.rb +108 -0
- data/test/twitter/request_test.rb +217 -0
- data/test/twitter/search_test.rb +208 -0
- data/test/twitter/trends_test.rb +112 -0
- data/test/twitter_test.rb +106 -0
- metadata +328 -0
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class GeoTest < Test::Unit::TestCase
|
4
|
+
include Twitter
|
5
|
+
|
6
|
+
context "Geographic place lookup" do
|
7
|
+
|
8
|
+
should "work" do
|
9
|
+
stub_get 'http://api.twitter.com/1/geo/id/ea76a36c5bc2bdff.json', 'geo_place.json'
|
10
|
+
place = Geo.place('ea76a36c5bc2bdff')
|
11
|
+
place.country.should == 'The United States of America'
|
12
|
+
place.full_name.should == 'Ballantyne West, Charlotte'
|
13
|
+
place.geometry.coordinates.should be_kind_of(Array)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
context "Geographic search" do
|
19
|
+
|
20
|
+
should "work" do
|
21
|
+
stub_get 'http://api.twitter.com:80/1/geo/search.json?lat=35.061161&long=-80.854568', 'geo_search.json'
|
22
|
+
places = Geo.search(:lat => 35.061161, :long => -80.854568)
|
23
|
+
places.size.should == 3
|
24
|
+
places[0].full_name.should eql('Ballantyne West, Charlotte')
|
25
|
+
places[0].name.should eql('Ballantyne West')
|
26
|
+
end
|
27
|
+
|
28
|
+
should "be able to search with free form text" do
|
29
|
+
stub_get 'http://api.twitter.com/1/geo/search.json?query=princeton%20record%20exchange', 'geo_search_query.json'
|
30
|
+
places = Geo.search(:query => 'princeton record exchange')
|
31
|
+
places.size.should == 1
|
32
|
+
places[0].name.should eql('Princeton Record Exchange')
|
33
|
+
places[0].place_type.should eql('poi')
|
34
|
+
places[0].attributes.street_address.should eql('20 S Tulane St')
|
35
|
+
end
|
36
|
+
|
37
|
+
should "be able to search by ip address" do
|
38
|
+
stub_get 'http://api.twitter.com/1/geo/search.json?ip=74.125.19.104', 'geo_search_ip_address.json'
|
39
|
+
places = Geo.search(:ip => '74.125.19.104')
|
40
|
+
places.size.should == 4
|
41
|
+
places[0].full_name.should eql("Mountain View, CA")
|
42
|
+
places[0].name.should eql("Mountain View")
|
43
|
+
places[1].full_name.should eql("Sunnyvale, CA")
|
44
|
+
places[1].name.should eql('Sunnyvale')
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
context "Geographic reverse_geocode" do
|
50
|
+
|
51
|
+
should "work" do
|
52
|
+
stub_get 'http://api.twitter.com:80/1/geo/reverse_geocode.json?lat=35.061161&long=-80.854568', 'geo_reverse_geocode.json'
|
53
|
+
places = Geo.reverse_geocode(:lat => 35.061161, :long => -80.854568)
|
54
|
+
places.size.should == 4
|
55
|
+
places[0].full_name.should eql('Ballantyne West, Charlotte')
|
56
|
+
places[0].name.should eql('Ballantyne West')
|
57
|
+
end
|
58
|
+
|
59
|
+
should "be able to limit the number of results returned" do
|
60
|
+
stub_get 'http://api.twitter.com/1/geo/reverse_geocode.json?lat=35.061161&max_results=2&long=-80.854568', 'geo_reverse_geocode_limit.json'
|
61
|
+
places = Geo.reverse_geocode(:lat => 35.061161, :long => -80.854568, :max_results => 2)
|
62
|
+
places.size.should == 2
|
63
|
+
places[0].full_name.should eql('Ballantyne West, Charlotte')
|
64
|
+
places[0].name.should eql('Ballantyne West')
|
65
|
+
end
|
66
|
+
|
67
|
+
should "be able to lookup with granularity" do
|
68
|
+
stub_get 'http://api.twitter.com/1/geo/reverse_geocode.json?lat=35.061161&long=-80.854568&granularity=city', 'geo_reverse_geocode_granularity.json'
|
69
|
+
places = Geo.reverse_geocode(:lat => 35.061161, :long => -80.854568, :granularity => 'city')
|
70
|
+
places.size.should == 3
|
71
|
+
places[0].full_name.should eql('Charlotte, NC')
|
72
|
+
places[0].name.should eql('Charlotte')
|
73
|
+
places[1].full_name.should eql('North Carolina, US')
|
74
|
+
places[1].name.should eql('North Carolina')
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class HTTPAuthTest < Test::Unit::TestCase
|
4
|
+
context "Creating new instance" do
|
5
|
+
should "should take user and password" do
|
6
|
+
twitter = Twitter::HTTPAuth.new('username', 'password')
|
7
|
+
twitter.username.should == 'username'
|
8
|
+
twitter.password.should == 'password'
|
9
|
+
end
|
10
|
+
|
11
|
+
should "accept options" do
|
12
|
+
twitter = Twitter::HTTPAuth.new('username', 'password', :ssl => true)
|
13
|
+
twitter.options.should == {:ssl => true}
|
14
|
+
end
|
15
|
+
|
16
|
+
should "default ssl to false" do
|
17
|
+
twitter = Twitter::HTTPAuth.new('username', 'password')
|
18
|
+
twitter.options[:ssl].should be(false)
|
19
|
+
end
|
20
|
+
|
21
|
+
should "use https if ssl is true" do
|
22
|
+
Twitter::HTTPAuth.expects(:base_uri).with('https://api.twitter.com/' + Twitter::API_VERSION)
|
23
|
+
twitter = Twitter::HTTPAuth.new('username', 'password', :ssl => true)
|
24
|
+
end
|
25
|
+
|
26
|
+
should "use http if ssl is false" do
|
27
|
+
Twitter::HTTPAuth.expects(:base_uri).with('http://api.twitter.com/' + Twitter::API_VERSION)
|
28
|
+
twitter = Twitter::HTTPAuth.new('username', 'password', :ssl => false)
|
29
|
+
end
|
30
|
+
|
31
|
+
should "use api version if provided" do
|
32
|
+
Twitter::HTTPAuth.expects(:base_uri).with('http://api.twitter.com/2')
|
33
|
+
twitter = Twitter::HTTPAuth.new('username', 'password', {:ssl => false, :api_version => 2})
|
34
|
+
end
|
35
|
+
|
36
|
+
should "not use api versioning if api_version is false " do
|
37
|
+
Twitter::HTTPAuth.expects(:base_uri).with('http://api.twitter.com')
|
38
|
+
twitter = Twitter::HTTPAuth.new('username', 'password', {:ssl => false, :api_version => false})
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "Client methods" do
|
43
|
+
setup do
|
44
|
+
@twitter = Twitter::HTTPAuth.new('username', 'password')
|
45
|
+
end
|
46
|
+
|
47
|
+
should "not throw error when accessing response message" do
|
48
|
+
stub_get('http://api.twitter.com:80/1/statuses/user_timeline.json', 'user_timeline.json')
|
49
|
+
response = @twitter.get('/statuses/user_timeline.json')
|
50
|
+
response.message.should == 'OK'
|
51
|
+
end
|
52
|
+
|
53
|
+
should "be able to get" do
|
54
|
+
stub_get('http://username:password@api.twitter.com:80/1/statuses/user_timeline.json', 'user_timeline.json')
|
55
|
+
response = @twitter.get('/statuses/user_timeline.json')
|
56
|
+
response.should == fixture_file('user_timeline.json')
|
57
|
+
end
|
58
|
+
|
59
|
+
should "be able to get with headers" do
|
60
|
+
@twitter.class.expects(:get).with(
|
61
|
+
'/statuses/user_timeline.json', {
|
62
|
+
:basic_auth => {:username => 'username', :password => 'password'},
|
63
|
+
:headers => {'Foo' => 'Bar'}
|
64
|
+
}
|
65
|
+
).returns(fixture_file('user_timeline.json'))
|
66
|
+
@twitter.get('/statuses/user_timeline.json', {'Foo' => 'Bar'})
|
67
|
+
end
|
68
|
+
|
69
|
+
should "be able to post" do
|
70
|
+
stub_post('http://username:password@api.twitter.com:80/1/statuses/update.json', 'status.json')
|
71
|
+
response = @twitter.post('/statuses/update.json', :text => 'My update.')
|
72
|
+
response.should == fixture_file('status.json')
|
73
|
+
end
|
74
|
+
|
75
|
+
should "be able to post with headers" do
|
76
|
+
@twitter.class.expects(:post).with(
|
77
|
+
'/statuses/update.json', {
|
78
|
+
:headers => {'Foo' => 'Bar'},
|
79
|
+
:body => {:text => 'My update.'},
|
80
|
+
:basic_auth => {:username => 'username', :password => 'password'}
|
81
|
+
}
|
82
|
+
).returns(fixture_file('status.json'))
|
83
|
+
@twitter.post('/statuses/update.json', {:text => 'My update.'}, {'Foo' => 'Bar'})
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class OAuthTest < Test::Unit::TestCase
|
4
|
+
should "initialize with consumer token and secret" do
|
5
|
+
twitter = Twitter::OAuth.new('token', 'secret')
|
6
|
+
|
7
|
+
twitter.ctoken.should == 'token'
|
8
|
+
twitter.csecret.should == 'secret'
|
9
|
+
end
|
10
|
+
|
11
|
+
should "set authorization path to '/oauth/authorize' by default" do
|
12
|
+
twitter = Twitter::OAuth.new('token', 'secret')
|
13
|
+
twitter.consumer.options[:authorize_path].should == '/oauth/authorize'
|
14
|
+
end
|
15
|
+
|
16
|
+
should "set authorization path to '/oauth/authenticate' if sign_in_with_twitter" do
|
17
|
+
twitter = Twitter::OAuth.new('token', 'secret', :sign_in => true)
|
18
|
+
twitter.consumer.options[:authorize_path].should == '/oauth/authenticate'
|
19
|
+
end
|
20
|
+
|
21
|
+
should "have a consumer" do
|
22
|
+
consumer = mock('oauth consumer')
|
23
|
+
OAuth::Consumer.expects(:new).with('token', 'secret', {:site => 'http://api.twitter.com'}).returns(consumer)
|
24
|
+
twitter = Twitter::OAuth.new('token', 'secret')
|
25
|
+
|
26
|
+
twitter.consumer.should == consumer
|
27
|
+
end
|
28
|
+
|
29
|
+
should "have a request token from the consumer" do
|
30
|
+
consumer = mock('oauth consumer')
|
31
|
+
request_token = mock('request token')
|
32
|
+
consumer.expects(:get_request_token).returns(request_token)
|
33
|
+
OAuth::Consumer.expects(:new).with('token', 'secret', {:site => 'http://api.twitter.com', :request_endpoint => 'http://api.twitter.com'}).returns(consumer)
|
34
|
+
twitter = Twitter::OAuth.new('token', 'secret')
|
35
|
+
|
36
|
+
twitter.request_token.should == request_token
|
37
|
+
end
|
38
|
+
|
39
|
+
context "set_callback_url" do
|
40
|
+
should "clear request token and set the callback url" do
|
41
|
+
consumer = mock('oauth consumer')
|
42
|
+
request_token = mock('request token')
|
43
|
+
|
44
|
+
OAuth::Consumer.
|
45
|
+
expects(:new).
|
46
|
+
with('token', 'secret', {:site => 'http://api.twitter.com', :request_endpoint => 'http://api.twitter.com'}).
|
47
|
+
returns(consumer)
|
48
|
+
|
49
|
+
twitter = Twitter::OAuth.new('token', 'secret')
|
50
|
+
|
51
|
+
consumer.
|
52
|
+
expects(:get_request_token).
|
53
|
+
with({:oauth_callback => 'http://myapp.com/oauth_callback'})
|
54
|
+
|
55
|
+
twitter.set_callback_url('http://myapp.com/oauth_callback')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
should "be able to create access token from request token, request secret and verifier" do
|
60
|
+
twitter = Twitter::OAuth.new('token', 'secret')
|
61
|
+
consumer = OAuth::Consumer.new('token', 'secret', {:site => 'http://api.twitter.com'})
|
62
|
+
twitter.stubs(:signing_consumer).returns(consumer)
|
63
|
+
|
64
|
+
access_token = mock('access token', :token => 'atoken', :secret => 'asecret')
|
65
|
+
request_token = mock('request token')
|
66
|
+
request_token.
|
67
|
+
expects(:get_access_token).
|
68
|
+
with(:oauth_verifier => 'verifier').
|
69
|
+
returns(access_token)
|
70
|
+
|
71
|
+
OAuth::RequestToken.
|
72
|
+
expects(:new).
|
73
|
+
with(consumer, 'rtoken', 'rsecret').
|
74
|
+
returns(request_token)
|
75
|
+
|
76
|
+
twitter.authorize_from_request('rtoken', 'rsecret', 'verifier')
|
77
|
+
twitter.access_token.class.should be(OAuth::AccessToken)
|
78
|
+
twitter.access_token.token.should == 'atoken'
|
79
|
+
twitter.access_token.secret.should == 'asecret'
|
80
|
+
end
|
81
|
+
|
82
|
+
should "be able to create access token from access token and secret" do
|
83
|
+
twitter = Twitter::OAuth.new('token', 'secret')
|
84
|
+
consumer = OAuth::Consumer.new('token', 'secret', {:site => 'http://api.twitter.com'})
|
85
|
+
twitter.stubs(:consumer).returns(consumer)
|
86
|
+
|
87
|
+
twitter.authorize_from_access('atoken', 'asecret')
|
88
|
+
twitter.access_token.class.should be(OAuth::AccessToken)
|
89
|
+
twitter.access_token.token.should == 'atoken'
|
90
|
+
twitter.access_token.secret.should == 'asecret'
|
91
|
+
end
|
92
|
+
|
93
|
+
should "delegate get to access token" do
|
94
|
+
access_token = mock('access token')
|
95
|
+
twitter = Twitter::OAuth.new('token', 'secret')
|
96
|
+
twitter.stubs(:access_token).returns(access_token)
|
97
|
+
access_token.expects(:get).returns(nil)
|
98
|
+
twitter.get('/foo')
|
99
|
+
end
|
100
|
+
|
101
|
+
should "delegate post to access token" do
|
102
|
+
access_token = mock('access token')
|
103
|
+
twitter = Twitter::OAuth.new('token', 'secret')
|
104
|
+
twitter.stubs(:access_token).returns(access_token)
|
105
|
+
access_token.expects(:post).returns(nil)
|
106
|
+
twitter.post('/foo')
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,217 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class RequestTest < Test::Unit::TestCase
|
4
|
+
context "new get request" do
|
5
|
+
setup do
|
6
|
+
@client = mock('twitter client')
|
7
|
+
@request = Twitter::Request.new(@client, :get, '/1/statuses/user_timeline.json', {:query => {:since_id => 1234}})
|
8
|
+
end
|
9
|
+
|
10
|
+
should "have client" do
|
11
|
+
@request.client.should == @client
|
12
|
+
end
|
13
|
+
|
14
|
+
should "have method" do
|
15
|
+
@request.method.should == :get
|
16
|
+
end
|
17
|
+
|
18
|
+
should "have path" do
|
19
|
+
@request.path.should == '/1/statuses/user_timeline.json'
|
20
|
+
end
|
21
|
+
|
22
|
+
should "have options" do
|
23
|
+
@request.options[:query].should == {:since_id => 1234}
|
24
|
+
end
|
25
|
+
|
26
|
+
should "have uri" do
|
27
|
+
@request.uri.should == '/1/statuses/user_timeline.json?since_id=1234'
|
28
|
+
end
|
29
|
+
|
30
|
+
context "performing request for collection" do
|
31
|
+
setup do
|
32
|
+
response = mock('response') do
|
33
|
+
stubs(:body).returns(fixture_file('user_timeline.json'))
|
34
|
+
stubs(:code).returns('200')
|
35
|
+
end
|
36
|
+
|
37
|
+
@client.expects(:get).returns(response)
|
38
|
+
@object = @request.perform
|
39
|
+
end
|
40
|
+
|
41
|
+
should "return array of mashes" do
|
42
|
+
@object.size.should == 20
|
43
|
+
@object.each { |obj| obj.class.should be(Hashie::Mash) }
|
44
|
+
@object.first.text.should == 'Colder out today than expected. Headed to the Beanery for some morning wakeup drink. Latte or coffee...hmmm...'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "performing a request for a single object" do
|
49
|
+
setup do
|
50
|
+
response = mock('response') do
|
51
|
+
stubs(:body).returns(fixture_file('status.json'))
|
52
|
+
stubs(:code).returns('200')
|
53
|
+
end
|
54
|
+
|
55
|
+
@client.expects(:get).returns(response)
|
56
|
+
@object = @request.perform
|
57
|
+
end
|
58
|
+
|
59
|
+
should "return a single mash" do
|
60
|
+
@object.class.should be(Hashie::Mash)
|
61
|
+
@object.text.should == 'Rob Dyrdek is the funniest man alive. That is all.'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "with no query string" do
|
66
|
+
should "not have any query string" do
|
67
|
+
request = Twitter::Request.new(@client, :get, '/1/statuses/user_timeline.json')
|
68
|
+
request.uri.should == '/1/statuses/user_timeline.json'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "with blank query string" do
|
73
|
+
should "not have any query string" do
|
74
|
+
request = Twitter::Request.new(@client, :get, '/1/statuses/user_timeline.json', :query => {})
|
75
|
+
request.uri.should == '/1/statuses/user_timeline.json'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
should "have get shortcut to initialize and perform all in one" do
|
80
|
+
Twitter::Request.any_instance.expects(:perform).returns(nil)
|
81
|
+
Twitter::Request.get(@client, '/foo')
|
82
|
+
end
|
83
|
+
|
84
|
+
should "allow setting query string and headers" do
|
85
|
+
response = mock('response') do
|
86
|
+
stubs(:body).returns('')
|
87
|
+
stubs(:code).returns('200')
|
88
|
+
end
|
89
|
+
|
90
|
+
@client.expects(:get).with('/1/statuses/friends_timeline.json?since_id=1234', {'Foo' => 'Bar'}).returns(response)
|
91
|
+
Twitter::Request.get(@client, '/1/statuses/friends_timeline.json?since_id=1234', :headers => {'Foo' => 'Bar'})
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "new post request" do
|
96
|
+
setup do
|
97
|
+
@client = mock('twitter client')
|
98
|
+
@request = Twitter::Request.new(@client, :post, '/1/statuses/update.json', {:body => {:status => 'Woohoo!'}})
|
99
|
+
end
|
100
|
+
|
101
|
+
should "allow setting body and headers" do
|
102
|
+
response = mock('response') do
|
103
|
+
stubs(:body).returns('')
|
104
|
+
stubs(:code).returns('200')
|
105
|
+
end
|
106
|
+
|
107
|
+
@client.expects(:post).with('/1/statuses/update.json', {:status => 'Woohoo!'}, {'Foo' => 'Bar'}).returns(response)
|
108
|
+
Twitter::Request.post(@client, '/1/statuses/update.json', :body => {:status => 'Woohoo!'}, :headers => {'Foo' => 'Bar'})
|
109
|
+
end
|
110
|
+
|
111
|
+
context "performing request" do
|
112
|
+
setup do
|
113
|
+
response = mock('response') do
|
114
|
+
stubs(:body).returns(fixture_file('status.json'))
|
115
|
+
stubs(:code).returns('200')
|
116
|
+
end
|
117
|
+
|
118
|
+
@client.expects(:post).returns(response)
|
119
|
+
@object = @request.perform
|
120
|
+
end
|
121
|
+
|
122
|
+
should "return a mash of the object" do
|
123
|
+
@object.text.should == 'Rob Dyrdek is the funniest man alive. That is all.'
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
should "have post shortcut to initialize and perform all in one" do
|
128
|
+
Twitter::Request.any_instance.expects(:perform).returns(nil)
|
129
|
+
Twitter::Request.post(@client, '/foo')
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context "error raising" do
|
134
|
+
setup do
|
135
|
+
oauth = Twitter::OAuth.new('token', 'secret')
|
136
|
+
oauth.authorize_from_access('atoken', 'asecret')
|
137
|
+
@client = Twitter::Base.new(oauth)
|
138
|
+
end
|
139
|
+
|
140
|
+
should "not raise error for 200" do
|
141
|
+
stub_get('http://api.twitter.com:80/foo', '', ['200'])
|
142
|
+
lambda {
|
143
|
+
Twitter::Request.get(@client, '/foo')
|
144
|
+
}.should_not raise_error
|
145
|
+
end
|
146
|
+
|
147
|
+
should "not raise error for 304" do
|
148
|
+
stub_get('http://api.twitter.com:80/foo', '', ['304'])
|
149
|
+
lambda {
|
150
|
+
Twitter::Request.get(@client, '/foo')
|
151
|
+
}.should_not raise_error
|
152
|
+
end
|
153
|
+
|
154
|
+
should "raise RateLimitExceeded for 400" do
|
155
|
+
stub_get('http://api.twitter.com:80/foo', 'rate_limit_exceeded.json', ['400'])
|
156
|
+
lambda {
|
157
|
+
Twitter::Request.get(@client, '/foo')
|
158
|
+
}.should raise_error(Twitter::RateLimitExceeded)
|
159
|
+
end
|
160
|
+
|
161
|
+
should "raise Unauthorized for 401" do
|
162
|
+
stub_get('http://api.twitter.com:80/foo', '', ['401'])
|
163
|
+
lambda {
|
164
|
+
Twitter::Request.get(@client, '/foo')
|
165
|
+
}.should raise_error(Twitter::Unauthorized)
|
166
|
+
end
|
167
|
+
|
168
|
+
should "raise General for 403" do
|
169
|
+
stub_get('http://api.twitter.com:80/foo', '', ['403'])
|
170
|
+
lambda {
|
171
|
+
Twitter::Request.get(@client, '/foo')
|
172
|
+
}.should raise_error(Twitter::General)
|
173
|
+
end
|
174
|
+
|
175
|
+
should "raise NotFound for 404" do
|
176
|
+
stub_get('http://api.twitter.com:80/foo', '', ['404'])
|
177
|
+
lambda {
|
178
|
+
Twitter::Request.get(@client, '/foo')
|
179
|
+
}.should raise_error(Twitter::NotFound)
|
180
|
+
end
|
181
|
+
|
182
|
+
should "raise InformTwitter for 500" do
|
183
|
+
stub_get('http://api.twitter.com:80/foo', '', ['500'])
|
184
|
+
lambda {
|
185
|
+
Twitter::Request.get(@client, '/foo')
|
186
|
+
}.should raise_error(Twitter::InformTwitter)
|
187
|
+
end
|
188
|
+
|
189
|
+
should "raise Unavailable for 502" do
|
190
|
+
stub_get('http://api.twitter.com:80/foo', '', ['502'])
|
191
|
+
lambda {
|
192
|
+
Twitter::Request.get(@client, '/foo')
|
193
|
+
}.should raise_error(Twitter::Unavailable)
|
194
|
+
end
|
195
|
+
|
196
|
+
should "raise Unavailable for 503" do
|
197
|
+
stub_get('http://api.twitter.com:80/foo', '', ['503'])
|
198
|
+
lambda {
|
199
|
+
Twitter::Request.get(@client, '/foo')
|
200
|
+
}.should raise_error(Twitter::Unavailable)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
context "Making request with mash option set to false" do
|
205
|
+
setup do
|
206
|
+
oauth = Twitter::OAuth.new('token', 'secret')
|
207
|
+
oauth.authorize_from_access('atoken', 'asecret')
|
208
|
+
@client = Twitter::Base.new(oauth)
|
209
|
+
end
|
210
|
+
|
211
|
+
should "not attempt to create mash of return object" do
|
212
|
+
stub_get('http://api.twitter.com:80/foo', 'friend_ids.json')
|
213
|
+
object = Twitter::Request.get(@client, '/foo', :mash => false)
|
214
|
+
object.class.should_not be(Hashie::Mash)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|