url_shortener 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -13,3 +13,18 @@
13
13
  * 2 additions:
14
14
  * adds url stats functionality
15
15
  * adds url info functionality
16
+
17
+ === 0.0.4 2009-11-15
18
+
19
+ * 4 additions:
20
+ * adds UrlShortener::Response
21
+ * adds UrlShortener::Response::Shorten
22
+ * adds UrlShortener::Response::Expand
23
+ * adds some convenience methods to the returned results for shortening and expanding urls
24
+
25
+ * 3 changes:
26
+ * api call method UrlShortener::Client#shorten now returns a UrlShortener::Response::Shorten object
27
+ * api call method UrlShortener::Client#expand now returns a UrlShortener::Response::Expand object
28
+ * other api call methods now return a UrlShortener::Response object
29
+ These changes are introduced to return the api call results in a flexible manner and provide more flexibility for manipulating result output
30
+
data/Manifest.txt CHANGED
@@ -6,10 +6,16 @@ lib/url_shortener/authorize.rb
6
6
  lib/url_shortener/client.rb
7
7
  lib/url_shortener/error.rb
8
8
  lib/url_shortener/interface.rb
9
+ lib/url_shortener/response.rb
10
+ lib/url_shortener/response/shorten.rb
11
+ lib/url_shortener/response/expand.rb
9
12
  spec/spec_helper.rb
10
13
  spec/url_shortener/authorize_spec.rb
11
14
  spec/url_shortener/client_spec.rb
12
15
  spec/url_shortener/interface_spec.rb
16
+ spec/url_shortener/response_spec.rb
17
+ spec/url_shortener/response/shorten_spec.rb
18
+ spec/url_shortener/response/expand_spec.rb
13
19
  features/request_failure.feature
14
20
  features/api_calls.feature
15
21
  features/support/env.rb
data/README.rdoc CHANGED
@@ -1,55 +1,83 @@
1
1
  ==Description
2
2
 
3
- Url Shortener is a Ruby library / gem and API wrapper for bit.ly to shorten the urls.
3
+ Url Shortener is a Ruby library / gem and API wrapper for bit.ly to shorten/expand the urls and retrieve other information about them.
4
4
 
5
5
  To use the url_shortener gem you need a bit.ly login and Api key.
6
6
 
7
7
  * If you are registered at bit.ly then you already have it.
8
8
  * If you are not registered then you can get it easily by registering at bit.ly as they give an api key to every registered user
9
9
 
10
+ ==INSTALL
11
+
12
+ gem sources -a http://gemcutter.org
13
+
14
+ sudo gem install url_shortener
15
+
16
+
10
17
  ==USAGE
11
18
 
19
+ ===Quick Start
20
+
21
+ * Start an irb session
22
+
23
+ require 'rubygems'
24
+
25
+ require 'url_shortener'
26
+
12
27
  ===To Shorten Urls
13
28
 
14
- * authorize = UrlShortener::Authorize.new 'your_bitly_login', 'your_bitly_api_key'
29
+ authorize = UrlShortener::Authorize.new 'your_bitly_login', 'your_bitly_api_key'
15
30
 
16
- * For example; authorize = UrlShortener::Authorize.new 'bitlyapidemo', 'R_0da49e0a9118ff35f52f629d2d71bf07'
31
+ For example; authorize = UrlShortener::Authorize.new 'bitlyapidemo', 'R_0da49e0a9118ff35f52f629d2d71bf07'
17
32
 
18
- * client = UrlShortener::Client.new authorize
33
+ client = UrlShortener::Client.new authorize
19
34
 
20
- * results = client.shorten('http://www.yahoo.com') # => Hash containing short url
35
+ shorten = client.shorten('http://www.yahoo.com') # => UrlShortener::Response::Shorten object
21
36
 
22
- * results['shortUrl'] # => short url string
37
+ shorten.result # => returns a hash of all data returned from bitly
23
38
 
24
- OR
39
+ shorten.urls # => Only returns the short urls
40
+
41
+ look for more convenience methods in the UrlShortener::Response::Shorten class
42
+
25
43
 
26
- * results = client.shorten('http://www.yahoo.com', 'http://www.google.com', 'http://github.com')
44
+ OR
27
45
 
28
- * results.each {|result| puts result['shortUrl']}
46
+ shorten = client.shorten('http://www.yahoo.com', 'http://www.google.com', 'http://github.com')
29
47
 
