class DocmagoClient::HTMLResourceArchiver
Constants
- SPROCKETS_RX
- URI_RX
Public Class Methods
new(options = {})
click to toggle source
# File lib/docmago_client/html_resource_archiver.rb, line 12 def initialize(options = {}) @html = options[:content] @base_path = options[:resource_path] @assets = options[:assets] @doc = Nokogiri::HTML(@html) end
Public Instance Methods
create_zip(file_path)
click to toggle source
# File lib/docmago_client/html_resource_archiver.rb, line 20 def create_zip(file_path) Zip::File.open(file_path, Zip::File::CREATE) do |zipfile| zipfile.get_output_stream('document.html') { |f| f.write @html } fetch_uris.each do |uri| uri = Addressable::URI.parse uri.to_s.strip path_digest = Digest::MD5.hexdigest(uri.to_s) file_data = URI.open(uri).read if uri.absolute? file_data ||= File.read(resolve_uri(uri)) if File.exist?(resolve_uri(uri)) file_data ||= @assets[normalize_uri(uri).gsub(SPROCKETS_RX, '')].to_s if File.extname(normalize_uri(uri)) == '.css' # embed resources within css file_data.scan(URI_RX).flatten.compact.uniq.each do |resource| resource_uri = Addressable::URI.parse resource.to_s.strip resource_path_digest = Digest::MD5.hexdigest(resource_uri.to_s) resource_file = open(resource_uri).read if resource_uri.absolute? resource_file = File.read(resolve_uri(resource_uri)) if File.exist?(resolve_uri(resource_uri)) resource_file ||= @assets[normalize_uri(resource_uri).gsub(SPROCKETS_RX, '')].to_s zipfile.get_output_stream(resource_path_digest) { |f| f.write resource_file } if resource_file end end zipfile.get_output_stream(path_digest) { |f| f.write file_data } if file_data end end file_path end
Private Instance Methods
fetch_uris()
click to toggle source
# File lib/docmago_client/html_resource_archiver.rb, line 55 def fetch_uris @doc.xpath("//link[@rel='stylesheet']/@href", '//img/@src') end
normalize_uri(uri)
click to toggle source
# File lib/docmago_client/html_resource_archiver.rb, line 59 def normalize_uri(uri) uri = Addressable::URI.parse uri.to_s.strip uri.query = nil uri.to_s end
resolve_uri(uri)
click to toggle source
# File lib/docmago_client/html_resource_archiver.rb, line 65 def resolve_uri(uri) File.join File.expand_path(@base_path), normalize_uri(uri) end