Parent

LongURL::Expander

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.expand("http://tinyurl.com/1c2")                              # => "http://www.google.com"
  e.expand("http://tinyurl.com/blnhsg")                           # => "http://www.google.com/search?q=number+of+horns+on+a+unicorn&ie=UTF-8"
  e.expand("http://is.gd/iUKg")                                   # => "http://fabien.jakimowicz.com"

  # not expandable urls
  e.expand("http://www.linuxfr.org")                              # => "http://www.linuxfr.org"

  # not expandable urls, calling longurl.org only
  e.expand("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.expand("http://is.gd/iUKg")                                   # => "http://fabien.jakimowicz.com"

Exceptions

Public Class Methods

new(options = {}) click to toggle source

Initialize a new Expander.

Options

  • :cache: define a cache which Expander can use. It must implements [] and []= methods. It can be disabled using false.
# File lib/longurl/expander.rb, line 39
    def initialize(options = {})
      # OPTIMIZE : This code is a complete duplicate of cache handling in service.
      if options[:cache].nil?
        @@cache = Hash.new
      elsif options[:cache] == false
        @@cache = nil
      else
        @@cache = options[:cache]
      end
      @@service = Service.new(:cache => @@cache)
    end

Public Instance Methods

direct_resolution(url) click to toggle source

Try to directly resolve url using LongURL::Direct to get final redirection. This call is cached.

# File lib/longurl/expander.rb, line 61
    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 = {}) click to toggle source

Expand given url using LongURL::Service class first and then try a direct_resolution, unless :direct_resolution is set to false in options hash.

# File lib/longurl/expander.rb, line 53
    def expand(url, options = {})
      @@service.query_supported_service_only url
    rescue UnsupportedService
      options[:direct_resolution] == false ? raise(UnsupportedService) : direct_resolution(url)
    end
expand_each_in(text, options = {}) click to toggle source

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.

# File lib/longurl/expander.rb, line 72
    def expand_each_in(text, options = {})
      text.gsub(ShortURLMatchRegexp) do |shorturl| 
        begin
          expand shorturl, options
        rescue  InvalidURL,
                NetworkError,
                TooManyRedirections,
                UnknownError,
                UnsupportedService,
                JSON::ParserError
          shorturl
        end
      end
    end

Disabled; run with $DEBUG to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.