Module: Paperclip::Storage::Dropbox

Defined in:
lib/paperclip/storage/dropbox.rb,
lib/paperclip/storage/dropbox/credentials.rb,
lib/paperclip/storage/dropbox/url_generator.rb,
lib/paperclip/storage/dropbox/path_generator.rb,
lib/paperclip/storage/dropbox/generator_factory.rb,
lib/paperclip/storage/dropbox/public_url_generator.rb,
lib/paperclip/storage/dropbox/private_url_generator.rb

Defined Under Namespace

Modules: GeneratorFactory Classes: Credentials, FileExists, PathGenerator, PrivateUrlGenerator, PublicUrlGenerator, UrlGenerator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/paperclip/storage/dropbox.rb', line 11

def self.extended(base)
  base.instance_eval do
    @options[:dropbox_options] ||= {}
    @options[:dropbox_credentials] = fetch_credentials
    @options[:path] = nil if @options[:path] == self.class.default_options[:path]
    @options[:dropbox_visibility] ||= "public"

    @path_generator = PathGenerator.new(self, @options)
    @url_generator = GeneratorFactory.build_url_generator(self, @options)

    dropbox_client # Force creation of dropbox_client
  end
end

Instance Method Details

#copy_to_local_file(style = default_style, destination_path) ⇒ Object



52
53
54
55
56
# File 'lib/paperclip/storage/dropbox.rb', line 52

def copy_to_local_file(style = default_style, destination_path)
  File.open(destination_path, "wb") do |file|
    file.write(dropbox_client.get_file(path(style)))
  end
end

#dropbox_clientObject



66
67
68
69
70
71
72
73
74
# File 'lib/paperclip/storage/dropbox.rb', line 66

def dropbox_client
  @dropbox_client ||= begin
    @options[:dropbox_credentials][:access_type] ||= "dropbox"
    credentials = @options[:dropbox_credentials]
    session = DropboxSession.new(credentials[:app_key], credentials[:app_secret])
    session.set_access_token(credentials[:access_token], credentials[:access_token_secret])
    DropboxClient.new(session, credentials[:access_type])
  end
end

#exists?(style = default_style) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
# File 'lib/paperclip/storage/dropbox.rb', line 58

def exists?(style = default_style)
  return false if not present?
   = dropbox_client.(path(style))
  not .nil? and not ["is_deleted"]
rescue DropboxError
  false
end

#flush_deletesObject



33
34
35
36
37
38
# File 'lib/paperclip/storage/dropbox.rb', line 33

def flush_deletes
  @queued_for_delete.each do |path|
    dropbox_client.file_delete(path)
  end
  @queued_for_delete.clear
end

#flush_writesObject



25
26
27
28
29
30
31
# File 'lib/paperclip/storage/dropbox.rb', line 25

def flush_writes
  @queued_for_write.each do |style, file|
    dropbox_client.put_file(path(style), file.read)
  end
  after_flush_writes
  @queued_for_write.clear
end

#path(style = default_style) ⇒ Object



46
47
48
49
50
# File 'lib/paperclip/storage/dropbox.rb', line 46

def path(style = default_style)
  path = @path_generator.generate(style)
  path = File.join("Public", path) if public_dropbox?
  path
end

#public_dropbox?Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/paperclip/storage/dropbox.rb', line 76

def public_dropbox?
  @options[:dropbox_credentials][:access_type] == "dropbox" &&
    @options[:dropbox_visibility] == "public"
end

#url(style_or_options = default_style, options = {}) ⇒ Object



40
41
42
43
44
# File 'lib/paperclip/storage/dropbox.rb', line 40

def url(style_or_options = default_style, options = {})
  options.merge!(style_or_options) if style_or_options.is_a?(Hash)
  style = style_or_options.is_a?(Hash) ? default_style : style_or_options
  @url_generator.generate(style, options)
end