bitlyr 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. data/History.txt +146 -0
  2. data/LICENSE +20 -0
  3. data/README.md +97 -0
  4. data/README.rdoc +37 -0
  5. data/Rakefile +43 -0
  6. data/VERSION +1 -0
  7. data/bitlyr.gemspec +154 -0
  8. data/lib/bitlyr/client.rb +153 -0
  9. data/lib/bitlyr/country.rb +10 -0
  10. data/lib/bitlyr/day.rb +12 -0
  11. data/lib/bitlyr/error.rb +13 -0
  12. data/lib/bitlyr/lib/core_ext/hash.rb +27 -0
  13. data/lib/bitlyr/lib/core_ext/string.rb +5 -0
  14. data/lib/bitlyr/missing_url.rb +12 -0
  15. data/lib/bitlyr/realtime_link.rb +16 -0
  16. data/lib/bitlyr/referrer.rb +12 -0
  17. data/lib/bitlyr/response.rb +39 -0
  18. data/lib/bitlyr/strategy/access_token.rb +26 -0
  19. data/lib/bitlyr/strategy/api_key.rb +33 -0
  20. data/lib/bitlyr/strategy/base.rb +26 -0
  21. data/lib/bitlyr/strategy/oauth.rb +50 -0
  22. data/lib/bitlyr/url.rb +124 -0
  23. data/lib/bitlyr/user.rb +100 -0
  24. data/lib/bitlyr.rb +39 -0
  25. data/test/fixtures/9uX1TE.json +1 -0
  26. data/test/fixtures/9uX1TEclicks.json +1 -0
  27. data/test/fixtures/9uX1TEclicks2.json +1 -0
  28. data/test/fixtures/9uX1TEinfo.json +1 -0
  29. data/test/fixtures/9uX1TEinfo2.json +1 -0
  30. data/test/fixtures/auth_fail.json +1 -0
  31. data/test/fixtures/auth_success.json +1 -0
  32. data/test/fixtures/betaworks.json +1 -0
  33. data/test/fixtures/betaworks2.json +1 -0
  34. data/test/fixtures/betaworks_jmp.json +1 -0
  35. data/test/fixtures/betaworks_other_user.json +1 -0
  36. data/test/fixtures/bitly9uX1TE.json +1 -0
  37. data/test/fixtures/bitly_pro_domain.json +1 -0
  38. data/test/fixtures/clicks_by_day.json +1 -0
  39. data/test/fixtures/clicks_by_day1.json +1 -0
  40. data/test/fixtures/clicks_by_day2.json +1 -0
  41. data/test/fixtures/clicks_by_minute1_url.json +1 -0
  42. data/test/fixtures/clicks_by_minute2_url.json +1 -0
  43. data/test/fixtures/clicks_by_minute_hash.json +1 -0
  44. data/test/fixtures/clicks_by_minute_hashes.json +1 -0
  45. data/test/fixtures/country_hash.json +1 -0
  46. data/test/fixtures/country_hash2.json +1 -0
  47. data/test/fixtures/country_url.json +1 -0
  48. data/test/fixtures/failure.json +1 -0
  49. data/test/fixtures/invalid_bitly_pro_domain.json +1 -0
  50. data/test/fixtures/invalid_credentials.json +1 -0
  51. data/test/fixtures/invalid_domain.json +1 -0
  52. data/test/fixtures/invalid_user.json +1 -0
  53. data/test/fixtures/invalid_x_api_key.json +1 -0
  54. data/test/fixtures/lookup_multiple_url.json +1 -0
  55. data/test/fixtures/lookup_not_real_url.json +1 -0
  56. data/test/fixtures/lookup_single_url.json +1 -0
  57. data/test/fixtures/missing_hash.json +1 -0
  58. data/test/fixtures/multiple_info.json +1 -0
  59. data/test/fixtures/multiple_url_click.json +1 -0
  60. data/test/fixtures/multiple_urls.json +1 -0
  61. data/test/fixtures/not_bitly_pro_domain.json +1 -0
  62. data/test/fixtures/not_found_info.json +1 -0
  63. data/test/fixtures/referrer_hash.json +1 -0
  64. data/test/fixtures/referrer_hash2.json +1 -0
  65. data/test/fixtures/referrer_url.json +1 -0
  66. data/test/fixtures/success.json +1 -0
  67. data/test/fixtures/url_info.json +1 -0
  68. data/test/fixtures/user_clicks.json +32 -0
  69. data/test/fixtures/user_countries.json +60 -0
  70. data/test/fixtures/user_realtime_links.json +15 -0
  71. data/test/fixtures/user_referrers.json +1 -0
  72. data/test/fixtures/valid_user.json +1 -0
  73. data/test/integration/strategy/test_api_key.rb +20 -0
  74. data/test/integration/strategy/test_oauth.rb +52 -0
  75. data/test/integration/test_client.rb +1415 -0
  76. data/test/integration/test_user.rb +97 -0
  77. data/test/test_helper.rb +54 -0
  78. data/test/unit/core_ext/test_hash.rb +69 -0
  79. data/test/unit/core_ext/test_string.rb +14 -0
  80. data/test/unit/strategy/test_access_token.rb +14 -0
  81. data/test/unit/strategy/test_api_key.rb +11 -0
  82. data/test/unit/strategy/test_base.rb +71 -0
  83. data/test/unit/strategy/test_oauth.rb +32 -0
  84. data/test/unit/test_bitly.rb +43 -0
  85. data/test/unit/test_client.rb +21 -0
  86. data/test/unit/test_country.rb +20 -0
  87. data/test/unit/test_day.rb +22 -0
  88. data/test/unit/test_error.rb +11 -0
  89. data/test/unit/test_missing.rb +34 -0
  90. data/test/unit/test_realtime_link.rb +30 -0
  91. data/test/unit/test_referrer.rb +20 -0
  92. data/test/unit/test_response.rb +86 -0
  93. data/test/unit/test_url.rb +155 -0
  94. data/test/unit/test_user.rb +17 -0
  95. metadata +233 -0