30
- ===To Expand Urls
48
+ shorten.result
49
+
50
+ shorten.urls
31
51
 
32
- * result = client.expand(:shortUrl => 'http://bit.ly/1RmnUT') # => long url string
52
+ ===To Expand Urls
33
53
 
34
- OR
54
+ expand = client.expand(:shortUrl => 'http://bit.ly/1RmnUT') # => UrlShortener::Response::Expand object
55
+
56
+ OR
35
57
 
36
- * result = client.expand(:hash => '2bYgqR') # => long url string
58
+ expand = client.expand(:hash => '1RmnUT')
59
+ expand.result # => returns a hash of all data returned from bitly
60
+ expand.url # => returns a long url string
37
61
 
38
62
  ===To get stats
39
63
 
40
- * result = client.stats(:shortUrl => 'http://bit.ly/1RmnUT') # => long url string
41
-
42
- OR
64
+ stats = client.stats(:shortUrl => 'http://bit.ly/1RmnUT') # => UrlShortener::Response object
65
+
66
+ OR
43
67
 
44
- * result = client.stats(:hash => '2bYgqR') # => long url string
68
+ stats = client.stats(:hash => '1RmnUT') # => UrlShortener::Response object
69
+ stats.result # => returns a hash of all data returned from bitly
70
+ #TODO: ADD convenience methods for stats
45
71
 
46
72
  ===To get url info
47
73
 
48
- * result = client.info(:shortUrl => 'http://bit.ly/1RmnUT') # => long url string
49
-
50
- OR
74
+ info = client.info(:shortUrl => 'http://bit.ly/1RmnUT') # => UrlShortener::Response object
75
+
76
+ OR
51
77
 
52
- * result = client.info(:hash => '2bYgqR') # => long url string
78
+ info = client.info(:hash => '1RmnUT') # => UrlShortener::Response object
79
+ info.result # => returns a hash of all data returned from bitly
80
+ #TODO: ADD convenience methods for info api call
53
81
 
54
82
  ==Docs
55
83
 
@@ -30,7 +30,7 @@ Feature: REST calls to bit.ly API
30
30
  And "bit.ly" is online
31
31
  When I submit a request to expand a short url using the hash "2oV4lu"
32
32
  Then I should get the response from "bit.ly"
33
- And the expanded url should be "http://www.github.com/nas"
33
+ And the expanded url should be "http://www.github.com/nas" for "2oV4lu"
34
34
 
35
35
  Scenario: Expand the short url when correct login and api key is present
36
36
  Given I use "correct" "bit.ly" login name as "bitlyapidemo"
@@ -39,7 +39,7 @@ Feature: REST calls to bit.ly API
39
39
  And "bit.ly" is online
40
40
  When I submit a request to expand a short url using the short url "http://bit.ly/6NaDb"
41
41
  Then I should get the response from "bit.ly"
42
- And the expanded url should be "http://www.github.com/nas"
42
+ And the expanded url should be "http://www.github.com/nas" for "6NaDb"
43
43
 
44
44
  Scenario: Get stats from the hash when correct login and api key is present
45
45
  Given I use "correct" "bit.ly" login name as "bitlyapidemo"
@@ -2,82 +2,76 @@
2
2
  # = When Steps =
3
3
  # ==============
4
4
  When /^I submit a request to shorten the url "([^\"]*)"$/ do |url|
5
- @result = client.shorten(url)
5
+ @response = client.shorten(url)
6
6
  end
7
7
 
8
8
  When /^I submit a request to shorten the urls "([^\"]*)" and "([^\"]*)"$/ do |url1, url2|
9
- @result = client.shorten(url1, url2)
9
+ @response = client.shorten(url1, url2)
10
10
  end
11
11
 
12
12
 
13
13
  When /^I submit a request to expand a short url using the hash "([^\"]*)"$/ do |hash|
14
- @result = client.expand(:hash => hash)
14
+ @response = client.expand(:hash => hash)
15
15
  end
16
16
 
17
17
  When /^I submit a request to expand a short url using the short url "([^\"]*)"$/ do |short_url|
18
- @result = client.expand(:shortUrl => short_url)
18
+ @response = client.expand(:shortUrl => short_url)
19
19
  end
20
20
 
21
21
  When /^I submit a request to get stats for a short url using the hash "([^\"]*)"$/ do |hash|
