aboutsummaryrefslogtreecommitdiffstats
path: root/lib/attachment_to_html/adapters/google_docs_viewer.rb
blob: 991fbb7579fab2c835cd84e0650086665fc779b8 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
module AttachmentToHTML
    module Adapters
        # Renders the attachment in a Google Docs Viewer
        class GoogleDocsViewer

            attr_reader :attachment, :attachment_url

            # Public: Initialize a GoogleDocsViewer converter
            #
            # attachment - the FoiAttachment to convert to HTML
            # opts       - a Hash of options (default: {}):
            #              :attachment_url - a String url to the attachment for
            #                                Google to render (default: nil)
            def initialize(attachment, opts = {})
                @attachment = attachment
                @attachment_url = opts.fetch(:attachment_url, nil)
            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

            # Public: Was the document conversion successful?
            # We can't really tell whether the document conversion has been
            # successful as such; We're assuming that given a correctly
            # constructed iframe (which is tested) that Google will make this
            # Just Work.
            #
            # Returns true
            def success?
                true
            end

            private

            def parse_body
                %Q(<iframe src="#{ protocol }://docs.google.com/viewer?url=#{ attachment_url }&amp;embedded=true" width="100%" height="100%" style="border: none;"></iframe>)
            end

            def protocol
                AlaveteliConfiguration.force_ssl ? 'https' : 'http'
            end

        end
    end
end