Module: ScribdFu

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

Defined Under Namespace

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

Constant Summary

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

+ (Object) access_level

Get the preferred access level for iPaper documents



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

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

+ (Boolean) amazon_based?(url)

See if a URL is S3 or CloudFront based



120
121
122
# File 'lib/scribd_fu.rb', line 120

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

+ (Object) config

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

Raises:



89
90
91
92
93
94
95
# File 'lib/scribd_fu.rb', line 89

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

+ (Object) destroy(document)

Delete an iPaper document



84
85
86
# File 'lib/scribd_fu.rb', line 84

def destroy(document)
  document.destroy
end

+ (Object) escape(str)

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



110
111
112
113
# File 'lib/scribd_fu.rb', line 110

def escape(str)
  basename = File.basename(str, ".*")
  File.join(File.dirname(str), "#{url_encode(basename)}#{File.extname(str)}").to_s
end

+ (Object) included(base)

:nodoc:



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

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

+ (Object) load_ipaper_document(id)

Load, store, and return the associated iPaper document



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

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

+ (Object) scribd_user

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

+ (Object) strip_cache_string(url)

Strip off any trailing "?1234567890" cache strings They cause headaches on Scribd's end.



126
127
128
129
# File 'lib/scribd_fu.rb', line 126

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

+ (Object) upload(obj, file_path)

Upload a file to Scribd



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/scribd_fu.rb', line 67

def upload(obj, file_path)
  begin
    args = { :file => escape(file_path), :access => access_level }
    res = if obj.ipaper_my_user_id
      scribd_user
      args[:my_user_id] = obj.ipaper_my_user_id
      Scribd::Document.create(args)
    else
      scribd_user.upload(args)
    end
    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

+ (Object) url_encode(str)



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

def url_encode(str)
  str.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
end