mysociety/alaveteli

View on GitHub
lib/attachment_to_html/adapter.rb

Summary

Maintainability
A
0 mins
Test Coverage
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 = Tempfile.new('foiextract', '.', encoding: text.encoding)
      tempfile.print(text)
      tempfile.flush
      tempfile
    end

    def cleanup_tempfile(tempfile)
      tempfile.close
      tempfile.delete
    end

    def attachment_body
      @attachment_body ||= attachment.default_body
    end
  end
end