22
- @result = client.stats(:hash => hash)
22
+ @response = client.stats(:hash => hash)
23
23
  end
24
24
 
25
25
  When /^I submit a request to get stats for a short url using the short url "([^\"]*)"$/ do |short_url|
26
- @result = client.stats(:shortUrl => short_url)
26
+ @response = client.stats(:shortUrl => short_url)
27
27
  end
28
28
 
29
29
  When /^I submit a request to get info for a short url using the hash "([^\"]*)"$/ do |hash|
30
- @result = client.info(:hash => hash)
30
+ @response = client.info(:hash => hash)
31
31
  end
32
32
 
33
33
  When /^I submit a request to get info for a short url using the short url "([^\"]*)"$/ do |short_url|
34
- @result = client.info(:shortUrl => short_url)
34
+ @response = client.info(:shortUrl => short_url)
35
35
  end
36
36
 
37
37
  # ==============
38
38
  # = Then Steps =
39
39
  # ==============
40
40
  Then /^I should get the response from "([^\"]*)"$/ do |arg1|
41
- @result.should_not be_nil
41
+ @response.should_not be_nil
42
42
  end
43
43
 
44
44
  Then /^the shorten url should be "([^\"]*)"$/ do |short_url|
45
- @result['shortUrl'].should eql(short_url)
45
+ @response.urls.should eql(short_url)
46
46
  end
47
47
 
48
48
  Then /^the shorten url should be "([^\"]*)" and "([^\"]*)"$/ do |expanded_url1, expanded_url2|
49
- @result.each do |res|
50
- res['shortUrl'].should eql(expanded_url1) if res['nodeKey'] == 'http://www.github.com/nas'
51
- res['shortUrl'].should eql(expanded_url2) if res['nodeKey'] == 'http://www.google.com'
52
- end
49
+ @response.urls.should include(expanded_url1, expanded_url2)
53
50
  end
54
51
 
55
52
  Then /^the hash values should be "([^\"]*)" and "([^\"]*)"$/ do |hash1, hash2|
56
- @result.each do |res|
57
- res['hash'].should eql(hash1) if res['nodeKey'] == 'http://www.github.com/nas'
58
- res['hash'].should eql(hash2) if res['nodeKey'] == 'http://www.google.com'
59
- end
53
+ @response.hashes.should include(hash1, hash2)
60
54
  end
61
55
 
62
56
 
63
57
  Then /^the hash should be "([^\"]*)"$/ do |hash|
64
- @result['hash'].should eql(hash)
58
+ @response.hashes.should eql(hash)
65
59
  end
66
60
 
67
- Then /^the expanded url should be "([^\"]*)"$/ do |expanded_url|
68
- @result.should eql(expanded_url)
61
+ Then /^the expanded url should be "([^\"]*)" for "([^\"]*)"$/ do |expanded_url, request_parameter|
62
+ @response.url.should eql(expanded_url)
69
63
  end
70
64
 
71
65
  Then /^the result should have "([^\"]*)" and "([^\"]*)" keys in the returned hash$/ do |key1,key2|
72
- @result.keys.should include(key1, key2)
66
+ @response.result.keys.should include(key1, key2)
73
67
  end
74
68
 
75
69
  Then /^the result should have "([^\"]*)" key in the returned hash$/ do |key|
76
- @result.keys.should include(key)
70
+ @response.result.keys.should include(key)
77
71
  end
78
72
 
79
73
  Then /^the result doc hash should have "([^\"]*)", "([^\"]*)", "([^\"]*)", etc keys$/ do |key1, key2, key3|
80
- @result['doc'].keys.should include(key1, key2, key3)
74
+ @response.result['doc'].keys.should include(key1, key2, key3)
81
75
  end
82
76
 
83
77
  private
@@ -16,29 +16,30 @@ module UrlShortener
16
16
  # duplicate keys in a hash hence this solution
17
17
  urls.collect!{|url| CGI.escape(url)}
18
18
  end_point_with_params = "#{end_point('shorten')}?longUrl=#{urls.join('&longUrl=')}"
19
- interface(end_point_with_params).get['nodeKeyVal']
19
+ response = interface(end_point_with_params).get
20
+ UrlShortener::Response::Shorten.new(response)
20
21
  end
21
22
 
22
23
  # Provide parameter as a key value pair of existing short url or its hash
