bitly 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,17 @@
1
+ === 0.3 / 2009-07-09
2
+
3
+ * 1 major enhancement
4
+
5
+ * a full set of tests, properly mocked
6
+
7
+ * 1 minor enhancement
8
+
9
+ * calling bitly.shorten(:history => 1) will add the url to the api user's history.
10
+
11
+ * 1 bug fix
12
+
13
+ * you can no longer call shorten with a keyword, this was unsupported in the API and consequently removed
14
+
1
15
  === 0.2 / 2009-06-23
2
16
 
3
17
  * 1 enhancement
data/README.txt CHANGED
@@ -6,6 +6,10 @@ A Ruby API for bit.ly
6
6
 
7
7
  http://code.google.com/p/bitly-api/wiki/ApiDocumentation
8
8
 
9
+ == INSTALLATION:
10
+
11
+ gem install bitly
12
+
9
13
  == USAGE:
10
14
 
11
15
  Create a Bitly client using your username and api key as follows:
@@ -15,7 +19,7 @@ bitly = Bitly.new(username, api_key)
15
19
  You can then use that client to shorten or expand urls or return more information or statistics as so:
16
20
 
17
21
  bitly.shorten('http://www.google.com')
18
- bitly.shorten('http://www.google.com', 'keyword') # returns a shortened url using the given keyword
22
+ bitly.shorten('http://www.google.com', :history => 1) # adds the url to the api user's history
19
23
  bitly.expand('wQaT')
20
24
  bitly.info('http://bit.ly/wQaT')
21
25
  bitly.stats('http://bit.ly/wQaT')
@@ -33,8 +37,6 @@ u.hash #=> "2V6CFi"
33
37
  u.info #=> a ruby hash of the JSON returned from the API
34
38
  u.stats #=> a ruby hash of the JSON returned from the API
35
39
 
36
- You should be able to specify a keyword when shortening (though at last test, this didn't seem to be working http://code.google.com/p/bitly-api/issues/detail?id=5). To do so, just add a keyword parameter:
37
-
38
40
  bitly.shorten('http://www.google.com', 'keyword')
39
41
 
40
42
  == LICENSE:
@@ -2,22 +2,22 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{bitly}
5
- s.version = "0.2"
5
+ s.version = "0.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Phil Nash"]
9
- s.date = %q{2009-06-23}
9
+ s.date = %q{2009-07-09}
10
10
  s.description = %q{Use the bit.ly API to shorten or expand URLs}
11
11
  s.email = %q{philnash@gmail.com}
12
12
  s.extra_rdoc_files = ["lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/version.rb", "lib/bitly.rb", "README.txt"]
