jakimowicz-longurl 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 1
2
+ :patch: 2
3
3
  :major: 0
4
4
  :minor: 0
@@ -0,0 +1,6 @@
1
+ module LongURL
2
+ class Cache < Hash
3
+ alias :get :[]
4
+ alias :set :[]=
5
+ end
6
+ end
@@ -1,13 +1,17 @@
1
1
  module LongURL
2
2
  # Raised by LongURL::Service class if longurl.org service returns unsupported service error.
3
- class UnsupportedService < Exception
3
+ class UnsupportedService < StandardError
4
4
  end
5
5
 
6
6
  # Raised by LongURL::Service class if longurl.org service returns a not supported answer.
7
- class UnknownError < Exception
7
+ class UnknownError < StandardError
8
8
  end
9
9
 
10
10
  # Raised by LongURL::Service if supplied url is invalid (nil, empty, ...)
11
- class InvalidURL < Exception
11
+ class InvalidURL < StandardError
12
+ end
13
+
14
+ # Raised if a network error occurs : timeout, unreachable network, ...
15
+ class NetworkError < StandardError
12
16
  end
13
17
  end
@@ -0,0 +1,30 @@
1
+ module LongURL
2
+
3
+ class << self
4
+
5
+ # Expand given <tt>:url</tt> to a longest one.
6
+ # First, expand will try to expand url using longurl.org service.
7
+ # Then, it will try to direct follow redirections on the given url and returns final one.
8
+ # === Options
9
+ # * <tt>:cache</tt> : cache object to use, must implement get and set functions. See LongURL::Cache.
10
+ # === Types
11
+ # <tt>url</tt> is expected to be a String and returns a String with the url.
12
+ # === Examples
13
+ # # simple expands
14
+ # LongURL.expand("http://tinyurl.com/1c2") # => "http://www.google.com"
15
+ # LongURL.expand("http://tinyurl.com/blnhsg") # => "http://www.google.com/search?q=number+of+horns+on+a+unicorn&ie=UTF-8"
16
+ # LongURL.expand("http://is.gd/iUKg") # => "http://fabien.jakimowicz.com"
17
+ #
18
+ # # not expandable urls
19
+ # LongURL.expand("http://www.linuxfr.org") # => "http://www.linuxfr.org"
20
+ #
21
+ # === Exceptions
22
+ # * LongURL::InvalidURL : will occurs if given url is nil, empty or invalid
23
+ # * LongURL::UnknownError : an unknown error occurs
24
+ def expand(url, options = {})
25
+ @@expander ||= Expander.new(:cache => options[:cache])
26
+ @@expander.expand(url)
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,56 @@
1
+ module LongURL
2
+
3
+ # == URL Expander class.
4
+ # Will use Service and Direct classes to expand an url.
5
+ # Each call to external services is cached to save time and cache object is customizable.
6
+ # You can for example use MemCache for the cache. it will allow different instances of Expander and Service to share the same cache.
7
+ # === Examples
8
+ # # Simple usage
9
+ # e = LongURL::Expander.new
10
+ # e.expand("http://tinyurl.com/1c2") # => "http://www.google.com"
11
+ # e.expand("http://tinyurl.com/blnhsg") # => "http://www.google.com/search?q=number+of+horns+on+a+unicorn&ie=UTF-8"
12
+ # e.expand("http://is.gd/iUKg") # => "http://fabien.jakimowicz.com"
13
+ #
14
+ # # not expandable urls
15
+ # e.expand("http://www.linuxfr.org") # => "http://www.linuxfr.org"
16
+ #
17
+ # # not expandable urls, calling longurl.org only
18
+ # e.expand_with_service_only("http://www.linuxfr.org") # => "http://www.linuxfr.org/pub"
19
+ #
20
+ # # not expandable urls, direct resolution only
21
+ # e.direct_resolution("http://www.linuxfr.org") # => "http://www.linuxfr.org/pub"
22
+ #
23
+ # # MemCache as cache
24
+ # e = LongURL::Expander.new(:cache => MemCache.new("localhost:11211", :namespace => "LongURL"))
25
+ # e.expand("http://is.gd/iUKg") # => "http://fabien.jakimowicz.com"
26
+ # === Exceptions
27
+ # * LongURL::InvalidURL : will occurs if given url is nil, empty or invalid
28
+ # * LongURL::UnknownError : an unknown error occurs
29
+ class Expander
30
+ # Initialize a new Expander.
31
+ # === Options
32
+ # * <tt>:cache</tt>: define a cache which Expander can use. It must implements set and get methods like LongURL::Cache.
33
+ def initialize(options = {})
34
+ @@cache = options[:cache] || LongURL::Cache.new
35
+ @@service = Service.new(:cache => @@cache)
36
+ end
37
+
38
+ # Expand given url using LongURL::Service class first and then try a direct_resolution.
39
+ def expand(url)
40
+ @@service.query_supported_service_only url
41
+ rescue UnsupportedService
42
+ direct_resolution url
43
+ end
44
+
45
+ # Try to directly resolve url using LongURL::Direct to get final redirection.
46
+ # This call is cached.
47
+ def direct_resolution(url)
48
+ @@cache.get(url) || @@cache.set(url, Direct.follow_redirections(url))
49
+ end
50
+
51
+ # Expand given url using LongURL::Service only. If given url is not a expandable url, it will still be given to Service.
52
+ def expand_with_service_only(url)
53
+ @@service.query url
54
+ end
55
+ end
56
+ end
@@ -10,14 +10,21 @@ module LongURL
10
10
 
