bkocik-twitter 0.6.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,217 @@
1
+ require File.dirname(__FILE__) + '/../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, '/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 == '/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 == '/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(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(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, '/statuses/user_timeline.json')
68
+ request.uri.should == '/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, '/statuses/user_timeline.json', :query => {})
75
+ request.uri.should == '/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('{"a":"b"}')
87
+ stubs(:code).returns('200')
88
+ end
89
+
90
+ @client.expects(:get).with('/statuses/friends_timeline.json?since_id=1234', {'Foo' => 'Bar'}).returns(response)
91
+ Twitter::Request.get(@client, '/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, '/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('{"a":"b"}')
104
+ stubs(:code).returns('200')
105
+ end
106
+
107
+ @client.expects(:post).with('/statuses/update.json', {:status => 'Woohoo!'}, {'Foo' => 'Bar'}).returns(response)
108
+ Twitter::Request.post(@client, '/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://twitter.com:80/foo', 'generic_response.json', ['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://twitter.com:80/foo', 'generic_response.json', ['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://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://twitter.com:80/foo', 'generic_response.json', ['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://twitter.com:80/foo', 'generic_response.json', ['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://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://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://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://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://twitter.com:80/foo', 'friend_ids.json')
213
+ object = Twitter::Request.get(@client, '/foo', :mash => false)
214
+ object.class.should_not be(Mash)
215
+ end
216
+ end
217
+ end
@@ -0,0 +1,144 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class SearchTest < Test::Unit::TestCase
4
+ context "searching" do
5
+ setup do
6
+ @search = Twitter::Search.new
7
+ end
8
+
9
+ should "should be able to initialize with a search term" do
10
+ Twitter::Search.new('httparty').query[:q].should include('httparty')
11
+ end
12
+
13
+ should "should be able to specify from" do
14
+ @search.from('jnunemaker').query[:q].should include('from:jnunemaker')
15
+ end
16
+
17
+ should "should be able to specify to" do
18
+ @search.to('jnunemaker').query[:q].should include('to:jnunemaker')
19
+ end
20
+
21
+ should "should be able to specify referencing" do
22
+ @search.referencing('jnunemaker').query[:q].should include('@jnunemaker')
23
+ end
24
+
25
+ should "should alias references to referencing" do
26
+ @search.references('jnunemaker').query[:q].should include('@jnunemaker')
27
+ end
28
+
29
+ should "should alias ref to referencing" do
30
+ @search.ref('jnunemaker').query[:q].should include('@jnunemaker')
31
+ end
32
+
33
+ should "should be able to specify containing" do
34
+ @search.containing('milk').query[:q].should include('milk')
35
+ end
36
+
37
+ should "should alias contains to containing" do
38
+ @search.contains('milk').query[:q].should include('milk')
39
+ end
40
+
41
+ should "should be able to specify hashed" do
42
+ @search.hashed('twitter').query[:q].should include('#twitter')
43
+ end
44
+
45
+ should "should be able to specify the language" do
46
+ @search.lang('en')
47
+ @search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:lang => 'en', :q => ''}, :format => :json).returns({'foo' => 'bar'})
48
+ @search.fetch()
49
+ end
50
+
51
+ should "should be able to specify the number of results per page" do
52
+ @search.per_page(25)
53
+ @search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:rpp => 25, :q => ''}, :format => :json).returns({'foo' => 'bar'})
54
+ @search.fetch()
55
+ end
56
+
57
+ should "should be able to specify the page number" do
58
+ @search.page(20)
59
+ @search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:page => 20, :q => ''}, :format => :json).returns({'foo' => 'bar'})
60
+ @search.fetch()
61
+ end
62
+
63
+ should "should be able to specify only returning results greater than an id" do
64
+ @search.since(1234)
65
+ @search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:since_id => 1234, :q => ''}, :format => :json).returns({'foo' => 'bar'})
66
+ @search.fetch()
67
+ end
68
+
69
+ should "should be able to specify geo coordinates" do
70
+ @search.geocode('40.757929', '-73.985506', '25mi')
71
+ @search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:geocode => '40.757929,-73.985506,25mi', :q => ''}, :format => :json).returns({'foo' => 'bar'})
72
+ @search.fetch()
73
+ end
74
+
75
+ should "should be able to specify max id" do
76
+ @search.max(1234)
77
+ @search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:max_id => 1234, :q => ''}, :format => :json).returns({'foo' => 'bar'})
78
+ @search.fetch()
79
+ end
80
+
81
+ should "should be able to clear the filters set" do
82
+ @search.from('jnunemaker').to('oaknd1')
83
+ @search.clear.query.should == {:q => []}
84
+ end
85
+
86
+ should "should be able to chain methods together" do
87
+ @search.from('jnunemaker').to('oaknd1').referencing('orderedlist').containing('milk').hashed('twitter').lang('en').per_page(20).since(1234).geocode('40.757929', '-73.985506', '25mi')
88
+ @search.query[:q].should == ['from:jnunemaker', 'to:oaknd1', '@orderedlist', 'milk', '#twitter']
89
+ @search.query[:lang].should == 'en'
90
+ @search.query[:rpp].should == 20
91
+ @search.query[:since_id].should == 1234
92
+ @search.query[:geocode].should == '40.757929,-73.985506,25mi'
93
+ end
94
+
95
+ context "fetching" do
96
+ setup do
97
+ stub_get('http://search.twitter.com:80/search.json?q=%40jnunemaker', 'search.json')
98
+ @search = Twitter::Search.new('@jnunemaker')
99
+ @response = @search.fetch
100
+ end
101
+
102
+ should "should return results" do
103
+ @response.results.size.should == 15
104
+ end
105
+
106
+ should "should support dot notation" do
107
+ first = @response.results.first
108
+ first.text.should == %q(Someone asked about a tweet reader. Easy to do in ruby with @jnunemaker's twitter gem and the win32-sapi gem, if you are on windows.)
109
+ first.from_user.should == 'PatParslow'
110
+ end
111
+
112
+ should "cache fetched results so multiple fetches don't keep hitting api" do
113
+ Twitter::Search.expects(:get).never
114
+ @search.fetch
115
+ end
116
+
117
+ should "rehit api if fetch is called with true" do
118
+ Twitter::Search.expects(:get).once
119
+ @search.fetch(true)
120
+ end
121
+ end
122
+
123
+ context "iterating over results" do
124
+ setup do
125
+ stub_get('http://search.twitter.com:80/search.json?q=from%3Ajnunemaker', 'search_from_jnunemaker.json')
126
+ @search.from('jnunemaker')
127
+ end
128
+
129
+ should "work" do
130
+ @search.each { |result| result.should_not be(nil) }
131
+ end
132
+
133
+ should "work multiple times in a row" do
134
+ @search.each { |result| result.should_not be(nil) }
135
+ @search.each { |result| result.should_not be(nil) }
136
+ end
137
+ end
138
+
139
+ should "should be able to iterate over results" do
140
+ @search.respond_to?(:each).should be(true)
141
+ end
142
+ end
143
+
144
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class TwitterTest < Test::Unit::TestCase
4
+ should "have firehose method for public timeline" do
5
+ stub_get('http://twitter.com:80/statuses/public_timeline.json', 'firehose.json')
6
+ hose = Twitter.firehose
7
+ hose.size.should == 20
8
+ first = hose.first
9
+ first.text.should == '#torrents Ultimativer Flirt Guide - In 10 Minuten jede Frau erobern: Ultimativer Flirt Guide - In 10 Mi.. http://tinyurl.com/d3okh4'
10
+ first.user.name.should == 'P2P Torrents'
11
+ end
12
+
13
+ should "have user method for unauthenticated calls to get a user's information" do
14
+ stub_get('http://twitter.com:80/users/show/jnunemaker.json', 'user.json')
15
+ user = Twitter.user('jnunemaker')
16
+ user.name.should == 'John Nunemaker'
17
+ user.description.should == 'Loves his wife, ruby, notre dame football and iu basketball'
18
+ end
19
+
20
+ should "have status method for unauthenticated calls to get a status" do
21
+ stub_get('http://twitter.com:80/statuses/show/1533815199.json', 'status_show.json')
22
+ status = Twitter.status(1533815199)
23
+ status.id.should == 1533815199
24
+ status.text.should == 'Eating some oatmeal and butterscotch cookies with a cold glass of milk for breakfast. Tasty!'
25
+ end
26
+
27
+ should "have friend_ids method" do
28
+ stub_get('http://twitter.com:80/friends/ids/jnunemaker.json', 'friend_ids.json')
29
+ ids = Twitter.friend_ids('jnunemaker')
30
+ ids.size.should == 161
31
+ end
32
+
33
+ should "have follower_ids method" do
34
+ stub_get('http://twitter.com:80/followers/ids/jnunemaker.json', 'follower_ids.json')
35
+ ids = Twitter.follower_ids('jnunemaker')
36
+ ids.size.should == 1252
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bkocik-twitter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.8
5
+ platform: ruby
6
+ authors:
7
+ - John Nunemaker
8
+ - Bill Kocik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-04-23 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.2
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: mash
28
+ type: :runtime
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.0.3
35
+ version:
36
+ - !ruby/object:Gem::Dependency
37
+ name: httparty
38
+ type: :runtime
39
+ version_requirement:
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - "="
43
+ - !ruby/object:Gem::Version
44
+ version: 0.4.3
45
+ version:
46
+ - !ruby/object:Gem::Dependency
47
+ name: thoughtbot-shoulda
48
+ type: :development
49
+ version_requirement:
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ - !ruby/object:Gem::Dependency
57
+ name: jeremymcanally-matchy
58
+ type: :development
59
+ version_requirement:
60
+ version_requirements: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ - !ruby/object:Gem::Dependency
67
+ name: mocha
68
+ type: :development
69
+ version_requirement:
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ - !ruby/object:Gem::Dependency
77
+ name: fakeweb
78
+ type: :development
79
+ version_requirement:
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ version:
86
+ - !ruby/object:Gem::Dependency
87
+ name: mash
88
+ type: :development
89
+ version_requirement:
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ description:
97
+ email: bkocik@gmail.com
98
+ executables: []
99
+
100
+ extensions: []
101
+
102
+ extra_rdoc_files:
103
+ - README.rdoc
104
+ files:
105
+ - History
106
+ - License
107
+ - Notes
108
+ - README.rdoc
109
+ - Rakefile
110
+ - VERSION.yml
111
+ - examples/connect.rb
112
+ - examples/friendship_existance.rb
113
+ - examples/helpers/config_store.rb
114
+ - examples/httpauth.rb
115
+ - examples/ids.rb
116
+ - examples/search.rb
117
+ - examples/timeline.rb
118
+ - examples/unauthorized.rb
119
+ - examples/update.rb
120
+ - examples/user.rb
121
+ - lib/twitter.rb
122
+ - lib/twitter/base.rb
123
+ - lib/twitter/httpauth.rb
124
+ - lib/twitter/oauth.rb
125
+ - lib/twitter/request.rb
126
+ - lib/twitter/search.rb
127
+ - test/fixtures/firehose.json
128
+ - test/fixtures/follower_ids.json
129
+ - test/fixtures/friend_ids.json
130
+ - test/fixtures/friends_timeline.json
131
+ - test/fixtures/rate_limit_exceeded.json
132
+ - test/fixtures/replies.json
133
+ - test/fixtures/search.json
134
+ - test/fixtures/search_from_jnunemaker.json
135
+ - test/fixtures/status.json
136
+ - test/fixtures/status_show.json
137
+ - test/fixtures/user.json
138
+ - test/fixtures/user_timeline.json
139
+ - test/test_helper.rb
140
+ - test/twitter/base_test.rb
141
+ - test/twitter/httpauth_test.rb
142
+ - test/twitter/oauth_test.rb
143
+ - test/twitter/request_test.rb
144
+ - test/twitter/search_test.rb
145
+ - test/twitter_test.rb
146
+ has_rdoc: true
147
+ homepage: http://github.com/bkocik/twitter
148
+ post_install_message:
149
+ rdoc_options:
150
+ - --charset=UTF-8
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: "0"
158
+ version:
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: "0"
164
+ version:
165
+ requirements: []
166
+
167
+ rubyforge_project: twitter
168
+ rubygems_version: 1.2.0
169
+ signing_key:
170
+ specification_version: 2
171
+ summary: wrapper for the twitter api (oauth only)
172
+ test_files:
173
+ - test/test_helper.rb
174
+ - test/twitter/base_test.rb
175
+ - test/twitter/httpauth_test.rb
176
+ - test/twitter/oauth_test.rb
177
+ - test/twitter/request_test.rb
178
+ - test/twitter/search_test.rb
179
+ - test/twitter_test.rb
180
+ - examples/connect.rb
181
+ - examples/friendship_existance.rb
182
+ - examples/helpers/config_store.rb
183
+ - examples/httpauth.rb
184
+ - examples/ids.rb
185
+ - examples/search.rb
186
+ - examples/timeline.rb
187
+ - examples/unauthorized.rb
188
+ - examples/update.rb
189
+ - examples/user.rb