bitly-oauth 0.1.0

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.
Files changed (88) hide show
  1. data/History.txt +154 -0
  2. data/LICENSE +20 -0
  3. data/README.md +87 -0
  4. data/README.rdoc +35 -0
  5. data/Rakefile +42 -0
  6. data/VERSION +1 -0
  7. data/lib/bitly_oauth.rb +28 -0
  8. data/lib/bitly_oauth/access_token.rb +37 -0
  9. data/lib/bitly_oauth/client.rb +149 -0
  10. data/lib/bitly_oauth/country.rb +10 -0
  11. data/lib/bitly_oauth/day.rb +12 -0
  12. data/lib/bitly_oauth/error.rb +15 -0
  13. data/lib/bitly_oauth/lib/core_ext/hash.rb +27 -0
  14. data/lib/bitly_oauth/lib/core_ext/string.rb +5 -0
  15. data/lib/bitly_oauth/missing_url.rb +12 -0
  16. data/lib/bitly_oauth/realtime_link.rb +16 -0
  17. data/lib/bitly_oauth/referrer.rb +12 -0
  18. data/lib/bitly_oauth/referring_domain.rb +11 -0
  19. data/lib/bitly_oauth/response.rb +39 -0
  20. data/lib/bitly_oauth/url.rb +118 -0
  21. data/lib/bitly_oauth/user.rb +104 -0
  22. data/test/fixtures/9uX1TE.json +1 -0
  23. data/test/fixtures/9uX1TEclicks.json +1 -0
  24. data/test/fixtures/9uX1TEclicks2.json +1 -0
  25. data/test/fixtures/9uX1TEinfo.json +1 -0
  26. data/test/fixtures/9uX1TEinfo2.json +1 -0
  27. data/test/fixtures/auth_fail.json +1 -0
  28. data/test/fixtures/auth_success.json +1 -0
  29. data/test/fixtures/betaworks.json +1 -0
  30. data/test/fixtures/betaworks2.json +1 -0
  31. data/test/fixtures/betaworks_jmp.json +1 -0
  32. data/test/fixtures/betaworks_other_user.json +1 -0
  33. data/test/fixtures/bitly9uX1TE.json +1 -0
  34. data/test/fixtures/bitly_pro_domain.json +1 -0
  35. data/test/fixtures/clicks_by_day.json +1 -0
  36. data/test/fixtures/clicks_by_day1.json +1 -0
  37. data/test/fixtures/clicks_by_day2.json +1 -0
  38. data/test/fixtures/clicks_by_minute1_url.json +1 -0
  39. data/test/fixtures/clicks_by_minute2_url.json +1 -0
  40. data/test/fixtures/clicks_by_minute_hash.json +1 -0
  41. data/test/fixtures/clicks_by_minute_hashes.json +1 -0
  42. data/test/fixtures/country_hash.json +1 -0
  43. data/test/fixtures/country_hash2.json +1 -0
  44. data/test/fixtures/country_url.json +1 -0
  45. data/test/fixtures/failure.json +1 -0
  46. data/test/fixtures/invalid_bitly_pro_domain.json +1 -0
  47. data/test/fixtures/invalid_credentials.json +1 -0
  48. data/test/fixtures/invalid_domain.json +1 -0
  49. data/test/fixtures/invalid_user.json +1 -0
  50. data/test/fixtures/invalid_x_api_key.json +1 -0
  51. data/test/fixtures/lookup_multiple_url.json +1 -0
  52. data/test/fixtures/lookup_not_real_url.json +1 -0
  53. data/test/fixtures/lookup_single_url.json +1 -0
  54. data/test/fixtures/missing_hash.json +1 -0
  55. data/test/fixtures/multiple_info.json +1 -0
  56. data/test/fixtures/multiple_url_click.json +1 -0
  57. data/test/fixtures/multiple_urls.json +1 -0
  58. data/test/fixtures/not_bitly_pro_domain.json +1 -0
  59. data/test/fixtures/not_found_info.json +1 -0
  60. data/test/fixtures/referrer_hash.json +1 -0
  61. data/test/fixtures/referrer_hash2.json +1 -0
  62. data/test/fixtures/referrer_url.json +1 -0
  63. data/test/fixtures/success.json +1 -0
  64. data/test/fixtures/url_info.json +1 -0
  65. data/test/fixtures/user_clicks.json +32 -0
  66. data/test/fixtures/user_countries.json +60 -0
  67. data/test/fixtures/user_realtime_links.json +15 -0
  68. data/test/fixtures/user_referrers.json +1 -0
  69. data/test/fixtures/valid_user.json +1 -0
  70. data/test/integration/strategy/old_test_api_key.rb +20 -0
  71. data/test/integration/test_client.rb +709 -0
  72. data/test/integration/test_user.rb +97 -0
  73. data/test/test_helper.rb +54 -0
  74. data/test/unit/core_ext/test_hash.rb +69 -0
  75. data/test/unit/core_ext/test_string.rb +14 -0
  76. data/test/unit/test_bitly_oauth.rb +19 -0
  77. data/test/unit/test_client.rb +87 -0
  78. data/test/unit/test_country.rb +19 -0
  79. data/test/unit/test_day.rb +22 -0
  80. data/test/unit/test_error.rb +11 -0
  81. data/test/unit/test_missing.rb +34 -0
  82. data/test/unit/test_realtime_link.rb +30 -0
  83. data/test/unit/test_referrer.rb +19 -0
  84. data/test/unit/test_referring_domain.rb +21 -0
  85. data/test/unit/test_response.rb +86 -0
  86. data/test/unit/test_url.rb +156 -0
  87. data/test/unit/test_user.rb +17 -0
  88. metadata +215 -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
