aboutsummaryrefslogtreecommitdiffstats
path: root/lib/attachment_to_html/attachment_to_html.rb
blob: 8675329c4c122594ccff11818609966a3423887c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require 'html'
require 'view'

Dir[File.dirname(__FILE__) + '/adapters/*.rb'].each do |file|
    require file
end

module AttachmentToHTML
    extend self

    def to_html(attachment, opts = {})
        adapter = adapter_for(attachment).new(attachment, opts)

        unless adapter.success?
          adapter = fallback_adapter_for(attachment).new(attachment, opts)
        end

        view = View.new(adapter)
        view.wrapper = 'wrapper_google_embed' if adapter.is_a?(Adapters::GoogleDocsViewer)

        view.render
    end

    private

    def adapter_for(attachment)
        case attachment.content_type
        when 'text/plain' then Adapters::Text
        when 'application/pdf' then Adapters::PDF
        when 'application/rtf' then Adapters::RTF
        else
            fallback_adapter_for(attachment)
        end
    end

    def fallback_adapter_for(attachment)
        if attachment.has_google_docs_viewer?
            Adapters::GoogleDocsViewer
        else
            Adapters::CouldNotConvert
        end
    end
end