Module: LongURL

Defined in:
lib/longurl/url.rb,
lib/longurl/direct.rb,
lib/longurl/expand.rb,
lib/longurl/service.rb,
lib/longurl/expander.rb,
lib/longurl/constants.rb,
lib/longurl/exceptions.rb

Defined Under Namespace

Modules: Direct, URL Classes: Expander, InvalidURL, NetworkError, Service, TooManyRedirections, UnknownError, UnsupportedService

Constant Summary collapse

ShortURLMatchRegexp =
/http:\/\/[\/\-_.a-z0-9]+/im
EndPoint =

Urls for longurl

URI.parse("http://api.longurl.org/v2/expand")
ServiceEndPoint =
URI.parse("http://api.longurl.org/v2/services")

Class Method Summary collapse

Class Method Details

.expand(url, options = {}) ⇒ Object

Expand given :url to a longest one. First, expand will try to expand url using longurl.org service. Then, it will try to direct follow redirections on the given url and returns final one.

Options

  • :cache : cache object to use, must implement [] and []= functions.

  • :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.

Types

url is expected to be a String and returns a String with the url.

Examples

# simple expands
LongURL.expand("http://tinyurl.com/1c2")                              # => "http://www.google.com"
LongURL.expand("http://tinyurl.com/blnhsg")                           # => "http://www.google.com/search?q=number+of+horns+on+a+unicorn&ie=UTF-8"
LongURL.expand("http://is.gd/iUKg")                                   # => "http://fabien.jakimowicz.com"

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

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



30
31
32
33
34
35
36
# File 'lib/longurl/expand.rb', line 30

def expand(url, options = {})
  @@expander ||= Expander.new(
    :cache => options[:cache],
    :first_only => options[:first_only]
  )
  @@expander.expand(url)
end