Module: ScribdFu

Defined in:
lib/scribd_fu.rb,
lib/scribd_fu/paperclip.rb,
lib/scribd_fu/attachment_fu.rb

Defined Under Namespace

Modules: AttachmentFu, ClassMethods, InstanceMethods, Paperclip Classes: ScribdFuError, ScribdFuUploadError

Constant Summary collapse

ConfigPath =
"config/scribd_fu.yml".freeze
ContentTypes =

A list of content types supported by iPaper.

[
  'application/pdf',
  'application/msword',
  'application/mspowerpoint',
  'application/vnd.ms-powerpoint',
  'application/excel',
  'application/vnd.ms-excel',
  'application/postscript',
  'text/plain',
  'text/rtf',
  'application/rtf',
  'application/vnd.oasis.opendocument.text',
  'application/vnd.oasis.opendocument.presentation',
  'application/vnd.oasis.opendocument.spreadsheet',
  'application/vnd.sun.xml.writer',
  'application/vnd.sun.xml.impress',
  'application/vnd.sun.xml.calc',
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  'application/vnd.openxmlformats-officedocument.presentationml.template',
  'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  'application/vnd.openxmlformats-officedocument.wordprocessingml.template'
]
S3 =

RegExp that matches AWS S3 URLs

/^https?:\/\/s3.amazonaws.com/
CLOUD_FRONT =
/^http:\/\/[A-Za-z0-9]*.cloudfront.net/
Available_JS_Params =
[ :height, :width, :page, :my_user_id, :search_query,
:jsapi_version, :disable_related_docs, :mode, :auto_size, :hide_disabled_buttons, :hide_full_screen_button]

Class Method Summary collapse

Class Method Details

.access_levelObject

Get the preferred access level for iPaper documents



91
92
93
# File 'lib/scribd_fu.rb', line 91

def access_level
  config[:access] || 'private'
end

.amazon_based?(url) ⇒ Boolean

See if a URL is S3 or CloudFront based

Returns:

  • (Boolean)


108
109
110
# File 'lib/scribd_fu.rb', line 108

def amazon_based?(url)
  url =~ S3 || url =~ CLOUD_FRONT
end

.configObject

Read, store, and return the ScribdFu config file’s contents

Raises:



82
83
84
85
86
87
88
# File 'lib/scribd_fu.rb', line 82

def config
  path = defined?(Rails) ? File.join(Rails.root, ConfigPath) : ConfigPath
  raise ScribdFuError, "#{path} does not exist" unless File.file?(path)

  # Load the config file and strip any whitespace from the values
  @config ||= YAML.load_file(path).each_pair{|k,v| {k=>v.to_s.strip}}.symbolize_keys!
end

.destroy(document) ⇒ Object

Delete an iPaper document



77
78
79
# File 'lib/scribd_fu.rb', line 77

def destroy(document)
  document.destroy
end

.escape(str) ⇒ Object

Replace spaces with ‘%20’ (needed by Paperclip models).



103
104
105
# File 'lib/scribd_fu.rb', line 103

def escape(str)
  str.gsub(' ', '%20')
end

.included(base) ⇒ Object

:nodoc:



50
51
52
# File 'lib/scribd_fu.rb', line 50

def included(base) #:nodoc:
  base.extend ClassMethods
end

.load_ipaper_document(id) ⇒ Object

Load, store, and return the associated iPaper document



96
97
98
99
100
# File 'lib/scribd_fu.rb', line 96

def load_ipaper_document(id)
  # Yes, catch-all rescues are bad, but the end rescue
  # should return nil, so laziness FTW.
  scribd_user.find_document(id) rescue nil
end

.scribd_userObject

Login, store, and return a handle to the Scribd user account



55
56
57
58
59
60
61
62
63
64
# File 'lib/scribd_fu.rb', line 55

def scribd_user
  begin
    # Ensure we can login to Scribd, and get a handle on the account
    Scribd::API.instance.key    = config[:key]
    Scribd::API.instance.secret = config[:secret]
    @scribd_user = Scribd::User.(config[:user], config[:password])
  rescue
    raise ScribdFuError, "Your Scribd credentials are incorrect"
  end
end

.strip_cache_string(url) ⇒ Object

Strip off any trailing “?1234567890” cache strings They cause headaches on Scribd’s end.



114
115
116
117
# File 'lib/scribd_fu.rb', line 114

def strip_cache_string(url)
  pos = url.rindex('?')
  (pos) ? url[0, pos] : url
end

.upload(obj, file_path) ⇒ Object

Upload a file to Scribd



67
68
69
70
71
72
73
74
# File 'lib/scribd_fu.rb', line 67

def upload(obj, file_path)
  begin
    res = scribd_user.upload(:file => escape(file_path), :access => access_level)
    obj.update_attributes({:ipaper_id => res.doc_id, :ipaper_access_key => res.access_key})
  rescue
    raise ScribdFuUploadError, "Sorry, but #{obj.class} ##{obj.id} could not be uploaded to Scribd"
  end
end