nabeta-scribd_fu 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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.textile ADDED
@@ -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 mdarby-scribd_fu --source 'http://gems.github.com'</pre>
26
+
27
+ h3. Add ScribdFu to your environment.rb
28
+
29
+ <pre>config.gem 'mdarby-scribd_fu', :lib => 'scribd_fu', :source => 'http://gems.github.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")
data/lib/scribd_fu.rb ADDED
@@ -0,0 +1,258 @@
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
+ obj.ipaper_id = res.doc_id
71
+ obj.ipaper_access_key = res.access_key
72
+ obj.save(false)
73
+ rescue
74
+ raise ScribdFuUploadError, "Sorry, but #{obj.class} ##{obj.id} could not be uploaded to Scribd"
75
+ end
76
+ end
77
+
78
+ # Delete an iPaper document
79
+ def destroy(document)
80
+ document.destroy
81
+ end
82
+
83
+ # Read, store, and return the ScribdFu config file's contents
84
+ def config
85
+ raise ScribdFuError, "#{ConfigPath} does not exist" unless File.file?(ConfigPath)
86
+
87
+ # Load the config file and strip any whitespace from the values
88
+ @config ||= YAML.load_file(ConfigPath).each_pair{|k,v| {k=>v.to_s.strip}}.symbolize_keys!
89
+ end
90
+
91
+ # Get the preferred access level for iPaper documents
92
+ def access_level
93
+ config[:access] || 'private'
94
+ end
95
+
96
+ # Load, store, and return the associated iPaper document
97
+ def load_ipaper_document(id)
98
+ # Yes, catch-all rescues are bad, but the end rescue
99
+ # should return nil, so laziness FTW.
100
+ scribd_user.find_document(id) rescue nil
101
+ end
102
+
103
+ # Replace spaces with '%20' (needed by Paperclip models).
104
+ def escape(str)
105
+ str.gsub(' ', '%20')
106
+ end
107
+
108
+ end
109
+
110
+ module ClassMethods
111
+
112
+ # Load and inject ScribdFu goodies
113
+ def has_ipaper_and_uses(str)
114
+ check_environment
115
+ load_base_plugin(str)
116
+
117
+ include InstanceMethods
118
+
119
+ after_save :upload_to_scribd # This *MUST* be an after_save
120
+ before_destroy :destroy_ipaper_document
121
+ end
122
+
123
+ private
124
+
125
+ # Configure ScribdFu for this particular environment
126
+ def check_environment
127
+ load_rscribd
128
+ check_config
129
+ check_fields
130
+ end
131
+
132
+ def check_config
133
+ ScribdFu::config
134
+ end
135
+
136
+ # Load the rscribd gem
137
+ def load_rscribd
138
+ begin
139
+ require 'rscribd'
140
+ rescue LoadError
141
+ raise ScribdFuError, 'Please install the rscribd gem'
142
+ end
143
+ end
144
+
145
+ # Load Attachment_Fu specific methods and files
146
+ def load_attachment_fu
147
+ require 'scribd_fu/attachment_fu'
148
+ include ScribdFu::AttachmentFu::InstanceMethods
149
+ end
150
+
151
+ # Load Paperclip specific methods and files
152
+ def load_paperclip
153
+ require 'scribd_fu/paperclip'
154
+ include ScribdFu::Paperclip::InstanceMethods
155
+ end
156
+
157
+ # Ensure ScribdFu-centric attributes exist
158
+ def check_fields
159
+ fields = %w{ipaper_id ipaper_access_key}.inject([]){|stack, f| stack << "#{name}##{f}" unless column_names.include?(f); stack}
160
+ raise ScribdFuError, "These fields are missing: #{fields.to_sentence}" if fields.size > 0
161
+ end
162
+
163
+ # Load either AttachmentFu or Paperclip-specific methods
164
+ def load_base_plugin(str)
165
+ if str == 'AttachmentFu'
166
+ load_attachment_fu
167
+ elsif str == 'Paperclip'
168
+ load_paperclip
169
+ else
170
+ raise ScribdFuError, "Sorry, only Attachment_fu and Paperclip are supported."
171
+ end
172
+ end
173
+
174
+ end
175
+
176
+ module InstanceMethods
177
+
178
+ def self.included(base)
179
+ base.extend ClassMethods
180
+ end
181
+
182
+ # Upload the associated file to Scribd for iPaper conversion
183
+ # This is called +after_save+ and cannot be called earlier,
184
+ # so don't get any ideas.
185
+ def upload_to_scribd
186
+ ScribdFu::upload(self, file_path) if scribdable?
187
+ end
188
+
189
+ # Checks whether the associated file is convertable to iPaper
190
+ def scribdable?
191
+ ContentTypes.include?(get_content_type) && ipaper_id.blank?
192
+ end
193
+
194
+ # Responds true if the conversion is converting
195
+ def conversion_processing?
196
+ !(conversion_complete? || conversion_successful? || conversion_error?)
197
+ end
198
+
199
+ # Responds true if the conversion is complete -- note that this gives no
200
+ # indication as to whether the conversion had an error or was succesful,
201
+ # just that the conversion completed.
202
+ def conversion_complete?
203
+ ipaper_document && ipaper_document.conversion_status != 'PROCESSING'
204
+ end
205
+
206
+ # Responds true if the document has been converted.
207
+ def conversion_successful?
208
+ ipaper_document && ipaper_document.conversion_status =~ /^DISPLAYABLE|DONE$/
209
+ end
210
+
211
+ # Responds true if there was a conversion error while converting to iPaper.
212
+ def conversion_error?
213
+ ipaper_document && ipaper_document.conversion_status == 'ERROR'
214
+ end
215
+
216
+ # Responds the Scribd::Document associated with this model, or nil if it does not exist.
217
+ def ipaper_document
218
+ @document ||= ScribdFu::load_ipaper_document(ipaper_id)
219
+ end
220
+
221
+ # Destroys the scribd document for this record. This is called +before_destroy+
222
+ def destroy_ipaper_document
223
+ ScribdFu::destroy(ipaper_document) if ipaper_document
224
+ end
225
+
226
+ # Display the iPaper document in a view
227
+ def display_ipaper(options = {})
228
+ <<-END
229
+ <script type="text/javascript" src="http://www.scribd.com/javascripts/view.js"></script>
230
+ <div id="embedded_flash">#{options.delete(:alt)}</div>
231
+ <script type="text/javascript">
232
+ var scribd_doc = scribd.Document.getDoc(#{ipaper_id}, '#{ipaper_access_key}');
233
+ #{js_params(options)}
234
+ scribd_doc.write("embedded_flash");
235
+ </script>
236
+ END
237
+ end
238
+
239
+
240
+ private
241
+
242
+ # Check and collect any Javascript params that might have been passed in
243
+ def js_params(options)
244
+ opt = []
245
+
246
+ options.each_pair do |k, v|
247
+ opt << "scribd_doc.addParam('#{k}', '#{v}');" if Available_JS_Params.include?(k)
248
+ end
249
+
250
+ opt.compact.join("\n")
251
+ end
252
+
253
+ end
254
+
255
+ end
256
+
257
+ # Let's do this.
258
+ 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
data/scribd_fu.gemspec ADDED
@@ -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", "Kosuke Tanabe"]
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
data/spec/database.yml ADDED
@@ -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,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nabeta-scribd_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Matt Darby
8
+ - Kosuke Tanabe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-04-12 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rscribd
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ version:
26
+ description: A Rails plugin that streamlines interactions with the Scribd service
27
+ email: matt@matt-darby.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files: []
33
+
34
+ files:
35
+ - MIT-LICENSE
36
+ - README.textile
37
+ - generators/scribd_fu
38
+ - generators/scribd_fu/scribd_fu_generator.rb
39
+ - generators/scribd_fu/templates
40
+ - generators/scribd_fu/templates/scribd_fu.yml
41
+ - init.rb
42
+ - lib/scribd_fu
43
+ - lib/scribd_fu/attachment_fu.rb
44
+ - lib/scribd_fu/paperclip.rb
45
+ - lib/scribd_fu.rb
46
+ - scribd_fu.gemspec
47
+ - spec/database.yml
48
+ - spec/scribd_fu.yml
49
+ - spec/scribd_fu_spec.rb
50
+ - spec/spec_helper.rb
51
+ has_rdoc: false
52
+ homepage: http://github.com/mdarby/scribd_fu/tree/master
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.2.0
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: Quick and easy interactions with Scribd's iPaper service
77
+ test_files: []
78
+