Class: ContentUrls::JavaScriptParser

Inherits:
Object
  • Object
show all
Defined in:
lib/content_urls/parsers/java_script_parser.rb

Overview

JavaScriptParser finds and rewrites URLs in JavaScript content.

Implementation note:

This methods in this class identify URLs by locating strings which match URI‘s regexp.

Class Method Summary collapse

Class Method Details

.rewrite_each_url(content, &block) ⇒ Object

Rewrites each URL in the JavaScript content by calling the supplied block with each URL.

Examples:

Rewrite URLs in JavaScript code

javascript = 'var link="http://example.com/"'
javascript = ContentUrls::JavaScriptParser.rewrite_each_url(javascript) {|url| url.upcase}
puts "Rewritten: #{javascript}"
# => "Rewritten: var link="HTTP://EXAMPLE.COM/""


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/content_urls/parsers/java_script_parser.rb', line 39

def self.rewrite_each_url(content, &block)
  done = false
  remaining = content
  rewritten = ''
  while ! remaining.empty?
    if match = URI.regexp.match(remaining)
      url = match.to_s
      rewritten += match.pre_match
      replacement = url.nil? ? nil : (yield url)
      if replacement.nil? or replacement == url  # no change in URL
        rewritten += url
      else
        rewritten += replacement
      end
      remaining = match.post_match
    else
      rewritten += remaining
      remaining = ''
    end
  end
  return rewritten
end

.urls(content) ⇒ Array

Returns the URLs found in the JavaScript content.

Examples:

Parse JavaScript code for URLs

javascript = 'var link="http://example.com/"'
ContentUrls::JavaScriptParser.urls(javascript).each do |url|
  puts "Found URL: #{url}"
end
# => "Found URL: http://example.com/"


22
23
24
25
26
27
# File 'lib/content_urls/parsers/java_script_parser.rb', line 22

def self.urls(content)
  urls = []
  URI.extract(content).each { |u| urls << u }
  urls.uniq!
  urls
end