Class: Unshorten

Inherits:
Object
  • Object
show all
Defined in:
lib/unshorten.rb

Overview

Get original URLs from shortened ones.

Constant Summary collapse

CACHE_SIZE_LIMIT =

Cache entities limit

1024
DEFAULT_OPTIONS =

Default options for unshorten

{
  :max_level => 10,
  :timeout => 2,
  :short_hosts => false,
  :short_urls => /^(?:https?:)?\/*[^\/]*\/*[^\/]*$/,
  :use_cache => true,
  :add_missing_http => true
}
@@cache =
{ }

Class Method Summary collapse

Class Method Details

.unshorten(url, options = {}) ⇒ Object Also known as: []

Unshorten a URL

Parameters:

  • url (String)

    A shortened URL

  • options (Hash) (defaults to: {})

    A set of options

Options Hash (options):

  • :max_level (Integer)

    Max redirect times

  • :timeout (Integer)

    Timeout in seconds, for every request

  • :short_hosts (Regexp)

    Hosts that provides short url services, only send requests if host matches this regexp. Set to nil to follow all redirects.

  • :short_urls (Regexp)

    URLs that looks like a short one. Only send requests when the URL match this regexp. Set to nil to follow all redirects.

  • :use_cache (Boolean)

    Use cached result if available

  • :add_missing_http (Boolean)

    add ‘http://’ if missing

Returns:

  • Original url, a url that does not redirect

See Also:



44
45
46
47
48
# File 'lib/unshorten.rb', line 44

def unshorten(url, options = {})
  DEFAULT_OPTIONS.each { |k, v| (options[k] = v) unless options.has_key? k }

  follow(url, options)
end