diff options
author | James McKinney <james@slashpoundbang.com> | 2015-05-20 16:36:51 +0200 |
---|---|---|
committer | James McKinney <james@slashpoundbang.com> | 2015-05-20 16:36:51 +0200 |
commit | f2d08c8ece66f8bac867214a65ab1b7f9fe5da92 (patch) | |
tree | b23cca80ebcf4e33bb774468ca4fbf499e43f7e6 /lib/attachment_to_html/adapter.rb | |
parent | da706b6c05ee7d0be15b59cf0fdec7f6d3ed58ec (diff) |
Move common AttachmentToHTML methods to a superclass
Diffstat (limited to 'lib/attachment_to_html/adapter.rb')
-rw-r--r-- | lib/attachment_to_html/adapter.rb | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/lib/attachment_to_html/adapter.rb b/lib/attachment_to_html/adapter.rb new file mode 100644 index 000000000..d8b9f41f7 --- /dev/null +++ b/lib/attachment_to_html/adapter.rb @@ -0,0 +1,67 @@ +module AttachmentToHTML + class Adapter + attr_reader :attachment + + # Public: Initialize a converter + # + # attachment - the FoiAttachment to convert to HTML + # opts - a Hash of options (default: {}): + # No options currently accepted + def initialize(attachment, opts = {}) + @attachment = attachment + end + + # Public: The title to use in the <title> tag + # + # Returns a String + def title + @title ||= attachment.display_filename + end + + # Public: The contents of the extracted html <body> tag + # + # Returns a String + def body + @body ||= parse_body + end + + def parse_body + convert + end + + # Public: Was the document conversion successful? + # + # Returns true + def success? + true + end + + def has_content? + !body.gsub(/\s+/,"").gsub(/\<[^\>]*\>/, "").empty? + end + + def contains_images? + !!body.match(/<img[^>]*>/mi) + end + + def create_tempfile(text) + tempfile = if RUBY_VERSION.to_f >= 1.9 + Tempfile.new('foiextract', '.', :encoding => text.encoding) + else + Tempfile.new('foiextract', '.') + end + tempfile.print(text) + tempfile.flush + tempfile + end + + def cleanup_tempfile(tempfile) + tempfile.close + tempfile.delete + end + + def attachment_body + @attachment_body ||= attachment.body + end + end +end |