aboutsummaryrefslogtreecommitdiffstats
path: root/lib/attachment_to_html/attachment_to_html.rb
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2014-05-15 17:01:22 +0100
committerLouise Crow <louise.crow@gmail.com>2014-05-15 17:01:22 +0100
commitbb5f95040d377d86629012347343fbf2c7dda016 (patch)
treebe696ec2615b098db34449a982fcb09a68c9c49b /lib/attachment_to_html/attachment_to_html.rb
parent6d215fba5cc709c43f12f86da39a643e4be5922b (diff)
parent21027d0d1197e7ac447296ab68a25159860888b2 (diff)
Merge remote-tracking branch 'origin/release/0.18'0.18
Diffstat (limited to 'lib/attachment_to_html/attachment_to_html.rb')
-rw-r--r--lib/attachment_to_html/attachment_to_html.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/attachment_to_html/attachment_to_html.rb b/lib/attachment_to_html/attachment_to_html.rb
new file mode 100644
index 000000000..2f7c08264
--- /dev/null
+++ b/lib/attachment_to_html/attachment_to_html.rb
@@ -0,0 +1,46 @@
+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 do
+ opts.fetch(:content_for, []).each do |k,v|
+ inject_content(k) { v }
+ end
+ end
+ 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