+ client = BitlyOAuth::Client.new('client_id', 'client_secret')
7
+ access_token = client.get_access_token_from_token('token')
8
+ @user = BitlyOAuth::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 BitlyOAuth::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 BitlyOAuth::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 BitlyOAuth::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 BitlyOAuth::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 'bitly_oauth'
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,19 @@
1
+ require 'test_helper'
2
+
3
+ class TestBitlyOAuth < Test::Unit::TestCase
4
+ context "bitly oauth module" do
5
+ context "with a client id and client secret" do
6
+ should "create a new client" do
7
+ client = BitlyOAuth.new("client_id", "client_secret")
8
+ assert client.is_a?(BitlyOAuth::Client)
9
+ end
10
+ end
11
+ context "with bad information" do
12
+ should "raise an error" do
13
+ assert_raise ArgumentError do
14
+ BitlyOAuth.new
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,87 @@
1
+ require 'test_helper'
2
+
3
+ class TestClient < Test::Unit::TestCase
4
+ context "creating a new client" do
5
+ should "initialize with clinet id and secret" do
6
+ assert_nothing_raised do
7
+ BitlyOAuth::Client.new('client_id', 'client_secret')
8
+ end
9
+ end
10
+ should "raise an exception when bad arguments are used" do
11
+ assert_raise ArgumentError do
12
+ BitlyOAuth::Client.new("Something Else")
13
+ end
14
+ end
15
+ end
16
+ context "with a new client" do
17
+ setup do
18
+ @client = BitlyOAuth.new('client_id', 'client_secret')
19
+ end
20
+ should 'get the oauth authorize url' do
21
+ redirect_url = 'http://localhost.local/'
22
+ authorize_url = @client.authorize_url(redirect_url)
23
+ assert_match %r{https://bitly.com/oauth/authorize\?.*client_id=client_id.*}, authorize_url
24
+ assert_match %r{https://bitly.com/oauth/authorize\?.*redirect_uri=#{CGI.escape(redirect_url)}.*}, authorize_url
25
+ end
26
+ context "get an access token" do
27
+ setup do
28
+ ::OAuth2::Client.stubs(:new => stub(:auth_code => stub(:get_token => true)))
29
+ end
30
+ should 'return an access token from a code' do
31
+ response = @client.get_access_token_from_code('code', 'redirect_url')
32
+ assert response.is_a?(BitlyOAuth::AccessToken)
33
+ end
34
+ should 'set an access token from a code' do
35
+ response = @client.set_access_token_from_code('code', 'redirect_url')
36
+ assert response.is_a?(BitlyOAuth::AccessToken)
37
+ assert (@client.send(:access_token)).is_a?(BitlyOAuth::AccessToken)
38
+ end
39
+ should 'return an access token from a token' do
40
+ response = @client.get_access_token_from_token('token')
41
+ assert response.is_a?(BitlyOAuth::AccessToken)
42
+ end
43
+ should 'set an access token from a token' do
44
+ response = @client.set_access_token_from_token('token')
45
+ assert response.is_a?(BitlyOAuth::AccessToken)
46
+ assert (@client.send(:access_token)).is_a?(BitlyOAuth::AccessToken)
47
+ end
48
+ end
49
+ context "requests to the bitly api" do
50
+ setup do
51
+ token = stub(:get => { 'bitly_pro_domain' => true },
52
+ :token => 'token' )
53
+ BitlyOAuth::AccessToken.stubs(:new => token)
54
+ @client.set_access_token_from_token('token')
55
+ end
56
+ context "bitly_pro_domain" do
57
+ should 'return true when it is a bitly pro domain' do
58
+ assert @client.bitly_pro_domain('http://pro.domain/')
59
+ end
60
+ end
61
+ context "bitly_pro_domain" do
62
+ should 'return true when it is a bitly pro domain' do
63
+ assert @client.pro?('http://pro.domain/')
64
+ end
65
+ end
66
+ end
67
+ end
68
+ context "#referring_domains" do
69
+ setup do
70
+ token = stub(:get => { "referring_domains" => [ {'domain' => 'direct', 'clicks' => 700} ] } )
71
+ BitlyOAuth::AccessToken.stubs(:new => token)
72
+ @client = BitlyOAuth::Client.new('id', 'secret')
73
+ @client.set_access_token_from_token('token')
74
+ end
75
+ should "return an array" do
76
+ assert @client.referring_domains('http://bit.ly/somelink/').is_a?(Array)
77
+ assert_equal 1, @client.referring_domains('http://bit.ly/somelink/').map(&:class).uniq.size
78
+ assert @client.referring_domains('http://bit.ly/somelink/').first.is_a?(BitlyOAuth::ReferringDomain)
79
+ end
80
+ should "all be the same class" do
81
+ assert_equal 1, @client.referring_domains('http://bit.ly/somelink/').map(&:class).uniq.size
82
+ end
83
+ should "all be referring domains" do
84
+ assert @client.referring_domains('http://bit.ly/somelink/').first.is_a?(BitlyOAuth::ReferringDomain)
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class TestCountry < Test::Unit::TestCase
4
+ context "a country" do
5
+ setup do
6
+ @country = BitlyOAuth::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
+ should "set #{method} when initializing" do
14
+ country = BitlyOAuth::Country.new(method.to_s => 'test')
15
+ assert_equal 'test', country.send(method)
16
+ end
17
+ end
18
+ end
19
+ 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 = BitlyOAuth::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 = BitlyOAuth::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 = BitlyOAuth::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 TestError < Test::Unit::TestCase
4
+ should "create a new bitly-oauth client" do
5
+ res = mock(:status => 'code', :reason => 'message')
6
+ error = BitlyOAuth::Error.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 = BitlyOAuth::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 = BitlyOAuth::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 = BitlyOAuth::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 = BitlyOAuth::RealtimeLink.new('clicks' => 12)
15
+ assert_equal 12, realtime_link.clicks
16
+ end
17
+ should "set user_hash when initializing" do
18
+ realtime_link = BitlyOAuth::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 = BitlyOAuth::RealtimeLink.new('user_hash' => 'sdfidn', 'clicks' => 12)
23
+ client = BitlyOAuth::Client.new('client_id', 'client_secret')
24
+ url = realtime_link.create_url(client)
25
+ assert_kind_of BitlyOAuth::Url, url
26
+ assert_equal 12, url.user_clicks
27
+ assert_equal 'sdfidn', url.user_hash
28
+ end
29
+ end
30
+ end