23
24
  # e.g. expand :shortUrl => 'http://bit.ly/15DlK' OR :hash => '31IqMl'
24
25
  def expand(option)
25
26
  check_request_parameters(option)
26
- /^.*\/(.*)$/.match option[:shortUrl] if option[:shortUrl]
27
- request_param = option[:hash] ? option[:hash] : $1
28
- result = interface(nil, {:rest_url => end_point('expand')}.merge(option)).get[request_param]
29
- return result['longUrl'] if result
27
+ response = interface(nil, endpoint_with_options('expand',option)).get
28
+ UrlShortener::Response::Expand.new(response)
30
29
  end
31
30
 
32
31
  # Provide parameter as a key value pair of existing short url or its hash
33
32
  # e.g. stats :shortUrl => 'http://bit.ly/15DlK' #OR :hash => '31IqMl'
34
33
  def stats(option)
35
34
  check_request_parameters(option)
36
- interface(nil, {:rest_url => end_point('stats')}.merge(option)).get
35
+ response = interface(nil, endpoint_with_options('stats',option)).get
36
+ UrlShortener::Response.new(response)
37
37
  end
38
38
 
39
39
  def info(option)
40
40
  check_request_parameters(option)
41
- interface(nil, {:rest_url => end_point('info')}.merge(option)).get
41
+ response = interface(nil, endpoint_with_options('info',option)).get
42
+ UrlShortener::Response.new(response)
42
43
  end
43
44
 
44
45
  # If a complex url need to be passed then use end_point_with_params else
@@ -69,6 +70,10 @@ module UrlShortener
69
70
 
70
71
  private
71
72
 
73
+ def endpoint_with_options(resource, option)
74
+ {:rest_url => end_point(resource)}.merge(option)
75
+ end
76
+
72
77
  def check_request_parameters(option)
73
78
  unless (option[:hash] || option[:shortUrl])
74
79
  raise UrlShortener::IncompleteRequestParameter, "Either hash or the shorUrl is required to complete the operation."
@@ -16,7 +16,7 @@ module UrlShortener
16
16
  class NoResult < Error
17
17
  end
18
18
 
19
- class UrlShortener::IncompleteRequestParameter < Error
19
+ class IncompleteRequestParameter < Error
20
20
 
21
21
  end
22
22
  end
@@ -0,0 +1,28 @@
1
+ module UrlShortener
2
+
3
+ class Response::Expand < UrlShortener::Response
4
+
5
+ def initialize(response)
6
+ super
7
+ end
8
+
9
+ def url
10
+ return unless result_present?
11
+ return unless key_present?
12
+ url_hash = result.keys.first
13
+ result[url_hash]['longUrl']
14
+ end
15
+
16
+ private
17
+
18
+ def result_present?
19
+ result && !result.empty?
20
+ end
21
+
22
+ def key_present?
23
+ result_present?
24
+ result.keys && !result.keys.empty?
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,55 @@
1
+ module UrlShortener
2
+ class Response::Shorten < UrlShortener::Response
3
+
4
+ def initialize(response)
5
+ super
6
+ end
7
+
8
+ def urls
9
+ raise_if_no_result
10
+ return result['nodeKeyVal']['shortUrl'] unless result['nodeKeyVal'].is_a?(Array)
11
+ result['nodeKeyVal'].collect{|res| res['shortUrl']}
12
+ end
13
+
14
+ def urls_with_long_url_keys
15
+ raise_if_no_result
16
+ return_single_or_mutiple_hash_values('shortUrl')
17
+ end
18
+
19
+ def hashes
20
+ raise_if_no_result
21
+ return result['nodeKeyVal']['hash'] unless result['nodeKeyVal'].is_a?(Array)
22
+ result['nodeKeyVal'].collect{|res| res['hash']}
23
+ end
24
+
25
+ def hashes_with_long_url_keys
26
+ raise_if_no_result
27
+ return_single_or_mutiple_hash_values('hash')
28
+ end
29
+
30
+ private
31
+
32
+ def return_single_or_mutiple_hash_values(value)
33
+ return long_url_with_result(value) unless result['nodeKeyVal'].is_a?(Array)
34
+ return array_of_long_url_with_result(value)
35
+ end
36
+
37
+ def long_url_with_result(value)
38
+ hash = {}
39
+ hash[result['nodeKeyVal']['nodeKey']] = result['nodeKeyVal'][value]
40
+ return hash
41
+ end
42
+
43
+ def array_of_long_url_with_result(value)
44
+ hash = {}
45
+ result['nodeKeyVal'].collect{|res| hash[res['nodeKey']] = res[value]}
46
+ return hash
47
+ end
48
+
49
+ def raise_if_no_result
50
+ if result.nil? || result['nodeKeyVal'].nil?
51
+ raise UrlShortener::NoResult, "Empty Result set."
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,11 @@
1
+ module UrlShortener
2
+ class Response
3
+
4
+ attr_reader :result
5
+
6
+ def initialize(result)
7
+ @result = result
8
+ end
9
+
10
+ end
11
+ end
data/lib/url_shortener.rb CHANGED
@@ -7,4 +7,7 @@ require 'cgi'
7
7
  require 'url_shortener/error.rb'
