Module: ScribdFu::AttachmentFu::InstanceMethods

Defined in:
lib/attachment_fu/methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
# File 'lib/attachment_fu/methods.rb', line 15

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#conversion_complete?Boolean

Responds true if the conversion is complete – note that this gives no indication as to whether the conversion had an error or was succesful, just that the conversion completed.

Note that this method still returns false if the model does not refer to a valid document. scribd_attributes_valid? should be used to determine the validity of the document.

Returns:

  • (Boolean)


131
132
133
# File 'lib/attachment_fu/methods.rb', line 131

def conversion_complete?
  scribd_document && scribd_document.conversion_status != 'PROCESSING'
end

#conversion_error?Boolean

Responds true if there was a conversion error while converting to iPaper.

Note that this method still returns false if the model does not refer to a valid document. scribd_attributes_valid? should be used to determine the validity of the document.

Returns:

  • (Boolean)


150
151
152
# File 'lib/attachment_fu/methods.rb', line 150

def conversion_error?
  scribd_document && scribd_document.conversion_status == 'ERROR'
end

#conversion_successful?Boolean

Responds true if the document has been converted.

Note that this method still returns false if the model does not refer to a valid document. scribd_attributes_valid? should be used to determine the validity of the document.

Returns:

  • (Boolean)


140
141
142
# File 'lib/attachment_fu/methods.rb', line 140

def conversion_successful?
  scribd_document && scribd_document.conversion_status =~ /^DISPLAYABLE|DONE$/
end

#scribd_access_key=(key) ⇒ Object



30
31
32
# File 'lib/attachment_fu/methods.rb', line 30

def scribd_access_key=(key)
  write_attribute :scribd_access_key, key.to_s.strip
end

#scribd_documentObject

Responds the Scribd::Document associated with this model, or nil if it does not exist.



156
157
158
159
160
# File 'lib/attachment_fu/methods.rb', line 156

def scribd_document
  @scribd_document ||= .find_document(scribd_id)
rescue Scribd::ResponseError # at minimum, the document was not found
  nil
end

#scribd_id=(id) ⇒ Object



26
27
28
# File 'lib/attachment_fu/methods.rb', line 26

def scribd_id=(id)
  write_attribute :scribd_id, id.to_s.strip
end

#scribdable?Boolean

Checks whether the attachment is scribdable. This boils down to a check to ensure that the contents of the attachment are of a content type that scribd can understand.

Returns:

  • (Boolean)


22
23
24
# File 'lib/attachment_fu/methods.rb', line 22

def scribdable?
  ScribdFu::CONTENT_TYPES.include?(content_type)
end

#thumbnail_fileObject

Returns the actual image data of a thumbnail for this model’s attachment.

If Scribd does not have a thumbnail for this file, then Attachment_fu’s thumbnanil is fallen back on by returning the file at full_filename(:thumb).

Sample use in a controller:

render :inline => @attachment.thumbnail_file,
       :content_type => 'image/jpeg'


93
94
95
96
97
98
99
100
# File 'lib/attachment_fu/methods.rb', line 93

def thumbnail_file
  path = (scribd_document && scribd_document.thumbnail_url) ||
            full_filename(:thumb)

  open(path).read
rescue Errno::ENOENT
  nil
end

#thumbnail_urlObject

Returns a URL for a thumbnail for this model’s attachment.

If Scribd does not provide a thumbnail URL, then Attachment_fu’s thumbnail is fallen back on by returning the value of public_filename(:thumb).

Sample use in a view:

<%= image_tag(@attachment .thumbnail_url, :alt => @attachment.name) %>


78
79
80
81
# File 'lib/attachment_fu/methods.rb', line 78

def thumbnail_url
  (scribd_document && scribd_document.thumbnail_url) or
    public_filename(:thumb)
end

#upload_to_scribdObject

Uploads the attachment to scribd for processing.. This is called before_validation, as set up by ScribdFu::ClassMethods#extended.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/attachment_fu/methods.rb', line 52

def upload_to_scribd
  if scribdable? and self.scribd_id.blank?
    with_file_path do |file_path|
      if resource = .upload(:file => "#{file_path}",
                                        :access => access_level)
        logger.info "[Scribd_fu] #{Time.now.rfc2822}: Object #{id} successfully uploaded for conversion to iPaper."

        self.scribd_id         = resource.doc_id
        self.scribd_access_key = resource.access_key

        save
      else
        logger.info "[Scribd_fu] #{Time.now.rfc2822}: Object #{id} upload failed!"
      end
    end
  end
end

#with_file_path(&block) ⇒ Object

Yields the correct path to the file, either the local filename or the S3 URL.

This method creates a temporary file of the correct filename if necessary, so as to be able to give scribd the right filename. The file is destroyed when the passed block ends.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/attachment_fu/methods.rb', line 108

def with_file_path(&block) # :yields: full_file_path
  # TODO We can probably do this using respond_to?
  if scribd_config['storage'].eql?('s3')
     yield s3_url
  elsif save_attachment? # file hasn't been saved, use the temp file
    temp_rename = File.join(Dir.tmpdir, filename)
    File.copy(temp_path, temp_rename)

    yield temp_rename
  else
    yield full_filename
  end
ensure
  temp_rename && File.unlink(temp_rename) # always delete this
end