Module: ScribdFu::Paperclip::InstanceMethods

Defined in:
lib/paperclip/methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



56
57
58
# File 'lib/paperclip/methods.rb', line 56

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

Instance Method Details

#conversion_complete?(attribute) ⇒ Boolean

Responds true if the conversion is complete for the given attribute – note that this gives no indication as to whether the conversion had an error or was succesful, just that the conversion completed. See conversion_successful? for that information.

Note also 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)


155
156
157
158
159
# File 'lib/paperclip/methods.rb', line 155

def conversion_complete?(attribute)
  doc = scribd_document_for(attribute)

  doc && doc.conversion_status != 'PROCESSING'
end

#conversion_error?(attribute) ⇒ Boolean

Responds true if there was a conversion error while converting the given attribute 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)


180
181
182
183
184
# File 'lib/paperclip/methods.rb', line 180

def conversion_error?(attribute)
  doc = scribd_document_for(attribute)

  doc && doc.conversion_status == 'ERROR'
end

#conversion_successful?(attribute) ⇒ Boolean

Responds true if the document for the given attribute has been converted successfully. This will respond false if the conversion has failed.

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)


168
169
170
171
172
# File 'lib/paperclip/methods.rb', line 168

def conversion_successful?(attribute)
  doc = scribd_document_for(attribute)

  doc && doc.conversion_status =~ /^DISPLAYABLE|DONE$/
end

#destroy_scribd_documentsObject

Destroys all scribd documents for this record. This is called before_destroy, as set up by ScribdFu::ClassMethods#extended.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/paperclip/methods.rb', line 69

def destroy_scribd_documents
  self.class.scribd_attributes.each do |attribute|
    document = scribd_document_for(self["#{attribute}_scribd_id"])

    unless document.nil?
      if document.destroy
        logger.info "[Scribd_fu] #{Time.now.rfc2822}: Removing Object #{id}##{attribute} successful"
      else
        logger.info "[Scribd_fu] #{Time.now.rfc2822}: Removing Object #{id}##{attribute} failed!"
      end
    end
  end
end

#scribd_document_for(attribute) ⇒ Object

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



188
189
190
191
192
# File 'lib/paperclip/methods.rb', line 188

def scribd_document_for(attribute)
  scribd_documents[attribute] ||= .find_document(self["#{attribute}_scribd_id"])
rescue Scribd::ResponseError # at minimum, the document was not found
  nil
end

#scribdable?(attribute) ⇒ Boolean

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

Returns:

  • (Boolean)


63
64
65
# File 'lib/paperclip/methods.rb', line 63

def scribdable?(attribute)
  ScribdFu::CONTENT_TYPES.include?(self["#{attribute}_content_type"])
end

#thumbnail_file(attribute) ⇒ Object

Returns the actual image data of a thumbnail for the specified attribute attachment.

If Scribd does not have a thumbnail for this file, then Paperclip’s thumbnanil is fallen back on by returning the file from attribute.to_file(:thumb).

Sample use in a controller:

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


135
136
137
138
139
140
141
142
143
144
145
# File 'lib/paperclip/methods.rb', line 135

def thumbnail_file(attribute)
  doc = scribd_document_for(attribute)

  if doc && doc.thumbnail_url
    open(doc.thumbnail_url).read
  else
    send(attribute).to_file(:thumb).open { |f| f.read }
  end
rescue Errno::ENOENT, NoMethodError # file not found or nil thumb file
  nil
end

#thumbnail_url(attribute) ⇒ Object

Returns a URL for a thumbnail for the specified attribute attachment.

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

Sample use in a view:

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


119
120
121
122
123
# File 'lib/paperclip/methods.rb', line 119

def thumbnail_url(attribute)
  doc = scribd_document_for(attribute)

  (doc && doc.thumbnail_url) or self.send(attribute).url(:thumb)
end

#upload_to_scribdObject

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/paperclip/methods.rb', line 86

def upload_to_scribd
  self.class.scribd_attributes.each do |attribute|
    scribd_id = self["#{attribute}_scribd_id"]

    if scribdable?(attribute) and scribd_id.blank?
      with_file_path_for(attribute) do |filename|
        if resource = .upload(:file   => filename,
                                          :access => access_level)
          self.send("#{attribute}_scribd_id=",         resource.doc_id)
          self.send("#{attribute}_scribd_access_key=", resource.access_key)

          logger.info "[Scribd_fu] #{Time.now.rfc2822}: Object " +
                      "#{id}##{attribute} successfully uploaded " +
                      "for conversion to iPaper."
        else
          logger.info "[Scribd_fu] #{Time.now.rfc2822}: Object " +
                      "#{id}##{attribute} upload failed!"

          false # cancel the save
        end
      end
    end
  end
end