11
11
  class Service
12
12
 
13
- def initialize
14
- @@supported_services = fetch_supported_services
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)
15
18
  end
16
19
 
17
20
  def query_supported_service_only(url)
18
21
  check url
19
22
  raise LongURL::UnsupportedService unless service_supported?(url)
20
- query url
23
+ cached_query url
24
+ end
25
+
26
+ def cached_query(url)
27
+ @@cache.get(url) || @@cache.set(url, query(url))
21
28
  end
22
29
 
23
30
  def query(url)
@@ -25,6 +32,8 @@ module LongURL
25
32
  Net::HTTP.start(EndPoint.host, EndPoint.port) do |http|
26
33
  handle_response http.get("#{EndPoint.path}?format=json&url=#{escaped_url}")
27
34
  end
35
+ rescue Timeout::Error, Errno::ENETUNREACH
36
+ raise LongURL::NetworkError
28
37
  end
29
38
 
30
39
  def service_supported?(url)
@@ -48,6 +57,8 @@ module LongURL
48
57
  parsed = JSON.parse(response.body)
49
58
  parsed.values.flatten
50
59
  end
60
+ rescue Timeout::Error, Errno::ENETUNREACH
61
+ raise LongURL::NetworkError
51
62
  end
52
63
 
53
64
  def handle_response(response)
data/lib/longurl.rb CHANGED
@@ -6,46 +6,6 @@ require 'longurl/constants'
6
6
  require 'longurl/exceptions'
7
7
  require 'longurl/service'
8
8
  require 'longurl/direct'
9
-
10
- module LongURL
11
-
12
- class << self
13
-
14
- # Expand given <tt>:url</tt> to a longest one.
15
- # First, expand will try to expand url using longurl.org service.
16
- # Then, it will try to direct follow redirections on the given url and returns final one.
17
- # === Options
18
- # * <tt>:try_unsupported</tt> : use longurl.org to resolve the address, even if it is not a supported service
19
- # * <tt>:direct_resolution</tt> : fetch url and follow redirection to find the final destination
20
- # === Types
21
- # <tt>url</tt> is expected to be a String and returns a String with the url.
22
- # === Examples
23
- # # simple expands
24
- # LongURL.expand("http://tinyurl.com/1c2") # => "http://www.google.com"
25
- # LongURL.expand("http://tinyurl.com/blnhsg") # => "http://www.google.com/search?q=number+of+horns+on+a+unicorn&ie=UTF-8"
26
- # LongURL.expand("http://is.gd/iUKg") # => "http://fabien.jakimowicz.com"
27
- #
28
- # # not expandable urls, without any http call
29
- # LongURL.expand("http://www.linuxfr.org") # => "http://www.linuxfr.org"
30
- #
31
- # # not expandable urls, calling longurl.org only
32
- # LongURL.expand("http://www.linuxfr.org", :try_unsupported => true) # => "http://www.linuxfr.org/pub"
33
- #
34
- # # not expandable urls, direct resolution only
35
- # LongURL.expand("http://www.linuxfr.org", :direct_resolution => true) # => "http://www.linuxfr.org/pub"
36
- #
37
- # # not expandable urls, longurl.org and direct resolution
38
- # LongURL.expand("http://www.linuxfr.org",
39
- # :try_unsupported => true, :direct_resolution => true) # => "http://www.linuxfr.org/pub"
40
- # === Exceptions
41
- # * LongURL::InvalidURL : will occurs if given url is nil, empty or invalid
42
- # * LongURL::UnknownError : an unknown error occurs
43
- def expand(url, options = {})
44
- @@service ||= Service.new
45
- options[:try_unsupported] ? @@service.query(url) : @@service.query_supported_service_only(url)
46
- rescue UnsupportedService
47
- options[:direct_resolution] ? Direct.follow_redirections(url) : url
48
- end
49
- end
50
-
51
- end
9
+ require 'longurl/cache'
10
+ require 'longurl/expander'
11
+ require 'longurl/expand'
data/test/service_test.rb CHANGED
@@ -1,7 +1,3 @@
1
- # tc_service.rb
2
- #
3
- # Created by Vincent Foley on 2005-06-01
4
-
5
1
  $test_lib_dir = File.join(File.dirname(__FILE__), "..", "lib")
6
2
  $:.unshift($test_lib_dir)
7
3
 
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.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabien Jakimowicz
@@ -9,11 +9,12 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-10 00:00:00 -08:00
12
+ date: 2009-02-23 00:00:00 -08:00
13
13
  default_executable: longurl
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
17
+ type: :runtime
17
18
  version_requirement:
18
19
  version_requirements: !ruby/object:Gem::Requirement
19
20
  requirements:
@@ -33,9 +34,12 @@ files:
33
34
  - VERSION.yml
34
35
  - bin/longurl
35
36
  - lib/longurl
37
+ - lib/longurl/cache.rb
36
38
  - lib/longurl/constants.rb
37
39
  - lib/longurl/direct.rb
38
40
  - lib/longurl/exceptions.rb
41
+ - lib/longurl/expand.rb
42
+ - lib/longurl/expander.rb
39
43
  - lib/longurl/service.rb
40
44
  - lib/longurl.rb
41
45
  - test/service_test.rb