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,97 @@
1
+ require 'test_helper'
2
+
3
+ class TestUser < Test::Unit::TestCase
4
+ context "with an access_token" do
5
+ setup do
6
+ consumer = Bitlyr::Strategy::OAuth.new('consumer_token', 'consumer_secret')
7
+ access_token = consumer.get_access_token_from_token('token')
8
+ @user = Bitlyr::User.new(access_token)
9
+ end
10
+
11
+ context 'referrers' do
12
+ setup do
13
+ stub_get(/https:\/\/api-ssl\.bit\.ly\/v3\/user\/referrers\?(access|oauth)_token=token/, 'user_referrers.json')
14
+ @referrers = @user.referrers
15
+ end
16
+
17
+ should 'return an array of arrays of referrers' do
18
+ assert_kind_of Array, @referrers
19
+ assert_kind_of Array, @referrers.first
20
+ assert_kind_of Bitlyr::Referrer, @referrers.first.first
21
+ end
22
+
23
+ should 'return data about the referrer' do
24
+ referrer = @referrers.first.first
25
+ assert_equal 'direct', referrer.referrer
26
+ assert_equal 1, referrer.clicks
27
+ end
28
+ end
29
+
30
+ context 'countries' do
31
+ setup do
32
+ stub_get(/https:\/\/api-ssl\.bit\.ly\/v3\/user\/countries\?(access|oauth)_token=token/, 'user_countries.json')
33
+ @countries = @user.countries
34
+ end
35
+
36
+ should 'return an array of arrays of countries' do
37
+ assert_kind_of Array, @countries
38
+ assert_kind_of Array, @countries.first
39
+ assert_kind_of Bitlyr::Country, @countries.first.first
40
+ end
41
+
42
+ should 'return data about the country' do
43
+ country = @countries.first.first
44
+ assert_equal "US", country.country
45
+ assert_equal 4, country.clicks
46
+ end
47
+ end
48
+
49
+ context 'clicks' do
50
+ setup do
51
+ stub_get(/https:\/\/api-ssl\.bit\.ly\/v3\/user\/clicks\?(access|oauth)_token=token/, 'user_clicks.json')
52
+ @clicks = @user.clicks
53
+ end
54
+
55
+ should 'return an array of days' do
56
+ assert_kind_of Array, @clicks
57
+ assert_kind_of Bitlyr::Day, @clicks.first
58
+ end
59
+
60
+ should 'return data about the day' do
61
+ day = @clicks.first
62
+ assert_equal 4, day.clicks
63
+ end
64
+
65
+ should 'get total clicks' do
66
+ assert_equal 29, @user.total_clicks
67
+ end
68
+ end
69
+
70
+ context 'total clicks' do
71
+ setup do
72
+ stub_get(/https:\/\/api-ssl\.bit\.ly\/v3\/user\/clicks\?(access|oauth)_token=token/, 'user_clicks.json')
73
+ end
74
+
75
+ should 'get total clicks' do
76
+ assert_equal 29, @user.total_clicks
77
+ end
78
+ end
79
+
80
+ context 'realtime links' do
81
+ setup do
82
+ stub_get(/https:\/\/api-ssl\.bit\.ly\/v3\/user\/realtime_links\?(access|oauth)_token=token/, 'user_realtime_links.json')
83
+ @realtime_links = @user.realtime_links
84
+ end
85
+
86
+ should 'get an array of realtime links' do
87
+ assert_kind_of Array, @realtime_links
88
+ assert_kind_of Bitlyr::RealtimeLink, @realtime_links.first
89
+ end
90
+
91
+ should 'get the data for the realtime links' do
92
+ assert_equal 15, @realtime_links.first.clicks
93
+ assert_equal "i7JWw0", @realtime_links.first.user_hash
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'fakeweb'
6
+ require 'ruby-debug'
7
+
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
10
+ require 'bitlyr'
11
+
12
+ FakeWeb.allow_net_connect = false
13
+
14
+ def fixture_file(filename)
15
+ return '' if filename == ''
16
+ file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
17
+ File.read(file_path)
18
+ end
19
+
20
+ def stub_get(path, filename)
21
+ if filename.is_a?(Array)
22
+ response = filename.map { |f| { :body => fixture_file(f), :content_type => 'text/json' } }
23
+ else
24
+ response = { :body => fixture_file(filename), :content_type => 'text/json' }
25
+ end
26
+ FakeWeb.register_uri(:get, path, response)
27
+ end
28
+
29
+ def stub_post(path, filename)
30
+ response = { :body => fixture_file(filename), :content_type => 'text/json' }
31
+ FakeWeb.register_uri(:post, path, response)
32
+ end
33
+
34
+ def api_key_fixture
35
+ 'test_key'
36
+ end
37
+ def login_fixture
38
+ 'test_account'
39
+ end
40
+ def client_id_fixture
41
+ 'client_id'
42
+ end
43
+ def client_secret_fixture
44
+ 'client_secret'
45
+ end
46
+ def fixture_token
47
+ 'token'
48
+ end
49
+
50
+ class Test::Unit::TestCase
51
+ def teardown
52
+ FakeWeb.clean_registry
53
+ end
54
+ end
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ class TestHash < Test::Unit::TestCase
4
+ context "calling #to_params" do
5
+ setup do
6
+ @hash = { :one => 1, :two => 2 }
7
+ end
8
+ should "return a string of key=value&" do
9
+ assert_equal @hash.to_query, "one=1&two=2"
10
+ end
11
+ end
12
+
13
+ context "calling #stringify_keys!" do
14
+ setup do
15
+ @hash = { :one => 1, :two => 2 }
16
+ end
17
+ should "return a hash with string keys" do
18
+ assert_equal @hash.stringify_keys!.keys.map(&:class).uniq.size, 1
19
+ assert_equal @hash.stringify_keys!.keys.map(&:class).uniq.first, String
20
+ end
21
+ should "should alter the original hash" do
22
+ @hash.stringify_keys!
23
+ assert_equal @hash.keys.map(&:class).uniq.size, 1
24
+ assert_equal @hash.keys.map(&:class).uniq.first, String
25
+ end
26
+ end
27
+ context "calling #stringify_keys" do
28
+ setup do
29
+ @hash = { :one => 1, :two => 2 }
30
+ end
31
+ should "return a hash with string keys" do
32
+ assert_equal @hash.stringify_keys.keys.map(&:class).uniq.size, 1
33
+ assert_equal @hash.stringify_keys.keys.map(&:class).uniq.first, String
34
+ end
35
+ should "should return a new hash" do
36
+ @hash.stringify_keys
37
+ assert_equal @hash.keys.map(&:class).uniq.size, 1
38
+ assert_equal @hash.keys.map(&:class).uniq.first, Symbol
39
+ end
40
+ end
41
+ context "calling #symbolize_keys!" do
42
+ setup do
43
+ @hash = { 'one' => 1, 'two' => 2 }
44
+ end
45
+ should "return a hash with string keys" do
46
+ assert_equal @hash.symbolize_keys!.keys.map(&:class).uniq.size, 1
47
+ assert_equal @hash.symbolize_keys!.keys.map(&:class).uniq.first, Symbol
48
+ end
49
+ should "should alter the original hash" do
50
+ @hash.symbolize_keys!
51
+ assert_equal @hash.keys.map(&:class).uniq.size, 1
52
+ assert_equal @hash.keys.map(&:class).uniq.first, Symbol
53
+ end
54
+ end
55
+ context "calling #symbolize_keys" do
56
+ setup do
57
+ @hash = { 'one' => 1, 'two' => 2 }
58
+ end
59
+ should "return a hash with string keys" do
60
+ assert_equal @hash.symbolize_keys.keys.map(&:class).uniq.size, 1
61
+ assert_equal @hash.symbolize_keys.keys.map(&:class).uniq.first, Symbol
62
+ end
63
+ should "should alter the original hash" do
64
+ @hash.symbolize_keys
65
+ assert_equal @hash.keys.map(&:class).uniq.size, 1
66
+ assert_equal @hash.keys.map(&:class).uniq.first, String
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+
3
+ class TestString < Test::Unit::TestCase
4
+ context "Ruby String" do
5
+ setup do
6
+ @string = "string"
7
+ end
8
+ context "calling to_params" do
9
+ should "return a string of key=value&" do
10
+ assert_equal @string.to_a, [ @string ]
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+
3
+ class TestAccessToken < Test::Unit::TestCase
4
+ context "with an id and secret" do
5
+ setup do
6
+ oauth = Bitlyr::Strategy::OAuth.new('id', 'secret')
7
+ @strategy = oauth.get_access_token_from_token('token')
8
+ end
9
+ should 'get access token with access token' do
10
+ access_token = @strategy.send(:access_token)
11
+ assert_kind_of OAuth2::AccessToken, access_token
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class TestApiKey < Test::Unit::TestCase
4
+ context "creating a new ApiKey Strategy" do
5
+ should "initialize with login and api key" do
6
+ assert_nothing_raised do
7
+ Bitlyr::Strategy::ApiKey.new(login_fixture, api_key_fixture)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+
3
+ class TestBase < Test::Unit::TestCase
4
+ context "a request" do
5
+ context "when successful" do
6
+ setup do
7
+ Bitlyr::Response.stubs(:new => stub('response', :success? => true, :body => {}))
8
+ @strategy = Bitlyr::Strategy::Base.new
9
+ end
10
+ should "should proxy the request to run_request" do
11
+ @strategy.expects(:run_request).once
12
+ @strategy.request
13
+ end
14
+ should "should pass the args to run request" do
15
+ @strategy.stubs(:run_request).with("these", "args")
16
+ @strategy.request("these", "args")
17
+ end
18
+ should "return the body of the bitlyr response" do
19
+ @strategy.stubs(:run_request => true)
20
+ assert_equal @strategy.request, {}
21
+ end
22
+ end
23
+ context "when unsuccessful" do
24
+ should "raise a Bitlyr Error" do
25
+ response = stub('response', :success? => false, :reason => "reason", :status => 500)
26
+ Bitlyr::Response.stubs(:new => response)
27
+ strategy = Bitlyr::Strategy::Base.new
28
+ strategy.stubs(:run_request => true)
29
+ assert_raise BitlyrError do
30
+ strategy.request
31
+ end
32
+ end
33
+ end
34
+ end
35
+ context "run_request" do
36
+ should "raise a Runtime Error" do
37
+ strategy = Bitlyr::Strategy::Base.new
38
+ assert_raise RuntimeError do
39
+ strategy.send :run_request
40
+ end
41
+ end
42
+ end
43
+ context "validating a login and apiKey" do
44
+ context "with valid login and apiKey" do
45
+ setup do
46
+ Bitlyr::Response.stubs(:new => stub('response', :success? => true, :body => {'valid' => 1}))
47
+ @strategy = Bitlyr::Strategy::Base.new
48
+ @strategy.expects(:run_request).once
49
+ end
50
+ should "return true when calling validate" do
51
+ assert @strategy.validate(login_fixture, api_key_fixture)
52
+ end
53
+ should "return true when calling valid?" do
54
+ assert @strategy.valid?(login_fixture, api_key_fixture)
55
+ end
56
+ end
57
+ context "with an invalid login and apiKey" do
58
+ setup do
59
+ Bitlyr::Response.stubs(:new => stub('response', :success? => true, :body => {'valid' => 0}))
60
+ @strategy = Bitlyr::Strategy::Base.new
61
+ @strategy.expects(:run_request).once
62
+ end
63
+ should "return false when calling validate" do
64
+ assert !@strategy.validate("bogus", "info")
65
+ end
66
+ should "return false when valling valid?" do
67
+ assert !@strategy.valid?("bogus", "info")
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class TestOAuthStrategy < Test::Unit::TestCase
4
+ context "with an id and secret" do
5
+ setup do
6
+ @client_id = 'id'
7
+ @client_secret = 'secret'
8
+ @strategy = Bitlyr::Strategy::OAuth.new(@client_id, @client_secret)
9
+ end
10
+ should 'create the oauth client' do
11
+ assert_kind_of OAuth2::Client, @strategy.send(:client)
12
+ end
13
+ should 'save the strategy details on the client' do
14
+ assert_equal @client_id, @strategy.send(:client).id
15
+ assert_equal @client_secret, @strategy.send(:client).secret
16
+ end
17
+ should 'get the oauth authorize url' do
18
+ assert_match %r{https://api-ssl\.bit\.ly/oauth/authorize\?.*client_id=#{@client_id}.*}, @strategy.authorize_url('http://test.local')
19
+ end
20
+ should 'get access token with access token' do
21
+ access_token = @strategy.get_access_token_from_token('hello')
22
+ assert_kind_of Bitlyr::Strategy::AccessToken, access_token
23
+ assert_equal @strategy.send(:client), access_token.client
24
+ end
25
+ should 'set access token with access token' do
26
+ @strategy.set_access_token_from_token!('hello')
27
+ access_token = @strategy.send(:access_token)
28
+ assert_kind_of Bitlyr::Strategy::AccessToken, access_token
29
+ assert_equal @strategy.send(:client), access_token.client
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ class TestBitlyr < Test::Unit::TestCase
4
+ context "bitlyr module" do
5
+ context "with a login and api key" do
6
+ should "create a new bitlyr client" do
7
+ bitlyr = Bitlyr.new(:login => "login", :api_key => "api key")
8
+ assert bitlyr.is_a?(Bitlyr::Client)
9
+ end
10
+ should "create have a ApiKey Strategy" do
11
+ strategy = Bitlyr.new(:login => "login", :api_key => "api key").send(:strategy)
12
+ assert strategy.is_a?(Bitlyr::Strategy::ApiKey)
13
+ end
14
+ end
15
+ context "with a client id and client secret" do
16
+ should "create a new bitlyr client" do
17
+ bitlyr = Bitlyr.new(:client_id => "client id", :client_secret => "client secret")
18
+ assert bitlyr.is_a?(Bitlyr::Client)
19
+ end
20
+ should "create have a OAuth Strategy" do
21
+ strategy = Bitlyr.new(:client_id => "client id", :client_secret => "client secret").send(:strategy)
22
+ assert strategy.is_a?(Bitlyr::Strategy::OAuth)
23
+ end
24
+ end
25
+ context "with a client id, client secret and token" do
26
+ should "create a new bitlyr client" do
27
+ bitlyr = Bitlyr.new(:client_id => "client id", :client_secret => "client secret", :token => "token")
28
+ assert bitlyr.is_a?(Bitlyr::Client)
29
+ end
30
+ should "create have a OAuth Strategy" do
31
+ strategy = Bitlyr.new(:client_id => "client id", :client_secret => "client secret", :token => "token").send(:strategy)
32
+ assert strategy.is_a?(Bitlyr::Strategy::OAuth)
33
+ end
34
+ end
35
+ context "with bad information" do
36
+ should "raise an error" do
37
+ assert_raise RuntimeError do
38
+ Bitlyr.new({})
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ class TestClient < Test::Unit::TestCase
4
+ context "creating a new client" do
5
+ should "initialize with ApiKey strategy" do
6
+ assert_nothing_raised do
7
+ Bitlyr::Client.new Bitlyr::Strategy::ApiKey.new(login_fixture, api_key_fixture)
8
+ end
9
+ end
10
+ should "initialize with OAuth strategy" do
11
+ assert_nothing_raised do
12
+ Bitlyr::Client.new Bitlyr::Strategy::OAuth.new(client_id_fixture, client_secret_fixture)
13
+ end
14
+ end
15
+ should "raise an exception when bad arguments are used" do
16
+ assert_raise ArgumentError do
17
+ Bitlyr::Client.new("Something Else")
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class TestCountry < Test::Unit::TestCase
4
+ context "a country" do
5
+ setup do
6
+ @country = Bitlyr::Country.new
7
+ end
8
+
9
+ [:clicks, :country].each do |method|
10
+ should "respond to #{method}" do
11
+ assert_respond_to @country, method
12
+ end
13
+
14
+ should "set #{method} when initializing" do
15
+ country = Bitlyr::Country.new(method.to_s => 'test')
16
+ assert_equal 'test', country.send(method)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class TestDay < Test::Unit::TestCase
4
+ context "a day" do
5
+ setup do
6
+ @day = Bitlyr::Day.new
7
+ end
8
+ [:clicks, :day_start].each do |method|
9
+ should "respond to #{method}" do
10
+ assert_respond_to @day, method
11
+ end
12
+ end
13
+ should "set clicks when initializing" do
14
+ day = Bitlyr::Day.new('clicks' => 12)
15
+ assert_equal 12, day.clicks
16
+ end
17
+ should "set day to a time object when initialising" do
18
+ day = Bitlyr::Day.new('day_start' => 1290488400)
19
+ assert_equal Time.parse('2010/11/23'), day.day_start
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class TestBitlyrError < Test::Unit::TestCase
4
+ should "create a new bitlyr client" do
5
+ res = mock(:status => 'code', :reason => 'message')
6
+ error = BitlyrError.new(res)
7
+ assert_equal "message - 'code'", error.message
8
+ assert_equal "message - 'code'", error.msg
9
+ assert_equal "code", error.code
10
+ end
11
+ end
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ class TestMissingUrl < Test::Unit::TestCase
4
+ context "a url" do
5
+ setup do
6
+ @url = Bitlyr::MissingUrl.new
7
+ end
8
+ [:short_url, :user_hash, :long_url, :error].each do |method|
9
+ should "respond to #{method}" do
10
+ assert_respond_to @url, method
11
+ end
12
+ end
13
+ end
14
+ context "#new" do
15
+ setup do
16
+ @url = Bitlyr::MissingUrl.new({ 'short_url' => 'short url',
17
+ 'hash' => 'hash',
18
+ 'long_url' => 'long url',
19
+ 'error' => 'error' })
20
+ end
21
+ should "return the short url" do
22
+ assert_equal 'short url', @url.short_url
23
+ end
24
+ should "return the user hash" do
25
+ assert_equal 'hash', @url.user_hash
26
+ end
27
+ should "return the long url" do
28
+ assert_equal 'long url', @url.long_url
29
+ end
30
+ should "return the error" do
31
+ assert_equal 'error', @url.error
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class TestRealtimeLink < Test::Unit::TestCase
4
+ context "a realtime link" do
5
+ setup do
6
+ @realtime_link = Bitlyr::RealtimeLink.new
7
+ end
8
+ [:clicks, :user_hash].each do |method|
9
+ should "respond to #{method}" do
10
+ assert_respond_to @realtime_link, method
11
+ end
12
+ end
13
+ should "set clicks when initializing" do
14
+ realtime_link = Bitlyr::RealtimeLink.new('clicks' => 12)
15
+ assert_equal 12, realtime_link.clicks
16
+ end
17
+ should "set user_hash when initializing" do
18
+ realtime_link = Bitlyr::RealtimeLink.new('user_hash' => 'sdfidn')
19
+ assert_equal 'sdfidn', realtime_link.user_hash
20
+ end
21
+ should 'create a url when supplied with a client' do
22
+ realtime_link = Bitlyr::RealtimeLink.new('user_hash' => 'sdfidn', 'clicks' => 12)
23
+ client = Bitlyr::Client.new(Bitlyr::Strategy::ApiKey.new(login_fixture, api_key_fixture))
24
+ url = realtime_link.create_url(client)
25
+ assert_kind_of Bitlyr::Url, url
26
+ assert_equal 12, url.user_clicks
27
+ assert_equal 'sdfidn', url.user_hash
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class TestReferrer < Test::Unit::TestCase
4
+ context "a referrer" do
5
+ setup do
6
+ @referrer = Bitlyr::Referrer.new({})
7
+ end
8
+
9
+ [:clicks, :referrer, :referrer_app, :url].each do |method|
10
+ should "respond to #{method}" do
11
+ assert_respond_to @referrer, method
12
+ end
13
+
14
+ should "set #{method} when initializing" do
15
+ referrer = Bitlyr::Referrer.new(method.to_s => 'test')
16
+ assert_equal 'test', referrer.send(method)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,86 @@
1
+ require 'test_helper'
2
+
3
+ class TestResponse < Test::Unit::TestCase
4
+ context "with a unsupported response" do
5
+ should "raise an error" do
6
+ assert_raises RuntimeError, "Unsupported Response type: Object" do
7
+ Bitlyr::Response.new(Object.new).success?
8
+ end
9
+ end
10
+ end
11
+ context "with an oauth response" do
12
+ setup do
13
+ OAuth2::Response.stubs(:=== => true)
14
+ end
15
+ should "return true" do
16
+ response = Bitlyr::Response.new(stub('oauth', :parsed => {'status_code' => 200}))
17
+ assert_equal true, response.success?
18
+ end
19
+ should "return false" do
20
+ response = Bitlyr::Response.new(stub('oauth', :parsed => {'status_code' => 'not 200'}))
21
+ assert_equal false, response.success?
22
+ end
23
+ should "return the status code" do
24
+ response = Bitlyr::Response.new(stub('oauth', :parsed => {'status_code' => 'some status code'}))
25
+ assert_equal 'some status code', response.status,
26
+ end
27
+ should "return the response body" do
28
+ response = Bitlyr::Response.new(stub('oauth', :parsed => { 'data' => {} } ))
29
+ assert_equal Hash.new, response.body
30
+ end
31
+ should "return the response body" do
32
+ response = Bitlyr::Response.new(stub('oauth', :parsed => {'data' => nil}))
33
+ assert_equal nil, response.body
34
+ end
35
+ should "return the status text reason" do
36
+ response = Bitlyr::Response.new(stub('oauth', :parsed => {'status_txt' => "OK"}))
37
+ assert_equal "OK", response.reason
38
+ end
39
+ should "return the status text reason" do
40
+ response = Bitlyr::Response.new(stub('oauth', :parsed => {'status_txt' => "RATE_LIMIT_EXCEEDED"}))
41
+ assert_equal "Rate Limit Exceeded", response.reason
42
+ end
43
+ should "return the status text reason" do
44
+ response = Bitlyr::Response.new(stub('oauth', :parsed => {'status_txt' => "INVALID_ANYTHING"}))
45
+ assert_equal "Invalid Anything", response.reason
46
+ end
47
+ end
48
+
49
+ context "with a HTTParty response" do
50
+ setup do
51
+ HTTParty::Response.stubs(:=== => true)
52
+ end
53
+ should "return true" do
54
+ response = Bitlyr::Response.new(stub(:parsed_response => { 'status_code' => 200 } ))
55
+ assert_equal true, response.success?
56
+ end
57
+ should "return false" do
58
+ response = Bitlyr::Response.new(stub(:parsed_response => { 'status_code' => 'not 200' }))
59
+ assert_equal false, response.success?
60
+ end
61
+ should "return the status code" do
62
+ response = Bitlyr::Response.new(stub(:parsed_response => { 'status_code' => 'some status code' }))
63
+ assert_equal 'some status code', response.status,
64
+ end
65
+ should "return the response body" do
66
+ response = Bitlyr::Response.new(stub(:parsed_response => { 'data' => {} } ))
67
+ assert_equal Hash.new, response.body
68
+ end
69
+ should "return the response body" do
70
+ response = Bitlyr::Response.new(stub(:parsed_response => { 'data' => nil }))
71
+ assert_equal nil, response.body
72
+ end
73
+ should "return the status text reason" do
74
+ response = Bitlyr::Response.new(stub(:parsed_response => { 'status_txt' => "OK" }))
75
+ assert_equal "OK", response.reason
76
+ end
77
+ should "return the status text reason" do
78
+ response = Bitlyr::Response.new(stub(:parsed_response => { 'status_txt' => "RATE_LIMIT_EXCEEDED" }))
79
+ assert_equal "Rate Limit Exceeded", response.reason
80
+ end
81
+ should "return the status text reason" do
82
+ response = Bitlyr::Response.new(stub(:parsed_response => { 'status_txt' => "INVALID_ANYTHING" }))
83
+ assert_equal "Invalid Anything", response.reason
84
+ end
85
+ end
86
+ end