Module: Garcon::UrlHelper

Extended by:
UrlHelper
Included in:
Provider, Resource, UrlHelper
Defined in:
lib/garcon/utility/url_helper.rb

Overview

Class methods that are added when you include Garcon

Instance Method Summary collapse

Instance Method Details

#unshorten(url, opts = {}) ⇒ Object

Unshorten a shortened URL

Parameters:

  • url (String)

    A shortened URL

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

Options Hash (opts):

  • :max_level (Integer)

    max redirect times

  • :timeout (Integer)

    timeout in seconds, for every request

  • :use_cache (Boolean)

    use cached result if available

Returns:

  • Original url, a url that does not redirects



57
58
59
60
61
62
63
64
65
# File 'lib/garcon/utility/url_helper.rb', line 57

def unshorten(url, opts= {})
  options = {
    max_level: opts.fetch(:max_level,   10),
    timeout:   opts.fetch(:timeout,      2),
    use_cache: opts.fetch(:use_cache, true)
  }
  url = (url =~ /^https?:/i) ? url : "http://#{url}"
  __unshorten__(url, options)
end

#uri_join(*paths) ⇒ URI

Return a cleanly join URI/URL segments into a cleanly normalized URL that the libraries can use when constructing URIs. URI.join is pure evil.

Parameters:

Returns:

  • (URI)


36
37
38
39
40
41
42
# File 'lib/garcon/utility/url_helper.rb', line 36

def uri_join(*paths)
  return nil if paths.length == 0
  leadingslash = paths[0][0] == '/' ? '/' : ''
  trailingslash = paths[-1][-1] == '/' ? '/' : ''
  paths.map! { |path| path.sub(/^\/+/, '').sub(/\/+$/, '') }
  leadingslash + paths.join('/') + trailingslash
end