mdarby-scribd_fu 1.1 → 1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -0
- data/Rakefile +3 -3
- data/generators/scribd_config/scribd_config_generator.rb +7 -0
- data/{scribd.yml.example → generators/scribd_config/templates/scribd.yml} +0 -0
- data/init.rb +1 -1
- data/lib/attachment_fu/methods.rb +163 -0
- data/lib/paperclip/methods.rb +231 -0
- data/lib/scribd_fu.rb +62 -133
- data/lib/scribd_fu_helper.rb +33 -6
- data/rails/init.rb +3 -3
- data/scribd_fu.gemspec +10 -4
- metadata +20 -9
- data/README.textile +0 -69
data/README
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
README.textile
|
data/Rakefile
CHANGED
@@ -5,17 +5,17 @@ require 'rake/rdoctask'
|
|
5
5
|
desc 'Default: run unit tests.'
|
6
6
|
task :default => :test
|
7
7
|
|
8
|
-
desc 'Test the
|
8
|
+
desc 'Test the Scribd_fu plugin.'
|
9
9
|
Rake::TestTask.new(:test) do |t|
|
10
10
|
t.libs << 'lib'
|
11
11
|
t.pattern = 'test/**/*_test.rb'
|
12
12
|
t.verbose = true
|
13
13
|
end
|
14
14
|
|
15
|
-
desc 'Generate documentation for the
|
15
|
+
desc 'Generate documentation for the Scribd_fu plugin.'
|
16
16
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
17
|
rdoc.rdoc_dir = 'rdoc'
|
18
|
-
rdoc.title = '
|
18
|
+
rdoc.title = 'Scribd_fu'
|
19
19
|
rdoc.options << '--line-numbers' << '--inline-source'
|
20
20
|
rdoc.rdoc_files.include('README')
|
21
21
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
File without changes
|
data/init.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/rails/init"
|
1
|
+
require File.dirname(__FILE__) + "/rails/init"
|
@@ -0,0 +1,163 @@
|
|
1
|
+
require 'ftools'
|
2
|
+
|
3
|
+
module ScribdFu
|
4
|
+
module AttachmentFu
|
5
|
+
module ClassMethods
|
6
|
+
# Adds validations to the current model to check that its attachment is
|
7
|
+
# scribdable.
|
8
|
+
def validates_as_scribd_document
|
9
|
+
validates_presence_of :scribd_id, :scribd_access_key, :content_type
|
10
|
+
validate :scribdable?
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module InstanceMethods
|
15
|
+
def self.included(base)
|
16
|
+
base.extend ClassMethods
|
17
|
+
end
|
18
|
+
|
19
|
+
# Checks whether the attachment is scribdable. This boils down to a check
|
20
|
+
# to ensure that the contents of the attachment are of a content type that
|
21
|
+
# scribd can understand.
|
22
|
+
def scribdable?
|
23
|
+
ScribdFu::CONTENT_TYPES.include?(content_type)
|
24
|
+
end
|
25
|
+
|
26
|
+
def scribd_id=(id)
|
27
|
+
write_attribute :scribd_id, id.to_s.strip
|
28
|
+
end
|
29
|
+
|
30
|
+
def scribd_access_key=(key)
|
31
|
+
write_attribute :scribd_access_key, key.to_s.strip
|
32
|
+
end
|
33
|
+
|
34
|
+
# Destroys the scribd document for this record. This is called
|
35
|
+
# +before_destroy+, as set up by ScribdFu::ClassMethods#extended.
|
36
|
+
#
|
37
|
+
# Also available as destroy_scribd_document, which makes more sense in
|
38
|
+
# terms of pluralization for external calls.
|
39
|
+
def destroy_scribd_documents
|
40
|
+
unless scribd_document.nil?
|
41
|
+
if scribd_document.destroy
|
42
|
+
logger.info "[Scribd_fu] #{Time.now.rfc2822}: Removing Object #{id} successful"
|
43
|
+
else
|
44
|
+
logger.info "[Scribd_fu] #{Time.now.rfc2822}: Removing Object #{id} failed!"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
alias_method :destroy_scribd_document, :destroy_scribd_documents
|
49
|
+
|
50
|
+
# Uploads the attachment to scribd for processing.. This is called
|
51
|
+
# +before_validation+, as set up by ScribdFu::ClassMethods#extended.
|
52
|
+
def upload_to_scribd
|
53
|
+
if scribdable? and self.scribd_id.blank?
|
54
|
+
with_file_path do |file_path|
|
55
|
+
if resource = scribd_login.upload(:file => "#{file_path}",
|
56
|
+
:access => access_level)
|
57
|
+
logger.info "[Scribd_fu] #{Time.now.rfc2822}: Object #{id} successfully uploaded for conversion to iPaper."
|
58
|
+
|
59
|
+
self.scribd_id = resource.doc_id
|
60
|
+
self.scribd_access_key = resource.access_key
|
61
|
+
|
62
|
+
save
|
63
|
+
else
|
64
|
+
logger.info "[Scribd_fu] #{Time.now.rfc2822}: Object #{id} upload failed!"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Returns a URL for a thumbnail for this model's attachment.
|
71
|
+
#
|
72
|
+
# If Scribd does not provide a thumbnail URL, then Attachment_fu's
|
73
|
+
# thumbnail is fallen back on by returning the value of
|
74
|
+
# <tt>public_filename(:thumb)</tt>.
|
75
|
+
#
|
76
|
+
# Sample use in a view:
|
77
|
+
# <%= image_tag(@attachment .thumbnail_url, :alt => @attachment.name) %>
|
78
|
+
def thumbnail_url
|
79
|
+
(scribd_document && scribd_document.thumbnail_url) or
|
80
|
+
public_filename(:thumb)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Returns the actual image data of a thumbnail for this model's
|
84
|
+
# attachment.
|
85
|
+
#
|
86
|
+
# If Scribd does not have a thumbnail for this file, then Attachment_fu's
|
87
|
+
# thumbnanil is fallen back on by returning the file at
|
88
|
+
# <tt>full_filename(:thumb)</tt>.
|
89
|
+
#
|
90
|
+
# Sample use in a controller:
|
91
|
+
# render :inline => @attachment.thumbnail_file,
|
92
|
+
# :content_type => 'image/jpeg'
|
93
|
+
def thumbnail_file
|
94
|
+
path = (scribd_document && scribd_document.thumbnail_url) ||
|
95
|
+
full_filename(:thumb)
|
96
|
+
|
97
|
+
open(path).read
|
98
|
+
rescue Errno::ENOENT
|
99
|
+
nil
|
100
|
+
end
|
101
|
+
|
102
|
+
# Yields the correct path to the file, either the local filename or the
|
103
|
+
# S3 URL.
|
104
|
+
#
|
105
|
+
# This method creates a temporary file of the correct filename if
|
106
|
+
# necessary, so as to be able to give scribd the right filename. The file
|
107
|
+
# is destroyed when the passed block ends.
|
108
|
+
def with_file_path(&block) # :yields: full_file_path
|
109
|
+
# TODO We can probably do this using respond_to?
|
110
|
+
if scribd_config['storage'].eql?('s3')
|
111
|
+
yield s3_url
|
112
|
+
elsif save_attachment? # file hasn't been saved, use the temp file
|
113
|
+
temp_rename = File.join(Dir.tmpdir, filename)
|
114
|
+
File.copy(temp_path, temp_rename)
|
115
|
+
|
116
|
+
yield temp_rename
|
117
|
+
else
|
118
|
+
yield full_filename
|
119
|
+
end
|
120
|
+
ensure
|
121
|
+
temp_rename && File.unlink(temp_rename) # always delete this
|
122
|
+
end
|
123
|
+
|
124
|
+
# Responds true if the conversion is complete -- note that this gives no
|
125
|
+
# indication as to whether the conversion had an error or was succesful,
|
126
|
+
# just that the conversion completed.
|
127
|
+
#
|
128
|
+
# Note that this method still returns false if the model does not refer to a
|
129
|
+
# valid document. scribd_attributes_valid? should be used to determine the
|
130
|
+
# validity of the document.
|
131
|
+
def conversion_complete?
|
132
|
+
scribd_document && scribd_document.conversion_status != 'PROCESSING'
|
133
|
+
end
|
134
|
+
|
135
|
+
# Responds true if the document has been converted.
|
136
|
+
#
|
137
|
+
# Note that this method still returns false if the model does not refer to a
|
138
|
+
# valid document. scribd_attributes_valid? should be used to determine the
|
139
|
+
# validity of the document.
|
140
|
+
def conversion_successful?
|
141
|
+
scribd_document && scribd_document.conversion_status =~ /^DISPLAYABLE|DONE$/
|
142
|
+
end
|
143
|
+
|
144
|
+
# Responds true if there was a conversion error while converting
|
145
|
+
# to iPaper.
|
146
|
+
#
|
147
|
+
# Note that this method still returns false if the model does not refer to a
|
148
|
+
# valid document. scribd_attributes_valid? should be used to determine the
|
149
|
+
# validity of the document.
|
150
|
+
def conversion_error?
|
151
|
+
scribd_document && scribd_document.conversion_status == 'ERROR'
|
152
|
+
end
|
153
|
+
|
154
|
+
# Responds the Scribd::Document associated with this model, or nil if it
|
155
|
+
# does not exist.
|
156
|
+
def scribd_document
|
157
|
+
@scribd_document ||= scribd_login.find_document(scribd_id)
|
158
|
+
rescue Scribd::ResponseError # at minimum, the document was not found
|
159
|
+
nil
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
@@ -0,0 +1,231 @@
|
|
1
|
+
require 'ftools'
|
2
|
+
|
3
|
+
module ScribdFu
|
4
|
+
module Paperclip
|
5
|
+
module ClassMethods
|
6
|
+
# Adds validations to the current model to check that the attachment at
|
7
|
+
# +attribute+ is scribdable. If +attribute+ is nil, then all attachments
|
8
|
+
# that have been marked as scribdable are validated.
|
9
|
+
#
|
10
|
+
# Note that all calls to has_scribdable_attachment should be made before
|
11
|
+
# calling validates_attachment_scribdability with a nil parameter;
|
12
|
+
# otherwise, only those that have been created already will be validated.
|
13
|
+
def validates_attachment_scribdability(attribute = nil)
|
14
|
+
attributes = attribute.nil? ? scribd_attributes : [attribute]
|
15
|
+
|
16
|
+
attributes.each do |attribute|
|
17
|
+
validates_presence_of "#{attribute}_scribd_id",
|
18
|
+
"#{attribute}_scribd_access_key",
|
19
|
+
"#{attribute}_content_type"
|
20
|
+
validates_attachment_content_type attribute,
|
21
|
+
:content_type => ScribdFu::CONTENT_TYPES
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Adds the given +attribute+ to the list of attributes that are uploaded
|
26
|
+
# to scribd.
|
27
|
+
#
|
28
|
+
# Note that a scribd attribute should not be added if it is not a
|
29
|
+
# Paperclip attachment attribute.
|
30
|
+
def add_scribd_attribute(attribute)
|
31
|
+
write_inheritable_attribute :scribd_attributes, [] if scribd_attributes.nil?
|
32
|
+
|
33
|
+
scribd_attributes << attribute
|
34
|
+
|
35
|
+
setup_scribd_attribute(attribute)
|
36
|
+
end
|
37
|
+
|
38
|
+
def scribd_attributes
|
39
|
+
read_inheritable_attribute :scribd_attributes
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
# Sets up methods needed for the given +attribute+ to be scribdable.
|
44
|
+
def setup_scribd_attribute(attribute)
|
45
|
+
define_method("#{attribute}_scribd_id=") do |id|
|
46
|
+
write_attribute "#{attribute}_scribd_id", id.to_s.strip
|
47
|
+
end
|
48
|
+
|
49
|
+
define_method("#{attribute}_scribd_access_key=") do |key|
|
50
|
+
write_attribute "#{attribute}_scribd_access_key", key.to_s.strip
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
module InstanceMethods
|
56
|
+
def self.included(base)
|
57
|
+
base.extend ClassMethods
|
58
|
+
end
|
59
|
+
|
60
|
+
# Checks whether the given attribute is scribdable. This boils down to a
|
61
|
+
# check to ensure that the contents of the attribute are of a content type
|
62
|
+
# that scribd can understand.
|
63
|
+
def scribdable?(attribute)
|
64
|
+
ScribdFu::CONTENT_TYPES.include?(self["#{attribute}_content_type"])
|
65
|
+
end
|
66
|
+
|
67
|
+
# Destroys all scribd documents for this record. This is called
|
68
|
+
# +before_destroy+, as set up by ScribdFu::ClassMethods#extended.
|
69
|
+
def destroy_scribd_documents
|
70
|
+
self.class.scribd_attributes.each do |attribute|
|
71
|
+
document = scribd_document_for(self["#{attribute}_scribd_id"])
|
72
|
+
|
73
|
+
unless document.nil?
|
74
|
+
if document.destroy
|
75
|
+
logger.info "[Scribd_fu] #{Time.now.rfc2822}: Removing Object #{id}##{attribute} successful"
|
76
|
+
else
|
77
|
+
logger.info "[Scribd_fu] #{Time.now.rfc2822}: Removing Object #{id}##{attribute} failed!"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Uploads all scribdable attributes to scribd for processing. This is
|
84
|
+
# called +before_validation+, as set up by
|
85
|
+
# ScribdFu::ClassMethods#extended.
|
86
|
+
def upload_to_scribd
|
87
|
+
self.class.scribd_attributes.each do |attribute|
|
88
|
+
scribd_id = self["#{attribute}_scribd_id"]
|
89
|
+
|
90
|
+
if scribdable?(attribute) and scribd_id.blank?
|
91
|
+
with_file_path_for(attribute) do |filename|
|
92
|
+
if resource = scribd_login.upload(:file => filename,
|
93
|
+
:access => access_level)
|
94
|
+
self.send("#{attribute}_scribd_id=", resource.doc_id)
|
95
|
+
self.send("#{attribute}_scribd_access_key=", resource.access_key)
|
96
|
+
|
97
|
+
logger.info "[Scribd_fu] #{Time.now.rfc2822}: Object " +
|
98
|
+
"#{id}##{attribute} successfully uploaded " +
|
99
|
+
"for conversion to iPaper."
|
100
|
+
else
|
101
|
+
logger.info "[Scribd_fu] #{Time.now.rfc2822}: Object " +
|
102
|
+
"#{id}##{attribute} upload failed!"
|
103
|
+
|
104
|
+
false # cancel the save
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# Returns a URL for a thumbnail for the specified +attribute+ attachment.
|
112
|
+
#
|
113
|
+
# If Scribd does not provide a thumbnail URL, then Paperclip's thumbnail
|
114
|
+
# is fallen back on by returning the value of
|
115
|
+
# <tt>attribute.url(:thumb)</tt>.
|
116
|
+
#
|
117
|
+
# Sample use in a view:
|
118
|
+
# <%= image_tag(@attachment.thumbnail_url, :alt => @attachment.name) %>
|
119
|
+
def thumbnail_url(attribute)
|
120
|
+
doc = scribd_document_for(attribute)
|
121
|
+
|
122
|
+
(doc && doc.thumbnail_url) or self.send(attribute).url(:thumb)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Returns the actual image data of a thumbnail for the specified
|
126
|
+
# +attribute+ attachment.
|
127
|
+
#
|
128
|
+
# If Scribd does not have a thumbnail for this file, then
|
129
|
+
# Paperclip's thumbnanil is fallen back on by returning the file from
|
130
|
+
# <tt>attribute.to_file(:thumb)</tt>.
|
131
|
+
#
|
132
|
+
# Sample use in a controller:
|
133
|
+
# render :inline => @attachment.thumbnail_file,
|
134
|
+
# :content_type => 'image/jpeg'
|
135
|
+
def thumbnail_file(attribute)
|
136
|
+
doc = scribd_document_for(attribute)
|
137
|
+
|
138
|
+
if doc && doc.thumbnail_url
|
139
|
+
open(doc.thumbnail_url).read
|
140
|
+
else
|
141
|
+
send(attribute).to_file(:thumb).open { |f| f.read }
|
142
|
+
end
|
143
|
+
rescue Errno::ENOENT, NoMethodError # file not found or nil thumb file
|
144
|
+
nil
|
145
|
+
end
|
146
|
+
|
147
|
+
# Responds true if the conversion is complete for the given +attribute+ --
|
148
|
+
# note that this gives no indication as to whether the conversion had an
|
149
|
+
# error or was succesful, just that the conversion completed. See
|
150
|
+
# <tt>conversion_successful?</tt> for that information.
|
151
|
+
#
|
152
|
+
# Note also that this method still returns false if the model does not
|
153
|
+
# refer to a valid document. scribd_attributes_valid? should be used to
|
154
|
+
# determine the validity of the document.
|
155
|
+
def conversion_complete?(attribute)
|
156
|
+
doc = scribd_document_for(attribute)
|
157
|
+
|
158
|
+
doc && doc.conversion_status != 'PROCESSING'
|
159
|
+
end
|
160
|
+
|
161
|
+
# Responds true if the document for the given +attribute+ has been
|
162
|
+
# converted successfully. This *will* respond false if the conversion has
|
163
|
+
# failed.
|
164
|
+
#
|
165
|
+
# Note that this method still returns false if the model does not refer to a
|
166
|
+
# valid document. <tt>scribd_attributes_valid?</tt> should be used to
|
167
|
+
# determine the validity of the document.
|
168
|
+
def conversion_successful?(attribute)
|
169
|
+
doc = scribd_document_for(attribute)
|
170
|
+
|
171
|
+
doc && doc.conversion_status =~ /^DISPLAYABLE|DONE$/
|
172
|
+
end
|
173
|
+
|
174
|
+
# Responds true if there was a conversion error while converting the given
|
175
|
+
# +attribute+ to iPaper.
|
176
|
+
#
|
177
|
+
# Note that this method still returns false if the model does not refer to a
|
178
|
+
# valid document. <tt>scribd_attributes_valid?</tt> should be used to
|
179
|
+
# determine the validity of the document.
|
180
|
+
def conversion_error?(attribute)
|
181
|
+
doc = scribd_document_for(attribute)
|
182
|
+
|
183
|
+
doc && doc.conversion_status == 'ERROR'
|
184
|
+
end
|
185
|
+
|
186
|
+
# Responds the Scribd::Document associated with the given +attribute+, or
|
187
|
+
# nil if it does not exist.
|
188
|
+
def scribd_document_for(attribute)
|
189
|
+
scribd_documents[attribute] ||= scribd_login.find_document(self["#{attribute}_scribd_id"])
|
190
|
+
rescue Scribd::ResponseError # at minimum, the document was not found
|
191
|
+
nil
|
192
|
+
end
|
193
|
+
|
194
|
+
private
|
195
|
+
def scribd_documents
|
196
|
+
@scribd_documents ||= HashWithIndifferentAccess.new
|
197
|
+
end
|
198
|
+
|
199
|
+
# Returns the full filename for the given attribute. If the file is
|
200
|
+
# stored on S3, this is a full S3 URI, while it is a full path to the
|
201
|
+
# local file if the file is stored locally.
|
202
|
+
def full_filename_for(attribute)
|
203
|
+
filename = attachment_for(attribute).path
|
204
|
+
end
|
205
|
+
|
206
|
+
# Yields the correct path to the file for the attachment in
|
207
|
+
# +attribute+, either the local filename or the S3 URL.
|
208
|
+
#
|
209
|
+
# This method creates a temporary file of the correct filename for the
|
210
|
+
# attachment in +attribute+ if necessary, so as to be able to give
|
211
|
+
# scribd the right filename. The file is destroyed when the passed block
|
212
|
+
# ends.
|
213
|
+
def with_file_path_for(attribute, &block) # :yields: full_file_path
|
214
|
+
attachment = attachment_for(attribute)
|
215
|
+
|
216
|
+
if attachment.respond_to?(:s3)
|
217
|
+
yield attachment.url
|
218
|
+
elsif File.exists?(attachment.path)
|
219
|
+
yield attachment.path
|
220
|
+
else # file hasn't been saved, use a tempfile
|
221
|
+
temp_rename = File.join(Dir.tmpdir, attachment.original_filename)
|
222
|
+
File.copy(attachment.to_file.path, temp_rename)
|
223
|
+
|
224
|
+
yield temp_rename
|
225
|
+
end
|
226
|
+
ensure
|
227
|
+
temp_rename && File.unlink(temp_rename) # always delete this
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
data/lib/scribd_fu.rb
CHANGED
@@ -1,39 +1,61 @@
|
|
1
|
-
|
1
|
+
require 'attachment_fu/methods'
|
2
|
+
require 'paperclip/methods'
|
3
|
+
|
4
|
+
module ScribdFu
|
5
|
+
# A list of content types supported by scribd.
|
6
|
+
CONTENT_TYPES = ['application/pdf', 'image/jpeg', 'image/pjpeg',
|
7
|
+
'image/gif', 'image/png', 'image/x-png', 'image/jpg',
|
8
|
+
'application/msword', 'application/mspowerpoint',
|
9
|
+
'application/vnd.ms-powerpoint', 'application/excel',
|
10
|
+
'application/vnd.ms-excel', 'application/postscript',
|
11
|
+
'text/plain', 'application/rtf',
|
12
|
+
'application/vnd.oasis.opendocument.text',
|
13
|
+
'application/vnd.oasis.opendocument.presentation',
|
14
|
+
'application/vnd.oasis.opendocument.spreadsheet',
|
15
|
+
'application/vnd.sun.xml.writer',
|
16
|
+
'application/vnd.sun.xml.impress',
|
17
|
+
'application/vnd.sun.xml.calc',
|
18
|
+
# OOXML, AKA `the MIME types from hell'. Seriously, these are long enough to
|
19
|
+
# start their own dictionary...
|
20
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
21
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
|
22
|
+
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
23
|
+
'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
|
24
|
+
'application/vnd.openxmlformats-officedocument.presentationml.template',
|
25
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
26
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.template']
|
2
27
|
|
3
28
|
def self.included(base)
|
4
|
-
base.extend
|
29
|
+
base.extend ActsAsScribdDocument
|
5
30
|
end
|
6
31
|
|
7
|
-
module
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
END
|
32
|
+
module ActsAsScribdDocument
|
33
|
+
# Synonym for <tt>has_scribdable_attachment(nil)</tt>.
|
34
|
+
def acts_as_scribd_document
|
35
|
+
has_scribdable_attachment
|
12
36
|
end
|
13
|
-
end
|
14
37
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
38
|
+
# Marks the given +attribute+ as a scribdable document file. If +attribute+
|
39
|
+
# is nil, assumes this is an Attachment_fu model and deals with the setup
|
40
|
+
# accordingly; otherwise, assumes a +paperclip+ model and sets up scribding
|
41
|
+
# related to the particular given attribute.
|
42
|
+
def has_scribdable_attachment(attribute = nil)
|
43
|
+
class_eval do
|
44
|
+
include ScribdFu::InstanceMethods
|
45
|
+
|
46
|
+
if attribute.nil?
|
47
|
+
include ScribdFu::AttachmentFu::InstanceMethods
|
48
|
+
else
|
49
|
+
include ScribdFu::Paperclip::InstanceMethods # ignored if already done
|
21
50
|
|
22
|
-
|
23
|
-
|
24
|
-
|
51
|
+
add_scribd_attribute attribute # class method added by above include
|
52
|
+
end
|
53
|
+
end
|
25
54
|
end
|
26
55
|
end
|
27
56
|
|
28
57
|
module InstanceMethods
|
29
|
-
|
30
|
-
'application/excel', 'application/vnd.ms-excel', 'application/postscript', 'text/plain', 'application/rtf',
|
31
|
-
'application/vnd.oasis.opendocument.text', 'vnd.oasis.opendocument.presentation',
|
32
|
-
'application/vnd.sun.xml.writer', 'application/vnd.sun.xml.impress',
|
33
|
-
'application/vnd.oasis.opendocument.spreadsheet', 'application/vnd.sun.xml.calc']
|
34
|
-
|
35
|
-
mattr_reader :content_types
|
36
|
-
|
58
|
+
# Sets up Scribd configuration info when this module is included.
|
37
59
|
def self.included(base)
|
38
60
|
base.extend ClassMethods
|
39
61
|
|
@@ -48,132 +70,39 @@ module Scribd_fu
|
|
48
70
|
begin
|
49
71
|
unless @@scribd_login
|
50
72
|
@@scribd_config = YAML.load_file("#{RAILS_ROOT}/config/scribd.yml").symbolize_keys
|
73
|
+
@@scribd_config = @@scribd_config[:scribd]
|
51
74
|
|
52
75
|
# Ensure we can connect to the Service
|
53
|
-
Scribd::API.instance.key = @@scribd_config[
|
54
|
-
Scribd::API.instance.secret = @@scribd_config[
|
76
|
+
Scribd::API.instance.key = @@scribd_config['key'].to_s.strip
|
77
|
+
Scribd::API.instance.secret = @@scribd_config['secret'].to_s.strip
|
55
78
|
|
56
|
-
@@scribd_login = Scribd::User.login @@scribd_config[
|
79
|
+
@@scribd_login = Scribd::User.login @@scribd_config['user'].to_s.strip, @@scribd_config['password'].to_s.strip
|
57
80
|
end
|
58
81
|
rescue
|
59
82
|
raise "config/scribd.yml file not found, or your credentials are incorrect."
|
60
83
|
end
|
61
84
|
end
|
62
85
|
|
63
|
-
def scribd_attributes_valid?
|
64
|
-
[:scribd_id, :scribd_access_id].each do |attr_name|
|
65
|
-
enum = scribd_options[attr_name]
|
66
|
-
errors.add attr_name, ActiveRecord::Errors.default_error_messages[:inclusion] unless enum.nil? || enum.include?(send(attr_name))
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def scribdable?
|
71
|
-
content_types.include?(content_type)
|
72
|
-
end
|
73
|
-
|
74
|
-
def scribd_id=(id)
|
75
|
-
write_attribute :scribd_id, id.to_s.strip
|
76
|
-
end
|
77
|
-
|
78
|
-
def scribd_access_key=(key)
|
79
|
-
write_attribute :scribd_access_key, key.to_s.strip
|
80
|
-
end
|
81
|
-
|
82
|
-
def destroy_scribd_document
|
83
|
-
if scribd_document
|
84
|
-
if scribd_document.destroy
|
85
|
-
logger.info "[Scribd_fu] #{Time.now.rfc2822}: Removing Object #{id} successful"
|
86
|
-
else
|
87
|
-
logger.info "[Scribd_fu] #{Time.now.rfc2822}: Removing Object #{id} failed!"
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
86
|
def access_level
|
93
87
|
if self.respond_to?(:is_public) && self.is_public != nil
|
94
88
|
scribd_access = self.is_public ? 'public' : 'private'
|
95
89
|
else
|
96
|
-
scribd_access = scribd_config[
|
90
|
+
scribd_access = scribd_config['access']
|
97
91
|
end
|
98
|
-
|
99
|
-
scribd_access
|
100
|
-
end
|
101
|
-
|
102
|
-
def final_path
|
103
|
-
if scribd_config[:scribd]['storage'].eql?('s3')
|
104
|
-
file_path = s3_url
|
105
|
-
else
|
106
|
-
file_path = full_filename
|
107
|
-
end
|
108
|
-
|
109
|
-
file_path
|
110
|
-
end
|
111
|
-
|
112
|
-
def upload_to_scribd
|
113
|
-
if scribdable? and self.scribd_id.blank?
|
114
|
-
|
115
|
-
if resource = scribd_login.upload(:file => "#{final_path}", :access => access_level)
|
116
|
-
logger.info "[Scribd_fu] #{Time.now.rfc2822}: Object #{id} successfully uploaded for conversion to iPaper."
|
117
|
-
|
118
|
-
self.scribd_id = resource.doc_id
|
119
|
-
self.scribd_access_key = resource.access_key
|
120
|
-
|
121
|
-
save
|
122
|
-
else
|
123
|
-
logger.info "[Scribd_fu] #{Time.now.rfc2822}: Object #{id} upload failed!"
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
# Sample of use in a view:
|
129
|
-
# image_tag(@attachment).thumbnail_url, :alt => @attachment.name)
|
130
|
-
def thumbnail_url
|
131
|
-
scribd_document ? scribd_document.thumbnail_url : nil
|
132
|
-
end
|
133
|
-
|
134
|
-
# Sample of use in a controller:
|
135
|
-
# render :inline => @attachment.thumbnail_file, :content_type => 'image/jpeg'
|
136
|
-
def thumbnail_file
|
137
|
-
scribd_document ? open(scribd_document.thumbnail_url).read : nil
|
138
|
-
end
|
139
92
|
|
140
|
-
|
141
|
-
# indication as to whether the conversion had an error or was succesful,
|
142
|
-
# just that the conversion completed.
|
143
|
-
#
|
144
|
-
# Note that this method still returns false if the model does not refer to a
|
145
|
-
# valid document. scribd_attributes_valid? should be used to determine the
|
146
|
-
# validity of the document.
|
147
|
-
def conversion_complete?
|
148
|
-
scribd_document && scribd_document.conversion_status != 'PROCESSING'
|
149
|
-
end
|
150
|
-
|
151
|
-
# Responds true if the document has been converted.
|
152
|
-
#
|
153
|
-
# Note that this method still returns false if the model does not refer to a
|
154
|
-
# valid document. scribd_attributes_valid? should be used to determine the
|
155
|
-
# validity of the document.
|
156
|
-
def conversion_successful?
|
157
|
-
scribd_document && scribd_document.conversion_status =~ /^DISPLAYABLE|DONE$/
|
93
|
+
scribd_access
|
158
94
|
end
|
95
|
+
end
|
159
96
|
|
160
|
-
|
161
|
-
# to
|
162
|
-
#
|
163
|
-
#
|
164
|
-
|
165
|
-
|
166
|
-
def conversion_error?
|
167
|
-
scribd_document && scribd_document.conversion_status == 'ERROR'
|
168
|
-
end
|
97
|
+
module ClassMethods
|
98
|
+
# Sets up the scribd_options accessor, a before_destroy hook to ensure the
|
99
|
+
# deletion of associated Scribd documents, and an after_save hook to upload
|
100
|
+
# to scribd.
|
101
|
+
def self.extended(base)
|
102
|
+
base.class_inheritable_accessor :scribd_options
|
169
103
|
|
170
|
-
|
171
|
-
|
172
|
-
def scribd_document
|
173
|
-
@scribd_document ||= scribd_login.find_document(scribd_id)
|
174
|
-
rescue Scribd::ResponseError # at minimum, the document was not found
|
175
|
-
nil
|
104
|
+
base.before_destroy :destroy_scribd_documents
|
105
|
+
base.before_validation :upload_to_scribd
|
176
106
|
end
|
177
107
|
end
|
178
|
-
|
179
108
|
end
|
data/lib/scribd_fu_helper.rb
CHANGED
@@ -1,14 +1,41 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
1
|
+
module ScribdFuHelper
|
2
|
+
# Displays the scribd object for the attachment on the given +object+. If
|
3
|
+
# +alt_text_or_attribute+ is given, then it will be used as the alternate text
|
4
|
+
# for an Attachment_fu model, or as the attribute name for a Paperclip
|
5
|
+
# model. If you want to specify alternate text for a Paperclip model, use the
|
6
|
+
# last parameter, +alt_text_if_paperclip+.
|
7
|
+
#
|
8
|
+
# If you are using Paperclip, you _must_ specify +alt_text_or_attribute+ as
|
9
|
+
# the attribute on which the scribd object exists.
|
10
|
+
#
|
11
|
+
# For example, using Attachment_fu:
|
12
|
+
# <%= display_scribd document %>
|
13
|
+
# <%= display_scribd document, 'You need Flash to view this document' %>
|
14
|
+
#
|
15
|
+
# Using Paperclip:
|
16
|
+
# <%= display_scribd user, :biography %>
|
17
|
+
# <%= display_scribd user, :biography, 'You need Flash for biographies." %>
|
18
|
+
def display_scribd(object, alt_text_or_attribute = '', alt_text_if_paperclip = nil)
|
19
|
+
# Resolve the right scribd ID, access key, and alt text.
|
20
|
+
if object.respond_to?("scribd_id")
|
21
|
+
scribd_id = object.scribd_id
|
22
|
+
scribd_ak = object.scribd_access_key
|
23
|
+
|
24
|
+
alt_text = alt_text_or_attribute
|
25
|
+
else
|
26
|
+
scribd_id = object.send "#{alt_text_or_attribute}_scribd_id"
|
27
|
+
scribd_ak = object.send "#{alt_text_or_attribute}_scribd_access_key"
|
28
|
+
|
29
|
+
alt_text = alt_text_if_paperclip
|
30
|
+
end
|
31
|
+
|
4
32
|
<<-END
|
5
33
|
<script type=\"text/javascript\" src=\"http://www.scribd.com/javascripts/view.js\"></script>
|
6
34
|
<div id=\"embedded_flash\">#{alt_text}</div>
|
7
35
|
<script type=\"text/javascript\">
|
8
|
-
var scribd_doc = scribd.Document.getDoc(#{
|
36
|
+
var scribd_doc = scribd.Document.getDoc(#{scribd_id}, '#{scribd_ak}');
|
9
37
|
scribd_doc.write(\"embedded_flash\");
|
10
38
|
</script>
|
11
39
|
END
|
12
40
|
end
|
13
|
-
|
14
|
-
end
|
41
|
+
end
|
data/rails/init.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'scribd_fu'
|
2
2
|
require 'scribd_fu_helper'
|
3
3
|
|
4
|
-
ActiveRecord::Base.send(:include,
|
5
|
-
ActionView::Base.send(:include,
|
4
|
+
ActiveRecord::Base.send(:include, ScribdFu)
|
5
|
+
ActionView::Base.send(:include, ScribdFuHelper)
|
6
6
|
|
7
|
-
RAILS_DEFAULT_LOGGER.debug "** [Scribd_fu] loaded"
|
7
|
+
RAILS_DEFAULT_LOGGER.debug "** [Scribd_fu] loaded"
|
data/scribd_fu.gemspec
CHANGED
@@ -1,12 +1,18 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "scribd_fu"
|
3
|
-
s.version = "1.
|
4
|
-
s.date = "2008-12-
|
3
|
+
s.version = "1.2"
|
4
|
+
s.date = "2008-12-16"
|
5
5
|
s.summary = "Quick and easy interactions with Scribd's iPaper service"
|
6
6
|
s.email = "matt@matt-darby.com"
|
7
7
|
s.homepage = "http://github.com/mdarby/scribd_fu/tree/master"
|
8
8
|
s.description = "A Rails plugin that streamlines interactions with the Scribd service"
|
9
9
|
s.has_rdoc = false
|
10
10
|
s.authors = ["Matt Darby"]
|
11
|
-
s.files = [
|
12
|
-
|
11
|
+
s.files = ['init.rb', 'install.rb', 'uninstall.rb', 'MIT-LICENSE', 'Rakefile',
|
12
|
+
'README', 'lib/scribd_fu.rb', 'lib/scribd_fu_helper.rb',
|
13
|
+
'lib/attachment_fu/methods.rb', 'lib/paperclip/methods.rb',
|
14
|
+
'rails/init.rb', 'scribd_fu.gemspec',
|
15
|
+
'generators/scribd_config/scribd_config_generator.rb',
|
16
|
+
'generators/scribd_config/templates/scribd.yml']
|
17
|
+
s.add_dependency 'rscribd'
|
18
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mdarby-scribd_fu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "1.
|
4
|
+
version: "1.2"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Darby
|
@@ -9,10 +9,18 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-12-
|
12
|
+
date: 2008-12-16 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rscribd
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
16
24
|
description: A Rails plugin that streamlines interactions with the Scribd service
|
17
25
|
email: matt@matt-darby.com
|
18
26
|
executables: []
|
@@ -24,15 +32,18 @@ extra_rdoc_files: []
|
|
24
32
|
files:
|
25
33
|
- init.rb
|
26
34
|
- install.rb
|
35
|
+
- uninstall.rb
|
36
|
+
- MIT-LICENSE
|
37
|
+
- Rakefile
|
38
|
+
- README
|
27
39
|
- lib/scribd_fu.rb
|
28
40
|
- lib/scribd_fu_helper.rb
|
29
|
-
-
|
41
|
+
- lib/attachment_fu/methods.rb
|
42
|
+
- lib/paperclip/methods.rb
|
30
43
|
- rails/init.rb
|
31
|
-
- Rakefile
|
32
|
-
- README.textile
|
33
|
-
- scribd.yml.example
|
34
44
|
- scribd_fu.gemspec
|
35
|
-
-
|
45
|
+
- generators/scribd_config/scribd_config_generator.rb
|
46
|
+
- generators/scribd_config/templates/scribd.yml
|
36
47
|
has_rdoc: false
|
37
48
|
homepage: http://github.com/mdarby/scribd_fu/tree/master
|
38
49
|
post_install_message:
|
data/README.textile
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
h1. Scribd_fu
|
2
|
-
|
3
|
-
A Ruby on Rails plugin that streamlines interaction with the Scribd service (scribd.com), and even works with Attachment_fu!
|
4
|
-
|
5
|
-
|
6
|
-
h2. What it does
|
7
|
-
|
8
|
-
Scribd_fu hides out in the shadows like a document converting ninja, just waiting to process your data into a convenient Flash format (like YouTube) with the help of the black majick of Scribd.com. Imagine imbedding huge documents right inline with your web UI, no downloading, no necessary programs on the client side to view your data. It’s pretty damned cool.
|
9
|
-
|
10
|
-
|
11
|
-
h2. Requirements
|
12
|
-
|
13
|
-
Scribd_fu requires the wicked awesome Attachment_fu plugin. You probably already have it installed.
|
14
|
-
You also need the rscribd gem (sudo gem install rscribd will do the trick)
|
15
|
-
|
16
|
-
|
17
|
-
h2. How to Install & Use
|
18
|
-
|
19
|
-
# Install the rscribd gem
|
20
|
-
<pre>gem install rscribd</pre>
|
21
|
-
# Install the scribd_fu gem
|
22
|
-
<pre>sudo gem install mdarby-scribd_fu</pre>
|
23
|
-
# Add this line to your config/environment.rb file
|
24
|
-
<pre>config.gem 'mdarby-scribd_fu', :lib => 'scribd_fu'</pre>
|
25
|
-
# Enter the below line into any attachment_fu-using model that you’d like to Scribdify
|
26
|
-
<pre>acts_as_scribd_document</pre>
|
27
|
-
# Add the following fields into a new migration for the target model (and update your schema!):
|
28
|
-
<pre>
|
29
|
-
t.integer :scribd_id
|
30
|
-
t.string :scribd_access_key
|
31
|
-
t.boolean :is_public
|
32
|
-
</pre>
|
33
|
-
# Sign up for Scribd (it’s totally free)
|
34
|
-
# Copy the vendor/plugins/scribd.yml.example file to config/scribd.yml and fill out with your Scribd login credentials
|
35
|
-
# Now, when you upload a file that is convertible in the Scribd system, Scribd_fu will automatically handle the CRUD for you. No muss, no fuss.
|
36
|
-
|
37
|
-
|
38
|
-
h2. Access
|
39
|
-
|
40
|
-
You can set the default access on all documents by setting the 'access' key in the scribd.yml file to either 'private' or 'public'
|
41
|
-
|
42
|
-
You can also override the default access level and control access on a per-document basis by setting the 'is_public' attribute to either true or false. If this column is not defined, the default option in the scribd.yml will be used.
|
43
|
-
|
44
|
-
Please note that setting the access level only works before the document is initially uploaded to Scribd.
|
45
|
-
|
46
|
-
|
47
|
-
h2. Displaying
|
48
|
-
|
49
|
-
To view a Scribd document, just throw the below code into your view (where @document is an object of your Scribd/Attachment_fu model):
|
50
|
-
<pre><%= display_scribd(@document) %></pre>
|
51
|
-
|
52
|
-
That’s it!
|
53
|
-
|
54
|
-
|
55
|
-
h2. Notes
|
56
|
-
|
57
|
-
Note that scribd_fu will only upload the file to scribd. Scribd then has to
|
58
|
-
convert it to their iPaper format. Usually this is a pretty fast operation, but
|
59
|
-
if you want to be safe or have a contingency plan in case someone tries to
|
60
|
-
access the document and it isn't converted yet, the set of methods
|
61
|
-
conversion_complete?, conversion_successful?, and conversion_error? can be used
|
62
|
-
to determine the current conversion status of the document.
|
63
|
-
|
64
|
-
|
65
|
-
h2. About the Author
|
66
|
-
|
67
|
-
My name is Matt Darby. I’m a 29 year old professional Web Developer and IT Manager. I am the IT Manager and Lead Web Developer at Dynamix Engineering and recently earned a Master’s Degree in Computer Science from Franklin University in Columbus, OH.
|
68
|
-
|
69
|
-
Feel free to check out my "blog":http://blgo.matt-darby.com or to "recommend me":http://workingwithrails.com
|