Class: LongURL::Expander
- Inherits:
-
Object
- Object
- LongURL::Expander
- Defined in:
- lib/longurl/expander.rb
Overview
URL Expander class.
Will use Service and Direct classes to expand an url. Each call to external services is cached to save time and cache object is customizable. You can for example use MemCache for the cache. it will allow different instances of Expander and Service to share the same cache.
Examples
# Simple usage
e = LongURL::Expander.new
e.("http://tinyurl.com/1c2") # => "http://www.google.com"
e.("http://tinyurl.com/blnhsg") # => "http://www.google.com/search?q=number+of+horns+on+a+unicorn&ie=UTF-8"
e.("http://is.gd/iUKg") # => "http://fabien.jakimowicz.com"
# not expandable urls
e.("http://www.linuxfr.org") # => "http://www.linuxfr.org"
# not expandable urls, calling longurl.org only
e.("http://www.linuxfr.org", :direct_resolution => true) # => "http://www.linuxfr.org/pub"
# not expandable urls, direct resolution only
e.direct_resolution("http://www.linuxfr.org") # => "http://www.linuxfr.org/pub"
# MemCache as cache
e = LongURL::Expander.new(:cache => MemCache.new("localhost:11211", :namespace => "LongURL"))
e.("http://is.gd/iUKg") # => "http://fabien.jakimowicz.com"
Exceptions
-
LongURL::InvalidURL : will occurs if given url is nil, empty or invalid
-
LongURL::NetworkError : a network (timeout, host could be reached, …) error occurs
-
LongURL::UnknownError : an unknown error occurs
Instance Attribute Summary collapse
-
#first_only ⇒ Object
Returns the value of attribute first_only.
Instance Method Summary collapse
-
#direct_resolution(url) ⇒ Object
Try to directly resolve url using LongURL::Direct to get final redirection.
-
#expand(url, options = { }) ⇒ Object
Expand given url using LongURL::Service class first and then try a direct_resolution, unless :direct_resolution is set to false in options hash.
-
#expand_each_in(text, options = {}) ⇒ Object
Expand all url in the given string, if an error occurs while expanding url, then the original url is used.
-
#initialize(options = {}) ⇒ Expander
constructor
Initialize a new Expander.
Constructor Details
#initialize(options = {}) ⇒ Expander
Initialize a new Expander.
Options
-
:cache
: define a cache which Expander can use. It must implements [] and []= methods. It can be disabled using false. -
:first_only
: if true, users all-redirects option to the API to fetch only the first redirect instead of following all of them. Useful to identify the original URL that was shortened, not the destination.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/longurl/expander.rb', line 45 def initialize( = {}) # OPTIMIZE : This code is a complete duplicate of cache handling in service. if [:cache].nil? @@cache = Hash.new elsif [:cache] == false @@cache = nil else @@cache = [:cache] end @@service = Service.new(:cache => @@cache) @first_only = [:first_only] end |
Instance Attribute Details
#first_only ⇒ Object
Returns the value of attribute first_only.
36 37 38 |
# File 'lib/longurl/expander.rb', line 36 def first_only @first_only end |
Instance Method Details
#direct_resolution(url) ⇒ Object
Try to directly resolve url using LongURL::Direct to get final redirection. This call is cached.
68 69 70 71 72 73 74 75 |
# File 'lib/longurl/expander.rb', line 68 def direct_resolution(url) # OPTIMIZE : this code is almost identical as the one in service for handling service retrieval. if @@cache @@cache[url] ||= Direct.follow_redirections(url) else Direct.follow_redirections(url) end end |
#expand(url, options = { }) ⇒ Object
Expand given url using LongURL::Service class first and then try a direct_resolution, unless :direct_resolution is set to false in options hash.
60 61 62 63 64 |
# File 'lib/longurl/expander.rb', line 60 def (url, ={ }) @@service.query_supported_service_only url, :first_only => first_only rescue UnsupportedService [:direct_resolution] == false ? raise(UnsupportedService) : direct_resolution(url) end |
#expand_each_in(text, options = {}) ⇒ Object
Expand all url in the given string, if an error occurs while expanding url, then the original url is used. options
accepts same options as expand, see expand for more details.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/longurl/expander.rb', line 79 def (text, = {}) text.gsub(ShortURLMatchRegexp) do |shorturl| begin shorturl rescue InvalidURL, NetworkError, TooManyRedirections, UnknownError, UnsupportedService, JSON::ParserError shorturl end end end |