jakimowicz-longurl 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/longurl/expand.rb +1 -1
- data/lib/longurl/expander.rb +3 -3
- data/lib/longurl/service.rb +6 -4
- data/lib/longurl.rb +0 -1
- metadata +1 -1
data/VERSION.yml
CHANGED
data/lib/longurl/expand.rb
CHANGED
@@ -6,7 +6,7 @@ module LongURL
|
|
6
6
|
# First, expand will try to expand url using longurl.org service.
|
7
7
|
# Then, it will try to direct follow redirections on the given url and returns final one.
|
8
8
|
# === Options
|
9
|
-
# * <tt>:cache</tt> : cache object to use, must implement
|
9
|
+
# * <tt>:cache</tt> : cache object to use, must implement [] and []= functions.
|
10
10
|
# === Types
|
11
11
|
# <tt>url</tt> is expected to be a String and returns a String with the url.
|
12
12
|
# === Examples
|
data/lib/longurl/expander.rb
CHANGED
@@ -29,9 +29,9 @@ module LongURL
|
|
29
29
|
class Expander
|
30
30
|
# Initialize a new Expander.
|
31
31
|
# === Options
|
32
|
-
# * <tt>:cache</tt>: define a cache which Expander can use. It must implements
|
32
|
+
# * <tt>:cache</tt>: define a cache which Expander can use. It must implements [] and []= methods.
|
33
33
|
def initialize(options = {})
|
34
|
-
@@cache = options[:cache] ||
|
34
|
+
@@cache = options[:cache] || Hash.new
|
35
35
|
@@service = Service.new(:cache => @@cache)
|
36
36
|
end
|
37
37
|
|
@@ -45,7 +45,7 @@ module LongURL
|
|
45
45
|
# Try to directly resolve url using LongURL::Direct to get final redirection.
|
46
46
|
# This call is cached.
|
47
47
|
def direct_resolution(url)
|
48
|
-
@@cache
|
48
|
+
@@cache[url] ||= Direct.follow_redirections(url)
|
49
49
|
end
|
50
50
|
|
51
51
|
# 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
@@ -11,10 +11,8 @@ module LongURL
|
|
11
11
|
class Service
|
12
12
|
|
13
13
|
def initialize(params = {})
|
14
|
-
@@cache = params[:cache]
|
15
|
-
|
16
|
-
@@supported_services = @@cache.get('supported_services')
|
17
|
-
@@supported_services ||= @@cache.set('supported_services', fetch_supported_services)
|
14
|
+
@@cache = params[:cache] || Hash.new
|
15
|
+
@@supported_services = cached_or_fetch_supported_services
|
18
16
|
end
|
19
17
|
|
20
18
|
def query_supported_service_only(url)
|
@@ -46,6 +44,10 @@ module LongURL
|
|
46
44
|
|
47
45
|
protected
|
48
46
|
|
47
|
+
def cached_or_fetch_supported_services
|
48
|
+
@@cache['supported_services'] ||= fetch_supported_services
|
49
|
+
end
|
50
|
+
|
49
51
|
def check_and_escape(url)
|
50
52
|
check url
|
51
53
|
CGI.escape url
|
data/lib/longurl.rb
CHANGED