sj-tinder 1.9.3

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.
@@ -0,0 +1,18 @@
1
+ {"rooms": [
2
+ {
3
+ "name": "Room 1",
4
+ "created_at": "2007/03/16 18:03:21 +0000",
5
+ "updated_at": "2007/03/16 18:03:21 +0000",
6
+ "topic": "Testing",
7
+ "id": 80749,
8
+ "membership_limit": 4
9
+ },
10
+ {
11
+ "name": "Room 2",
12
+ "created_at": "2007/03/16 18:04:54 +0000",
13
+ "updated_at": "2007/03/16 18:04:54 +0000",
14
+ "topic": "test",
15
+ "id": 80751,
16
+ "membership_limit": 4
17
+ }]
18
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "messages": [{
3
+ "starred": false,
4
+ "type": "TextMessage",
5
+ "room_id": 490096,
6
+ "created_at": "2012/04/05 10:53:14 +0000",
7
+ "id": 537713173,
8
+ "user_id": 1158839,
9
+ "body": "https://github.com/technomancy/dotfiles/commit/e19989d33777bb392c0ad1205444762dfecbaa5f "
10
+ }, {
11
+ "starred": false,
12
+ "type": "TextMessage",
13
+ "room_id": 490096,
14
+ "created_at": "2012/04/05 10:54:20 +0000",
15
+ "id": 537713420,
16
+ "user_id": 1158837,
17
+ "body": "Lol"
18
+ }]
19
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ "room": {
3
+ "full": false,
4
+ "name": "Room 1",
5
+ "created_at": "2007/03/16 18:03:21 +0000",
6
+ "updated_at": "2007/03/16 18:03:21 +0000",
7
+ "users": [{
8
+ "type": "Member",
9
+ "created_at": "2006/12/07 21:20:39 +0000",
10
+ "email_address": "jane@example.com",
11
+ "admin": true,
12
+ "id": 1,
13
+ "name": "Jane Doe"
14
+ }],
15
+ "topic": "Testing",
16
+ "active_token_value": "90cf7",
17
+ "id": 80749,
18
+ "open_to_guests": true,
19
+ "membership_limit": 4
20
+ }
21
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ "room": {
3
+ "full": false,
4
+ "name": "Room 2",
5
+ "created_at": "2007/03/16 18:03:21 +0000",
6
+ "updated_at": "2007/03/16 18:03:21 +0000",
7
+ "users": [{
8
+ "type": "Member",
9
+ "created_at": "2006/12/07 21:20:39 +0000",
10
+ "email_address": "john@example.com",
11
+ "admin": true,
12
+ "id": 2,
13
+ "name": "John Doe"
14
+ }],
15
+ "topic": "Testing 2",
16
+ "active_token_value": "90cf7",
17
+ "id": 80751,
18
+ "open_to_guests": true,
19
+ "membership_limit": 4
20
+ }
21
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ "room": {
3
+ "full": false,
4
+ "name": "Room 1",
5
+ "created_at": "2007/03/16 18:03:21 +0000",
6
+ "updated_at": "2007/03/16 18:03:21 +0000",
7
+ "users": [{
8
+ "type": "Member",
9
+ "created_at": "2006/12/07 21:20:39 +0000",
10
+ "email_address": "brandon@opensoul.org",
11
+ "admin": true,
12
+ "id": 129553,
13
+ "name": "Brandon Keepers"
14
+ }],
15
+ "topic": "Testing",
16
+ "active_token_value": "90cf7",
17
+ "id": 80749,
18
+ "open_to_guests": true,
19
+ "membership_limit": 4
20
+ }
21
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "user": {
3
+ "email_address": "user@example.com",
4
+ "type": "Member",
5
+ "created_at": "2006/04/06 00:38:28 +0000",
6
+ "admin": true,
7
+ "id": 12345,
8
+ "name": "John Doe",
9
+ "api_auth_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
10
+ }
11
+ }
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
3
+
4
+ require 'rspec'
5
+ require 'tinder'
6
+ require 'fakeweb'
7
+
8
+ FakeWeb.allow_net_connect = false
9
+
10
+ def fixture(name)
11
+ File.read(File.dirname(__FILE__) + "/fixtures/#{name}")
12
+ end
13
+
14
+ def stub_connection(object, &block)
15
+ @stubs ||= Faraday::Adapter::Test::Stubs.new
16
+
17
+ object.connection.build do |builder|
18
+ builder.use FaradayMiddleware::EncodeJson
19
+ builder.use FaradayMiddleware::Mashify
20
+ builder.use FaradayMiddleware::ParseJson
21
+ builder.use Faraday::Response::RemoveWhitespace
22
+ builder.use Faraday::Response::RaiseOnAuthenticationFailure
23
+ builder.adapter :test, @stubs
24
+ end
25
+
26
+ block.call(@stubs)
27
+ end
@@ -0,0 +1,93 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Tinder::Campfire do
5
+ before do
6
+ @campfire = Tinder::Campfire.new('test', :token => 'mytoken')
7
+ end
8
+
9
+ describe "rooms" do
10
+ before do
11
+ stub_connection(@campfire.connection) do |stub|
12
+ stub.get('/rooms.json') {[200, {}, fixture('rooms.json')]}
13
+ end
14
+ end
15
+
16
+ it "should return rooms" do
17
+ @campfire.rooms.size.should be == 2
18
+ @campfire.rooms.first.should be_kind_of(Tinder::Room)
19
+ end
20
+
21
+ it "should set the room name and id" do
22
+ room = @campfire.rooms.first
23
+ room.name.should be == 'Room 1'
24
+ room.id.should be == 80749
25
+ end
26
+ end
27
+
28
+ describe "find_room_by_id" do
29
+ before do
30
+ stub_connection(@campfire.connection) do |stub|
31
+ stub.get('/rooms.json') {[200, {}, fixture('rooms.json')]}
32
+ end
33
+ end
34
+
35
+ it "should return a Tinder::Room object when a match is found" do
36
+ room = @campfire.find_room_by_id 80749
37
+ room.should be_kind_of(Tinder::Room)
38
+ end
39
+
40
+ it "should return nil when no match is found" do
41
+ room = @campfire.find_room_by_id 123
42
+ room.should be nil
43
+ end
44
+ end
45
+
46
+ describe "find_room_by_name" do
47
+ before do
48
+ stub_connection(@campfire.connection) do |stub|
49
+ stub.get('/rooms.json') {[200, {}, fixture('rooms.json')]}
50
+ end
51
+ end
52
+
53
+ it "should return a Tinder::Room object when a match is found" do
54
+ room = @campfire.find_room_by_name 'Room 1'
55
+ room.should be_kind_of(Tinder::Room)
56
+ end
57
+
58
+ it "should return nil when no match is found" do
59
+ room = @campfire.find_room_by_name 'asdf'
60
+ room.should be nil
61
+ end
62
+ end
63
+
64
+ describe "users" do
65
+ before do
66
+ stub_connection(@campfire.connection) do |stub|
67
+ stub.get('/rooms.json') {[200, {}, fixture('rooms.json')]}
68
+
69
+ [80749, 80751].each do |id|
70
+ stub.get("/room/#{id}.json") {[200, {}, fixture("rooms/room#{id}.json")]}
71
+ end
72
+ end
73
+ end
74
+
75
+ it "should return a sorted list of users in all rooms" do
76
+ @campfire.users.length.should be == 2
77
+ @campfire.users.first[:name].should be == "Jane Doe"
78
+ @campfire.users.last[:name].should be == "John Doe"
79
+ end
80
+ end
81
+
82
+ describe "me" do
83
+ before do
84
+ stub_connection(@campfire.connection) do |stub|
85
+ stub.get("/users/me.json") {[200, {}, fixture('users/me.json')]}
86
+ end
87
+ end
88
+
89
+ it "should return the current user's information" do
90
+ @campfire.me["name"].should be == "John Doe"
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,104 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Tinder::Connection do
5
+ describe "authentication" do
6
+ it "should raise an exception with bad credentials" do
7
+ stub_connection(Tinder::Connection) do |stub|
8
+ stub.get("/rooms.json") {[401, {}, "Unauthorized"]}
9
+ end
10
+
11
+ connection = Tinder::Connection.new('test', :token => 'foo')
12
+ lambda { connection.get('/rooms.json') }.should raise_error(Tinder::AuthenticationFailed)
13
+ end
14
+
15
+ it "should raise an exception when an invalid subdomain is specified" do
16
+ stub_connection(Tinder::Connection) do |stub|
17
+ stub.get("/rooms.json") {[404, {}, "Not found"]}
18
+ end
19
+
20
+ connection = Tinder::Connection.new('test', :token => 'foo')
21
+ lambda { connection.get('/rooms.json') }.should raise_error(Tinder::AuthenticationFailed)
22
+ end
23
+
24
+ it "should lookup token when username/password provided" do
25
+ stub_connection(Tinder::Connection) do |stub|
26
+ stub.get("/users/me.json") {[200, {}, fixture('users/me.json')]}
27
+ end
28
+
29
+ connection = Tinder::Connection.new('test', :username => 'user', :password => 'pass')
30
+ connection.token.should == "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
31
+ end
32
+
33
+ it "should use basic auth for credentials" do
34
+ stub_connection(Tinder::Connection) do |stub|
35
+ stub.get("/rooms.json") {[200, {}, fixture('rooms.json')]}
36
+ end
37
+ connection = Tinder::Connection.new('test', :token => 'mytoken')
38
+ lambda { connection.get('/rooms.json') }.should_not raise_error
39
+ end
40
+
41
+ end
42
+
43
+ describe "oauth" do
44
+ let (:oauth_token) { "myoauthtoken" }
45
+ let (:connection) { Tinder::Connection.new('test', :oauth_token => oauth_token) }
46
+
47
+ before do
48
+ stub_connection(Tinder::Connection) do |stub|
49
+ stub.get("/rooms.json") {[200, {}, fixture('rooms.json')]}
50
+ end
51
+ end
52
+
53
+ it "should authenticate" do
54
+ lambda { connection.get('/rooms.json') }.should_not raise_error
55
+ end
56
+
57
+ it "should set the oauth_token" do
58
+ connection.get('/rooms.json')
59
+ connection.options[:oauth_token].should == oauth_token
60
+ end
61
+
62
+ it "should set an Authorization header" do
63
+ connection.get('/rooms.json')
64
+ connection.connection.headers["Authorization"].should == "Bearer #{oauth_token}"
65
+ end
66
+
67
+ end
68
+
69
+ describe "ssl" do
70
+ it "should turn on ssl by default" do
71
+ stub_connection(Tinder::Connection) do |stub|
72
+ stub.get("/users/me.json") {[200, {}, fixture('users/me.json')]}
73
+ end
74
+
75
+ connection = Tinder::Connection.new('test', :username => 'user', :password => 'pass')
76
+ connection.ssl?.should be_true
77
+ end
78
+
79
+ it "should should allow peer verification to be turned off" do
80
+ stub_connection(Tinder::Connection) do |stub|
81
+ stub.get("/users/me.json") {[200, {}, fixture('users/me.json')]}
82
+ end
83
+
84
+ connection = Tinder::Connection.new('test', :username => 'user', :password => 'pass', :ssl_verify => false)
85
+ connection.connection.ssl[:verify].should be == false
86
+ end
87
+
88
+ it "should allow passing any ssl_options to Faraday" do
89
+ stub_connection(Tinder::Connection) do |stub|
90
+ stub.get("/users/me.json") {[200, {}, fixture('users/me.json')]}
91
+ end
92
+ connection = Tinder::Connection.new('test',
93
+ :username => 'user',
94
+ :password => 'pass',
95
+ :ssl_options => {
96
+ :verify => false,
97
+ :ca_path => "/usr/lib/ssl/certs",
98
+ :ca_file => "/etc/ssl/custom"
99
+ }
100
+ )
101
+ connection.connection.ssl.should eql(:verify => false, :ca_path => "/usr/lib/ssl/certs", :ca_file => "/etc/ssl/custom")
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,330 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+ require 'date'
4
+
5
+ describe Tinder::Room do
6
+ before do
7
+ @connection = Tinder::Connection.new('test', :token => 'mytoken')
8
+
9
+ stub_connection(@connection) do |stub|
10
+ stub.get('/room/80749.json') {[200, {}, fixture('rooms/show.json')]}
11
+ end
12
+
13
+ @room = Tinder::Room.new(@connection, 'id' => 80749, 'name' => 'Room 1')
14
+
15
+ # Get EventMachine out of the way. We could be using em-spec, but seems like overkill
16
+ require 'twitter/json_stream'
17
+ module EventMachine; def self.run; yield end end
18
+ EventMachine.stub(:reactor_running?).and_return(true)
19
+ @stream = double(Twitter::JSONStream)
20
+ @stream.stub(:each_item)
21
+ @stream.stub(:on_error)
22
+ @stream.stub(:on_max_reconnects)
23
+ end
24
+
25
+ describe "join" do
26
+ before do
27
+ stub_connection(@connection) do |stub|
28
+ stub.post('/room/80749/join.json') {[200, {}, ""]}
29
+ end
30
+ end
31
+
32
+ it "should post to join url" do
33
+ @room.join
34
+ end
35
+ end
36
+
37
+ describe "leave" do
38
+ before do
39
+ stub_connection(@connection) do |stub|
40
+ stub.post('/room/80749/leave.json') {[200, {}, ""]}
41
+ end
42
+ end
43
+
44
+ it "should post to leave url" do
45
+ @room.leave
46
+ end
47
+
48
+ it "stops listening" do
49
+ @room.should_receive(:stop_listening)
50
+ @room.leave
51
+ end
52
+ end
53
+
54
+ describe "lock" do
55
+ before do
56
+ stub_connection(@connection) do |stub|
57
+ stub.post('/room/80749/lock.json') {[200, {}, ""]}
58
+ end
59
+ end
60
+
61
+ it "should post to lock url" do
62
+ @room.lock
63
+ end
64
+ end
65
+
66
+ describe "search" do
67
+ before do
68
+ stub_connection(@connection) do |stub|
69
+ stub.get('/search/foo.json') {[200, {}, fixture("rooms/recent.json")]}
70
+ end
71
+ end
72
+
73
+ it "should GET the search endpoint with the search term and filter by room" do
74
+ @room.stub(:id).and_return(490096)
75
+ @room.should_receive(:parse_message).exactly(2).times
76
+ @room.search("foo")
77
+ end
78
+
79
+ it "should return empty array if no messages in room" do
80
+ @room.should_receive(:parse_message).never
81
+ @room.search("foo").should be_empty
82
+ end
83
+ end
84
+
85
+ describe "transcript" do
86
+ it "should GET the transcript endpoint with the provided date" do
87
+ stub_connection(@connection) do |stub|
88
+ stub.get('/room/80749/transcript/2012/10/15.json') {[200, {}, fixture("rooms/recent.json")]}
89
+ end
90
+ @room.should_receive(:parse_message).exactly(2).times
91
+ @room.transcript(Date.parse('2012-10-15'))
92
+ end
93
+
94
+ it "should default to today's date" do
95
+ stub_connection(@connection) do |stub|
96
+ stub.get('/room/80749/transcript/1981/03/21.json') {[200, {}, fixture("rooms/recent.json")]}
97
+ end
98
+ Date.stub(:today).and_return(Date.parse('1981-03-21'))
99
+ @room.should_receive(:parse_message).exactly(2).times
100
+ @room.transcript
101
+ end
102
+ end
103
+
104
+ describe "unlock" do
105
+ before do
106
+ stub_connection(@connection) do |stub|
107
+ stub.post('/room/80749/unlock.json') {[200, {}, ""]}
108
+ end
109
+ end
110
+
111
+ it "should post to unlock url" do
112
+ @room.unlock
113
+ end
114
+ end
115
+
116
+ describe "guest_url" do
117
+ it "should use guest_invite_code if active" do
118
+ @room.stub(:guest_access_enabled? => true, :guest_invite_code => '123')
119
+ @room.guest_url.should == "https://test.campfirenow.com/123"
120
+ end
121
+
122
+ it "should return nil when guest access is not enabled" do
123
+ @room.stub(:guest_access_enabled?).and_return(false)
124
+ @room.guest_url.should be_nil
125
+ end
126
+ end
127
+
128
+ it "should set guest_invite_code" do
129
+ @room.guest_invite_code.should == "90cf7"
130
+ end
131
+
132
+ it "should set guest_access_enabled?" do
133
+ @room.guest_access_enabled?.should be_true
134
+ end
135
+
136
+ describe "topic" do
137
+ it "should get the current topic" do
138
+ @room.topic.should == "Testing"
139
+ end
140
+
141
+ it "should get the current topic even if it's changed" do
142
+ @room.topic.should == "Testing"
143
+
144
+ # reinitialize a new connection since we can't modify the
145
+ # faraday stack after a request has already been submitted
146
+ @connection = Tinder::Connection.new('test', :token => 'mytoken')
147
+
148
+ # returning a different room's json to get a diff topic
149
+ stub_connection(@connection) do |stub|
150
+ stub.get('/room/80749.json') {[200, {}, fixture('rooms/room80751.json')]}
151
+ end
152
+
153
+ @room.topic.should == "Testing 2"
154
+
155
+ end
156
+ end
157
+
158
+
159
+ describe "name=" do
160
+ it "should put to update the room" do
161
+ stub_connection(@connection) do |stub|
162
+ stub.put('/room/80749.json') {[200, {}, ""]}
163
+ end
164
+
165
+ @room.name = "Foo"
166
+ end
167
+ end
168
+
169
+ describe "listen" do
170
+ before do
171
+ stub_connection(@connection) do |stub|
172
+ stub.post('/room/80749/join.json') {[200, {}, ""]}
173
+ end
174
+ end
175
+
176
+ it "should get from the streaming url" do
177
+ Twitter::JSONStream.should_receive(:connect).with(
178
+ {
179
+ :host=>"streaming.campfirenow.com",
180
+ :path=>"/room/80749/live.json",
181
+ :auth=>"mytoken:X",
182
+ :timeout=>6,
183
+ :ssl=>true
184
+ }
185
+ ).and_return(@stream)
186
+
187
+ @room.listen { }
188
+ end
189
+
190
+ it "should raise an exception if no block is given" do
191
+ lambda {
192
+ @room.listen
193
+ }.should raise_error(ArgumentError, "no block provided")
194
+ end
195
+
196
+ it "marks the room as listening" do
197
+ Twitter::JSONStream.stub(:connect).and_return(@stream)
198
+ lambda {
199
+ @room.listen { }
200
+ }.should change(@room, :listening?).from(false).to(true)
201
+ end
202
+ end
203
+
204
+ describe "stop_listening" do
205
+ before do
206
+ stub_connection(@connection) do |stub|
207
+ stub.post('/room/80749/join.json') {[200, {}, ""]}
208
+ end
209
+
210
+ Twitter::JSONStream.stub(:connect).and_return(@stream)
211
+ @stream.stub(:stop)
212
+ end
213
+
214
+ it "changes a listening room to a non-listening room" do
215
+ @room.listen { }
216
+ lambda {
217
+ @room.stop_listening
218
+ }.should change(@room, :listening?).from(true).to(false)
219
+ end
220
+
221
+ it "tells the json stream to stop" do
222
+ @room.listen { }
223
+ @stream.should_receive(:stop)
224
+ @room.stop_listening
225
+ end
226
+
227
+ it "does nothing if the room is not listening" do
228
+ @room.listen { }
229
+ @room.stop_listening
230
+ @room.stop_listening
231
+ end
232
+ end
233
+
234
+ describe "recent" do
235
+ before do
236
+ stub_connection(@connection) do |stub|
237
+ stub.get('/room/80749/recent.json') {[
238
+ 200, {}, fixture('rooms/recent.json')
239
+ ]}
240
+ end
241
+ end
242
+
243
+ it "should get a list of parsed recent messages" do
244
+ @room.should_receive(:parse_message).exactly(2).times
245
+ messages = @room.recent
246
+ end
247
+ end
248
+
249
+ describe "parse_message" do
250
+ it "expands user and parses created_at" do
251
+ unparsed_message = {
252
+ :user_id => 123,
253
+ :body => 'An aunt is worth two nieces',
254
+ :created_at => '2012/02/14 16:21:00 +0000'
255
+ }
256
+ expected = {
257
+ :user => {
258
+ :name => 'Dr. Noodles'
259
+ },
260
+ :body => 'An aunt is worth two nieces',
261
+ :created_at => Time.parse('2012/02/14 16:21:00 +0000')
262
+ }
263
+ @room.stub(:user).with(123).and_return({ :name => 'Dr. Noodles' })
264
+
265
+ actual = @room.parse_message(unparsed_message)
266
+ actual.should == expected
267
+ end
268
+ end
269
+
270
+ describe "user" do
271
+ before do
272
+ @room.stub(:current_users).and_return([
273
+ { :id => 1, :name => 'The Amazing Crayon Executive'},
274
+ { :id => 2, :name => 'Lord Pants'},
275
+ ])
276
+ @not_current_user = { :id => 3, :name => 'Patriot Sally'}
277
+ end
278
+
279
+ it "looks up user if not already in room's cache" do
280
+ @room.should_receive(:fetch_user).with(3).
281
+ and_return(@not_current_user)
282
+ @room.user(3).should == @not_current_user
283
+ end
284
+
285
+ it "pulls user from room's cache if user in participant list" do
286
+ @room.should_receive(:fetch_user).never
287
+ user = @room.user(1)
288
+ end
289
+
290
+ it "adds user to cache after first lookup" do
291
+ @room.should_receive(:fetch_user).with(3).at_most(:once).
292
+ and_return(@not_current_user)
293
+ @room.user(3).should == @not_current_user
294
+ @room.user(3).should == @not_current_user
295
+ end
296
+ end
297
+
298
+ describe "fetch_user" do
299
+ before do
300
+ stub_connection(@connection) do |stub|
301
+ stub.get("/users/5.json") {[200, {}, fixture('users/me.json')]}
302
+ end
303
+ end
304
+
305
+ it "requests via GET and returns the requested user's information" do
306
+ @room.fetch_user(5)['name'].should == 'John Doe'
307
+ end
308
+ end
309
+
310
+ describe "current_users" do
311
+ it "returns list of currently participating users" do
312
+ current_users = @room.current_users
313
+ current_users.size.should == 1
314
+ current_users.first[:name].should == 'Brandon Keepers'
315
+ end
316
+ end
317
+
318
+ describe "users" do
319
+ it "returns current users if cache has not been initialized yet" do
320
+ @room.should_receive(:current_users).and_return(:the_whole_spittoon)
321
+ @room.users.should == :the_whole_spittoon
322
+ end
323
+
324
+ it "returns current users plus any added cached users" do
325
+ @room.should_receive(:current_users).and_return([:mia_cuttlefish])
326
+ @room.users << :guy_wearing_new_mexico_as_a_hat
327
+ @room.users.should == [:mia_cuttlefish, :guy_wearing_new_mexico_as_a_hat]
328
+ end
329
+ end
330
+ end