Class: ContentUrls::JavaScriptParser
- Inherits:
-
Object
- Object
- ContentUrls::JavaScriptParser
- 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
-
.rewrite_each_url(content, &block) ⇒ Object
Rewrites each URL in the JavaScript content by calling the supplied block with each URL.
-
.urls(content) ⇒ Array
Returns the URLs found in the JavaScript content.
Class Method Details
.rewrite_each_url(content, &block) ⇒ Object
Rewrites each URL in the JavaScript content by calling the supplied block with each URL.
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.
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 |