diff options
-rw-r--r-- | lib/attachment_to_html/adapters/google_docs_viewer.rb | 43 | ||||
-rw-r--r-- | spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb | 52 |
2 files changed, 23 insertions, 72 deletions
diff --git a/lib/attachment_to_html/adapters/google_docs_viewer.rb b/lib/attachment_to_html/adapters/google_docs_viewer.rb index 86908ad5c..991fbb757 100644 --- a/lib/attachment_to_html/adapters/google_docs_viewer.rb +++ b/lib/attachment_to_html/adapters/google_docs_viewer.rb @@ -3,28 +3,31 @@ module AttachmentToHTML # Renders the attachment in a Google Docs Viewer class GoogleDocsViewer - attr_reader :attachment, :wrapper, :attachment_url + attr_reader :attachment, :attachment_url - # Public: Initialize a PDF converter + # Public: Initialize a GoogleDocsViewer converter # # attachment - the FoiAttachment to convert to HTML # opts - a Hash of options (default: {}): - # :wrapper - String id of the div that wraps the - # attachment body - # (default: 'wrapper_google_embed') # :attachment_url - a String url to the attachment for # Google to render (default: nil) def initialize(attachment, opts = {}) @attachment = attachment - @wrapper = opts.fetch(:wrapper, 'wrapper_google_embed') @attachment_url = opts.fetch(:attachment_url, nil) end - # Public: Convert the attachment to HTML + # Public: The title to use in the <title> tag # # Returns a String - def to_html - @html ||= generate_html + 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 # Public: Was the document conversion successful? @@ -40,27 +43,7 @@ module AttachmentToHTML private - def generate_html - html = "<!DOCTYPE html>" - html += "<html>" - html += "<head>" - html += "<title>#{ title }</title>" - html += "</head>" - html += "<body>" - html += "<div id=\"#{ wrapper }\">" - html += "<div id=\"view-html-content\">" - html += body - html += "</div>" - html += "</div>" - html += "</body>" - html += "</html>" - end - - def title - @title ||= attachment.display_filename - end - - def body + def parse_body %Q(<iframe src="#{ protocol }://docs.google.com/viewer?url=#{ attachment_url }&embedded=true" width="100%" height="100%" style="border: none;"></iframe>) end diff --git a/spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb b/spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb index 166ca3241..e7aafb40d 100644 --- a/spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb +++ b/spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb @@ -3,67 +3,35 @@ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') describe AttachmentToHTML::Adapters::GoogleDocsViewer do let(:attachment) { FactoryGirl.build(:pdf_attachment) } - let(:google_adapter) do + let(:adapter) do AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, :attachment_url => 'http://example.com/test.pdf') end - describe :wrapper do + describe :title do - it 'defaults to wrapper_google_embed' do - google_adapter.wrapper.should == 'wrapper_google_embed' - end - - it 'accepts a wrapper option' do - google_adapter = AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, :wrapper => 'wrap') - google_adapter.wrapper.should == 'wrap' + it 'uses the attachment filename for the title' do + adapter.title.should == attachment.display_filename end end - describe :to_html do - - it 'should be a valid html document' do - parsed = Nokogiri::HTML.parse(google_adapter.to_html) do |config| - config.strict - end - parsed.errors.any?.should be_false - end - - it 'contains the attachment filename in the title tag' do - parsed = Nokogiri::HTML.parse(google_adapter.to_html) do |config| - config.strict - end - parsed.css('title').inner_html.should == attachment.display_filename - end - - it 'contains the wrapper div in the body tag' do - google_adapter = AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, :wrapper => 'wrap') - parsed = Nokogiri::HTML.parse(google_adapter.to_html) do |config| - config.strict - end - parsed.css('body div').first.attributes['id'].value.should == 'wrap' - end + describe :body do - it 'contains the google docs viewer url in the wrapper div' do - options = { :wrapper => 'wrap', :attachment_url => 'http://example.com/test.pdf' } - google_adapter = AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, options) - parsed = Nokogiri::HTML.parse(google_adapter.to_html) do |config| - config.strict - end + it 'contains the google docs viewer iframe' do expected = %Q(<iframe src="http://docs.google.com/viewer?url=http://example.com/test.pdf&embedded=true" width="100%" height="100%" style="border: none;"></iframe>) - parsed.css('div#wrap div#view-html-content').inner_html.should include(expected) + adapter.body.should == expected end describe 'uses the confugured alaveteli protocol' do it 'https if force_ssl is on' do AlaveteliConfiguration.stub(:force_ssl).and_return(true) - google_adapter.to_html.should include('https://docs.google.com') + adapter.body.should include('https://docs.google.com') end it 'http if force_ssl is off' do AlaveteliConfiguration.stub(:force_ssl).and_return(false) - google_adapter.to_html.should include('http://docs.google.com') + adapter.body.should include('http://docs.google.com') end end @@ -73,7 +41,7 @@ describe AttachmentToHTML::Adapters::GoogleDocsViewer do describe :success? do it 'is always true' do - google_adapter.success?.should be_true + adapter.success?.should be_true end end |