Module: UrlyBird

Defined in:
lib/urlybird.rb,
lib/urlybird/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.seek(content, opts = {}, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/urlybird.rb', line 6

def self.seek(content, opts={}, &block)

  opts = default_opts.merge(opts)

  # find URI matches within a set of schemes (e.g. ['http','mailto']) if
  # provided, nil will return all schemes
  content.gsub(regexp(opts)) do |matched|
    # capture the first captured part in case we're working with a anchor
    # tag
    url_raw = $1

    # check if the current URL is within an anchor
    is_anchor = !!matched.match(/^<\s*a/)

    # if we're not dealing with an anchored URL the whole string match is
    # the raw URL
    url_raw = matched unless is_anchor

    begin
      # create an Addressable::URI object, un-escaping "&amp;" if the URL is
      # within an anchor tag
      url = Addressable::URI.parse(
        is_anchor ? url_raw.gsub('&amp;', '&') : url_raw)

      if block_given? && valid?(url, opts)

        # yield valid URLs
        block.call(url)

        # turn URL back into a string and clone the string due to what seems
        # like internal string caching in Addressable
        url = url.to_s.clone

        # FIXME: Temporary fix to dealing with dollar signs ($) in URLs
        # which in most use cases are required as placeholders
        # and need to remain unencoded
        #
        # Ideally UrlyBird should provide some form of option to unencode
        # specific characters, or simply forcing developers to deal with
        # these kinds of special cases in their apps.
        url.gsub!('%24', '$')

        # escape ampersands (&) in anchor tag URLs
        url.gsub!(/&(?!amp;)/, '&amp;') if is_anchor

        # if we're working with an anchor tag inject the new URL, otherwise
        # just return the new URL as is
        is_anchor ? matched.gsub(url_raw, url) : url
      else
        matched
      end
    rescue Addressable::URI::InvalidURIError
      matched
    end
  end

end