scribd_fu 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -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.
@@ -0,0 +1,68 @@
1
+ h1. Scribd_fu
2
+
3
+ A Ruby on Rails plugin that streamlines interaction with Scribd.com's iPaper service, and works along side of either Attachment_fu or Paperclip
4
+
5
+ h1. Big Changes! v2.0 OMGZ!
6
+
7
+ Please make sure to read below if you're using an older version of ScribdFu. Pretty much everything has changed. Don't fret, it's now easier and cleaner. ScribdFu has been taken out into the backyard and given a thorough scrubbing. It's now lean, mean, fully tested, and works beautifully with Attachment_fu, Paperclip, and Amazon's S3 service.
8
+
9
+ h2. What it does
10
+
11
+ 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 embedding documents inline with your web UI — no downloading, no required programs on the client side (except for Flash) to view your data. It's pretty damned cool, and free to boot!
12
+
13
+ h2. Requirements
14
+
15
+ Scribd_fu requires the rscribd gem via @sudo gem install rscribd@ (along with either the Attachment_fu plugin or the Paperclip gem)
16
+
17
+ h2. How to Install
18
+
19
+ h3. ScribdFu requires a base plugin
20
+
21
+ ScribdFu is awesome, but it needs help from either "Attachment_fu":http://github.com/technoweenie/attachment_fu/tree/master or "Paperclip":http://github.com/thoughtbot/paperclip/tree/master
22
+
23
+ h3. Install ScribdFu
24
+
25
+ <pre>sudo gem install scribd_fu --source 'http://gemcutter.com'</pre>
26
+
27
+ h3. Add ScribdFu to your environment.rb
28
+
29
+ <pre>config.gem 'scribd_fu', :source => 'http://gemcutter.com'</pre>
30
+
31
+ h2. How to Use
32
+
33
+ * Sign up for "Scribd.com's API access":http://www.scribd.com/developers/signup_api (it's totally free)
34
+
35
+ * Run @./script/generate scribd_fu@ and fill out @config/scribd_fu.yml@ file with your Scribd account info.
36
+
37
+ * If you're using AttachmentFu, add @has_ipaper_and_uses 'AttachmentFu'@ to your model that uses ScribdFu
38
+
39
+ * If you're using Paperclip, add @has_ipaper_and_uses 'Paperclip'@ to your model that uses ScribdFu
40
+
41
+ * Add the following fields into a new migration for the target model:
42
+
43
+ <pre>
44
+ t.integer :ipaper_id
45
+ t.string :ipaper_access_key
46
+ </pre>
47
+
48
+ Scribd_fu will use these to track Scribd-related information.
49
+
50
+ Now, when you upload a file to that model, Scribd_fu will automatically handle the Scribd side of things for you. Files are uploaded to Scribd after the model is saved. No muss, no fuss. Scribd's conversion of your document is usally quite fast, but if you want a contingency plan, the @conversion_processing?@, @conversion_complete?@, @conversion_successful?@, and @conversion_error?@ instance methods can be used to determine the conversion status of the document.
51
+
52
+ h2. Viewing the iPaper Document
53
+
54
+ Just add @<%= document.display_ipaper %>@ into your view (where @document@ is an object of your ScribdFu model). That's it!
55
+
56
+ h2. Access
57
+
58
+ You can set the access level on all documents by setting the @access@ key in the scribd_fu.yml file to either 'private' or 'public'. ScribdFu assumes that you'd like to keep your documents private.
59
+
60
+ h2. Help
61
+
62
+ Add a ticket to "ScribdFu's Lighthouse Account":http://mdarby.lighthouseapp.com/projects/28697-scribdfu/overview
63
+
64
+ h2. About the Author
65
+
66
+ My name is "Matt Darby.":http://blog.matt-darby.com I’m an IT Manager and pro-web-dev at for "Dynamix Engineering":http://dynamix-ltd.com and hold a Master’s Degree in Computer Science from "Franklin University":http://www.franklin.edu in sunny "Columbus, OH.":http://en.wikipedia.org/wiki/Columbus,_Ohio
67
+
68
+ Feel free to check out my "blog":http://blog.matt-darby.com or "recommend me":http://www.workingwithrails.com/person/10908-matt-darby
@@ -0,0 +1,7 @@
1
+ class ScribdFuGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.file 'scribd_fu.yml', 'config/scribd_fu.yml', :collision => :skip
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ #Scribd Account Info
2
+
3
+ key:
4
+ secret:
5
+ user:
6
+ password:
7
+ access:
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "lib", "scribd_fu")
@@ -0,0 +1,255 @@
1
+ module ScribdFu
2
+
3
+ ConfigPath = "#{RAILS_ROOT}/config/scribd_fu.yml".freeze
4
+
5
+ # A list of content types supported by iPaper.
6
+ ContentTypes = [
7
+ 'application/pdf',
8
+ 'application/msword',
9
+ 'application/mspowerpoint',
10
+ 'application/vnd.ms-powerpoint',
11
+ 'application/excel',
12
+ 'application/vnd.ms-excel',
13
+ 'application/postscript',
14
+ 'text/plain',
15
+ 'text/rtf',
16
+ 'application/rtf',
17
+ 'application/vnd.oasis.opendocument.text',
18
+ 'application/vnd.oasis.opendocument.presentation',
19
+ 'application/vnd.oasis.opendocument.spreadsheet',
20
+ 'application/vnd.sun.xml.writer',
21
+ 'application/vnd.sun.xml.impress',
22
+ 'application/vnd.sun.xml.calc',
23
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
24
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
25
+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
26
+ 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
27
+ 'application/vnd.openxmlformats-officedocument.presentationml.template',
28
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
29
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.template'
30
+ ]
31
+
32
+ # RegExp that matches AWS S3 URLs
33
+ S3 = /^https?:\/\/s3.amazonaws.com/
34
+
35
+ # Available parameters for the JS API
36
+ # http://www.scribd.com/publisher/api/api?method_name=Javascript+API
37
+ Available_JS_Params = [ :height, :width, :page, :my_user_id, :search_query,
38
+ :jsapi_version, :disable_related_docs, :mode, :auto_size ]
39
+
40
+ class ScribdFuError < StandardError #:nodoc:
41
+ end
42
+
43
+ class ScribdFuUploadError < ScribdFuError #:nodoc:
44
+ end
45
+
46
+
47
+ class << self
48
+
49
+ def included(base) #:nodoc:
50
+ base.extend ClassMethods
51
+ end
52
+
53
+ # Login, store, and return a handle to the Scribd user account
54
+ def scribd_user
55
+ begin
56
+ # Ensure we can login to Scribd, and get a handle on the account
57
+ Scribd::API.instance.key = config[:key]
58
+ Scribd::API.instance.secret = config[:secret]
59
+ @scribd_user = Scribd::User.login(config[:user], config[:password])
60
+ rescue
61
+ raise ScribdFuError, "Your Scribd credentials are incorrect"
62
+ end
63
+ end
64
+
65
+ # Upload a file to Scribd
66
+ def upload(obj, file_path)
67
+ begin
68
+ res = scribd_user.upload(:file => escape(file_path), :access => access_level)
69
+ obj.update_attributes({:ipaper_id => res.doc_id, :ipaper_access_key => res.access_key})
70
+ rescue
71
+ raise ScribdFuUploadError, "Sorry, but #{obj.class} ##{obj.id} could not be uploaded to Scribd"
72
+ end
73
+ end
74
+
75
+ # Delete an iPaper document
76
+ def destroy(document)
77
+ document.destroy
78
+ end
79
+
80
+ # Read, store, and return the ScribdFu config file's contents
81
+ def config
82
+ raise ScribdFuError, "#{ConfigPath} does not exist" unless File.file?(ConfigPath)
83
+
84
+ # Load the config file and strip any whitespace from the values
85
+ @config ||= YAML.load_file(ConfigPath).each_pair{|k,v| {k=>v.to_s.strip}}.symbolize_keys!
86
+ end
87
+
88
+ # Get the preferred access level for iPaper documents
89
+ def access_level
90
+ config[:access] || 'private'
91
+ end
92
+
93
+ # Load, store, and return the associated iPaper document
94
+ def load_ipaper_document(id)
95
+ # Yes, catch-all rescues are bad, but the end rescue
96
+ # should return nil, so laziness FTW.
97
+ scribd_user.find_document(id) rescue nil
98
+ end
99
+
100
+ # Replace spaces with '%20' (needed by Paperclip models).
101
+ def escape(str)
102
+ str.gsub(' ', '%20')
103
+ end
104
+
105
+ end
106
+
107
+ module ClassMethods
108
+
109
+ # Load and inject ScribdFu goodies
110
+ def has_ipaper_and_uses(str)
111
+ check_environment
112
+ load_base_plugin(str)
113
+
114
+ include InstanceMethods
115
+
116
+ after_save :upload_to_scribd # This *MUST* be an after_save
117
+ before_destroy :destroy_ipaper_document
118
+ end
119
+
120
+ private
121
+
122
+ # Configure ScribdFu for this particular environment
123
+ def check_environment
124
+ load_rscribd
125
+ check_config
126
+ check_fields
127
+ end
128
+
129
+ def check_config
130
+ ScribdFu::config
131
+ end
132
+
133
+ # Load the rscribd gem
134
+ def load_rscribd
135
+ begin
136
+ require 'rscribd'
137
+ rescue LoadError
138
+ raise ScribdFuError, 'Please install the rscribd gem'
139
+ end
140
+ end
141
+
142
+ # Load Attachment_Fu specific methods and files
143
+ def load_attachment_fu
144
+ require 'scribd_fu/attachment_fu'
145
+ include ScribdFu::AttachmentFu::InstanceMethods
146
+ end
147
+
148
+ # Load Paperclip specific methods and files
149
+ def load_paperclip
150
+ require 'scribd_fu/paperclip'
151
+ include ScribdFu::Paperclip::InstanceMethods
152
+ end
153
+
154
+ # Ensure ScribdFu-centric attributes exist
155
+ def check_fields
156
+ fields = %w{ipaper_id ipaper_access_key}.inject([]){|stack, f| stack << "#{name}##{f}" unless column_names.include?(f); stack}
157
+ raise ScribdFuError, "These fields are missing: #{fields.to_sentence}" if fields.size > 0
158
+ end
159
+
160
+ # Load either AttachmentFu or Paperclip-specific methods
161
+ def load_base_plugin(str)
162
+ if str == 'AttachmentFu'
163
+ load_attachment_fu
164
+ elsif str == 'Paperclip'
165
+ load_paperclip
166
+ else
167
+ raise ScribdFuError, "Sorry, only Attachment_fu and Paperclip are supported."
168
+ end
169
+ end
170
+
171
+ end
172
+
173
+ module InstanceMethods
174
+
175
+ def self.included(base)
176
+ base.extend ClassMethods
177
+ end
178
+
179
+ # Upload the associated file to Scribd for iPaper conversion
180
+ # This is called +after_save+ and cannot be called earlier,
181
+ # so don't get any ideas.
182
+ def upload_to_scribd
183
+ ScribdFu::upload(self, file_path) if scribdable?
184
+ end
185
+
186
+ # Checks whether the associated file is convertable to iPaper
187
+ def scribdable?
188
+ ContentTypes.include?(get_content_type) && ipaper_id.blank?
189
+ end
190
+
191
+ # Responds true if the conversion is converting
192
+ def conversion_processing?
193
+ !(conversion_complete? || conversion_successful? || conversion_error?)
194
+ end
195
+
196
+ # Responds true if the conversion is complete -- note that this gives no
197
+ # indication as to whether the conversion had an error or was succesful,
198
+ # just that the conversion completed.
199
+ def conversion_complete?
200
+ ipaper_document && ipaper_document.conversion_status != 'PROCESSING'
201
+ end
202
+
203
+ # Responds true if the document has been converted.
204
+ def conversion_successful?
205
+ ipaper_document && ipaper_document.conversion_status =~ /^DISPLAYABLE|DONE$/
206
+ end
207
+
208
+ # Responds true if there was a conversion error while converting to iPaper.
209
+ def conversion_error?
210
+ ipaper_document && ipaper_document.conversion_status == 'ERROR'
211
+ end
212
+
213
+ # Responds the Scribd::Document associated with this model, or nil if it does not exist.
214
+ def ipaper_document
215
+ @document ||= ScribdFu::load_ipaper_document(ipaper_id)
216
+ end
217
+
218
+ # Destroys the scribd document for this record. This is called +before_destroy+
219
+ def destroy_ipaper_document
220
+ ScribdFu::destroy(ipaper_document) if ipaper_document
221
+ end
222
+
223
+ # Display the iPaper document in a view
224
+ def display_ipaper(options = {})
225
+ <<-END
226
+ <script type="text/javascript" src="http://www.scribd.com/javascripts/view.js"></script>
227
+ <div id="embedded_flash">#{options.delete(:alt)}</div>
228
+ <script type="text/javascript">
229
+ var scribd_doc = scribd.Document.getDoc(#{ipaper_id}, '#{ipaper_access_key}');
230
+ #{js_params(options)}
231
+ scribd_doc.write("embedded_flash");
232
+ </script>
233
+ END
234
+ end
235
+
236
+
237
+ private
238
+
239
+ # Check and collect any Javascript params that might have been passed in
240
+ def js_params(options)
241
+ opt = []
242
+
243
+ options.each_pair do |k, v|
244
+ opt << "scribd_doc.addParam('#{k}', '#{v}');" if Available_JS_Params.include?(k)
245
+ end
246
+
247
+ opt.compact.join("\n")
248
+ end
249
+
250
+ end
251
+
252
+ end
253
+
254
+ # Let's do this.
255
+ ActiveRecord::Base.send(:include, ScribdFu) if Object.const_defined?("ActiveRecord")
@@ -0,0 +1,30 @@
1
+ module ScribdFu
2
+ module AttachmentFu
3
+
4
+ module ClassMethods
5
+ end
6
+
7
+ module InstanceMethods
8
+
9
+ def self.included(base)
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ # Returns a URL for a thumbnail for this model's attachment.
14
+ def thumbnail_url
15
+ (ipaper_document && ipaper_document.thumbnail_url) || public_filename(:thumb)
16
+ end
17
+
18
+ # Returns the content type for this model's attachment.
19
+ def get_content_type
20
+ self.content_type
21
+ end
22
+
23
+ # Yields the correct path to the file, either the local filename or the S3 URL.
24
+ def file_path
25
+ public_filename =~ ScribdFu::S3 ? public_filename : "#{RAILS_ROOT}/public#{public_filename}"
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,51 @@
1
+ module ScribdFu
2
+ module Paperclip
3
+
4
+ module ClassMethods
5
+ end
6
+
7
+ module InstanceMethods
8
+
9
+ def self.included(base)
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ # Find the content type of the associated file
14
+ def get_content_type
15
+ self.send("#{prefix}_content_type")
16
+ end
17
+
18
+ # Returns a URL for a thumbnail for the attached file object.
19
+ def thumbnail_url
20
+ begin
21
+ (ipaper_document && ipaper_document.thumbnail_url) || attached_file.url(:thumb)
22
+ rescue
23
+ raise ScribdFu::ScribdFuError, "The thumbnail doesn't exist."
24
+ end
25
+ end
26
+
27
+ # Returns the full filename for the given attribute. If the file is
28
+ # stored on S3, this is a full S3 URI, while it is a full path to the
29
+ # local file if the file is stored locally.
30
+ def file_path
31
+ attached_file.url =~ ScribdFu::S3 ? attached_file.url : attached_file.path
32
+ end
33
+
34
+
35
+ private
36
+
37
+ # Figure out what Paperclip is calling the attached file object
38
+ # ie. has_attached_file :attachment => "attachment"
39
+ def prefix
40
+ @prefix ||= self.class.column_names.detect{|c| c.ends_with?("_file_name")}.gsub("_file_name", '')
41
+ end
42
+
43
+ # Return the attached file object
44
+ def attached_file
45
+ @file ||= self.send(prefix)
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,30 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "scribd_fu"
3
+ s.version = "2.0.4"
4
+ s.date = "2009-04-12"
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 = [
12
+ 'MIT-LICENSE',
13
+ 'README.textile',
14
+ 'generators/scribd_fu',
15
+ 'generators/scribd_fu/scribd_fu_generator.rb',
16
+ 'generators/scribd_fu/templates',
17
+ 'generators/scribd_fu/templates/scribd_fu.yml',
18
+ 'init.rb',
19
+ 'lib/scribd_fu',
20
+ 'lib/scribd_fu/attachment_fu.rb',
21
+ 'lib/scribd_fu/paperclip.rb',
22
+ 'lib/scribd_fu.rb',
23
+ 'scribd_fu.gemspec',
24
+ 'spec/database.yml',
25
+ 'spec/scribd_fu.yml',
26
+ 'spec/scribd_fu_spec.rb',
27
+ 'spec/spec_helper.rb'
28
+ ]
29
+ s.add_dependency 'rscribd'
30
+ end
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
@@ -0,0 +1,7 @@
1
+ #Scribd Account Info
2
+
3
+ key: 'key'
4
+ secret: 'secret'
5
+ user: 'user'
6
+ password: 'password'
7
+ access: 'access'
@@ -0,0 +1,308 @@
1
+ require File.join(File.dirname(__FILE__), %w[spec_helper])
2
+
3
+ describe "ScribdFu" do
4
+
5
+ describe "with incorrect Scribd credentials" do
6
+ before do
7
+ Scribd::User.stub!(:login).and_raise(Scribd::ResponseError)
8
+ end
9
+
10
+ it "should throw an error" do
11
+ lambda { ScribdFu::scribd_user }.should raise_error(ScribdFu::ScribdFuError)
12
+ end
13
+ end
14
+
15
+ describe "that is missing a config file" do
16
+ before do
17
+ File.should_receive(:file?).with("#{RAILS_ROOT}/config/scribd_fu.yml").and_return(false)
18
+ end
19
+
20
+ it "should raise an error" do
21
+ lambda { ScribdFu::config }.should raise_error(ScribdFu::ScribdFuError)
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+
28
+ describe "An AttachmentFu model" do
29
+ before do
30
+ rebuild_model
31
+ end
32
+
33
+ describe "that is configured for ScribdFu" do
34
+ before do
35
+ config = YAML.load_file("spec/scribd_fu.yml")
36
+ File.stub!(:file?).with(ScribdFu::ConfigPath).and_return(true)
37
+ YAML.stub!(:load_file).and_return(config)
38
+
39
+ Document.class_eval do
40
+ has_ipaper_and_uses 'AttachmentFu'
41
+ end
42
+
43
+ @document = Document.new
44
+ end
45
+
46
+ describe "with correct credentials" do
47
+ before do
48
+ @scribd_user = mock("scribd_user")
49
+ Scribd::User.stub!(:login).and_return(@scribd_user)
50
+ end
51
+
52
+ describe "that was just created" do
53
+ before do
54
+ @document.attributes = {:ipaper_id => nil, :ipaper_access_key => nil, :content_type => "application/pdf"}
55
+ @document.stub!(:public_filename => "/path/to/somewhere")
56
+ end
57
+
58
+ describe "and is scribdable?" do
59
+ before do
60
+ @document.stub!(:update_attributes)
61
+ end
62
+
63
+ describe "and has spaces in the filename" do
64
+ it "should sanitize the file path" do
65
+ res = mock('response', :doc_id => 1, :access_key => "ASDF")
66
+ @scribd_user.should_receive(:upload).with(:file => "some%20filename%20with%20spaces", :access => 'access').and_return(res)
67
+ ScribdFu::upload(@document, "some filename with spaces")
68
+ end
69
+
70
+ end
71
+
72
+ describe "and uploading to Scribd succeeded" do
73
+ before do
74
+ @scribd_response = mock('scribd_response', :doc_id => "doc_id", :access_key => "access_key")
75
+ @scribd_user.should_receive(:upload).and_return(@scribd_response)
76
+ end
77
+
78
+ it "should update the Scribd-centric attributes" do
79
+ @document.should_receive(:update_attributes).with({:ipaper_id => 'doc_id', :ipaper_access_key => 'access_key'})
80
+ @document.save
81
+ end
82
+
83
+ end
84
+
85
+ describe "and uploading to Scribd failed" do
86
+ before do
87
+ @scribd_user.stub!(:upload).and_raise(StandardError)
88
+ end
89
+
90
+ it "should throw an error" do
91
+ lambda { ScribdFu::upload(@document, 'private') }.should raise_error(ScribdFu::ScribdFuUploadError)
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ describe "and is not scribdable?" do
99
+ before do
100
+ @document.stub!(:scribdable? => false)
101
+ end
102
+
103
+ it "should not upload to Scribd" do
104
+ ScribdFu.should_not_receive(:upload)
105
+ @document.save
106
+ end
107
+
108
+ it "should not error out when deleted" do
109
+ lambda {@document.destroy}.should_not raise_error(ScribdFu::ScribdFuError)
110
+ end
111
+
112
+ end
113
+
114
+ end
115
+
116
+ describe "that was just updated" do
117
+ before do
118
+ @document.stub!(:ipaper_id => 'doc_id')
119
+ end
120
+
121
+ it "should not reupload to Scribd" do
122
+ @scribd_user.should_not_receive(:upload)
123
+ @document.save
124
+ end
125
+
126
+ end
127
+
128
+ describe "that is about to be destroyed" do
129
+ before do
130
+ @ipaper_document = mock("ipaper_document")
131
+ ScribdFu.stub!(:load_ipaper_document).and_return(@ipaper_document)
132
+ end
133
+
134
+ it "should destroy the ipaper document" do
135
+ ScribdFu.should_receive(:destroy).with(@ipaper_document)
136
+ @document.destroy
137
+ end
138
+
139
+ end
140
+
141
+ end
142
+
143
+ end
144
+
145
+ end
146
+
147
+ describe "A Paperclip model" do
148
+ before do
149
+ rebuild_model
150
+ end
151
+
152
+ describe "that is configured for ScribdFu" do
153
+ before do
154
+ config = YAML.load_file("spec/scribd_fu.yml")
155
+ File.stub!(:file?).with(ScribdFu::ConfigPath).and_return(true)
156
+ YAML.stub!(:load_file).and_return(config)
157
+
158
+ Attachment.class_eval do
159
+ has_ipaper_and_uses 'Paperclip'
160
+ end
161
+
162
+ @attached_file = mock("attached_file", :url => "http://test.com/path/to/somewhere", :path => "/path/to/somewhere")
163
+
164
+ @attachment = Attachment.new
165
+ @attachment.stub!(:prefix).and_return("attachment")
166
+ @attachment.stub!(:attached_file).and_return(@attached_file)
167
+ end
168
+
169
+ describe "with correct credentials" do
170
+ before do
171
+ @scribd_user = mock("scribd_user")
172
+ Scribd::User.stub!(:login).and_return(@scribd_user)
173
+ end
174
+
175
+ describe "that was just created" do
176
+ before do
177
+ @attachment.attributes = {:ipaper_id => nil, :ipaper_access_key => nil, :attachment_content_type => "application/pdf"}
178
+ end
179
+
180
+ describe "and is scribdable?" do
181
+ before do
182
+ @attachment.stub!(:scribdable? => true)
183
+ end
184
+
185
+ describe "and has spaces in the filename" do
186
+ before do
187
+ @attached_file.stub!(:path => "/path/to/somewhere with spaces.pdf")
188
+ @attachment.stub!(:update_attributes)
189
+ end
190
+
191
+ it "should sanitize the file path" do
192
+ res = mock('response', :doc_id => 1, :access_key => "ASDF")
193
+ @scribd_user.should_receive(:upload).with(:file => "/path/to/somewhere%20with%20spaces.pdf", :access => 'access').and_return(res)
194
+ ScribdFu::upload(@attachment, "/path/to/somewhere with spaces.pdf")
195
+ end
196
+
197
+ end
198
+
199
+ describe "and uploading to Scribd succeeded" do
200
+ before do
201
+ @scribd_response = mock('scribd_response', :doc_id => "doc_id", :access_key => "access_key")
202
+ @scribd_user.should_receive(:upload).and_return(@scribd_response)
203
+ end
204
+
205
+ it "should update the Scribd-centric attributes" do
206
+ @attachment.should_receive(:update_attributes).with({:ipaper_id => 'doc_id', :ipaper_access_key => 'access_key'})
207
+ @attachment.save
208
+ end
209
+
210
+ end
211
+
212
+ describe "and uploading to Scribd failed" do
213
+ before do
214
+ @scribd_user.stub!(:upload).and_raise(StandardError)
215
+ end
216
+
217
+ it "should throw an error" do
218
+ lambda { ScribdFu::upload(@attachment, 'private') }.should raise_error(ScribdFu::ScribdFuUploadError)
219
+ end
220
+
221
+ end
222
+
223
+ end
224
+
225
+ describe "and is not scribdable?" do
226
+ before do
227
+ @attachment.stub!(:scribdable? => false)
228
+ end
229
+
230
+ it "should not upload to Scribd" do
231
+ ScribdFu.should_not_receive(:upload)
232
+ @attachment.save
233
+ end
234
+
235
+ end
236
+
237
+ end
238
+
239
+ describe "that was just updated" do
240
+ before do
241
+ @attachment.stub!(:ipaper_id => 'doc_id')
242
+ end
243
+
244
+ it "should not reupload to Scribd" do
245
+ @scribd_user.should_not_receive(:upload)
246
+ @attachment.save
247
+ end
248
+
249
+ end
250
+
251
+ describe "that is about to be destroyed" do
252
+ before do
253
+ @ipaper_document = mock("ipaper_document")
254
+ ScribdFu.stub!(:load_ipaper_document).and_return(@ipaper_document)
255
+ end
256
+
257
+ it "should destroy the ipaper document" do
258
+ ScribdFu.should_receive(:destroy).with(@ipaper_document)
259
+ @attachment.destroy
260
+ end
261
+
262
+ end
263
+
264
+ end
265
+
266
+ end
267
+
268
+ end
269
+
270
+ describe "Viewing an iPaper document" do
271
+ before do
272
+ rebuild_model
273
+
274
+ config = YAML.load_file("spec/scribd_fu.yml")
275
+ File.stub!(:file?).with(ScribdFu::ConfigPath).and_return(true)
276
+ YAML.stub!(:load_file).and_return(config)
277
+
278
+ Document.class_eval do
279
+ has_ipaper_and_uses 'AttachmentFu'
280
+ end
281
+
282
+ @document = Document.new
283
+ @document.attributes = {:ipaper_id => 'doc_id', :ipaper_access_key => 'access_key'}
284
+ end
285
+
286
+ it "should return this HTML by default" do
287
+ @document.display_ipaper.gsub(/\s{2,}/, "").should == "<script type=\"text/javascript\" src=\"http://www.scribd.com/javascripts/view.js\"></script><div id=\"embedded_flash\"></div><script type=\"text/javascript\">var scribd_doc = scribd.Document.getDoc(doc_id, 'access_key');scribd_doc.write(\"embedded_flash\");</script>\n"
288
+ end
289
+
290
+ it "should allow custom alt text" do
291
+ @document.display_ipaper(:alt => "something").should =~ /.*<div id="embedded_flash">something<\/div>.*/
292
+ end
293
+
294
+ it "should allow custom Javascript params" do
295
+ options = {:height => 100, :width => 100}
296
+
297
+ @document.display_ipaper(options).should =~ /.*scribd_doc\.addParam\('height', '100'\);.*/
298
+ @document.display_ipaper(options).should =~ /.*scribd_doc\.addParam\('width', '100'\);.*/
299
+ end
300
+
301
+ it "should allow not allow crazy custom Javascript params" do
302
+ options = {:some_dumb_setting => 100, :width => 100}
303
+
304
+ @document.display_ipaper(options).should =~ /.*scribd_doc\.addParam\('width', '100'\);.*/
305
+ @document.display_ipaper(options).should_not =~ /.*scribd_doc\.addParam\('some_dumb_setting', '100'\);.*/
306
+ end
307
+
308
+ end
@@ -0,0 +1,42 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require 'rscribd'
4
+
5
+ ROOT = File.join(File.dirname(__FILE__), '..')
6
+ RAILS_ROOT = ROOT
7
+
8
+ $LOAD_PATH << File.join(ROOT, 'lib')
9
+ $LOAD_PATH << File.join(ROOT, 'lib', 'scribd_fu')
10
+
11
+ require File.join(ROOT, 'lib', 'scribd_fu.rb')
12
+
13
+ ENV['RAILS_ENV'] ||= 'test'
14
+
15
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
16
+ ActiveRecord::Base.establish_connection(config[ENV['RAILS_ENV'] || 'test'])
17
+
18
+ def rebuild_model options = {}
19
+ ActiveRecord::Base.connection.create_table :documents, :force => true do |table|
20
+ table.column :ipaper_id, :string
21
+ table.column :ipaper_access_key, :string
22
+ table.column :content_type, :string
23
+ end
24
+
25
+ ActiveRecord::Base.connection.create_table :attachments, :force => true do |table|
26
+ table.column :ipaper_id, :string
27
+ table.column :ipaper_access_key, :string
28
+ table.column :attachment_content_type, :string
29
+ end
30
+
31
+ Object.send(:remove_const, "Document") rescue nil
32
+ Object.const_set("Document", Class.new(ActiveRecord::Base))
33
+
34
+ Object.send(:remove_const, "Attachment") rescue nil
35
+ Object.const_set("Attachment", Class.new(ActiveRecord::Base))
36
+ end
37
+
38
+ Spec::Runner.configure do |config|
39
+ end
40
+
41
+
42
+ # EOF
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scribd_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Matt Darby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-12 00:00:00 -04: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
+ - MIT-LICENSE
35
+ - README.textile
36
+ - generators/scribd_fu/scribd_fu_generator.rb
37
+ - generators/scribd_fu/templates/scribd_fu.yml
38
+ - init.rb
39
+ - lib/scribd_fu/attachment_fu.rb
40
+ - lib/scribd_fu/paperclip.rb
41
+ - lib/scribd_fu.rb
42
+ - scribd_fu.gemspec
43
+ - spec/database.yml
44
+ - spec/scribd_fu.yml
45
+ - spec/scribd_fu_spec.rb
46
+ - spec/spec_helper.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/mdarby/scribd_fu/tree/master
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.5
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Quick and easy interactions with Scribd's iPaper service
75
+ test_files: []
76
+