longurl 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +2 -2
- data/lib/longurl.rb +1 -0
- data/lib/longurl/direct.rb +7 -2
- 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/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 +11 -4
- data/lib/longurl/cache.rb +0 -6
- data/test/service_test.rb +0 -43
data/VERSION.yml
CHANGED
data/lib/longurl.rb
CHANGED
data/lib/longurl/direct.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
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
|
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 ...)
|
6
11
|
def self.follow_redirections(orig)
|
7
|
-
uri =
|
12
|
+
uri = LongURL::URL.check(orig)
|
8
13
|
Net::HTTP.start(uri.host, uri.port) do |http|
|
9
14
|
answer = http.get(uri.path.empty? ? '/' : uri.path)
|
10
15
|
dest = answer['Location']
|
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/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: longurl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
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-03-
|
12
|
+
date: 2009-03-08 00:00:00 +01:00
|
13
13
|
default_executable: longurl
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -34,15 +34,22 @@ files:
|
|
34
34
|
- VERSION.yml
|
35
35
|
- bin/longurl
|
36
36
|
- lib/longurl
|
37
|
-
- lib/longurl/cache.rb
|
38
37
|
- lib/longurl/constants.rb
|
39
38
|
- lib/longurl/direct.rb
|
40
39
|
- lib/longurl/exceptions.rb
|
41
40
|
- lib/longurl/expand.rb
|
42
41
|
- lib/longurl/expander.rb
|
43
42
|
- lib/longurl/service.rb
|
43
|
+
- lib/longurl/url.rb
|
44
44
|
- lib/longurl.rb
|
45
|
-
- test/
|
45
|
+
- test/cache_mock.rb
|
46
|
+
- test/constants.rb
|
47
|
+
- test/service
|
48
|
+
- test/service/no_cache_service_test.rb
|
49
|
+
- test/service/service_cache_test.rb
|
50
|
+
- test/service/service_test.rb
|
51
|
+
- test/service/supported_services_test.rb
|
52
|
+
- test/url_test.rb
|
46
53
|
has_rdoc: true
|
47
54
|
homepage: http://longurl.rubyforge.org
|
48
55
|
post_install_message:
|
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
|