jastix-scribd_fu 1.2.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,191 @@
1
+ h1. Scribd_fu
2
+
3
+ A Ruby on Rails plugin that streamlines interaction with the Scribd service
4
+ (scribd.com), and even works Attachment_fu or Paperclip!
5
+
6
+
7
+ h2. What it does
8
+
9
+ Scribd_fu hides out in the shadows like a document converting ninja, just
10
+ waiting to process your data into a convenient Flash format (like YouTube) with
11
+ the help of the black majick of Scribd.com. Imagine embedding huge documents
12
+ right inline with your web UI, no downloading, no necessary programs on the
13
+ client side (except for Flash) to view your data. It's pretty damned cool.
14
+
15
+
16
+ h2. Requirements
17
+
18
+ Scribd_fu requires the rscribd gem for talking to scribd, and either the wicked
19
+ awesome Attachment_fu plugin or the equally awesome Paperclip plugin.
20
+
21
+
22
+ h2. How to Install & Use
23
+
24
+ Scribd_fu depends on rscribd, so installing it as a gem should also pull in
25
+ rscribd. First, add Scribd_fu to your list of gems in
26
+ <tt>config/environment.rb</tt>:
27
+
28
+ <pre>config.gem 'mdarby-scribd_fu', :lib => 'scribd_fu'</pre>
29
+
30
+ Note that this will only work if you have http://gems.github.com in your list of
31
+ gem sources:
32
+
33
+ <pre>$ gem source -a http://gems.github.com</pre>
34
+
35
+ Then run:
36
+
37
+ <pre>$ sudo rake gems:install</pre>
38
+
39
+ To install it and the rscribd gem. If you don't add github as a gem source, you
40
+ can add the <tt>:source</tt> option to the <tt>config.gem</tt> call; however,
41
+ this means you will have to separately require the rscribd gem, since using
42
+ <tt>:source</tt> will not search for the rscribd gem in your regular sources.
43
+
44
+ Then, install either Paperclip or Attachment_fu.
45
+
46
+ h3. Attachment_fu
47
+
48
+ If you want to install Attachment_fu, you should probably use the git
49
+ repository on github:
50
+
51
+ <pre>$ script/plugin install git://github.com/technoweenie/attachment_fu.git</pre>
52
+
53
+ h3. Paperclip
54
+
55
+ If you'd rather use Paperclip (the most important advantage, with respect to
56
+ Scribd_fu, will be being able to have more than one document-related column in a
57
+ single model), you should probably require it as a gem dependency rather than
58
+ using it in plugin form. In Rails 2.1 or greater:
59
+
60
+ <pre>config.gem 'paperclip'</pre>
61
+
62
+ Then run:
63
+
64
+ <pre>$ rake gems:install</pre>
65
+
66
+ To install it.
67
+
68
+
69
+ h2. How to Use
70
+
71
+ h3. Shared steps
72
+
73
+ * Sign up for Scribd (it's totally free) at
74
+ http://www.scribd.com/publisher/signup_api.
75
+ * Run <tt>script/generate scribd_config</tt> and fill out the new
76
+ config/scribd.yml file with your Scribd login credentials.
77
+
78
+ h3. Attachment_fu
79
+
80
+ Enter the below line into any attachment_fu-using model whose attachment you'd like to
81
+ Scribdify:
82
+
83
+ <pre>acts_as_scribd_document</pre>
84
+
85
+ Add the following fields into a new migration for the target model (and run the
86
+ migration!):
87
+
88
+ <pre>t.integer :scribd_id
89
+ t.string :scribd_access_key</pre>
90
+
91
+ Scribd_fu will use these to track scribd-related information.
92
+
93
+ Now, when you upload a file to that model, Scribd_fu will automatically handle
94
+ the scribd side of things for you. No muss, no fuss. Files are uploaded to
95
+ scribd when the model is saved. If the file fails to upload to scribd, the model
96
+ save will fail.
97
+
98
+ Scribd_fu also provides the <tt>validates_as_scribd_document</tt> validation,
99
+ which verifies that the document's information is being saved correctly (i.e.,
100
+ that the <tt>content_type</tt>, <tt>scribd_id</tt>, and
101
+ <tt>scribd_access_key</tt> are present) and that the attachment is scribdable to
102
+ begin with (that is to say, that the content type of the file is a scribdable
103
+ content type).
104
+
105
+ Note that Scribd_fu will only upload the file to Scribd. Scribd then has to
106
+ convert it to their iPaper format. Usually this is a pretty fast operation, but
107
+ if you want to be safe or have a contingency plan in case someone tries to
108
+ access the document and it isn't converted yet, the set of methods
109
+ <tt>conversion_complete?</tt>, <tt>conversion_successful?</tt>, and
110
+ <tt>conversion_error?</tt> can be used to determine the current conversion
111
+ status of the document. Also note that if you try to display a file that hasn't
112
+ been converted to scribd yet, the iPaper viewer will actually say that it is
113
+ still converting complete with a spinner, so these methods will often not be
114
+ necessary.
115
+
116
+ To view a Scribd document, just throw the below code into your view (where
117
+ <tt>@document</tt> is an object of your Scribd/Attachment_fu model):
118
+ <pre><%= display_scribd(@document) %></pre>
119
+ That's it!
120
+
121
+ h3. Paperclip
122
+
123
+ For each attachment that you want to be scribdable, mark it using the
124
+ <tt>has_scribdable_attachment</tt> method. For example, if I had the line:
125
+
126
+ <pre>has_attached_file :document</pre>
127
+
128
+ And I wanted the <tt>document</tt> attachment to be scribdable, I would add:
129
+
130
+ <pre>has_scribdable_attachment :document</pre>
131
+
132
+ In addition, you have to add two columns for each scribdable attachment. These
133
+ are <tt>_scribd_id</tt> and <tt>_scribd_access_key</tt>, both prefixed by the
134
+ attachment name. For the above example, we would have the following migration
135
+ statements:
136
+
137
+ <pre>t.integer :document_scribd_id
138
+ t.string :document_scribd_access_key</pre>
139
+
140
+ Scribdable attachments can also be validated using the
141
+ <tt>validates_attachment_scribdability</tt> validation:
142
+
143
+ <pre>validates_attachment_scribdability :document</pre>
144
+
145
+ This validation checks that the content type, scribd id, and access key are all
146
+ available, and it verifies that the content type is a scribdable content type.
147
+
148
+ As mentioned in the Attachment_fu section, Scribd_fu will only upload the file
149
+ to Scribd. Scribd then has to do the actual conversion to iPaper. For tracking
150
+ the status of this conversion, you have three methods, each of which take the
151
+ attachment whose status you want:
152
+
153
+ * <tt>conversion_complete?</tt> -- e.g.,
154
+ <tt>model.conversion_complete?(:document)</tt>
155
+ * <tt>conversion_successful?</tt> -- e.g.,
156
+ <tt>model.conversion_successful?(:document)</tt>
157
+ * <tt>conversion_error?</tt> -- e.g.,
158
+ <tt>model.conversion_error?(:document)</tt>
159
+
160
+ Also note that if you try to display a file that hasn't been converted to scribd
161
+ yet, the iPaper viewer will actually say that it is still converting with a
162
+ spinner, so these methods will often not be necessary.
163
+
164
+ To view a Scribd document, just throw the below code into your view (where
165
+ <tt>@model</tt> is an object of your Scribd/Paperclip model, and
166
+ <tt>document</tt> is the name of the scribdable attachment):
167
+
168
+ <pre><%= display_scribd(@model, :document) %></pre>
169
+
170
+ That's it!
171
+
172
+
173
+ h2. Access
174
+
175
+ You can set the default access on all documents by setting the 'access' key in
176
+ the scribd.yml file to either 'private' or 'public'.
177
+
178
+ You can also override the default access level and control access on a
179
+ per-document basis by setting the 'is_public' attribute to either true or false.
180
+ If this column is not defined, the default option in the scribd.yml will be
181
+ used.
182
+
183
+ Please note that setting the access level only works before the document is
184
+ initially uploaded to Scribd.
185
+
186
+
187
+ h2. About the Author
188
+
189
+ 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.
190
+
191
+ Feel free to check out my "blog":http://blgo.matt-darby.com or to "recommend me":http://workingwithrails.com
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the Scribd_fu plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the Scribd_fu plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'Scribd_fu'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,7 @@
1
+ class ScribdConfigGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.file 'scribd.yml', 'config/scribd.yml', :collision => :skip
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ #Scribd Account Info
2
+
3
+ scribd:
4
+ key:
5
+ secret:
6
+ user:
7
+ password:
8
+ access:
9
+ #storage: s3 # Uncomment if Attachment_fu is using Amazon S3 for storage
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,164 @@
1
+ require 'fileutils'
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
164
+
@@ -0,0 +1,232 @@
1
+ require 'fileutils'
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
232
+
data/lib/scribd_fu.rb ADDED
@@ -0,0 +1,108 @@
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']
27
+
28
+ def self.included(base)
29
+ base.extend ActsAsScribdDocument
30
+ end
31
+
32
+ module ActsAsScribdDocument
33
+ # Synonym for <tt>has_scribdable_attachment(nil)</tt>.
34
+ def acts_as_scribd_document
35
+ has_scribdable_attachment
36
+ end
37
+
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
50
+
51
+ add_scribd_attribute attribute # class method added by above include
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ module InstanceMethods
58
+ # Sets up Scribd configuration info when this module is included.
59
+ def self.included(base)
60
+ base.extend ClassMethods
61
+
62
+ mattr_reader :scribd_config, :scribd_login
63
+
64
+ begin
65
+ require 'rscribd'
66
+ rescue LoadError
67
+ raise RequiredLibraryNotFoundError.new('rscribd could not be loaded')
68
+ end
69
+
70
+ begin
71
+ unless @@scribd_login
72
+ @@scribd_config = YAML.load_file("#{RAILS_ROOT}/config/scribd.yml").symbolize_keys
73
+ @@scribd_config = @@scribd_config[:scribd]
74
+
75
+ # Ensure we can connect to the Service
76
+ Scribd::API.instance.key = @@scribd_config['key'].to_s.strip
77
+ Scribd::API.instance.secret = @@scribd_config['secret'].to_s.strip
78
+
79
+ @@scribd_login = Scribd::User.login @@scribd_config['user'].to_s.strip, @@scribd_config['password'].to_s.strip
80
+ end
81
+ rescue
82
+ raise "config/scribd.yml file not found, or your credentials are incorrect."
83
+ end
84
+ end
85
+
86
+ def access_level
87
+ if self.respond_to?(:is_public) && self.is_public != nil
88
+ scribd_access = self.is_public ? 'public' : 'private'
89
+ else
90
+ scribd_access = scribd_config['access']
91
+ end
92
+
93
+ scribd_access
94
+ end
95
+ end
96
+
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
103
+
104
+ base.before_destroy :destroy_scribd_documents
105
+ base.before_validation :upload_to_scribd
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,58 @@
1
+ module ScribdFuHelper
2
+ # Available parameters for the JS API
3
+ # http://www.scribd.com/publisher/api/api?method_name=Javascript+API
4
+ AVAILABLE_JS_PARAMS = [ :height, :width, :page, :my_user_id, :search_query,
5
+ :jsapi_version, :disable_related_docs, :mode, :auto_size ]
6
+
7
+
8
+ # Displays the scribd object for the attachment on the given +object+. If
9
+ # +alt_text_or_attribute+ is given, then it will be used as the alternate text
10
+ # for an Attachment_fu model, or as the attribute name for a Paperclip
11
+ # model. If you want to specify alternate text for a Paperclip model, use the
12
+ # last parameter, +alt_text_if_paperclip+.
13
+ #
14
+ # If you are using Paperclip, you _must_ specify +alt_text_or_attribute+ as
15
+ # the attribute on which the scribd object exists.
16
+ #
17
+ # For example, using Attachment_fu:
18
+ # <%= display_scribd document %>
19
+ # <%= display_scribd document, 'You need Flash to view this document' %>
20
+ #
21
+ # Using Paperclip:
22
+ # <%= display_scribd user, :biography %>
23
+ # <%= display_scribd user, :biography, 'You need Flash for biographies." %>
24
+ def display_scribd(object, alt_text_or_attribute = '', alt_text_if_paperclip = nil)
25
+ # Resolve the right scribd ID, access key, and alt text.
26
+ if object.respond_to?("scribd_id")
27
+ scribd_id = object.scribd_id
28
+ scribd_ak = object.scribd_access_key
29
+
30
+ alt_text = alt_text_or_attribute
31
+ else
32
+ scribd_id = object.send "#{alt_text_or_attribute}_scribd_id"
33
+ scribd_ak = object.send "#{alt_text_or_attribute}_scribd_access_key"
34
+
35
+ alt_text = alt_text_if_paperclip
36
+ end
37
+
38
+ begin
39
+ # Collect a set of addParam statements to set up JS parameters for the scribd document
40
+ # (only if they are valid).
41
+ param_includes = options[:params].collect do |param, value|
42
+ "scribd_doc.addParam('#{param}', '#{value}');" if AVAILABLE_JS_PARAMS.include?(param)
43
+ end.compact.join("\n")
44
+ rescue
45
+ # Where is 'options' coming from???
46
+ end
47
+
48
+ <<-END
49
+ <script type="text/javascript" src="http://www.scribd.com/javascripts/view.js"></script>
50
+ <div id="embedded_flash">#{alt_text}</div>
51
+ <script type="text/javascript">
52
+ var scribd_doc = scribd.Document.getDoc(#{scribd_id}, '#{scribd_ak}');
53
+ #{param_includes}
54
+ scribd_doc.write("embedded_flash");
55
+ </script>
56
+ END
57
+ end
58
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'scribd_fu'
2
+ require 'scribd_fu_helper'
3
+
4
+ ActiveRecord::Base.send(:include, ScribdFu)
5
+ ActionView::Base.send(:include, ScribdFuHelper)
6
+
7
+ RAILS_DEFAULT_LOGGER.debug "** [Scribd_fu] loaded"
data/scribd_fu.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "scribd_fu"
3
+ s.version = "1.2.3.3"
4
+ s.date = "2009-04-26"
5
+ s.summary = "Quick and easy interactions with Scribd's iPaper service"
6
+ s.email = "matt@matt-darby.com"
7
+ s.homepage = "http://github.com/mdarby/scribd_fu/tree/master"
8
+ s.description = "A Rails plugin that streamlines interactions with the Scribd service"
9
+ s.has_rdoc = false
10
+ s.authors = ["Matt Darby"]
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
19
+
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jastix-scribd_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.3.3
5
+ platform: ruby
6
+ authors:
7
+ - Matt Darby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-26 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rscribd
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: A Rails plugin that streamlines interactions with the Scribd service
26
+ email: matt@matt-darby.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - init.rb
35
+ - install.rb
36
+ - uninstall.rb
37
+ - MIT-LICENSE
38
+ - Rakefile
39
+ - README
40
+ - lib/scribd_fu.rb
41
+ - lib/scribd_fu_helper.rb
42
+ - lib/attachment_fu/methods.rb
43
+ - lib/paperclip/methods.rb
44
+ - rails/init.rb
45
+ - scribd_fu.gemspec
46
+ - generators/scribd_config/scribd_config_generator.rb
47
+ - generators/scribd_config/templates/scribd.yml
48
+ has_rdoc: false
49
+ homepage: http://github.com/mdarby/scribd_fu/tree/master
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.2.0
71
+ signing_key:
72
+ specification_version: 2
73
+ summary: Quick and easy interactions with Scribd's iPaper service
74
+ test_files: []
75
+