8
8
  require 'url_shortener/authorize.rb'
9
9
  require 'url_shortener/client.rb'
10
- require 'url_shortener/interface.rb'
10
+ require 'url_shortener/interface.rb'
11
+ require 'url_shortener/response.rb'
12
+ require 'url_shortener/response/shorten.rb'
13
+ require 'url_shortener/response/expand.rb'
@@ -115,6 +115,10 @@ describe UrlShortener::Client do
115
115
  @interface.should_receive(:get)
116
116
  @client.shorten(@url)
117
117
  end
118
+
119
+ it "should return the UrlShortener::Response::Shorten object" do
120
+ @client.shorten(@url).should be_instance_of(UrlShortener::Response::Shorten)
121
+ end
118
122
 
119
123
  end
120
124
 
@@ -218,12 +222,16 @@ describe UrlShortener::Client do
218
222
 
219
223
  it "should return the long url of the hash provided" do
220
224
  @interface.should_receive(:get).and_return('qweWE' => {'longUrl' => 'http://www.goog.com'})
221
- @client.expand(:hash => @hash).should eql('http://www.goog.com')
225
+ @client.expand(:hash => @hash).result['qweWE'].values.should include('http://www.goog.com')
222
226
  end
223
227
 
224
228
  it "should return the long url for the shortUrl provided" do
225
229
  @interface.should_receive(:get).and_return('wesSD' => {'longUrl' => 'http://www.goog.com'})
226
- @client.expand(:shortUrl => @short_url).should eql('http://www.goog.com')
230
+ @client.expand(:shortUrl => @short_url).result['wesSD'].values.should include('http://www.goog.com')
231
+ end
232
+
233
+ it "should return the UrlShortener::Response::Expand object" do
234
+ @client.expand(:hash => @hash).should be_instance_of(UrlShortener::Response::Expand)
227
235
  end
228
236
 