13
- s.files = ["bitly.gemspec", "History.txt", "lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/version.rb", "lib/bitly.rb", "Manifest", "Rakefile", "README.txt", "test/test_bitly.rb"]
13
+ s.files = ["bitly.gemspec", "History.txt", "lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/version.rb", "lib/bitly.rb", "Manifest", "Rakefile", "README.txt", "test/test_bitly.rb", "test/bitly/test_client.rb", "test/bitly/test_url.rb", "test/bitly/test_utils.rb", "test/test_helper.rb"]
14
14
  s.homepage = %q{http://github.com/philnash/bitly}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Bitly", "--main", "README.txt"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{bitly}
18
18
  s.rubygems_version = %q{1.3.4}
19
19
  s.summary = %q{Use the bit.ly API to shorten or expand URLs}
20
- s.test_files = ["test/test_bitly.rb"]
20
+ s.test_files = ["test/bitly/test_client.rb", "test/bitly/test_url.rb", "test/bitly/test_utils.rb", "test/test_bitly.rb", "test/test_helper.rb"]
21
21
 
22
22
  if s.respond_to? :specification_version then
23
23
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -18,15 +18,15 @@ module Bitly
18
18
  @login = login
19
19
  @api_key = api_key
20
20
  end
21
-
22
- def shorten(input, keyword=nil)
21
+
22
+ def shorten(input, opts={})
23
23
  if input.is_a? String
24
- request = create_url "shorten", :longUrl => input, :keyword => keyword
24
+ request = create_url("shorten", :longUrl => input, :history => (opts[:history] ? 1 : nil))
25
25
  result = get_result(request)
26
26
  result = {:long_url => input}.merge result[input]
27
27
  Bitly::Url.new(@login,@api_key,result)
28
28
  elsif input.is_a? Array
29
- request = create_url "shorten"
29
+ request = create_url("shorten", :history => (opts[:history] ? 1 : nil))
30
30
  request.query << "&" + input.map { |long_url| "longUrl=#{CGI.escape(long_url)}" }.join("&") unless input.nil?
31
31
  result = get_result(request)
32
32
  input.map do |long_url|
@@ -34,14 +34,14 @@ module Bitly
34
34
  long_url = Bitly::Url.new(@login,@api_key,new_url)
35
35
  end
36
36
  else
37
- raise ArgumentError
37
+ raise ArgumentError.new("Shorten requires either a url or an array of urls")
38
38
  end
39
39
  end
40
40
 
41
41
  def expand(input)
42
42
  if input.is_a? String
43
43
  if input.include? "bit.ly/"
44
- hash = input.gsub(/^.*bit.ly\//,'')
44
+ hash = create_hash_from_url(input)
45
45
  request = create_url "expand", :hash => hash
46
46
  result = get_result(request)
47
47
  result = { :short_url => input, :hash => hash }.merge result[hash]
@@ -59,14 +59,14 @@ module Bitly
59
59
  hsh = Bitly::Url.new(@login,@api_key,new_url)
60
60
  end
61
61
  else
62
- raise ArgumentError
62
+ raise ArgumentError('Expand requires either a short url, a hash or an array of hashes')
63
63
  end
64
64
  end
65
65
 
66
66
  def info(input)
67
67
  if input.is_a? String
68
68
  if input.include? "bit.ly/"
69
- hash = input.gsub(/^.*bit.ly\//,'')
69
+ hash = create_hash_from_url(input)
70
70
  request = create_url 'info', :hash => hash
71
71
  result = get_result(request)
72
72
  result = { :short_url => "http://bit.ly/#{hash}", :hash => hash }.merge result[hash]
@@ -83,13 +83,15 @@ module Bitly
83
83
  new_url = {:hash => hsh, :short_url => "http://bit.ly/#{hsh}"}.merge result[hsh]
84
84
  hsh = Bitly::Url.new(@login,@api_key,:info => new_url)
85
85
  end
86
+ else
87
+ raise ArgumentError.new('Info requires either a short url, a hash or an array of hashes')
86
88
  end
87
89
  end
88
90
 
89
91
  def stats(input)
90
92
  if input.is_a? String
91
93
  if input.include? "bit.ly/"
92
- hash = input.gsub(/^.*bit.ly\//,'')
94
+ hash = create_hash_from_url(input)
93
95
  request = create_url 'stats', :hash => hash
94
96
  result = get_result(request)
95
97
  result = { :short_url => "http://bit.ly/#{hash}", :hash => hash }.merge result
@@ -100,7 +102,7 @@ module Bitly
100
102
  end
101
103
  Bitly::Url.new(@login,@api_key,:stats => result)
102
104
  else
103
- raise ArgumentError
105
+ raise ArgumentError.new("Stats requires either a short url or a hash")
104
106
  end
105
107
  end
106
108
 
@@ -111,9 +113,9 @@ end
111
113
  class BitlyError < StandardError
112
114
  attr_reader :code
113
115
  alias :msg :message
114
- def initialize(msg, code, req)
116
+ def initialize(msg, code)
115
117
  @code = code
116
- super("'#{req}' - #{msg}")
118
+ super("#{msg} - '#{code}'")
117
119
  end
118
120
  end
119
121
 
@@ -3,19 +3,55 @@ module Bitly
3
3
  class Url
4
4
  include Bitly::Utils
5
5
 
6
- VARIABLES = ['long_url', 'short_url', 'hash', 'user_hash', 'short_keyword_url']
6
+ attr_accessor :long_url, :short_url, :hash, :user_hash
7
+ attr_reader :info, :stats
8
+ VARIABLES = ['long_url', 'short_url', 'hash', 'user_hash']
7
9
 
8
10
  def initialize(login,api_key,obj=nil)
9
11
  unless obj.nil?
10
-
11
- raise BitlyError.new(obj['errorMessage'],obj['errorCode'],'expand') if obj['statusCode'] == "ERROR"
12
-
12
+ raise BitlyError.new(result['errorMessage'],result['errorCode']) if obj['statusCode'] == "ERROR"
13
13
  instance_variablise(obj, VARIABLES)
14
14
  @info = obj[:info] if obj[:info]
15
15
  @stats = obj[:stats] if obj[:stats]
16
16
  end
17
17
  @login = login
18
18
  @api_key = api_key
19
+ raise ArgumentError.new("Please provide a login and api_key") if @login.nil? || @api_key.nil?
20
+ end
21
+
22
+ def shorten(opts = {})
23
+ return @short_url if @short_url
24
+ unless @long_url.nil?
25
+ request = create_url("shorten", :longUrl => @long_url, :history => (opts[:history] ? 1 : nil))
26
+ result = get_result(request)[@long_url.gsub(/\/$/,'')]
27
+ if result['statusCode'] == "ERROR"
28
+ raise BitlyError.new(result['errorMessage'],result['errorCode'])
29
+ else
30
+ instance_variablise(result,VARIABLES)
31
+ return @short_url
32
+ end
33
+ else
34
+ raise ArgumentError.new("You need a long_url in order to shorten it")
35
+ end
36
+ end
37
+
38
+ def expand
39
+ return @long_url if @long_url
40
+ unless !(@short_url || @hash)
41
+ unless @hash
42
+ @hash = create_hash_from_url(@short_url)
43
+ end
44
+ request = create_url("expand", :hash => @hash)
45
+ result = get_result(request)[@hash]
46
+ if result['statusCode'] == "ERROR"
47
+ raise BitlyError.new(result['errorMessage'],result['errorCode'])
48
+ else
49
+ instance_variablise(result,VARIABLES)
50
+ return @long_url
51
+ end
52
+ else
53
+ raise ArgumentError.new("You need a short_url or a hash in order to expand it")
54
+ end
19
55
  end
20
56
 
21
57
  def info
@@ -26,14 +62,15 @@ module Bitly
26
62
  instance_variablise(result, VARIABLES)
27
63
  @info = result
28
64
  elsif @short_url
29
- hash = @short_url.gsub(/^.*bit.ly\//,'')
65
+ @hash = create_hash_from_url(@short_url)
30
66
  request = create_url "info", :hash => hash
31
67
  result = get_result(request)[hash]
32
68
  instance_variablise(result, VARIABLES)
33
69
  @info = result
34
70
  else
35
- nil
71
+ raise ArgumentError.new("You need a hash or short_url in order to get info")
36
72
  end
73
+ return @info
37
74
  else
38
75
  @info
39
76
  end
@@ -44,7 +81,10 @@ module Bitly
44
81
  if @hash
45
82
  request = create_url "stats", :hash => @hash
46
83
  elsif @short_url
47
- request = create_url "stats", :shortUrl => @short_url
84
+ @hash = create_hash_from_url(@short_url)
85
+ request = create_url "stats", :hash => @hash
86
+ else
87
+ raise ArgumentError.new("You need a hash or short_url in order to get stats")
48
88
  end
49
89
  @stats = get_result(request)
50
90
  else
@@ -2,7 +2,6 @@ require 'cgi'
2
2
 
3
3
  module Bitly
4
4
  module Utils
5
- private
6
5
  def underscore(camel_cased_word) # stolen from rails
7
6
  camel_cased_word.to_s.gsub(/::/, '/').
8
7
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
@@ -11,6 +10,10 @@ module Bitly
11
10
  downcase
12
11
  end
13
12
 
13
+ def create_hash_from_url(url)
14
+ url.gsub(/^.*bit.ly\//,'')
15
+ end
16
+
14
17
  def attr_define(k,v)
15
18
  instance_variable_set("@#{k}", v)
16
19
  meta = class << self; self; end
@@ -42,12 +45,12 @@ module Bitly
42
45
  begin
43
46
  result = Crack::JSON.parse(Net::HTTP.get(request))
44
47
  rescue
45
- result = {'statusCode' => 'JSON Parse Error(Bit.ly messed up)', 'errorCode' => 69}
48
+ result = {'errorMessage' => 'JSON Parse Error(Bit.ly messed up)', 'errorCode' => 69, 'statusCode' => 'ERROR'}
46
49
  end
47
50
  if result['statusCode'] == "OK"
48
51
  result = result['results']
49
52
  else
50
- raise BitlyError.new(result['errorMessage'],result['errorCode'],'expand')
53
+ raise BitlyError.new(result['errorMessage'],result['errorCode'])
51
54
  end
52
55
  end
53
56
 
@@ -1,3 +1,3 @@
1
1
  module Bitly
2
- VERSION = '0.2'
2
+ VERSION = '0.3'
3
3
  end
@@ -0,0 +1,187 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper.rb')
2
+
3
+ class TestClient < Test::Unit::TestCase
4
+ context "bitly module" do
5
+ should "create a new bitly client" do
6
+ b = Bitly.new(login, api_key)
7
+ assert_equal Bitly::Client, b.class
8
+ end
9
+ end
10
+ context "using the bitly client" do
11
+ setup do
12
+ @bitly = Bitly.new(login, api_key)
13
+ end
14
+
15
+ context "shortening" do
16
+ context "a single link" do
17
+ setup do
18
+ stub_get(/^http:\/\/api.bit.ly\/shorten\?.*longUrl=.*cnn.com.*$/,"cnn.json")
19
+ @url = @bitly.shorten('http://cnn.com')
20
+ end
21
+ should "return a Bitly::Url" do
22
+ assert_kind_of Bitly::Url, @url
23
+ end
24
+ should "return a short bitly url" do
25
+ assert_equal "http://bit.ly/15DlK", @url.short_url
26
+ end
27
+ should "save the long url" do
28
+ assert_equal "http://cnn.com", @url.long_url
29
+ end
30
+ end
31
+ context "multiple links" do
32
+ setup do
33
+ stub_get(/^http:\/\/api.bit.ly\/shorten\?.*longUrl=.*longUrl=.*$/,"cnn_and_google.json")
34
+ @urls = @bitly.shorten(['http://cnn.com', 'http://google.com'])
35
+ end
36
+ should "return an array of Bitly::Urls" do
37
+ assert_kind_of Array, @urls
38
+ assert_kind_of Bitly::Url, @urls[0]
39
+ end
40
+ should "shorten the urls in order" do
41
+ assert_equal "http://bit.ly/15DlK", @urls[0].short_url
42
+ assert_equal "http://bit.ly/11etr", @urls[1].short_url
43
+ end
44
+ should "save the long urls" do
45
+ assert_equal "http://cnn.com", @urls[0].long_url
46
+ assert_equal "http://google.com", @urls[1].long_url
47
+ end
48
+ end
49
+ context "no links" do
50
+ should "raise an ArgumentError" do
51
+ assert_raise ArgumentError do
52
+ @bitly.shorten
53
+ end
54
+ end
55
+ end
56
+ end
57
+ context "expanding" do
58
+ context "a hash" do
59
+ setup do
60
+ stub_get(/^http:\/\/api.bit.ly\/expand\?.*hash=31IqMl.*$/,"expand_cnn.json")
61
+ @url = @bitly.expand("31IqMl")
62
+ end
63
+ should "return a Bitly::Url" do
64
+ assert_kind_of Bitly::Url, @url
65
+ end
66
+ should "return the expanded url" do
67
+ assert_equal "http://cnn.com/", @url.long_url
68
+ end
69
+ should "save the hash" do
70
+ assert_equal "31IqMl", @url.hash
71
+ end
72
+ should "save the short url" do
73
+ assert_equal "http://bit.ly/31IqMl", @url.short_url
74
+ end
75
+ end
76
+ context "a short url" do
77
+ setup do
78
+ stub_get(/^http:\/\/api.bit.ly\/expand\?.*hash=31IqMl.*$/,"expand_cnn.json")
79
+ @url = @bitly.expand("http://bit.ly/31IqMl")
80
+ end
81
+ should "return a Bitly::Url" do
82
+ assert_kind_of Bitly::Url, @url
83
+ end
84
+ should "return the expanded url" do
85
+ assert_equal "http://cnn.com/", @url.long_url
86
+ end
87
+ should "save the hash" do
88
+ assert_equal "31IqMl", @url.hash
89
+ end
90
+ should "save the short url" do
91
+ assert_equal "http://bit.ly/31IqMl", @url.short_url
92
+ end
93
+ end
94
+ context "multiple hashes" do
95
+ setup do
96
+ stub_get(/^http:\/\/api.bit.ly\/expand\?.*hash=15DlK.*3j4ir4.*$/,"expand_cnn_and_google.json")
97
+ @urls = @bitly.expand(["15DlK","3j4ir4"])
98
+ end
99
+ should "return an array of Bitly::Urls" do
100
+ assert_kind_of Array, @urls
101
+ assert_kind_of Bitly::Url, @urls[0]
102
+ assert_kind_of Bitly::Url, @urls[1]
103
+ end
104
+ should "expand the hashes in order" do
105
+ assert_equal "http://cnn.com/", @urls[0].long_url
106
+ assert_equal "http://google.com/", @urls[1].long_url
107
+ end
108
+ should "save the hash to each url" do
109
+ assert_equal "15DlK", @urls[0].hash
110
+ assert_equal "3j4ir4", @urls[1].hash
111
+ end
112
+ end
113
+ end
114
+ context "to get info on" do
115
+ context "a single link" do
116
+ setup do
117
+ stub_get(/^http:\/\/api.bit.ly\/info\?.*hash=3j4ir4.*$/,"google_info.json")
118
+ @url = @bitly.info('http://bit.ly/3j4ir4')
119
+ end
120
+ should "return a Bitly::Url" do
121
+ assert_kind_of Bitly::Url, @url
122
+ end
123
+ should "return an info object with the url" do
124
+ assert_not_nil @url.info
125
+ end
126
+ end
127
+ context "a single hash" do
128
+ setup do
129
+ stub_get(/^http:\/\/api.bit.ly\/info\?.*hash=3j4ir4.*$/,"google_info.json")
130
+ @url = @bitly.info('3j4ir4')
131
+ end
132
+ should "return a Bitly::Url" do
133
+ assert_kind_of Bitly::Url, @url
134
+ end
135
+ should "return an info object with the url" do
136
+ assert_not_nil @url.info
137
+ end
138
+ end
139
+ context "a list of hashes" do
140
+ setup do
141
+ stub_get(/^http:\/\/api.bit.ly\/info\?.*hash=3j4ir4.*31IqMl.*$/,"google_and_cnn_info.json")
142
+ @urls = @bitly.info(['3j4ir4','31IqMl'])
143
+ end
144
+ should "return a Bitly::Url" do
145
+ assert_kind_of Array, @urls
146
+ end
147
+ should "return an info object with the url" do
148
+ assert_not_nil @urls[0].info
149
+ assert_not_nil @urls[1].info
150
+ end
151
+ end
152
+ end
153
+ context "to get stats on" do
154
+ context "a single link" do
155
+ setup do
156
+ stub_get(/^http:\/\/api.bit.ly\/stats\?.*hash=3j4ir4.*$/,"google_stats.json")
157
+ @url = @bitly.stats('http://bit.ly/3j4ir4')
158
+ end
159
+ should "return a Bitly::Url" do
160
+ assert_kind_of Bitly::Url, @url
161
+ end
162
+ should "return an stats object" do
163
+ assert_not_nil @url.stats
164
+ end
165
+ end
166
+ context "a single hash" do
167
+ setup do
168
+ stub_get(/^http:\/\/api.bit.ly\/stats\?.*hash=3j4ir4.*$/,"google_stats.json")
169
+ @url = @bitly.stats('3j4ir4')
170
+ end
171
+ should "return a Bitly::Url" do
172
+ assert_kind_of Bitly::Url, @url
173
+ end
174
+ should "return an stats object" do
175
+ assert_not_nil @url.stats
176
+ end
177
+ end
178
+ context "a list of hashes" do
179
+ should "return an argument error" do
180
+ assert_raise ArgumentError do
181
+ @bitly.stats(['3j4ir4','31IqMl'])
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,168 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper.rb')
2
+
3
+ class TestUrl < Test::Unit::TestCase
4
+ context "a new Bitly::Url" do
5
+ should "require a login and api_key" do
6
+ assert_raise ArgumentError do Bitly::Url.new end
7
+ assert_raise ArgumentError do Bitly::Url.new(login) end
8
+ assert_raise ArgumentError do Bitly::Url.new(nil, api_key) end
9
+ assert_nothing_raised do
10
+ Bitly::Url.new(login, api_key)
11
+ Bitly::Url.new(login, api_key, :hash => '3j4ir4')
12
+ Bitly::Url.new(login, api_key, :short_url => 'http://bit.ly/3j4ir4')
13
+ Bitly::Url.new(login, api_key, :long_url => 'http://google.com/')
14
+ end
15
+ end
16
+ context "shortening" do
17
+ context "with a long url" do
18
+ setup do
19
+ stub_get(/^http:\/\/api.bit.ly\/shorten\?.*longUrl=.*cnn.com.*$/,"cnn.json")
20
+ @url = Bitly::Url.new(login, api_key, :long_url => 'http://cnn.com/')
21
+ end
22
+ should "return a short url" do
23
+ assert_equal "http://bit.ly/15DlK", @url.shorten
24
+ end
25
+ end
26
+ context "with no long url" do
27
+ setup do
28
+ @url = Bitly::Url.new(login, api_key)
29
+ end
30
+ should "raise an error" do
31
+ assert_raise ArgumentError do
32
+ @url.shorten
33
+ end
34
+ end
35
+ end
36
+ context "with a short url already" do
37
+ setup do
38
+ @url = Bitly::Url.new(login, api_key, :short_url => 'http://bit.ly/31IqMl')
39
+ flexmock(@url).should_receive(:create_url).never
40
+ end
41
+ should "not need to call the api" do
42
+ assert_equal "http://bit.ly/31IqMl", @url.shorten
43
+ end
44
+ end
45
+ end
46
+ context "expanding" do
47
+ context "with a hash" do
48
+ setup do
49
+ stub_get(/^http:\/\/api.bit.ly\/expand\?.*hash=31IqMl.*$/,"expand_cnn.json")
50
+ @url = Bitly::Url.new(login, api_key, :hash => '31IqMl')
51
+ end
52
+ should "return an expanded url" do
53
+ assert_equal "http://cnn.com/", @url.expand
54
+ end
55
+ end
56
+ context "with a short url" do
57
+ setup do
58
+ stub_get(/^http:\/\/api.bit.ly\/expand\?.*hash=31IqMl.*$/,"expand_cnn.json")
59
+ @url = Bitly::Url.new(login, api_key, :short_url => 'http://bit.ly/31IqMl')
60
+ end
61
+ should "return an expanded url" do
62
+ assert_equal "http://cnn.com/", @url.expand
63
+ end
64
+ end
65
+ context "with no short url or hash" do
66
+ setup do
67
+ @url = Bitly::Url.new(login, api_key)
68
+ end
69
+ should "raise an error" do
70
+ assert_raise ArgumentError do
71
+ @url.expand
72
+ end
73
+ end
74
+ end
75
+ context "with a long url already" do
76
+ setup do
77
+ @url = Bitly::Url.new(login, api_key, :long_url => 'http://google.com')
78
+ flexmock(@url).should_receive(:create_url).never
79
+ end
80
+ should "not need to call the api" do
81
+ assert_equal "http://google.com", @url.expand
82
+ end
83
+ end
84
+ end
85
+ context "info" do
86
+ context "with a hash" do
87
+ setup do
88
+ stub_get(/^http:\/\/api.bit.ly\/info\?.*hash=3j4ir4.*$/,"google_info.json")
89
+ @url = Bitly::Url.new(login, api_key, :hash => '3j4ir4')
90
+ end
91
+ should "return info" do
92
+ assert_equal "Google", @url.info['htmlTitle']
93
+ end
94
+ end
95
+ context "with a short url" do
96
+ setup do
97
+ stub_get(/^http:\/\/api.bit.ly\/info\?.*hash=3j4ir4.*$/,"google_info.json")
98
+ @url = Bitly::Url.new(login, api_key, :short_url => 'http://bit.ly/3j4ir4')
99
+ end
100
+ should "return an expanded url" do
101
+ assert_equal "Google", @url.info['htmlTitle']
102
+ end
103
+ end
104
+ context "without a short url or hash" do
105
+ setup do
106
+ @url = Bitly::Url.new(login, api_key, :long_url => 'http://google.com')
107
+ end
108
+ should "raise an error" do
109
+ assert_raise ArgumentError do
110
+ @url.info
111
+ end
112
+ end
113
+ end
114
+ context "with info already" do
115
+ setup do
116
+ stub_get(/^http:\/\/api.bit.ly\/info\?.*hash=3j4ir4.*$/,"google_info.json")
117
+ @url = Bitly::Url.new(login, api_key, :short_url => 'http://bit.ly/3j4ir4')
118
+ @url.info
119
+ end
120
+ should "not call the api twice" do
121
+ flexmock(@url).should_receive(:create_url).never
122
+ @url.info
123
+ end
124
+ end
125
+ end
126
+ context "stats" do
127
+ context "with a hash" do
128
+ setup do
129
+ stub_get(/^http:\/\/api.bit.ly\/stats\?.*hash=3j4ir4.*$/,"google_stats.json")
130
+ @url = Bitly::Url.new(login, api_key, :hash => '3j4ir4')
131
+ end
132
+ should "return info" do
133
+ assert_equal 2644, @url.stats['clicks']
134
+ end
135
+ end
136
+ context "with a short url" do
137
+ setup do
138
+ stub_get(/^http:\/\/api.bit.ly\/stats\?.*hash=3j4ir4.*$/,"google_stats.json")
139
+ @url = Bitly::Url.new(login, api_key, :short_url => 'http://bit.ly/3j4ir4')
140
+ end
141
+ should "return an expanded url" do
142
+ assert_equal 2644, @url.stats['clicks']
143
+ end
144
+ end
145
+ context "without a short url or hash" do
146
+ setup do
147
+ @url = Bitly::Url.new(login, api_key, :long_url => 'http://google.com')
148
+ end
149
+ should "raise an error" do
150
+ assert_raise ArgumentError do
151
+ @url.stats
152
+ end
153
+ end
154
+ end
155
+ context "with info already" do
156
+ setup do
157
+ stub_get(/^http:\/\/api.bit.ly\/stats\?.*hash=3j4ir4.*$/,"google_stats.json")
158
+ @url = Bitly::Url.new(login, api_key, :short_url => 'http://bit.ly/3j4ir4')
159
+ @url.stats
160
+ end
161
+ should "not call the api twice" do
162
+ flexmock(@url).should_receive(:create_url).never
163
+ @url.stats
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,76 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper.rb')
2
+
3
+ class TestUtils < Test::Unit::TestCase
4
+ include Bitly::Utils
5
+ API_VERSION = '2.0.1'
6
+
7
+ context "text utils" do
8
+ should "underscore a word" do
9
+ assert_equal "hello_world", underscore("HelloWorld")
10
+ end
11
+ should "create a hash from a short url" do
12
+ assert_equal "hello", create_hash_from_url("http://bit.ly/hello")
13
+ end
14
+ end
15
+
16
+ context "class utils" do
17
+ should "turn a key value pair into an instance variable" do
18
+ attr_define('hello','goodbye')
19
+ assert_equal @hello, "goodbye"
20
+ end
21
+
22
+ should "turn a hash into instance variables" do
23
+ instance_variablise({'hello' => 'goodbye'}, ['hello'])
24
+ assert_equal @hello, "goodbye"
25
+ end
26
+
27
+ should "not turn nonspecified variables into instance variables" do
28
+ instance_variablise({'hello' => 'goodbye'}, [])
29
+ assert_nil @hello
30
+ end
31
+ end
32
+
33
+ context "creating a url" do
34
+ setup do
35
+ @api_key = api_key
36
+ @login = login
37
+ end
38
+ should "contain all the basic information" do
39
+ url = create_url.to_s
40
+ assert url.include?('http://api.bit.ly/')
41
+ assert url.include?("version=#{API_VERSION}")
42
+ assert url.include?("apiKey=#{api_key}")
43
+ assert url.include?("login=#{login}")
44
+ end
45
+ should "contain the right resource" do
46
+ url = create_url('shorten').to_s
47
+ assert url.include?('/shorten')
48
+ end
49
+ should "contain extra parameters" do
50
+ url = create_url('shorten', :longUrl => 'http://google.com').to_s
51
+ assert url.include?("longUrl=#{CGI.escape('http://google.com')}")
52
+ end
53
+ end
54
+
55
+ context "fetching a url" do
56
+ context "successfully" do
57
+ setup do
58
+ stub_get("http://example.com","cnn.json")
59
+ end
60
+ should "return a json object successfully" do
61
+ result = get_result(URI.join("http://example.com"))
62
+ assert_equal Hash, result.class
63
+ end
64
+ end
65
+ context "unsuccessfully" do
66
+ setup do
67
+ stub_get("http://example.com", 'shorten_error.json')
68
+ end
69
+ should "raise BitlyError" do
70
+ assert_raise BitlyError do
71
+ result = get_result(URI.join("http://example.com"))
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,65 +1,81 @@
1
- require 'test/unit'
2
- require 'rubygems'
3
- require 'bitly'
4
-
5
- class TestBitly < Test::Unit::TestCase
6
-
7
- def setup
8
- @api_key = 'R_7776acc394294b2b0ad2c261a91c483d'
9
- @login = 'philnash'
10
- @bitly = Bitly.new(@login,@api_key)
11
- end
12
-
13
- # not a good test, but it makes sure things are working for now.
14
- def test_returns_short_url
15
- url = @bitly.shorten("http://google.com")
16
- assert_kind_of Bitly::Url, url
17
- assert_equal "http://google.com", url.long_url
18
- assert_equal "http://bit.ly/wQaT", url.short_url
19
- urls = @bitly.shorten(["http://google.com","http://cnn.com"])
20
- assert_equal "http://google.com", urls[0].long_url
21
- assert_equal "http://bit.ly/wQaT", urls[0].short_url
22
- url = @bitly.shorten("http://www.google.com/search?hl=en&q=url&btnG=Google+Search&aq=f&oq=")
23
- assert_kind_of Bitly::Url, url
24
- assert_equal "http://www.google.com/search?hl=en&q=url&btnG=Google+Search&aq=f&oq=", url.long_url
25
- assert_equal "http://bit.ly/NqK6i", url.short_url
26
- end
27
-
28
- def test_returns_a_long_url
29
- urls = @bitly.expand(["2bYgqR","1RmnUT"])
30
- assert_kind_of Bitly::Url, urls[0]
31
- assert_equal "http://cnn.com", urls[0].long_url
32
- assert_equal "2bYgqR", urls[0].hash
33
- assert_equal "http://google.com", urls[1].long_url
34
- assert_equal "1RmnUT", urls[1].hash
35
- url = @bitly.expand("http://bit.ly/wQaT")
36
- assert_kind_of Bitly::Url, url
37
- assert_equal "http://bit.ly/wQaT", url.short_url
38
- assert_equal "http://google.com/", url.long_url
39
- assert_equal "wQaT", url.hash
40
- url2 = @bitly.expand("wQaT")
41
- assert_kind_of Bitly::Url, url2
42
- assert_equal "wQaT", url2.hash
43
- assert_equal "http://bit.ly/wQaT", url2.short_url
44
- assert_equal "http://google.com/", url2.long_url
45
- end
46
-
47
- def test_returns_keyword_url
48
- #kind of ghetto test but we need it to be different every time
49
- require 'digest/sha1'
50
- keyword = Digest::SHA1.hexdigest(DateTime.now.to_s)
51
-
52
- url = @bitly.shorten("http://google.com", keyword)
53
- assert_equal url.short_keyword_url, "http://bit.ly/#{keyword}"
54
- end
55
-
56
- def test_returns_error_on_existing_keyword
57
- keyword = 'apple'
58
- assert_raise BitlyError do
59
- @bitly.shorten("http://apple.com/itunes", keyword)
60
- end
61
- end
62
-
63
-
64
-
65
- end
1
+ # require 'test/unit'
2
+ # require 'rubygems'
3
+ # require 'shoulda'
4
+ # require 'flexmock'
5
+ # require 'bitly'
6
+ #
7
+ # class TestBitly < Test::Unit::TestCase
8
+ #
9
+ # context "with a bitly client" do
10
+ # setup do
11
+ # @api_key = 'test_key'
12
+ # @login = 'test_account'
13
+ # @bitly = Bitly.new(@login,@api_key)
14
+ # end
15
+ #
16
+ # context "shortening" do
17
+ #
18
+ # context "a single url" do
19
+ # setup do
20
+ #
21
+ # end
22
+ # def test_returns_single_short_url
23
+ # url = @bitly.shorten("http://cnn.com")
24
+ # assert_kind_of Bitly::Url, url
25
+ # assert_equal "http://cnn.com", url.long_url
26
+ # assert_equal "http://bit.ly/15DlK", url.short_url
27
+ # end
28
+ #
29
+ # def test_shortens_multiple_urls
30
+ # urls = @bitly.shorten(["http://cnn.com","http://google.com"])
31
+ # assert_equal "http://cnn.com", urls[0].long_url
32
+ # assert_equal "http://bit.ly/15DlK", urls[0].short_url
33
+ # assert_equal "http://google.com", urls[1].long_url
34
+ # assert_equal "http://bit.ly/11etr", urls[1].short_url
35
+ # end
36
+ #
37
+ # def test_can_shorten_a_url_with_parameters
38
+ # url = @bitly.shorten("http://www.google.com/search?hl=en&q=url&btnG=Google+Search&aq=f&oq=")
39
+ # assert_kind_of Bitly::Url, url
40
+ # assert_equal "http://www.google.com/search?hl=en&q=url&btnG=Google+Search&aq=f&oq=", url.long_url
41
+ # assert_equal "http://bit.ly/NqK6i", url.short_url
42
+ # end
43
+ #
44
+ # def test_returns_a_long_url
45
+ # urls = @bitly.expand(["2bYgqR","1RmnUT"])
46
+ # assert_kind_of Bitly::Url, urls[0]
47
+ # assert_equal "http://cnn.com", urls[0].long_url
48
+ # assert_equal "2bYgqR", urls[0].hash
49
+ # assert_equal "http://google.com", urls[1].long_url
50
+ # assert_equal "1RmnUT", urls[1].hash
51
+ # url = @bitly.expand("http://bit.ly/wQaT")
52
+ # assert_kind_of Bitly::Url, url
53
+ # assert_equal "http://bit.ly/wQaT", url.short_url
54
+ # assert_equal "http://google.com/", url.long_url
55
+ # assert_equal "wQaT", url.hash
56
+ # url2 = @bitly.expand("wQaT")
57
+ # assert_kind_of Bitly::Url, url2
58
+ # assert_equal "wQaT", url2.hash
59
+ # assert_equal "http://bit.ly/wQaT", url2.short_url
60
+ # assert_equal "http://google.com/", url2.long_url
61
+ # end
62
+ #
63
+ # # def test_returns_keyword_url
64
+ # # #kind of ghetto test but we need it to be different every time
65
+ # # require 'digest/sha1'
66
+ # # keyword = Digest::SHA1.hexdigest(DateTime.now.to_s)
67
+ # #
68
+ # # url = @bitly.shorten("http://google.com", :keyword => keyword)
69
+ # # assert_equal url.short_keyword_url, "http://bit.ly/#{keyword}"
70
+ # # end
71
+ #
72
+ # def test_returns_error_on_existing_keyword
73
+ # keyword = 'apple'
74
+ # assert_raise BitlyError do
75
+ # @bitly.shorten("http://apple.com/itunes", :keyword => keyword)
76
+ # end
77
+ # end
78
+ #
79
+ #
80
+ #
81
+ # end
@@ -0,0 +1,29 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'shoulda'
4
+ require 'flexmock/test_unit'
5
+ require 'fakeweb'
6
+
7
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'bitly')
8
+
9
+ FakeWeb.allow_net_connect = false
10
+
11
+ def fixture_file(filename)
12
+ return '' if filename == ''
13
+ file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
14
+ File.read(file_path)
15
+ end
16
+
17
+ def stub_get(url, filename, status=nil)
18
+ options = {:body => fixture_file(filename)}
19
+ options.merge!({:status => status}) unless status.nil?
20
+
21
+ FakeWeb.register_uri(:get, url, options)
22
+ end
23
+
24
+ def api_key
25
+ 'test_key'
26
+ end
27
+ def login
28
+ 'test_account'
29
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitly
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phil Nash
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-23 00:00:00 +01:00
12
+ date: 2009-07-09 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -81,4 +81,8 @@ signing_key:
81
81
  specification_version: 3
82
82
  summary: Use the bit.ly API to shorten or expand URLs
83
83
  test_files:
84
+ - test/bitly/test_client.rb
85
+ - test/bitly/test_url.rb
86
+ - test/bitly/test_utils.rb
84
87
  - test/test_bitly.rb
88
+ - test/test_helper.rb