jakimowicz-longurl 0.0.4 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README +40 -0
- data/Rakefile +47 -0
- data/VERSION.yml +2 -2
- data/lib/longurl/direct.rb +11 -4
- data/lib/longurl/exceptions.rb +4 -0
- data/lib/longurl/expand.rb +2 -0
- data/lib/longurl/expander.rb +20 -3
- data/lib/longurl/service.rb +27 -7
- data/lib/longurl/url.rb +18 -0
- data/lib/longurl.rb +1 -0
- data/test/cache_mock.rb +20 -0
- data/test/constants.rb +10 -0
- data/test/service/no_cache_service_test.rb +31 -0
- data/test/service/service_cache_test.rb +34 -0
- data/test/service/service_test.rb +30 -0
- data/test/service/supported_services_test.rb +41 -0
- data/test/url_test.rb +43 -0
- metadata +25 -11
- data/lib/longurl/cache.rb +0 -6
- data/test/service_test.rb +0 -43
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2009, Fabien Jakimowicz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
== LongURL
|
2
|
+
|
3
|
+
LongURL expands short urls (tinyurl, is.gd, ...) to original ones, using on LongURL.org, internal resolution or direct resolution
|
4
|
+
First, expand will try to expand url using longurl.org service.
|
5
|
+
Then, it will try to direct follow redirections on the given url and returns final one.
|
6
|
+
|
7
|
+
=== Options
|
8
|
+
* <tt>:cache</tt> : cache object to use, must implement [] and []= functions.
|
9
|
+
|
10
|
+
=== Types
|
11
|
+
<tt>url</tt> is expected to be a String and returns a String with the url.
|
12
|
+
|
13
|
+
=== Examples
|
14
|
+
# simple expands
|
15
|
+
LongURL.expand("http://tinyurl.com/1c2") # => "http://www.google.com"
|
16
|
+
LongURL.expand("http://tinyurl.com/blnhsg") # => "http://www.google.com/search?q=number+of+horns+on+a+unicorn&ie=UTF-8"
|
17
|
+
LongURL.expand("http://is.gd/iUKg") # => "http://fabien.jakimowicz.com"
|
18
|
+
|
19
|
+
# not expandable urls, without any http call
|
20
|
+
LongURL.expand("http://www.linuxfr.org") # => "http://www.linuxfr.org"
|
21
|
+
|
22
|
+
# Use MemCache
|
23
|
+
LongURL.expand("http://is.gd/iUKg", :cache => MemCache.new("localhost:11211", :namespace => "LongURL"))
|
24
|
+
# => "http://fabien.jakimowicz.com"
|
25
|
+
|
26
|
+
# Expander class
|
27
|
+
expander = LongURL::Expander.new
|
28
|
+
expander.expand("http://tinyurl.com/1c2") # => "http://www.google.com"
|
29
|
+
# not expandable urls, direct resolution only
|
30
|
+
expander.direct_resolution("http://www.linuxfr.org") # => "http://www.linuxfr.org/pub"
|
31
|
+
# not expandable urls, calling longurl.org only
|
32
|
+
expander.expand_with_service_only("http://www.linuxfr.org") # => "http://www.linuxfr.org/pub"
|
33
|
+
# ... with MemCache
|
34
|
+
expander = LongURL::Expander.new(:cache => MemCache.new("localhost:11211", :namespace => "LongURL"))
|
35
|
+
expander.expand("http://tinyurl.com/1c2") # => "http://www.google.com"
|
36
|
+
|
37
|
+
=== Exceptions
|
38
|
+
* LongURL::InvalidURL : will occurs if given url is nil, empty or invalid
|
39
|
+
* LongURL::NetworkError : a network (timeout, host could be reached, ...) error occurs
|
40
|
+
* LongURL::UnknownError : an unknown error occurs
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |s|
|
6
|
+
s.name = "longurl"
|
7
|
+
s.summary = %q{LongURL expands shorten urls (tinyurl, is.gd, ...)}
|
8
|
+
s.homepage = "http://longurl.rubyforge.org"
|
9
|
+
s.description = %q{LongURL expands short urls (tinyurl, is.gd, ...) to original ones, using on LongURL.org, internal resolution or direct resolution}
|
10
|
+
s.authors = ["Fabien Jakimowicz"]
|
11
|
+
s.email = "fabien@jakimowicz.com"
|
12
|
+
s.rubyforge_project = 'longurl'
|
13
|
+
|
14
|
+
s.add_dependency 'json'
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/rdoctask'
|
21
|
+
Rake::RDocTask.new do |rdoc|
|
22
|
+
rdoc.rdoc_dir = 'rdoc'
|
23
|
+
rdoc.title = 'longurl'
|
24
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
25
|
+
rdoc.rdoc_files.include('README*')
|
26
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
27
|
+
end
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |t|
|
31
|
+
t.libs << 'lib' << 'test'
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
33
|
+
t.verbose = false
|
34
|
+
end
|
35
|
+
|
36
|
+
begin
|
37
|
+
require 'rcov/rcovtask'
|
38
|
+
Rcov::RcovTask.new do |t|
|
39
|
+
t.libs << 'test'
|
40
|
+
t.test_files = FileList['test/**/*_test.rb']
|
41
|
+
t.verbose = true
|
42
|
+
end
|
43
|
+
rescue LoadError
|
44
|
+
puts "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
45
|
+
end
|
46
|
+
|
47
|
+
task :default => :test
|
data/VERSION.yml
CHANGED
data/lib/longurl/direct.rb
CHANGED
@@ -1,14 +1,21 @@
|
|
1
|
-
require 'uri'
|
2
1
|
require "net/http"
|
2
|
+
require "longurl/url"
|
3
|
+
require "longurl/exceptions"
|
3
4
|
|
4
5
|
module LongURL
|
5
6
|
module Direct
|
6
|
-
|
7
|
-
|
7
|
+
# Will follow redirections given url <tt>orig</tt>.
|
8
|
+
# === Exceptions
|
9
|
+
# * LongURL::NetworkError in case of a network error (timeout, socket error, ...)
|
10
|
+
# * LongURL::InvalidURL in case of a bad url (nil, empty, not http scheme ...)
|
11
|
+
# * LongURL::TooManyRedirections if there are too many redirection for destination
|
12
|
+
def self.follow_redirections(orig, limit = 5)
|
13
|
+
raise LongURL::TooManyRedirections if limit == 0
|
14
|
+
uri = LongURL::URL.check(orig)
|
8
15
|
Net::HTTP.start(uri.host, uri.port) do |http|
|
9
16
|
answer = http.get(uri.path.empty? ? '/' : uri.path)
|
10
17
|
dest = answer['Location']
|
11
|
-
(dest && dest[0, 7] == 'http://' && follow_redirections(dest)) || orig
|
18
|
+
(dest && dest[0, 7] == 'http://' && follow_redirections(dest, limit - 1)) || orig
|
12
19
|
end
|
13
20
|
rescue Timeout::Error, Errno::ENETUNREACH, Errno::ETIMEDOUT, SocketError
|
14
21
|
raise LongURL::NetworkError
|
data/lib/longurl/exceptions.rb
CHANGED
@@ -14,4 +14,8 @@ module LongURL
|
|
14
14
|
# Raised if a network error occurs : timeout, unreachable network, ...
|
15
15
|
class NetworkError < StandardError
|
16
16
|
end
|
17
|
+
|
18
|
+
# Raised if there are too many redirection in a direct resolution.
|
19
|
+
class TooManyRedirections < StandardError
|
20
|
+
end
|
17
21
|
end
|
data/lib/longurl/expand.rb
CHANGED
data/lib/longurl/expander.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
require "longurl/exceptions"
|
2
|
+
require "longurl/service"
|
3
|
+
require "longurl/direct"
|
4
|
+
|
1
5
|
module LongURL
|
2
6
|
|
3
7
|
# == URL Expander class.
|
@@ -30,9 +34,17 @@ module LongURL
|
|
30
34
|
class Expander
|
31
35
|
# Initialize a new Expander.
|
32
36
|
# === Options
|
33
|
-
# * <tt>:cache</tt>: define a cache which Expander can use.
|
37
|
+
# * <tt>:cache</tt>: define a cache which Expander can use.
|
38
|
+
# It must implements [] and []= methods. It can be disabled using false.
|
34
39
|
def initialize(options = {})
|
35
|
-
|
40
|
+
# OPTIMIZE : This code is a complete duplicate of cache handling in service.
|
41
|
+
if options[:cache].nil?
|
42
|
+
@@cache = Hash.new
|
43
|
+
elsif options[:cache] == false
|
44
|
+
@@cache = nil
|
45
|
+
else
|
46
|
+
@@cache = options[:cache]
|
47
|
+
end
|
36
48
|
@@service = Service.new(:cache => @@cache)
|
37
49
|
end
|
38
50
|
|
@@ -46,7 +58,12 @@ module LongURL
|
|
46
58
|
# Try to directly resolve url using LongURL::Direct to get final redirection.
|
47
59
|
# This call is cached.
|
48
60
|
def direct_resolution(url)
|
49
|
-
|
61
|
+
# OPTIMIZE : this code is almost identical as the one in service for handling service retrieval.
|
62
|
+
if @@cache
|
63
|
+
@@cache[url] ||= Direct.follow_redirections(url)
|
64
|
+
else
|
65
|
+
Direct.follow_redirections(url)
|
66
|
+
end
|
50
67
|
end
|
51
68
|
|
52
69
|
# Expand given url using LongURL::Service only. If given url is not a expandable url, it will still be given to Service.
|
data/lib/longurl/service.rb
CHANGED
@@ -5,20 +5,27 @@ require "rubygems"
|
|
5
5
|
require "json"
|
6
6
|
require "longurl/constants"
|
7
7
|
require "longurl/exceptions"
|
8
|
+
require "longurl/url"
|
8
9
|
|
9
10
|
module LongURL
|
10
11
|
|
11
12
|
class Service
|
12
13
|
|
13
14
|
def initialize(params = {})
|
14
|
-
|
15
|
+
if params[:cache].nil?
|
16
|
+
@@cache = Hash.new
|
17
|
+
elsif params[:cache] == false
|
18
|
+
@@cache = nil
|
19
|
+
else
|
20
|
+
@@cache = params[:cache]
|
21
|
+
end
|
15
22
|
@@supported_services = cached_or_fetch_supported_services
|
16
23
|
end
|
17
24
|
|
18
25
|
def query_supported_service_only(url)
|
19
26
|
check url
|
20
27
|
raise LongURL::UnsupportedService unless service_supported?(url)
|
21
|
-
cached_query url
|
28
|
+
(@@cache && cached_query(url)) || query(url)
|
22
29
|
end
|
23
30
|
|
24
31
|
def cached_query(url)
|
@@ -34,25 +41,38 @@ module LongURL
|
|
34
41
|
raise LongURL::NetworkError
|
35
42
|
end
|
36
43
|
|
44
|
+
# Check among supported services by longurl.org if given <tt>url</tt> is supported.
|
45
|
+
# Returns true if supported, false otherwise.
|
37
46
|
def service_supported?(url)
|
38
47
|
@@supported_services.include? URI.parse(url).host.downcase
|
39
48
|
end
|
40
49
|
|
41
|
-
def check(url)
|
42
|
-
raise LongURL::InvalidURL if url.nil? or url.empty?
|
43
|
-
end
|
44
|
-
|
45
50
|
protected
|
46
51
|
|
52
|
+
# Returns a list of supported services.
|
53
|
+
# Use cache to get the list or fetch it if cache is empty.
|
47
54
|
def cached_or_fetch_supported_services
|
48
|
-
@@cache
|
55
|
+
if @@cache
|
56
|
+
@@cache['supported_services'] ||= fetch_supported_services
|
57
|
+
else
|
58
|
+
fetch_supported_services
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Check given <tt>url</tt> using LongURL::URL.check
|
63
|
+
def check(url)
|
64
|
+
LongURL::URL.check url
|
49
65
|
end
|
50
66
|
|
67
|
+
# Check given url and escape it for http query argument passing.
|
51
68
|
def check_and_escape(url)
|
52
69
|
check url
|
53
70
|
CGI.escape url
|
54
71
|
end
|
55
72
|
|
73
|
+
# Fetch supported services from longurl.org api.
|
74
|
+
# Returns supported services in an Array.
|
75
|
+
# Raises LongURL::NetworkError in case of a network error (timeout, ...)
|
56
76
|
def fetch_supported_services
|
57
77
|
Net::HTTP.start(ServiceEndPoint.host, ServiceEndPoint.port) do |http|
|
58
78
|
response = http.get("#{ServiceEndPoint.path}?format=json")
|
data/lib/longurl/url.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require "longurl/exceptions"
|
3
|
+
|
4
|
+
module LongURL
|
5
|
+
module URL
|
6
|
+
# Check given <tt>url</tt>
|
7
|
+
# Raises LongURL::InvalidURL if <tt>url</tt> is invalid.
|
8
|
+
# Returns a parsed http uri object on success.
|
9
|
+
def self.check(url)
|
10
|
+
raise LongURL::InvalidURL if url.nil? or url.empty?
|
11
|
+
result = URI.parse(url)
|
12
|
+
raise LongURL::InvalidURL unless result.is_a?(URI::HTTP)
|
13
|
+
result
|
14
|
+
rescue URI::InvalidURIError
|
15
|
+
raise LongURL::InvalidURL
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/longurl.rb
CHANGED
data/test/cache_mock.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
class CacheMock
|
2
|
+
|
3
|
+
attr_accessor :keys_stored, :keys_asked, :storage
|
4
|
+
|
5
|
+
def initialize(params = {})
|
6
|
+
@storage = {}
|
7
|
+
@keys_stored = []
|
8
|
+
@keys_asked = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def [](key)
|
12
|
+
@keys_asked << key
|
13
|
+
@storage[key]
|
14
|
+
end
|
15
|
+
|
16
|
+
def []=(key, value)
|
17
|
+
@keys_stored << key
|
18
|
+
@storage[key] = value
|
19
|
+
end
|
20
|
+
end
|
data/test/constants.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
ShortToLong = {
|
2
|
+
:tiny_url => {
|
3
|
+
"http://tinyurl.com/cnuw9a" => "http://fabien.jakimowicz.com",
|
4
|
+
"http://tinyurl.com/blnhsg" => "http://www.google.com/search?q=number+of+horns+on+a+unicorn&ie=UTF-8"
|
5
|
+
},
|
6
|
+
:is_gd => {
|
7
|
+
"http://is.gd/iUKg" => "http://fabien.jakimowicz.com",
|
8
|
+
"http://is.gd/iYCo" => "http://www.google.com/search?q=number+of+horns+on+a+unicorn&ie=UTF-8"
|
9
|
+
}
|
10
|
+
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
$test_lib_dir = File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
$:.unshift($test_lib_dir)
|
3
|
+
|
4
|
+
require "test/unit"
|
5
|
+
require "constants"
|
6
|
+
require "longurl/exceptions"
|
7
|
+
require "longurl/service"
|
8
|
+
|
9
|
+
class TestNoCacheService < Test::Unit::TestCase
|
10
|
+
# OPTIMIZE : all these tests are a plain copy from service_test.rb, we can make something better.
|
11
|
+
|
12
|
+
def setup
|
13
|
+
@service = LongURL::Service.new(:cache => false)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_query_should_raise_invalid_url_if_url_is_nil
|
17
|
+
assert_raise(LongURL::InvalidURL) { @service.query(nil) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_query_should_raise_invalid_url_if_url_is_empty
|
21
|
+
assert_raise(LongURL::InvalidURL) { @service.query('') }
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_query_should_returns_given_url_if_not_shorten_url
|
25
|
+
assert_equal "http://www.google.com", @service.query("http://www.google.com")
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_query_should_returns_expanded_url_for_supported_services
|
29
|
+
ShortToLong.each_value {|service| service.each {|short, long| assert_equal long, @service.query(short)}}
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
$test_lib_dir = File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
$:.unshift($test_lib_dir)
|
3
|
+
|
4
|
+
require "test/unit"
|
5
|
+
require "cache_mock"
|
6
|
+
require "constants"
|
7
|
+
require "longurl/exceptions"
|
8
|
+
require "longurl/service"
|
9
|
+
|
10
|
+
class TestServiceCache < Test::Unit::TestCase
|
11
|
+
|
12
|
+
def setup
|
13
|
+
@cache = CacheMock.new
|
14
|
+
@service = LongURL::Service.new(:cache => @cache)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_query_should_use_cache_before_external_fetch
|
18
|
+
url = ShortToLong[:is_gd].keys.first
|
19
|
+
@service.query_supported_service_only(url)
|
20
|
+
assert_equal ['supported_services', url], @cache.keys_asked
|
21
|
+
@service.query_supported_service_only(url)
|
22
|
+
assert_equal ['supported_services', url, url], @cache.keys_asked
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_query_should_cache_results_from_supported_services
|
26
|
+
ShortToLong.each_value do |service|
|
27
|
+
service.each do |short, long|
|
28
|
+
@service.query_supported_service_only(short)
|
29
|
+
assert @cache.keys_stored.include?(short)
|
30
|
+
assert_equal long, @cache[short]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
$test_lib_dir = File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
$:.unshift($test_lib_dir)
|
3
|
+
|
4
|
+
require "test/unit"
|
5
|
+
require "constants"
|
6
|
+
require "longurl/exceptions"
|
7
|
+
require "longurl/service"
|
8
|
+
|
9
|
+
class TestService < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def setup
|
12
|
+
@service = LongURL::Service.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_query_should_raise_invalid_url_if_url_is_nil
|
16
|
+
assert_raise(LongURL::InvalidURL) { @service.query(nil) }
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_query_should_raise_invalid_url_if_url_is_empty
|
20
|
+
assert_raise(LongURL::InvalidURL) { @service.query('') }
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_query_should_returns_given_url_if_not_shorten_url
|
24
|
+
assert_equal "http://www.google.com", @service.query("http://www.google.com")
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_query_should_returns_expanded_url_for_supported_services
|
28
|
+
ShortToLong.each_value {|service| service.each {|short, long| assert_equal long, @service.query(short)}}
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
$test_lib_dir = File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
$:.unshift($test_lib_dir)
|
3
|
+
|
4
|
+
require "test/unit"
|
5
|
+
require "cache_mock"
|
6
|
+
require "constants"
|
7
|
+
require "longurl/exceptions"
|
8
|
+
require "longurl/service"
|
9
|
+
|
10
|
+
class TestSupportedServices < Test::Unit::TestCase
|
11
|
+
|
12
|
+
def setup
|
13
|
+
@cache = CacheMock.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_service_should_check_if_available_services_are_in_cache
|
17
|
+
assert_equal [], @cache.keys_asked
|
18
|
+
@service = LongURL::Service.new(:cache => @cache)
|
19
|
+
assert_equal ['supported_services'], @cache.keys_asked
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_service_should_store_available_services_in_cache
|
23
|
+
assert_equal [], @cache.keys_stored
|
24
|
+
@service = LongURL::Service.new(:cache => @cache)
|
25
|
+
assert_equal ['supported_services'], @cache.keys_stored
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_supported_services_stored_in_cache_should_be_a_flat_array_of_strings
|
29
|
+
@service = LongURL::Service.new(:cache => @cache)
|
30
|
+
assert_kind_of Array, @cache['supported_services']
|
31
|
+
assert @cache['supported_services'].all? {|object| object.is_a?(String)}
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_service_should_use_supported_services_stored_in_cache_if_available
|
35
|
+
@cache['supported_services'] = ['bleh.com', 'bli.com']
|
36
|
+
@service = LongURL::Service.new(:cache => @cache)
|
37
|
+
assert_equal ['supported_services'], @cache.keys_asked
|
38
|
+
assert_equal ['supported_services'], @cache.keys_stored
|
39
|
+
assert_raise(LongURL::UnsupportedService) { @service.query_supported_service_only(ShortToLong[:is_gd].keys.first) }
|
40
|
+
end
|
41
|
+
end
|
data/test/url_test.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
$test_lib_dir = File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
$:.unshift($test_lib_dir)
|
3
|
+
|
4
|
+
require "test/unit"
|
5
|
+
require "uri"
|
6
|
+
require "longurl/exceptions"
|
7
|
+
require "longurl/url"
|
8
|
+
|
9
|
+
class TestURL < Test::Unit::TestCase
|
10
|
+
|
11
|
+
BadURLs = ["http:",
|
12
|
+
"http://",
|
13
|
+
"http://hoth.entp..."]
|
14
|
+
|
15
|
+
NotHTTPURLs = ["bleh",
|
16
|
+
"bleh://",
|
17
|
+
"bleh://asfd.com",
|
18
|
+
"ftp://asdf.com",
|
19
|
+
"google.com",
|
20
|
+
"asdf@toto.com"]
|
21
|
+
|
22
|
+
GoodURL = "http://www.google.com"
|
23
|
+
|
24
|
+
def test_check_should_raise_invalid_url_if_url_is_nil
|
25
|
+
assert_raise(LongURL::InvalidURL) { LongURL::URL.check nil }
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_check_should_raise_invalid_url_if_url_is_empty
|
29
|
+
assert_raise(LongURL::InvalidURL) { LongURL::URL.check "" }
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_check_should_raise_invalid_url_if_url_is_invalid
|
33
|
+
BadURLs.each {|bad_url| assert_raise(LongURL::InvalidURL) { LongURL::URL.check bad_url } }
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_check_should_raise_invalid_url_if_url_is_not_http
|
37
|
+
NotHTTPURLs.each {|bad_url| assert_raise(LongURL::InvalidURL) { LongURL::URL.check bad_url } }
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_check_should_returns_parsed_url_on_success
|
41
|
+
assert_equal URI.parse(GoodURL), LongURL::URL.check(GoodURL)
|
42
|
+
end
|
43
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jakimowicz-longurl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fabien Jakimowicz
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-04-30 00:00:00 -07:00
|
13
13
|
default_executable: longurl
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -28,26 +28,34 @@ executables:
|
|
28
28
|
- longurl
|
29
29
|
extensions: []
|
30
30
|
|
31
|
-
extra_rdoc_files:
|
32
|
-
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README
|
33
34
|
files:
|
35
|
+
- LICENSE
|
36
|
+
- Rakefile
|
34
37
|
- VERSION.yml
|
35
38
|
- bin/longurl
|
36
|
-
- lib/longurl
|
37
|
-
- lib/longurl/cache.rb
|
39
|
+
- lib/longurl.rb
|
38
40
|
- lib/longurl/constants.rb
|
39
41
|
- lib/longurl/direct.rb
|
40
42
|
- lib/longurl/exceptions.rb
|
41
43
|
- lib/longurl/expand.rb
|
42
44
|
- lib/longurl/expander.rb
|
43
45
|
- lib/longurl/service.rb
|
44
|
-
- lib/longurl.rb
|
45
|
-
- test/
|
46
|
+
- lib/longurl/url.rb
|
47
|
+
- test/cache_mock.rb
|
48
|
+
- test/constants.rb
|
49
|
+
- test/service/no_cache_service_test.rb
|
50
|
+
- test/service/service_cache_test.rb
|
51
|
+
- test/service/service_test.rb
|
52
|
+
- test/service/supported_services_test.rb
|
53
|
+
- test/url_test.rb
|
54
|
+
- README
|
46
55
|
has_rdoc: true
|
47
56
|
homepage: http://longurl.rubyforge.org
|
48
57
|
post_install_message:
|
49
58
|
rdoc_options:
|
50
|
-
- --inline-source
|
51
59
|
- --charset=UTF-8
|
52
60
|
require_paths:
|
53
61
|
- lib
|
@@ -70,5 +78,11 @@ rubygems_version: 1.2.0
|
|
70
78
|
signing_key:
|
71
79
|
specification_version: 2
|
72
80
|
summary: LongURL expands shorten urls (tinyurl, is.gd, ...)
|
73
|
-
test_files:
|
74
|
-
|
81
|
+
test_files:
|
82
|
+
- test/cache_mock.rb
|
83
|
+
- test/constants.rb
|
84
|
+
- test/service/no_cache_service_test.rb
|
85
|
+
- test/service/service_cache_test.rb
|
86
|
+
- test/service/service_test.rb
|
87
|
+
- test/service/supported_services_test.rb
|
88
|
+
- test/url_test.rb
|
data/lib/longurl/cache.rb
DELETED
data/test/service_test.rb
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
$test_lib_dir = File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
-
$:.unshift($test_lib_dir)
|
3
|
-
|
4
|
-
require "test/unit"
|
5
|
-
require "longurl/exceptions"
|
6
|
-
require "longurl/service"
|
7
|
-
|
8
|
-
class TestService < Test::Unit::TestCase
|
9
|
-
|
10
|
-
ShortToLong = {:tiny_url => {
|
11
|
-
"http://tinyurl.com/cnuw9a" => "http://fabien.jakimowicz.com",
|
12
|
-
"http://tinyurl.com/blnhsg" => "http://www.google.com/search?q=number+of+horns+on+a+unicorn&ie=UTF-8"
|
13
|
-
},
|
14
|
-
:is_gd => {
|
15
|
-
"http://is.gd/iUKg" => "http://fabien.jakimowicz.com",
|
16
|
-
"http://is.gd/iYCo" => "http://www.google.com/search?q=number+of+horns+on+a+unicorn&ie=UTF-8"
|
17
|
-
}
|
18
|
-
}
|
19
|
-
|
20
|
-
def setup
|
21
|
-
@service = LongURL::Service.new
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_query_should_raise_invalid_url_if_url_is_nil
|
25
|
-
assert_raise(LongURL::InvalidURL) { @service.query(nil) }
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_query_should_raise_invalid_url_if_url_is_empty
|
29
|
-
assert_raise(LongURL::InvalidURL) { @service.query('') }
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_query_should_returns_given_url_if_not_shorten_url
|
33
|
-
assert_equal "http://www.google.com", @service.query("http://www.google.com")
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_query_should_returns_expanded_url_for_tiny_url
|
37
|
-
ShortToLong[:tiny_url].each {|short, long| assert_equal long, @service.query(short)}
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_query_should_returns_expanded_url_for_is_gd
|
41
|
-
ShortToLong[:is_gd].each {|short, long| assert_equal long, @service.query(short)}
|
42
|
-
end
|
43
|
-
end
|