229
237
  end
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe UrlShortener::Response::Expand do
4
+
5
+ describe "#url" do
6
+ before(:each) do
7
+ @result_hash = {"1RmnUT" => {"longUrl" => "http://google.com"}}
8
+ @expand = UrlShortener::Response::Expand.new(@result_hash)
9
+ @expand.stub!(:result).and_return(@result_hash)
10
+ @keys = ['1RmnUT']
11
+ @key = '1RmnUT'
12
+ @result_hash.stub!(:keys).and_return(@keys)
13
+ @keys.stub!(:first).and_return(@key)
14
+ @expand.stub!(:result_present?).and_return(true)
15
+ @expand.stub!(:key_present?).and_return(true)
16
+ end
17
+
18
+ it "should check if keys are present when results are present" do
19
+ @expand.stub!(:result_present?).and_return(true)
20
+ @expand.should_receive(:key_present?)
21
+ @expand.url.should
22
+ end
23
+
24
+ it "should not check if keys are present when results are not present" do
25
+ @expand.stub!(:result_present?).and_return(false)
26
+ @expand.should_receive(:key_present?).never
27
+ @expand.url.should
28
+ end
29
+
30
+ it "should return nil if there are no keys" do
31
+ @expand.stub!(:key_present?).and_return(nil)
32
+ @expand.url.should eql(nil)
33
+ end
34
+
35
+ it "should check if results are present" do
36
+ @expand.should_receive(:result_present?)
37
+ @expand.url.should
38
+ end
39
+
40
+ it "should return nil is result is not present" do
41
+ @expand.stub!(:result_present?).and_return(nil)
42
+ @expand.url.should eql(nil)
43
+ end
44
+
45
+ it "should get the result" do
46
+ @expand.should_receive(:result).and_return(@result_hash)
47
+ @expand.url
48
+ end
49
+
50
+ it "should find the keys in the result returned from bitley" do
51
+ @result_hash.should_receive(:keys).and_return(@keys)
52
+ @expand.url
53
+ end
54
+
55
+ it "should get first key as there will be only key returning from bitly" do
56
+ @keys.should_receive(:first).and_return(@key)
57
+ @expand.url
58
+ end
59
+
60
+ it "should return the longUrl" do
61
+ @expand.url.should eql("http://google.com")
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,278 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe UrlShortener::Response::Shorten do
4
+
5
+ describe "#urls" do
6
+ before(:each) do
7
+ @response = {'nodeKeyVal' => {'shortUrl' => 'http://a/short/url'}}
8
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
9
+ @shorten_response.stub!(:result).and_return(@response)
10
+ end
11
+
12
+ it "should get the result" do
13
+ @shorten_response.should_receive(:result).at_least(1).and_return(@response)
14
+ @shorten_response.urls
15
+ end
16
+
17
+ it "should check whether result's nodeKeyVal values is an array" do
18
+ @response['nodeKeyVal'].should_receive(:is_a?).with(Array)
19
+ @shorten_response.urls
20
+ end
21
+
22
+ it "should raise when there are no results" do
23
+ @shorten_response.should_receive(:raise_if_no_result)
24
+ @shorten_response.urls
25
+ end
26
+
27
+ describe "when no results are returned" do
28
+ before(:each) do
29
+ @response = {}
30
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
31
+ end
32
+
33
+ it "should raise error if there is no 'nodeKeyVal' key in the response parameter" do
34
+ lambda { @shorten_response.urls }.should raise_error(UrlShortener::NoResult, "Empty Result set.")
35
+ end
36
+
37
+ it "should raise error when if the response parameter or result is empty?" do
38
+ @response = nil
39
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
40
+ lambda { @shorten_response.urls }.should raise_error(UrlShortener::NoResult, "Empty Result set.")
41
+ end
42
+ end
43
+
44
+ describe "when returned result is not an array" do
45
+ before(:each) do
46
+ @response['nodeKeyVal'].stub!(:is_a?).and_return(false)
47
+ end
48
+
49
+ it "should return the short url" do
50
+ @shorten_response.urls.should eql('http://a/short/url')
51
+ end
52
+
53
+ end
54
+
55
+ describe "when returned result is an array" do
56
+ before(:each) do
57
+ @response = {'nodeKeyVal' => [{'shortUrl' => 'http://a/short/url'}, {'shortUrl' => 'http://another/short/url'}]}
58
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
59
+ @shorten_response.stub!(:result).and_return(@response)
60
+ end
61
+
62
+ it "should return an array of short urls" do
63
+ @shorten_response.urls.should include('http://a/short/url', 'http://another/short/url')
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ describe "#urls with long url keys" do
71
+ before(:each) do
72
+ @response = {'nodeKeyVal' => {'shortUrl' => 'http://a/short/url', 'nodeKey' => 'http://a/long/url'}}
73
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
74
+ @shorten_response.stub!(:result).and_return(@response)
75
+ end
76
+
77
+ describe "when no results are returned" do
78
+ before(:each) do
79
+ @response = {}
80
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
81
+ end
82
+
83
+ it "should raise error if there is no 'nodeKeyVal' key in the response parameter" do
84
+ lambda { @shorten_response.urls_with_long_url_keys }.should raise_error(UrlShortener::NoResult, "Empty Result set.")
85
+ end
86
+
87
+ it "should raise error when if the response parameter or result is empty?" do
88
+ @response = nil
89
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
90
+ lambda { @shorten_response.urls_with_long_url_keys }.should raise_error(UrlShortener::NoResult, "Empty Result set.")
91
+ end
92
+ end
93
+
94
+ describe "when returned result is not an array" do
95
+ before(:each) do
96
+ @response['nodeKeyVal'].stub!(:is_a?).and_return(false)
97
+ end
98
+
99
+ it "should return the long url as the hash key" do
100
+ @shorten_response.urls_with_long_url_keys.keys.should include('http://a/long/url')
101
+ end
102
+
103
+ it "should return the short url as the hash value" do
104
+ @shorten_response.urls_with_long_url_keys.values.should include('http://a/short/url')
105
+ end
106
+
107
+ end
108
+
109
+ describe "when returned result is an array" do
110
+ before(:each) do
111
+ @response = {'nodeKeyVal' => [{'shortUrl' => 'http://a1/short/url', 'nodeKey' => 'http://a1/long/url'},
112
+ {'shortUrl' => 'http://a2/short/url', 'nodeKey' => 'http://a2/long/url'}]}
113
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
114
+ @shorten_response.stub!(:result).and_return(@response)
115
+ end
116
+
117
+ it "should return long urls as keys" do
118
+ @shorten_response.urls_with_long_url_keys.keys.should include('http://a1/long/url', 'http://a2/long/url')
119
+ end
120
+
121
+ it "should return short urls as values" do
122
+ @shorten_response.urls_with_long_url_keys.values.should include('http://a1/short/url', 'http://a2/short/url')
123
+ end
124
+
125
+ it "should have the first long url key value set to the first short url" do
126
+ @shorten_response.urls_with_long_url_keys['http://a1/long/url'].should eql('http://a1/short/url')
127
+ end
128
+
129
+ it "should have the second long url key value set to the second short url" do
130
+ @shorten_response.urls_with_long_url_keys['http://a2/long/url'].should eql('http://a2/short/url')
131
+ end
132
+
133
+ it "should only have two keys values when two short urls are returned" do
134
+ @shorten_response.urls_with_long_url_keys.size.should eql(2)
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+
141
+ describe "#hashes" do
142
+
143
+ before(:each) do
144
+ @response = {'nodeKeyVal' => {'hash' => 'aHash'}}
145
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
146
+ @shorten_response.stub!(:result).and_return(@response)
147
+ end
148
+
149
+ it "should get the result" do
150
+ @shorten_response.should_receive(:result).at_least(1).and_return(@response)
151
+ @shorten_response.hashes
152
+ end
153
+
154
+ it "should check whether result's nodeKeyVal values is an array" do
155
+ @response['nodeKeyVal'].should_receive(:is_a?).with(Array)
156
+ @shorten_response.hashes
157
+ end
158
+
159
+ it "should raise when there are no results" do
160
+ @shorten_response.should_receive(:raise_if_no_result)
161
+ @shorten_response.hashes
162
+ end
163
+
164
+ describe "when no results are returned" do
165
+ before(:each) do
166
+ @response = {}
167
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
168
+ end
169
+
170
+ it "should raise error if there is no 'nodeKeyVal' key in the response parameter" do
171
+ lambda { @shorten_response.hashes }.should raise_error(UrlShortener::NoResult, "Empty Result set.")
172
+ end
173
+
174
+ it "should raise error when if the response parameter or result is empty?" do
175
+ @response = nil
176
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
177
+ lambda { @shorten_response.hashes }.should raise_error(UrlShortener::NoResult, "Empty Result set.")
178
+ end
179
+ end
180
+
181
+ describe "when returned result is not an array" do
182
+ before(:each) do
183
+ @response['nodeKeyVal'].stub!(:is_a?).and_return(false)
184
+ end
185
+
186
+ it "should return the short url" do
187
+ @shorten_response.hashes.should eql('aHash')
188
+ end
189
+
190
+ end
191
+
192
+ describe "when returned result is an array" do
193
+ before(:each) do
194
+ @response = {'nodeKeyVal' => [{'hash' => 'aHash'}, {'hash' => 'anHash'}]}
195
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
196
+ @shorten_response.stub!(:result).and_return(@response)
197
+ end
198
+
199
+ it "should return an array of hashes" do
200
+ @shorten_response.hashes.should include('aHash', 'anHash')
201
+ end
202
+
203
+ end
204
+
205
+ end
206
+
207
+ describe "#hashes with long url keys" do
208
+ before(:each) do
209
+ @response = {'nodeKeyVal' => {'hash' => 'aHash', 'nodeKey' => 'http://a/long/url'}}
210
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
211
+ @shorten_response.stub!(:result).and_return(@response)
212
+ end
213
+
214
+ describe "when no results are returned" do
215
+ before(:each) do
216
+ @response = {}
217
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
218
+ end
219
+
220
+ it "should raise error if there is no 'nodeKeyVal' key in the response parameter" do
221
+ lambda { @shorten_response.hashes_with_long_url_keys }.should raise_error(UrlShortener::NoResult, "Empty Result set.")
222
+ end
223
+
224
+ it "should raise error when if the response parameter or result is empty?" do
225
+ @response = nil
226
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
227
+ lambda { @shorten_response.hashes_with_long_url_keys }.should raise_error(UrlShortener::NoResult, "Empty Result set.")
228
+ end
229
+ end
230
+
231
+ describe "when returned result is not an array" do
232
+ before(:each) do
233
+ @response['nodeKeyVal'].stub!(:is_a?).and_return(false)
234
+ end
235
+
236
+ it "should return the long url as the hash key" do
237
+ @shorten_response.hashes_with_long_url_keys.keys.should include('http://a/long/url')
238
+ end
239
+
240
+ it "should return the short url as the hash value" do
241
+ @shorten_response.hashes_with_long_url_keys.values.should include('aHash')
242
+ end
243
+
244
+ end
245
+
246
+ describe "when returned result is an array" do
247
+ before(:each) do
248
+ @response = {'nodeKeyVal' => [{'hash' => 'a1Hash', 'nodeKey' => 'http://a1/long/url'},
249
+ {'hash' => 'a2Hash', 'nodeKey' => 'http://a2/long/url'}]}
250
+ @shorten_response = UrlShortener::Response::Shorten.new(@response)
251
+ @shorten_response.stub!(:result).and_return(@response)
252
+ end
253
+
254
+ it "should return long urls as keys" do
255
+ @shorten_response.hashes_with_long_url_keys.keys.should include('http://a1/long/url', 'http://a2/long/url')
256
+ end
257
+
258
+ it "should return hashes as values" do
259
+ @shorten_response.hashes_with_long_url_keys.values.should include('a1Hash', 'a2Hash')
260
+ end
261
+
262
+ it "should have the first long url key value set to the first hash value" do
263
+ @shorten_response.hashes_with_long_url_keys['http://a1/long/url'].should eql('a1Hash')
264
+ end
265
+
266
+ it "should have the second long url key value set to the second hash value" do
267
+ @shorten_response.hashes_with_long_url_keys['http://a2/long/url'].should eql('a2Hash')
268
+ end
269
+
270
+ it "should only have two keys values when two short urls are returned" do
271
+ @shorten_response.hashes_with_long_url_keys.size.should eql(2)
272
+ end
273
+
274
+ end
275
+
276
+ end
277
+
278
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe UrlShortener::Response do
4
+
5
+ describe ".new" do
6
+ before(:each) do
7
+ @response = {:a_key => 'value'}
8
+ end
9
+
10
+ it "should set the result attribute" do
11
+ response = UrlShortener::Response.new(@response)
12
+ response.result.should eql(@response)
13
+ end
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: url_shortener
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nasir Jamal
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-10 00:00:00 +00:00
12
+ date: 2009-11-15 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,7 +22,7 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: 0.4.5
24
24
  version:
