Class: DocmagoClient::HTMLResourceArchiver
- Inherits:
-
Object
- Object
- DocmagoClient::HTMLResourceArchiver
- Defined in:
- lib/docmago_client/html_resource_archiver.rb
Constant Summary collapse
- SPROCKETS_RX =
%r{^/assets/}- URI_RX =
/url\(("([^"]*)"|'([^']*)'|([^)]*))\)/im
Instance Method Summary collapse
- #create_zip(file_path) ⇒ Object
-
#initialize(options = {}) ⇒ HTMLResourceArchiver
constructor
A new instance of HTMLResourceArchiver.
Constructor Details
#initialize(options = {}) ⇒ HTMLResourceArchiver
Returns a new instance of HTMLResourceArchiver.
19 20 21 22 23 24 25 |
# File 'lib/docmago_client/html_resource_archiver.rb', line 19 def initialize( = {}) @html = [:content] @base_path = [:resource_path] @assets = [:assets] @doc = Nokogiri::HTML(@html) end |
Instance Method Details
#create_zip(file_path) ⇒ Object
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 |
# File 'lib/docmago_client/html_resource_archiver.rb', line 27 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 = 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 |