@@ -0,0 +1,1415 @@
1
+ require 'test_helper'
2
+
3
+ class TestClient < Test::Unit::TestCase
4
+ context "with a valid client" do
5
+ context "with the ApiKey Strategy" do
6
+ setup do
7
+ @client = Bitlyr::Client.new Bitlyr::Strategy::ApiKey.new(login_fixture, api_key_fixture)
8
+ end
9
+
10
+ context "validating another account credentials" do
11
+ context "with valid credentials" do
12
+ setup do
13
+ stub_get(%r{http://api\.bit\.ly/v3/validate?.*x_login=correct.*}, "valid_user.json")
14
+ end
15
+
16
+ should "return true" do
17
+ assert @client.validate('correct','well_done')
18
+ end
19
+
20
+ should "return true for valid? as well" do
21
+ assert @client.valid?('correct','well_done')
22
+ end
23
+ end
24
+
25
+ context "with invalid credentials" do
26
+ setup do
27
+ stub_get(%r{http://api\.bit\.ly/v3/validate?.*x_login=wrong.*},"invalid_user.json")
28
+ end
29
+
30
+ should "return false" do
31
+ assert !@client.validate('wrong','so_very_wrong')
32
+ end
33
+
34
+ should "return false for valid? too" do
35
+ assert !@client.valid?('wrong','so_very_wrong')
36
+ end
37
+ end
38
+ end
39
+
40
+ context "checking a bitly pro domain" do
41
+ context "with a bitly pro domain" do
42
+ setup do
43
+ stub_get(%r|http://api\.bit\.ly/v3/bitly_pro_domain?.*domain=nyti\.ms.*|, 'bitly_pro_domain.json')
44
+ end
45
+
46
+ should "return true" do
47
+ assert @client.bitly_pro_domain('nyti.ms')
48
+ end
49
+ end
50
+
51
+ context "with a non bitly pro domain" do
52
+ setup do
53
+ stub_get(%r|http://api\.bit\.ly/v3/bitly_pro_domain?.*domain=philnash\.co\.uk.*|, 'not_bitly_pro_domain.json')
54
+ end
55
+
56
+ should "return false" do
57
+ assert !@client.bitly_pro_domain('philnash.co.uk')
58
+ end
59
+ end
60
+
61
+ context "with an invalid domain" do
62
+ setup do
63
+ stub_get(%r|http://api\.bit\.ly/v3/bitly_pro_domain?.*domain=philnash.*|, 'invalid_bitly_pro_domain.json')
64
+ end
65
+
66
+ should "raise an error" do
67
+ assert_raise BitlyrError do
68
+ @client.bitly_pro_domain('philnash')
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ context "shortening a url" do
75
+ context "with just the url" do
76
+ setup do
77
+ @long_url = "http://betaworks.com/"
78
+ stub_get(%r|http://api\.bit\.ly/v3/shorten\?.*longUrl=#{CGI.escape(@long_url)}.*|, ['betaworks.json', 'betaworks2.json'])
79
+ @url = @client.shorten(@long_url)
80
+ end
81
+
82
+ should "return a url object" do
83
+ assert_instance_of Bitlyr::Url, @url
84
+ end
85
+
86
+ should "shorten the url" do
87
+ assert_equal 'http://bit.ly/9uX1TE', @url.short_url
88
+ end
89
+
90
+ should "return the original long url" do
91
+ assert_equal @long_url, @url.long_url
92
+ end
93
+
94
+ should "return a hash" do
95
+ assert_equal '9uX1TE', @url.user_hash
96
+ end
97
+
98
+ should "return a global hash" do
99
+ assert_equal '18H1ET', @url.global_hash
100
+ end
101
+
102
+ should "be a new hash the first time" do
103
+ assert @url.new_hash?
104
+ end
105
+
106
+ should "not be a new hash the second time" do
107
+ new_url = @client.shorten(@long_url)
108
+ assert !new_url.new_hash?
109
+ assert_not_same @url, new_url
110
+ end
111
+ end
112
+
113
+ context "with extra options" do
114
+ context "with the j.mp domain" do
115
+ setup do
116
+ stub_get("http://api.bit.ly/v3/shorten?longUrl=#{CGI.escape('http://betaworks.com/')}&apiKey=test_key&login=test_account&domain=j.mp", 'betaworks_jmp.json')
117
+ @url = @client.shorten('http://betaworks.com/', :domain => "j.mp")
118
+ end
119
+
120
+ should "return a j.mp short url" do
121
+ assert_equal "http://j.mp/9uX1TE", @url.short_url
122
+ end
123
+ end
124
+
125
+ context "with another domain" do
126
+ setup do
127
+ stub_get("http://api.bit.ly/v3/shorten?longUrl=#{CGI.escape('http://betaworks.com/')}&apiKey=test_key&login=test_account&domain=nyti.ms", 'invalid_domain.json')
128
+ end
129
+
130
+ should "raise an error" do
131
+ assert_raise BitlyrError do
132
+ url = @client.shorten('http://betaworks.com/', :domain => "nyti.ms")
133
+ end
134
+ end
135
+ end
136
+
137
+ context "with another user details" do
138
+ context "with correct details" do
139
+ setup do
140
+ @long_url = "http://betaworks.com/"
141
+ stub_get(%r{http://api\.bit\.ly/v3/shorten?.*longUrl=#{CGI.escape('http://betaworks.com/')}.*}, 'betaworks.json')
142
+ stub_get("http://api.bit.ly/v3/shorten?longUrl=#{CGI.escape('http://betaworks.com/')}&apiKey=test_key&login=test_account&x_login=other_account&x_apiKey=other_apiKey", 'betaworks_other_user.json' )
143
+ @normal_url = @client.shorten(@long_url)
144
+ @other_user_url = @client.shorten(@long_url, :x_login => 'other_account', :x_apiKey => 'other_apiKey')
145
+ end
146
+
147
+ should "return a different hash" do
148
+ assert_not_equal @normal_url.user_hash, @other_user_url.user_hash
149
+ end
150
+
151
+ should "return a new hash" do
152
+ assert @other_user_url.new_hash?
153
+ end
154
+ end
155
+
156
+ context "without an api key" do
157
+ setup do
158
+ stub_get("http://api.bit.ly/v3/shorten?longUrl=#{CGI.escape('http://betaworks.com/')}&apiKey=test_key&login=test_account&x_login=other_account", 'invalid_x_api_key.json' )
159
+ end
160
+
161
+ should "raise an error" do
162
+ assert_raise BitlyrError do
163
+ @client.shorten('http://betaworks.com/', :x_login => 'other_account')
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
170
+
171
+ context "expanding a url" do
172
+ context "a single url" do
173
+ context "with a hash" do
174
+ setup do
175
+ @hash = '9uX1TE'
176
+ stub_get(%r|http://api\.bit\.ly/v3/expand\?.*hash=9uX1TE.*|, '9uX1TE.json')
177
+ @url = @client.expand(@hash)
178
+ end
179
+
180
+ should 'return a url object' do
181
+ assert_instance_of Bitlyr::Url, @url
182
+ end
183
+
184
+ should 'return the original hash' do
185
+ assert_equal @hash, @url.user_hash
186
+ end
187
+
188
+ should "return a global hash" do
189
+ assert_equal '18H1ET', @url.global_hash
190
+ end
191
+
192
+ should 'return a long url' do
193
+ assert_equal 'http://betaworks.com/', @url.long_url
194
+ end
195
+
196
+ should 'return a short url' do
197
+ assert_equal "http://bit.ly/#{@hash}", @url.short_url
198
+ end
199
+ end
200
+
201
+ context "with the short url" do
202
+ setup do
203
+ @short_url = 'http://bit.ly/9uX1TE'
204
+ stub_get(%r|http://api\.bit\.ly/v3/expand\?.*shortUrl=http%3A%2F%2Fbit\.ly%2F9uX1TE.*|, 'bitly9uX1TE.json')
205
+ @url = @client.expand(@short_url)
206
+ end
207
+
208
+ should 'return a url object' do
209
+ assert_instance_of Bitlyr::Url, @url
210
+ end
211
+
212
+ should 'return the original hash' do
213
+ assert_equal "9uX1TE", @url.user_hash
214
+ end
215
+
216
+ should "return a global hash" do
217
+ assert_equal '18H1ET', @url.global_hash
218
+ end
219
+
220
+ should 'return a long url' do
221
+ assert_equal 'http://betaworks.com/', @url.long_url
222
+ end
223
+
224
+ should 'return a short url' do
225
+ assert_equal "http://bit.ly/9uX1TE", @url.short_url
226
+ end
227
+ end
228
+
229
+ context "that doesn't exist" do
230
+ setup do
231
+ @shortUrl = 'http://bit.ly/9uX1TEsd'
232
+ stub_get(%r|http://api\.bit\.ly/v3/expand\?.*shortUrl=http%3A%2F%2Fbit\.ly%2F9uX1TEsd.*|, 'missing_hash.json')
233
+ @url = @client.expand(@shortUrl)
234
+ end
235
+ should 'return a missing url' do
236
+ assert_instance_of Bitlyr::MissingUrl, @url
237
+ end
238
+ should 'return an error' do
239
+ assert_equal 'NOT_FOUND', @url.error
240
+ end
241
+ should 'return the original url' do
242
+ assert_equal @shortUrl, @url.short_url
243
+ end
244
+ end
245
+ end
246
+ context "multiple urls" do
247
+ setup do
248
+ @hash = '9uX1TE'
249
+ @short_url = 'http://bit.ly/cEFx9W'
250
+ stub_get("http://api.bit.ly/v3/expand?hash=9uX1TE&shortUrl=http%3A%2F%2Fbit.ly%2FcEFx9W&login=test_account&apiKey=test_key", 'multiple_urls.json')
251
+ @urls = @client.expand([@hash, @short_url])
252
+ end
253
+ should "return an array of results" do
254
+ assert_instance_of Array, @urls
255
+ end
256
+ should "return an array of bitlyr urls" do
257
+ @urls.each { |url| assert_instance_of Bitlyr::Url, url }
258
+ end
259
+ should "return the original url" do
260
+ assert_equal 'http://betaworks.com/', @urls[0].long_url
261
+ assert_equal 'http://philnash.co.uk', @urls[1].long_url
262
+ end
263
+ end
264
+ end
265
+
266
+ context "clicks for urls" do
267
+ context "with multiple urls" do
268
+ setup do
269
+ @hash = '9uX1TE'
270
+ @short_url = 'http://bit.ly/cEFx9W'
271
+ stub_get("http://api.bit.ly/v3/clicks?hash=9uX1TE&shortUrl=http%3A%2F%2Fbit.ly%2FcEFx9W&login=test_account&apiKey=test_key", 'multiple_url_click.json')
272
+ @urls = @client.clicks([@hash, @short_url])
273
+ end
274
+
275
+ should "return an array of results" do
276
+ assert_instance_of Array, @urls
277
+ end
278
+ should "return an array of bitlyr urls" do
279
+ @urls.each { |url| assert_instance_of Bitlyr::Url, url }
280
+ end
281
+ should "return the user and global clicks for each url" do
282
+ assert_equal 0, @urls[0].user_clicks
283
+ assert_equal 67, @urls[0].global_clicks
284
+ assert_equal 0, @urls[1].user_clicks
285
+ assert_equal 0, @urls[1].global_clicks
286
+ end
287
+ end
288
+ end
289
+
290
+ context "looking up" do
291
+ context "a single url" do
292
+ setup do
293
+ @url = "http://code.google.com/p/bitly-api/"
294
+ stub_get("http://api.bit.ly/v3/lookup?url=#{CGI.escape(@url)}&login=test_account&apiKey=test_key", 'lookup_single_url.json')
295
+ @lookup = @client.lookup(@url)
296
+ end
297
+ should "return a url object" do
298
+ assert_instance_of Bitlyr::Url, @lookup
299
+ end
300
+ should "return the original url" do
301
+ assert_equal @url, @lookup.long_url
302
+ end
303
+ should "return the global hash" do
304
+ assert_equal '1oDCU', @lookup.global_hash
305
+ end
306
+ should 'return the short url' do
307
+ assert_equal 'http://bit.ly/1oDCU', @lookup.short_url
308
+ end
309
+ end
310
+ context "multiple urls" do
311
+ setup do
312
+ @url1 = 'http://betaworks.com/'
313
+ @url2 = 'http://code.google.com/p/bitly-api/'
314
+ stub_get("http://api.bit.ly/v3/lookup?url=#{CGI.escape(@url1)}&url=#{CGI.escape(@url2)}&login=test_account&apiKey=test_key", 'lookup_multiple_url.json')
315
+ @lookup = @client.lookup([@url1, @url2])
316
+ end
317
+ should 'return an array' do
318
+ assert_instance_of Array, @lookup
319
+ end
320
+ should 'return an array of urls' do
321
+ @lookup.each { |url| assert_instance_of Bitlyr::Url, url }
322
+ end
323
+ should 'return the original urls in order' do
324
+ assert_equal @url1, @lookup[0].long_url
325
+ assert_equal @url2, @lookup[1].long_url
326
+ end
327
+ should 'return global hashes' do
328
+ assert_equal 'aboutus', @lookup[0].global_hash
329
+ assert_equal '1oDCU', @lookup[1].global_hash
330
+ end
331
+ should 'return short urls' do
332
+ assert_equal 'http://bit.ly/aboutus', @lookup[0].short_url
333
+ assert_equal 'http://bit.ly/1oDCU', @lookup[1].short_url
334
+ end
335
+ end
336
+ context "a non existant url" do
337
+ setup do
338
+ @url = "asdf://www.google.com/not/a/real/link"
339
+ stub_get("http://api.bit.ly/v3/lookup?url=#{CGI.escape(@url)}&login=test_account&apiKey=test_key", 'lookup_not_real_url.json')
340
+ @lookup = @client.lookup(@url)
341
+ end
342
+ should 'return a missing url' do
343
+ assert_instance_of Bitlyr::MissingUrl, @lookup
344
+ end
345
+ should 'return the original url' do
346
+ assert_equal @url, @lookup.long_url
347
+ end
348
+ should 'return the error' do
349
+ assert_equal 'NOT_FOUND', @lookup.error
350
+ end
351
+ end
352
+ end
353
+
354
+ context "info for urls" do
355
+ context "a single url" do
356
+ setup do
357
+ @url = "http://bit.ly/1YKMfY"
358
+ stub_get("http://api.bit.ly/v3/info?shortUrl=#{CGI.escape(@url)}&login=test_account&apiKey=test_key", "url_info.json")
359
+ @info = @client.info(@url)
360
+ end
361
+ should "return a url object" do
362
+ assert_instance_of Bitlyr::Url, @info
363
+ end
364
+ should "return the original short url" do
365
+ assert_equal @url, @info.short_url
366
+ end
367
+ should "return the global hash" do
368
+ assert_equal '1YKMfY', @info.global_hash
369
+ end
370
+ should "return the user hash" do
371
+ assert_equal '1YKMfY', @info.user_hash
372
+ end
373
+ should "return the creator" do
374
+ assert_equal 'bitly', @info.created_by
375
+ end
376
+ should "return the title" do
377
+ assert_equal 'betaworks', @info.title
378
+ end
379
+ end
380
+ context "a single hash" do
381
+ setup do
382
+ @hash = "1YKMfY"
383
+ stub_get("http://api.bit.ly/v3/info?hash=#{@hash}&login=test_account&apiKey=test_key", "url_info.json")
384
+ @info = @client.info(@hash)
385
+ end
386
+ should "return a url object" do
387
+ assert_instance_of Bitlyr::Url, @info
388
+ end
389
+ should "return the original short url" do
390
+ assert_equal "http://bit.ly/#{@hash}", @info.short_url
391
+ end
392
+ should "return the global hash" do
393
+ assert_equal @hash, @info.global_hash
394
+ end
395
+ should "return the user hash" do
396
+ assert_equal @hash, @info.user_hash
397
+ end
398
+ should "return the creator" do
399
+ assert_equal 'bitly', @info.created_by
400
+ end
401
+ should "return the title" do
402
+ assert_equal 'betaworks', @info.title
403
+ end
404
+ end
405
+ context "multiple urls with urls and hashes" do
406
+ setup do
407
+ @url = 'http://bit.ly/1YKMfY'
408
+ @hash = '9uX1TE'
409
+ stub_get("http://api.bit.ly/v3/info?shortUrl=#{CGI.escape(@url)}&hash=#{@hash}&login=test_account&apiKey=test_key", "multiple_info.json")
410
+ @infos = @client.info([@url, @hash])
411
+ end
412
+ should 'return an array' do
413
+ assert_instance_of Array, @infos
414
+ end
415
+ should 'return an array of urls' do
416
+ @infos.each { |url| assert_instance_of Bitlyr::Url, url }
417
+ end
418
+ should 'return the original urls in order' do
419
+ assert_equal @url, @infos[0].short_url
420
+ assert_equal @hash, @infos[1].user_hash
421
+ end
422
+ should 'return info for each' do
423
+ assert_equal 'bitly', @infos[0].created_by
424
+ assert_equal 'philnash', @infos[1].created_by
425
+ end
426
+ end
427
+ context "a nonexistant url" do
428
+ setup do
429
+ @url = 'http://bit.ly/1YKMfYasb'
430
+ stub_get("http://api.bit.ly/v3/info?shortUrl=#{CGI.escape(@url)}&login=test_account&apiKey=test_key", 'not_found_info.json')
431
+ @info = @client.info(@url)
432
+ end
433
+ should "return a missing url" do
434
+ assert_instance_of Bitlyr::MissingUrl, @info
435
+ end
436
+ should 'return the original url' do
437
+ assert_equal @url, @info.short_url
438
+ end
439
+ should 'return the error' do
440
+ assert_equal 'NOT_FOUND', @info.error
441
+ end
442
+ end
443
+ end
444
+
445
+ context "referrers for url" do
446
+ context "a single url" do
447
+ setup do
448
+ @url = 'http://bit.ly/djZ9g4'
449
+ stub_get("http://api.bit.ly/v3/referrers?shortUrl=#{CGI.escape(@url)}&login=test_account&apiKey=test_key", 'referrer_url.json')
450
+ @client_url = @client.referrers(@url)
451
+ end
452
+ should 'return a url object' do
453
+ assert_instance_of Bitlyr::Url, @client_url
454
+ end
455
+ should 'return the original short url' do
456
+ assert_equal @url, @client_url.short_url
457
+ end
458
+ should 'return the global hash' do
459
+ assert_equal 'djZ9g4', @client_url.global_hash
460
+ end
461
+ should 'return the user hash' do
462
+ assert_equal 'djZ9g4', @client_url.user_hash
463
+ end
464
+ should 'return an array of referrers' do
465
+ assert_instance_of Array, @client_url.referrers
466
+ end
467
+ should 'return a referrer' do
468
+ assert_instance_of Bitlyr::Referrer, @client_url.referrers.first
469
+ end
470
+ should 'return the clicks and referrer from that url' do
471
+ assert_equal 'direct', @client_url.referrers.first.referrer
472
+ assert_equal 62, @client_url.referrers.first.clicks
473
+ end
474
+ end
475
+ context "a single hash" do
476
+ setup do
477
+ @hash = 'djZ9g4'
478
+ stub_get("http://api.bit.ly/v3/referrers?hash=#{CGI.escape(@hash)}&login=test_account&apiKey=test_key", 'referrer_url.json')
479
+ @client_url = @client.referrers(@hash)
480
+ end
481
+ should 'return a url object' do
482
+ assert_instance_of Bitlyr::Url, @client_url
483
+ end
484
+ should 'return the original short url' do
485
+ assert_equal "http://bit.ly/#{@hash}", @client_url.short_url
486
+ end
487
+ should 'return the global hash' do
488
+ assert_equal @hash, @client_url.global_hash
489
+ end
490
+ should 'return the user hash' do
491
+ assert_equal @hash, @client_url.user_hash
492
+ end
493
+ should 'return an array of referrers' do
494
+ assert_instance_of Array, @client_url.referrers
495
+ end
496
+ should 'return a referrer' do
497
+ assert_instance_of Bitlyr::Referrer, @client_url.referrers.first
498
+ end
499
+ should 'return the clicks and referrer from that url' do
500
+ assert_equal 'direct', @client_url.referrers.first.referrer
501
+ assert_equal 62, @client_url.referrers.first.clicks
502
+ end
503
+ end
504
+
505
+ context "an array" do
506
+ should "raise an argument error" do
507
+ assert_raises ArgumentError do
508
+ @client.referrers(['http://bit.ly/djZ9g4'])
509
+ end
510
+ end
511
+ end
512
+ end
513
+
514
+ context "countries for url" do
515
+ context "a single url" do
516
+ setup do
517
+ @url = 'http://bit.ly/djZ9g4'
518
+ stub_get("http://api.bit.ly/v3/countries?shortUrl=#{CGI.escape(@url)}&login=test_account&apiKey=test_key", 'country_url.json')
519
+ @client_url = @client.countries(@url)
520
+ end
521
+ should 'return a url object' do
522
+ assert_instance_of Bitlyr::Url, @client_url
523
+ end
524
+ should 'return the original short url' do
525
+ assert_equal @url, @client_url.short_url
526
+ end
527
+ should 'return the global hash' do
528
+ assert_equal 'djZ9g4', @client_url.global_hash
529
+ end
530
+ should 'return the user hash' do
531
+ assert_equal 'djZ9g4', @client_url.user_hash
532
+ end
533
+ should 'return an array of countries' do
534
+ assert_instance_of Array, @client_url.countries
535
+ end
536
+ should 'return a country' do
537
+ assert_instance_of Bitlyr::Country, @client_url.countries.first
538
+ end
539
+ should 'return the clicks and country from that url' do
540
+ assert_equal 'US', @client_url.countries.first.country
541
+ assert_equal 58, @client_url.countries.first.clicks
542
+ end
543
+ end
544
+ context "a single hash" do
545
+ setup do
546
+ @hash = 'djZ9g4'
547
+ stub_get("http://api.bit.ly/v3/countries?hash=#{CGI.escape(@hash)}&login=test_account&apiKey=test_key", 'country_hash.json')
548
+ @client_url = @client.countries(@hash)
549
+ end
550
+ should 'return a url object' do
551
+ assert_instance_of Bitlyr::Url, @client_url
552
+ end
553
+ should 'return the original short url' do
554
+ assert_equal "http://bit.ly/#{@hash}", @client_url.short_url
555
+ end
556
+ should 'return the global hash' do
557
+ assert_equal @hash, @client_url.global_hash
558
+ end
559
+ should 'return the user hash' do
560
+ assert_equal @hash, @client_url.user_hash
561
+ end
562
+ should 'return an array of countries' do
563
+ assert_instance_of Array, @client_url.countries
564
+ end
565
+ should 'return a country' do
566
+ assert_instance_of Bitlyr::Country, @client_url.countries.first
567
+ end
568
+ should 'return the clicks and country from that url' do
569
+ assert_equal 'US', @client_url.countries.first.country
570
+ assert_equal 58, @client_url.countries.first.clicks
571
+ end
572
+ end
573
+
574
+ context "an array" do
575
+ should "raise an argument error" do
576
+ assert_raises ArgumentError do
577
+ @client.countries(['http://bit.ly/djZ9g4'])
578
+ end
579
+ end
580
+ end
581
+ end
582
+
583
+ context "clicks by minute for urls" do
584
+ context "with a single short url" do
585
+ setup do
586
+ @short_url = "http://j.mp/9DguyN"
587
+ stub_get("http://api.bit.ly/v3/clicks_by_minute?shortUrl=#{CGI.escape(@short_url)}&login=test_account&apiKey=test_key", 'clicks_by_minute1_url.json')
588
+ @url = @client.clicks_by_minute(@short_url)
589
+ end
590
+ should "return a url object" do
591
+ assert_instance_of Bitlyr::Url, @url
592
+ end
593
+ should 'return the original hash' do
594
+ assert_equal "9DguyN", @url.user_hash
595
+ end
596
+ should "return a global hash" do
597
+ assert_equal '9DguyN', @url.global_hash
598
+ end
599
+ should 'return a short url' do
600
+ assert_equal @short_url, @url.short_url
601
+ end
602
+ should 'return an array of clicks by minute' do
603
+ assert_instance_of Array, @url.clicks_by_minute
604
+ assert_equal 0, @url.clicks_by_minute[0]
605
+ assert_equal 1, @url.clicks_by_minute[1]
606
+ end
607
+ end
608
+ context "with a single hash" do
609
+ setup do
610
+ @hash = '9DguyN'
611
+ stub_get("http://api.bit.ly/v3/clicks_by_minute?hash=#{@hash}&login=test_account&apiKey=test_key", 'clicks_by_minute_hash.json')
612
+ @url = @client.clicks_by_minute(@hash)
613
+ end
614
+ should 'return a url object' do
615
+ assert_instance_of Bitlyr::Url, @url
616
+ end
617
+ should 'return the original hash' do
618
+ assert_equal "9DguyN", @url.user_hash
619
+ end
620
+ should "return a global hash" do
621
+ assert_equal '9DguyN', @url.global_hash
622
+ end
623
+ should 'return an array of clicks by minute' do
624
+ assert_instance_of Array, @url.clicks_by_minute
625
+ assert_equal 0, @url.clicks_by_minute[0]
626
+ assert_equal 1, @url.clicks_by_minute[6]
627
+ end
628
+ end
629
+ context "with multiple hashes" do
630
+ setup do
631
+ @hash1 = '9DguyN'
632
+ @hash2 = 'dvxi6W'
633
+ @hashes = [@hash1, @hash2]
634
+ stub_get("http://api.bit.ly/v3/clicks_by_minute?hash=#{@hash1}&hash=#{@hash2}&login=test_account&apiKey=test_key", 'clicks_by_minute_hashes.json')
635
+ @urls = @client.clicks_by_minute(@hashes)
636
+ end
637
+ should 'return an array of urls' do
638
+ assert_instance_of Array, @urls
639
+ assert_instance_of Bitlyr::Url, @urls[0]
640
+ assert_instance_of Bitlyr::Url, @urls[1]
641
+ end
642
+ should 'return the original hashes in order' do
643
+ assert_equal @hash1, @urls[0].user_hash
644
+ assert_equal @hash2, @urls[1].user_hash
645
+ end
646
+ should 'return arrays of clicks for each hash' do
647
+ assert_instance_of Array, @urls[0].clicks_by_minute
648
+ assert_instance_of Array, @urls[1].clicks_by_minute
649
+ end
650
+ end
651
+ end
652
+
653
+ context "clicks by day for urls" do
654
+ setup do
655
+ @hash1 = "9DguyN"
656
+ @hash2 = "dvxi6W"
657
+ @hashes = [@hash1, @hash2]
658
+ end
659
+ context "for multiple hashes" do
660
+ setup do
661
+ stub_get("http://api.bit.ly/v3/clicks_by_day?hash=9DguyN&hash=dvxi6W&login=test_account&apiKey=test_key", 'clicks_by_day.json')
662
+ @urls = @client.clicks_by_day(@hashes)
663
+ end
664
+ should "return an array of urls" do
665
+ assert_instance_of Array, @urls
666
+ assert_instance_of Bitlyr::Url, @urls[0]
667
+ assert_instance_of Bitlyr::Url, @urls[1]
668
+ end
669
+ should "return an array of days for each url" do
670
+ assert_instance_of Array, @urls[0].clicks_by_day
671
+ assert_instance_of Bitlyr::Day, @urls[0].clicks_by_day[0]
672
+ end
673
+ should "return a Time for the day" do
674
+ assert_instance_of Time, @urls[0].clicks_by_day[0].day_start
675
+ assert_equal Time.parse('2010/11/23'), @urls[0].clicks_by_day[0].day_start
676
+ end
677
+ should 'return the number of clicks for that day' do
678
+ assert_equal 1, @urls[0].clicks_by_day[0].clicks
679
+ end
680
+ end
681
+ context "with optional days parameter" do
682
+ should 'add days to url' do
683
+ stub_get("http://api.bit.ly/v3/clicks_by_day?hash=9DguyN&hash=dvxi6W&login=test_account&apiKey=test_key&days=30", 'clicks_by_day.json')
684
+ @urls = @client.clicks_by_day(@hashes, :days => 30)
685
+ end
686
+
687
+ should 'not add other parameters' do
688
+ stub_get("http://api.bit.ly/v3/clicks_by_day?hash=9DguyN&hash=dvxi6W&login=test_account&apiKey=test_key&days=30", 'clicks_by_day.json')
689
+ @urls = @client.clicks_by_day(@hashes, :days => 30, :something_else => 'bacon')
690
+ end
691
+ end
692
+ end
693
+ end
694
+
695
+ context "without valid credentials" do
696
+ setup do
697
+ @client = Bitlyr::Client.new(Bitlyr::Strategy::ApiKey.new('rubbish', 'wrong'))
698
+ stub_get(%r|http://api\.bit\.ly/v3/shorten?.*|, 'invalid_credentials.json')
699
+ end
700
+ should "raise an error on any call" do
701
+ assert_raise BitlyrError do
702
+ @client.shorten('http://google.com')
703
+ end
704
+ end
705
+ end
706
+ end
707
+
708
+ context "with a valid client" do
709
+ context "with the OAuth Strategy" do
710
+ setup do
711
+ strategy = Bitlyr::Strategy::OAuth.new("id", "secret")
712
+ strategy.set_access_token_from_token!('token')
713
+ @client = Bitlyr::Client.new(strategy)
714
+ end
715
+
716
+ context "validating another account credentials" do
717
+ context "with valid credentials" do
718
+ setup do
719
+ stub_get(%r{https://api-ssl\.bit\.ly/v3/validate?.*x_login=correct.*}, "valid_user.json")
720
+ end
721
+
722
+ should "return true" do
723
+ assert @client.validate('correct','well_done')
724
+ end
725
+
726
+ should "return true for valid? as well" do
727
+ assert @client.valid?('correct','well_done')
728
+ end
729
+ end
730
+
731
+ context "with invalid credentials" do
732
+ setup do
733
+ stub_get(%r{https://api-ssl\.bit\.ly/v3/validate?.*x_login=wrong.*},"invalid_user.json")
734
+ end
735
+
736
+ should "return false" do
737
+ assert !@client.validate('wrong','so_very_wrong')
738
+ end
739
+
740
+ should "return false for valid? too" do
741
+ assert !@client.valid?('wrong','so_very_wrong')
742
+ end
743
+ end
744
+ end
745
+
746
+ context "checking a bitly pro domain" do
747
+ context "with a bitly pro domain" do
748
+ setup do
749
+ stub_get(%r|https://api-ssl\.bit\.ly/v3/bitly_pro_domain?.*domain=nyti\.ms.*|, 'bitly_pro_domain.json')
750
+ end
751
+
752
+ should "return true" do
753
+ assert @client.bitly_pro_domain('nyti.ms')
754
+ end
755
+ end
756
+
757
+ context "with a non bitly pro domain" do
758
+ setup do
759
+ stub_get(%r|https://api-ssl\.bit\.ly/v3/bitly_pro_domain?.*domain=philnash\.co\.uk.*|, 'not_bitly_pro_domain.json')
760
+ end
761
+
762
+ should "return false" do
763
+ assert !@client.bitly_pro_domain('philnash.co.uk')
764
+ end
765
+ end
766
+
767
+ context "with an invalid domain" do
768
+ setup do
769
+ stub_get(%r|https://api-ssl\.bit\.ly/v3/bitly_pro_domain?.*domain=philnash.*|, 'invalid_bitly_pro_domain.json')
770
+ end
771
+
772
+ should "raise an error" do
773
+ assert_raise BitlyrError do
774
+ @client.bitly_pro_domain('philnash')
775
+ end
776
+ end
777
+ end
778
+ end
779
+
780
+ context "shortening a url" do
781
+ context "with just the url" do
782
+ setup do
783
+ @long_url = "http://betaworks.com/"
784
+ stub_get(%r|https://api-ssl\.bit\.ly/v3/shorten\?.*longUrl=#{CGI.escape(@long_url)}.*|, ['betaworks.json', 'betaworks2.json'])
785
+ @url = @client.shorten(@long_url)
786
+ end
787
+
788
+ should "return a url object" do
789
+ assert_instance_of Bitlyr::Url, @url
790
+ end
791
+
792
+ should "shorten the url" do
793
+ assert_equal 'http://bit.ly/9uX1TE', @url.short_url
794
+ end
795
+
796
+ should "return the original long url" do
797
+ assert_equal @long_url, @url.long_url
798
+ end
799
+
800
+ should "return a hash" do
801
+ assert_equal '9uX1TE', @url.user_hash
802
+ end
803
+
804
+ should "return a global hash" do
805
+ assert_equal '18H1ET', @url.global_hash
806
+ end
807
+
808
+ should "be a new hash the first time" do
809
+ assert @url.new_hash?
810
+ end
811
+
812
+ should "not be a new hash the second time" do
813
+ new_url = @client.shorten(@long_url)
814
+ assert !new_url.new_hash?
815
+ assert_not_same @url, new_url
816
+ end
817
+ end
818
+
819
+ context "with extra options" do
820
+ context "with the j.mp domain" do
821
+ setup do
822
+ stub_get("https://api-ssl.bit.ly/v3/shorten?longUrl=#{CGI.escape('http://betaworks.com/')}&access_token=token&domain=j.mp", 'betaworks_jmp.json')
823
+ @url = @client.shorten('http://betaworks.com/', :domain => "j.mp")
824
+ end
825
+
826
+ should "return a j.mp short url" do
827
+ assert_equal "http://j.mp/9uX1TE", @url.short_url
828
+ end
829
+ end
830
+
831
+ context "with another domain" do
832
+ setup do
833
+ stub_get("https://api-ssl.bit.ly/v3/shorten?longUrl=#{CGI.escape('http://betaworks.com/')}&access_token=token&domain=nyti.ms", 'invalid_domain.json')
834
+ end
835
+
836
+ should "raise an error" do
837
+ assert_raise BitlyrError do
838
+ url = @client.shorten('http://betaworks.com/', :domain => "nyti.ms")
839
+ end
840
+ end
841
+ end
842
+
843
+ context "with another user details" do
844
+ context "with correct details" do
845
+ setup do
846
+ @long_url = "http://betaworks.com/"
847
+ stub_get(%r{https://api-ssl\.bit\.ly/v3/shorten?.*longUrl=#{CGI.escape('http://betaworks.com/')}.*}, 'betaworks.json')
848
+ stub_get("https://api-ssl.bit.ly/v3/shorten?longUrl=#{CGI.escape('http://betaworks.com/')}&access_token=token&x_login=other_account&x_apiKey=other_apiKey", 'betaworks_other_user.json' )
849
+ @normal_url = @client.shorten(@long_url)
850
+ @other_user_url = @client.shorten(@long_url, :x_login => 'other_account', :x_apiKey => 'other_apiKey')
851
+ end
852
+
853
+ should "return a different hash" do
854
+ assert_not_equal @normal_url.user_hash, @other_user_url.user_hash
855
+ end
856
+
857
+ should "return a new hash" do
858
+ assert @other_user_url.new_hash?
859
+ end
860
+ end
861
+
862
+ context "without an api key" do
863
+ setup do
864
+ stub_get("https://api-ssl.bit.ly/v3/shorten?longUrl=#{CGI.escape('http://betaworks.com/')}&access_token=token&x_login=other_account", 'invalid_x_api_key.json' )
865
+ end
866
+
867
+ should "raise an error" do
868
+ assert_raise BitlyrError do
869
+ @client.shorten('http://betaworks.com/', :x_login => 'other_account')
870
+ end
871
+ end
872
+ end
873
+ end
874
+ end
875
+ end
876
+
877
+ context "expanding a url" do
878
+ context "a single url" do
879
+ context "with a hash" do
880
+ setup do
881
+ @hash = '9uX1TE'
882
+ stub_get(%r|https://api-ssl\.bit\.ly/v3/expand\?.*hash=9uX1TE.*|, '9uX1TE.json')
883
+ @url = @client.expand(@hash)
884
+ end
885
+
886
+ should 'return a url object' do
887
+ assert_instance_of Bitlyr::Url, @url
888
+ end
889
+
890
+ should 'return the original hash' do
891
+ assert_equal @hash, @url.user_hash
892
+ end
893
+
894
+ should "return a global hash" do
895
+ assert_equal '18H1ET', @url.global_hash
896
+ end
897
+
898
+ should 'return a long url' do
899
+ assert_equal 'http://betaworks.com/', @url.long_url
900
+ end
901
+
902
+ should 'return a short url' do
903
+ assert_equal "http://bit.ly/#{@hash}", @url.short_url
904
+ end
905
+ end
906
+
907
+ context "with the short url" do
908
+ setup do
909
+ @short_url = 'http://bit.ly/9uX1TE'
910
+ stub_get(%r|https://api-ssl\.bit\.ly/v3/expand\?.*shortUrl=http%3A%2F%2Fbit\.ly%2F9uX1TE.*|, 'bitly9uX1TE.json')
911
+ @url = @client.expand(@short_url)
912
+ end
913
+
914
+ should 'return a url object' do
915
+ assert_instance_of Bitlyr::Url, @url
916
+ end
917
+
918
+ should 'return the original hash' do
919
+ assert_equal "9uX1TE", @url.user_hash
920
+ end
921
+
922
+ should "return a global hash" do
923
+ assert_equal '18H1ET', @url.global_hash
924
+ end
925
+
926
+ should 'return a long url' do
927
+ assert_equal 'http://betaworks.com/', @url.long_url
928
+ end
929
+
930
+ should 'return a short url' do
931
+ assert_equal "http://bit.ly/9uX1TE", @url.short_url
932
+ end
933
+ end
934
+
935
+ context "that doesn't exist" do
936
+ setup do
937
+ @shortUrl = 'http://bit.ly/9uX1TEsd'
938
+ stub_get(%r|https://api-ssl\.bit\.ly/v3/expand\?.*shortUrl=http%3A%2F%2Fbit\.ly%2F9uX1TEsd.*|, 'missing_hash.json')
939
+ @url = @client.expand(@shortUrl)
940
+ end
941
+ should 'return a missing url' do
942
+ assert_instance_of Bitlyr::MissingUrl, @url
943
+ end
944
+ should 'return an error' do
945
+ assert_equal 'NOT_FOUND', @url.error
946
+ end
947
+ should 'return the original url' do
948
+ assert_equal @shortUrl, @url.short_url
949
+ end
950
+ end
951
+ end
952
+ context "multiple urls" do
953
+ setup do
954
+ @hash = '9uX1TE'
955
+ @short_url = 'http://bit.ly/cEFx9W'
956
+ stub_get("https://api-ssl.bit.ly/v3/expand?hash=9uX1TE&shortUrl=http%3A%2F%2Fbit.ly%2FcEFx9W&access_token=token", 'multiple_urls.json')
957
+ @urls = @client.expand([@hash, @short_url])
958
+ end
959
+ should "return an array of results" do
960
+ assert_instance_of Array, @urls
961
+ end
962
+ should "return an array of bitly urls" do
963
+ @urls.each { |url| assert_instance_of Bitlyr::Url, url }
964
+ end
965
+ should "return the original url" do
966
+ assert_equal 'http://betaworks.com/', @urls[0].long_url
967
+ assert_equal 'http://philnash.co.uk', @urls[1].long_url
968
+ end
969
+ end
970
+ end
971
+
972
+ context "clicks for urls" do
973
+ context "with multiple urls" do
974
+ setup do
975
+ @hash = '9uX1TE'
976
+ @short_url = 'http://bit.ly/cEFx9W'
977
+ stub_get("https://api-ssl.bit.ly/v3/clicks?hash=9uX1TE&shortUrl=http%3A%2F%2Fbit.ly%2FcEFx9W&access_token=token", 'multiple_url_click.json')
978
+ @urls = @client.clicks([@hash, @short_url])
979
+ end
980
+
981
+ should "return an array of results" do
982
+ assert_instance_of Array, @urls
983
+ end
984
+ should "return an array of bitly urls" do
985
+ @urls.each { |url| assert_instance_of Bitlyr::Url, url }
986
+ end
987
+ should "return the user and global clicks for each url" do
988
+ assert_equal 0, @urls[0].user_clicks
989
+ assert_equal 67, @urls[0].global_clicks
990
+ assert_equal 0, @urls[1].user_clicks
991
+ assert_equal 0, @urls[1].global_clicks
992
+ end
993
+ end
994
+ end
995
+
996
+ context "looking up" do
997
+ context "a single url" do
998
+ setup do
999
+ @url = "http://code.google.com/p/bitly-api/"
1000
+ stub_get("https://api-ssl.bit.ly/v3/lookup?url=#{CGI.escape(@url)}&access_token=token", 'lookup_single_url.json')
1001
+ @lookup = @client.lookup(@url)
1002
+ end
1003
+ should "return a url object" do
1004
+ assert_instance_of Bitlyr::Url, @lookup
1005
+ end
1006
+ should "return the original url" do
1007
+ assert_equal @url, @lookup.long_url
1008
+ end
1009
+ should "return the global hash" do
1010
+ assert_equal '1oDCU', @lookup.global_hash
1011
+ end
1012
+ should 'return the short url' do
1013
+ assert_equal 'http://bit.ly/1oDCU', @lookup.short_url
1014
+ end
1015
+ end
1016
+ context "multiple urls" do
1017
+ setup do
1018
+ @url1 = 'http://betaworks.com/'
1019
+ @url2 = 'http://code.google.com/p/bitly-api/'
1020
+ stub_get("https://api-ssl.bit.ly/v3/lookup?url=#{CGI.escape(@url1)}&url=#{CGI.escape(@url2)}&access_token=token", 'lookup_multiple_url.json')
1021
+ @lookup = @client.lookup([@url1, @url2])
1022
+ end
1023
+ should 'return an array' do
1024
+ assert_instance_of Array, @lookup
1025
+ end
1026
+ should 'return an array of urls' do
1027
+ @lookup.each { |url| assert_instance_of Bitlyr::Url, url }
1028
+ end
1029
+ should 'return the original urls in order' do
1030
+ assert_equal @url1, @lookup[0].long_url
1031
+ assert_equal @url2, @lookup[1].long_url
1032
+ end
1033
+ should 'return global hashes' do
1034
+ assert_equal 'aboutus', @lookup[0].global_hash
1035
+ assert_equal '1oDCU', @lookup[1].global_hash
1036
+ end
1037
+ should 'return short urls' do
1038
+ assert_equal 'http://bit.ly/aboutus', @lookup[0].short_url
1039
+ assert_equal 'http://bit.ly/1oDCU', @lookup[1].short_url
1040
+ end
1041
+ end
1042
+ context "a non existant url" do
1043
+ setup do
1044
+ @url = "asdf://www.google.com/not/a/real/link"
1045
+ stub_get("https://api-ssl.bit.ly/v3/lookup?url=#{CGI.escape(@url)}&access_token=token", 'lookup_not_real_url.json')
1046
+ @lookup = @client.lookup(@url)
1047
+ end
1048
+ should 'return a missing url' do
1049
+ assert_instance_of Bitlyr::MissingUrl, @lookup
1050
+ end
1051
+ should 'return the original url' do
1052
+ assert_equal @url, @lookup.long_url
1053
+ end
1054
+ should 'return the error' do
1055
+ assert_equal 'NOT_FOUND', @lookup.error
1056
+ end
1057
+ end
1058
+ end
1059
+
1060
+ context "info for urls" do
1061
+ context "a single url" do
1062
+ setup do
1063
+ @url = "http://bit.ly/1YKMfY"
1064
+ stub_get("https://api-ssl.bit.ly/v3/info?shortUrl=#{CGI.escape(@url)}&access_token=token", "url_info.json")
1065
+ @info = @client.info(@url)
1066
+ end
1067
+ should "return a url object" do
1068
+ assert_instance_of Bitlyr::Url, @info
1069
+ end
1070
+ should "return the original short url" do
1071
+ assert_equal @url, @info.short_url
1072
+ end
1073
+ should "return the global hash" do
1074
+ assert_equal '1YKMfY', @info.global_hash
1075
+ end
1076
+ should "return the user hash" do
1077
+ assert_equal '1YKMfY', @info.user_hash
1078
+ end
1079
+ should "return the creator" do
1080
+ assert_equal 'bitly', @info.created_by
1081
+ end
1082
+ should "return the title" do
1083
+ assert_equal 'betaworks', @info.title
1084
+ end
1085
+ end
1086
+ context "a single hash" do
1087
+ setup do
1088
+ @hash = "1YKMfY"
1089
+ stub_get("https://api-ssl.bit.ly/v3/info?hash=#{@hash}&access_token=token", "url_info.json")
1090
+ @info = @client.info(@hash)
1091
+ end
1092
+ should "return a url object" do
1093
+ assert_instance_of Bitlyr::Url, @info
1094
+ end
1095
+ should "return the original short url" do
1096
+ assert_equal "http://bit.ly/#{@hash}", @info.short_url
1097
+ end
1098
+ should "return the global hash" do
1099
+ assert_equal @hash, @info.global_hash
1100
+ end
1101
+ should "return the user hash" do
1102
+ assert_equal @hash, @info.user_hash
1103
+ end
1104
+ should "return the creator" do
1105
+ assert_equal 'bitly', @info.created_by
1106
+ end
1107
+ should "return the title" do
1108
+ assert_equal 'betaworks', @info.title
1109
+ end
1110
+ end
1111
+ context "multiple urls with urls and hashes" do
1112
+ setup do
1113
+ @url = 'http://bit.ly/1YKMfY'
1114
+ @hash = '9uX1TE'
1115
+ stub_get("https://api-ssl.bit.ly/v3/info?shortUrl=#{CGI.escape(@url)}&hash=#{@hash}&access_token=token", "multiple_info.json")
1116
+ @infos = @client.info([@url, @hash])
1117
+ end
1118
+ should 'return an array' do
1119
+ assert_instance_of Array, @infos
1120
+ end
1121
+ should 'return an array of urls' do
1122
+ @infos.each { |url| assert_instance_of Bitlyr::Url, url }
1123
+ end
1124
+ should 'return the original urls in order' do
1125
+ assert_equal @url, @infos[0].short_url
1126
+ assert_equal @hash, @infos[1].user_hash
1127
+ end
1128
+ should 'return info for each' do
1129
+ assert_equal 'bitly', @infos[0].created_by
1130
+ assert_equal 'philnash', @infos[1].created_by
1131
+ end
1132
+ end
1133
+ context "a nonexistant url" do
1134
+ setup do
1135
+ @url = 'http://bit.ly/1YKMfYasb'
1136
+ stub_get("https://api-ssl.bit.ly/v3/info?shortUrl=#{CGI.escape(@url)}&access_token=token", 'not_found_info.json')
1137
+ @info = @client.info(@url)
1138
+ end
1139
+ should "return a missing url" do
1140
+ assert_instance_of Bitlyr::MissingUrl, @info
1141
+ end
1142
+ should 'return the original url' do
1143
+ assert_equal @url, @info.short_url
1144
+ end
1145
+ should 'return the error' do
1146
+ assert_equal 'NOT_FOUND', @info.error
1147
+ end
1148
+ end
1149
+ end
1150
+
1151
+ context "referrers for url" do
1152
+ context "a single url" do
1153
+ setup do
1154
+ @url = 'http://bit.ly/djZ9g4'
1155
+ stub_get("https://api-ssl.bit.ly/v3/referrers?shortUrl=#{CGI.escape(@url)}&access_token=token", 'referrer_url.json')
1156
+ @client_url = @client.referrers(@url)
1157
+ end
1158
+ should 'return a url object' do
1159
+ assert_instance_of Bitlyr::Url, @client_url
1160
+ end
1161
+ should 'return the original short url' do
1162
+ assert_equal @url, @client_url.short_url
1163
+ end
1164
+ should 'return the global hash' do
1165
+ assert_equal 'djZ9g4', @client_url.global_hash
1166
+ end
1167
+ should 'return the user hash' do
1168
+ assert_equal 'djZ9g4', @client_url.user_hash
1169
+ end
1170
+ should 'return an array of referrers' do
1171
+ assert_instance_of Array, @client_url.referrers
1172
+ end
1173
+ should 'return a referrer' do
1174
+ assert_instance_of Bitlyr::Referrer, @client_url.referrers.first
1175
+ end
1176
+ should 'return the clicks and referrer from that url' do
1177
+ assert_equal 'direct', @client_url.referrers.first.referrer
1178
+ assert_equal 62, @client_url.referrers.first.clicks
1179
+ end
1180
+ end
1181
+ context "a single hash" do
1182
+ setup do
1183
+ @hash = 'djZ9g4'
1184
+ stub_get("https://api-ssl.bit.ly/v3/referrers?hash=#{CGI.escape(@hash)}&access_token=token", 'referrer_url.json')
1185
+ @client_url = @client.referrers(@hash)
1186
+ end
1187
+ should 'return a url object' do
1188
+ assert_instance_of Bitlyr::Url, @client_url
1189
+ end
1190
+ should 'return the original short url' do
1191
+ assert_equal "http://bit.ly/#{@hash}", @client_url.short_url
1192
+ end
1193
+ should 'return the global hash' do
1194
+ assert_equal @hash, @client_url.global_hash
1195
+ end
1196
+ should 'return the user hash' do
1197
+ assert_equal @hash, @client_url.user_hash
1198
+ end
1199
+ should 'return an array of referrers' do
1200
+ assert_instance_of Array, @client_url.referrers
1201
+ end
1202
+ should 'return a referrer' do
1203
+ assert_instance_of Bitlyr::Referrer, @client_url.referrers.first
1204
+ end
1205
+ should 'return the clicks and referrer from that url' do
1206
+ assert_equal 'direct', @client_url.referrers.first.referrer
1207
+ assert_equal 62, @client_url.referrers.first.clicks
1208
+ end
1209
+ end
1210
+
1211
+ context "an array" do
1212
+ should "raise an argument error" do
1213
+ assert_raises ArgumentError do
1214
+ @client.referrers(['http://bit.ly/djZ9g4'])
1215
+ end
1216
+ end
1217
+ end
1218
+ end
1219
+
1220
+ context "countries for url" do
1221
+ context "a single url" do
1222
+ setup do
1223
+ @url = 'http://bit.ly/djZ9g4'
1224
+ stub_get("https://api-ssl.bit.ly/v3/countries?shortUrl=#{CGI.escape(@url)}&access_token=token", 'country_url.json')
1225
+ @client_url = @client.countries(@url)
1226
+ end
1227
+ should 'return a url object' do
1228
+ assert_instance_of Bitlyr::Url, @client_url
1229
+ end
1230
+ should 'return the original short url' do
1231
+ assert_equal @url, @client_url.short_url
1232
+ end
1233
+ should 'return the global hash' do
1234
+ assert_equal 'djZ9g4', @client_url.global_hash
1235
+ end
1236
+ should 'return the user hash' do
1237
+ assert_equal 'djZ9g4', @client_url.user_hash
1238
+ end
1239
+ should 'return an array of countries' do
1240
+ assert_instance_of Array, @client_url.countries
1241
+ end
1242
+ should 'return a country' do
1243
+ assert_instance_of Bitlyr::Country, @client_url.countries.first
1244
+ end
1245
+ should 'return the clicks and country from that url' do
1246
+ assert_equal 'US', @client_url.countries.first.country
1247
+ assert_equal 58, @client_url.countries.first.clicks
1248
+ end
1249
+ end
1250
+ context "a single hash" do
1251
+ setup do
1252
+ @hash = 'djZ9g4'
1253
+ stub_get("https://api-ssl.bit.ly/v3/countries?hash=#{CGI.escape(@hash)}&access_token=token", 'country_hash.json')
1254
+ @client_url = @client.countries(@hash)
1255
+ end
1256
+ should 'return a url object' do
1257
+ assert_instance_of Bitlyr::Url, @client_url
1258
+ end
1259
+ should 'return the original short url' do
1260
+ assert_equal "http://bit.ly/#{@hash}", @client_url.short_url
1261
+ end
1262
+ should 'return the global hash' do
1263
+ assert_equal @hash, @client_url.global_hash
1264
+ end
1265
+ should 'return the user hash' do
1266
+ assert_equal @hash, @client_url.user_hash
1267
+ end
1268
+ should 'return an array of countries' do
1269
+ assert_instance_of Array, @client_url.countries
1270
+ end
1271
+ should 'return a country' do
1272
+ assert_instance_of Bitlyr::Country, @client_url.countries.first
1273
+ end
1274
+ should 'return the clicks and country from that url' do
1275
+ assert_equal 'US', @client_url.countries.first.country
1276
+ assert_equal 58, @client_url.countries.first.clicks
1277
+ end
1278
+ end
1279
+
1280
+ context "an array" do
1281
+ should "raise an argument error" do
1282
+ assert_raises ArgumentError do
1283
+ @client.countries(['http://bit.ly/djZ9g4'])
1284
+ end
1285
+ end
1286
+ end
1287
+ end
1288
+
1289
+ context "clicks by minute for urls" do
1290
+ context "with a single short url" do
1291
+ setup do
1292
+ @short_url = "http://j.mp/9DguyN"
1293
+ stub_get("https://api-ssl.bit.ly/v3/clicks_by_minute?shortUrl=#{CGI.escape(@short_url)}&access_token=token", 'clicks_by_minute1_url.json')
1294
+ @url = @client.clicks_by_minute(@short_url)
1295
+ end
1296
+ should "return a url object" do
1297
+ assert_instance_of Bitlyr::Url, @url
1298
+ end
1299
+ should 'return the original hash' do
1300
+ assert_equal "9DguyN", @url.user_hash
1301
+ end
1302
+ should "return a global hash" do
1303
+ assert_equal '9DguyN', @url.global_hash
1304
+ end
1305
+ should 'return a short url' do
1306
+ assert_equal @short_url, @url.short_url
1307
+ end
1308
+ should 'return an array of clicks by minute' do
1309
+ assert_instance_of Array, @url.clicks_by_minute
1310
+ assert_equal 0, @url.clicks_by_minute[0]
1311
+ assert_equal 1, @url.clicks_by_minute[1]
1312
+ end
1313
+ end
1314
+ context "with a single hash" do
1315
+ setup do
1316
+ @hash = '9DguyN'
1317
+ stub_get("https://api-ssl.bit.ly/v3/clicks_by_minute?hash=#{@hash}&access_token=token", 'clicks_by_minute_hash.json')
1318
+ @url = @client.clicks_by_minute(@hash)
1319
+ end
1320
+ should 'return a url object' do
1321
+ assert_instance_of Bitlyr::Url, @url
1322
+ end
1323
+ should 'return the original hash' do
1324
+ assert_equal "9DguyN", @url.user_hash
1325
+ end
1326
+ should "return a global hash" do
1327
+ assert_equal '9DguyN', @url.global_hash
1328
+ end
1329
+ should 'return an array of clicks by minute' do
1330
+ assert_instance_of Array, @url.clicks_by_minute
1331
+ assert_equal 0, @url.clicks_by_minute[0]
1332
+ assert_equal 1, @url.clicks_by_minute[6]
1333
+ end
1334
+ end
1335
+ context "with multiple hashes" do
1336
+ setup do
1337
+ @hash1 = '9DguyN'
1338
+ @hash2 = 'dvxi6W'
1339
+ @hashes = [@hash1, @hash2]
1340
+ stub_get("https://api-ssl.bit.ly/v3/clicks_by_minute?hash=#{@hash1}&hash=#{@hash2}&access_token=token", 'clicks_by_minute_hashes.json')
1341
+ @urls = @client.clicks_by_minute(@hashes)
1342
+ end
1343
+ should 'return an array of urls' do
1344
+ assert_instance_of Array, @urls
1345
+ assert_instance_of Bitlyr::Url, @urls[0]
1346
+ assert_instance_of Bitlyr::Url, @urls[1]
1347
+ end
1348
+ should 'return the original hashes in order' do
1349
+ assert_equal @hash1, @urls[0].user_hash
1350
+ assert_equal @hash2, @urls[1].user_hash
1351
+ end
1352
+ should 'return arrays of clicks for each hash' do
1353
+ assert_instance_of Array, @urls[0].clicks_by_minute
1354
+ assert_instance_of Array, @urls[1].clicks_by_minute
1355
+ end
1356
+ end
1357
+ end
1358
+
1359
+ context "clicks by day for urls" do
1360
+ setup do
1361
+ @hash1 = "9DguyN"
1362
+ @hash2 = "dvxi6W"
1363
+ @hashes = [@hash1, @hash2]
1364
+ end
1365
+ context "for multiple hashes" do
1366
+ setup do
1367
+ stub_get("https://api-ssl.bit.ly/v3/clicks_by_day?hash=9DguyN&hash=dvxi6W&access_token=token", 'clicks_by_day.json')
1368
+ @urls = @client.clicks_by_day(@hashes)
1369
+ end
1370
+ should "return an array of urls" do
1371
+ assert_instance_of Array, @urls
1372
+ assert_instance_of Bitlyr::Url, @urls[0]
1373
+ assert_instance_of Bitlyr::Url, @urls[1]
1374
+ end
1375
+ should "return an array of days for each url" do
1376
+ assert_instance_of Array, @urls[0].clicks_by_day
1377
+ assert_instance_of Bitlyr::Day, @urls[0].clicks_by_day[0]
1378
+ end
1379
+ should "return a Time for the day" do
1380
+ assert_instance_of Time, @urls[0].clicks_by_day[0].day_start
1381
+ assert_equal Time.parse('2010/11/23'), @urls[0].clicks_by_day[0].day_start
1382
+ end
1383
+ should 'return the number of clicks for that day' do
1384
+ assert_equal 1, @urls[0].clicks_by_day[0].clicks
1385
+ end
1386
+ end
1387
+ context "with optional days parameter" do
1388
+ should 'add days to url' do
1389
+ stub_get("https://api-ssl.bit.ly/v3/clicks_by_day?hash=9DguyN&hash=dvxi6W&access_token=token&days=30", 'clicks_by_day.json')
1390
+ @urls = @client.clicks_by_day(@hashes, :days => 30)
1391
+ end
1392
+
1393
+ should 'not add other parameters' do
1394
+ stub_get("https://api-ssl.bit.ly/v3/clicks_by_day?hash=9DguyN&hash=dvxi6W&access_token=token&days=30", 'clicks_by_day.json')
1395
+ @urls = @client.clicks_by_day(@hashes, :days => 30, :something_else => 'bacon')
1396
+ end
1397
+ end
1398
+ end
1399
+ end
1400
+
1401
+ context "without valid credentials" do
1402
+ setup do
1403
+ strategy = Bitlyr::Strategy::OAuth.new('rubbish', 'wrong')
1404
+ strategy.set_access_token_from_token!('lies')
1405
+ @client = Bitlyr::Client.new(strategy)
1406
+ stub_get(%r|https://api-ssl\.bit\.ly/v3/shorten?.*|, 'invalid_credentials.json')
1407
+ end
1408
+ should "raise an error on any call" do
1409
+ assert_raise BitlyrError do
1410
+ @client.shorten('http://google.com')
1411
+ end
1412
+ end
1413
+ end
1414
+ end
1415
+ end