25
- description: Url Shortener is a Ruby library / gem and API wrapper for bit.ly to shorten, expand urls and other APIs.
25
+ description: Url Shortener is a Ruby library / gem and API wrapper for bit.ly to shorten/expand the urls and retrieve other information about them.
26
26
  email: nas35_in@yahoo.com
27
27
  executables: []
28
28
 
@@ -39,6 +39,9 @@ files:
39
39
  - lib/url_shortener/client.rb
40
40
  - lib/url_shortener/error.rb
41
41
  - lib/url_shortener/interface.rb
42
+ - lib/url_shortener/response.rb
43
+ - lib/url_shortener/response/shorten.rb
44
+ - lib/url_shortener/response/expand.rb
42
45
  has_rdoc: true
43
46
  homepage: http://github.com/nas/url_shortener
44
47
  licenses: []
@@ -72,6 +75,9 @@ test_files:
72
75
  - spec/url_shortener/authorize_spec.rb
73
76
  - spec/url_shortener/client_spec.rb
74
77
  - spec/url_shortener/interface_spec.rb
78
+ - spec/url_shortener/response_spec.rb
79
+ - spec/url_shortener/response/shorten_spec.rb
80
+ - spec/url_shortener/response/expand_spec.rb
75
81
  - features/api_calls.feature
76
82
  - features/request_failure.feature
77
83
  - features/support/env.rb