Sha256: 84ed68fe4938a27dc461a5e0292b91dc75aebd9eaf91e2c7872c2e99986373ed

Contents?: true

Size: 718 Bytes

Versions: 1

Compression:

Stored size: 718 Bytes

Contents

class PageDownloader
  class Filters
    class EmbedScript
      attr_reader :fetcher

      def initialize(options = {})
        @fetcher = options.fetch :fetcher
        @url_resolver = options.fetch :url_resolver
      end

      def filter(content)
        content.css("script").select do |script|
          script.attributes["src"]
        end.each do |script|
          embed(script)
        end

        content
      end

      private

      def embed(script)
        html = fetch_content(script.attributes["src"].value)
        script.remove_attribute("src")
        script.add_child(html)
      end

      def fetch_content(url)
        fetcher.fetch(@url_resolver.resolve(url))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
page_downloader-1.0 lib/page_downloader/filters/embed_script.rb