Class: Dragonfly::DropboxDataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/dropbox_data_store.rb,
lib/dragonfly/dropbox_data_store/rake.rb,
lib/dragonfly/dropbox_data_store/railtie.rb,
lib/dragonfly/dropbox_data_store/version.rb

Defined Under Namespace

Modules: Rake Classes: Railtie

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ DropboxDataStore

Returns a new instance of DropboxDataStore.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dragonfly/dropbox_data_store.rb', line 13

def initialize(opts = {})
  @app_key             = opts[:app_key]
  @app_secret          = opts[:app_secret]
  @access_token        = opts[:access_token]
  @access_token_secret = opts[:access_token_secret]
  @user_id             = opts[:user_id]
  @access_type         = opts[:access_type] || 'app_folder' # dropbox|app_folder

  @store_meta          = opts[:store_meta]
  # TODO: this should default to 'dragonfly' for dropbox access type
  @root_path           = opts[:root_path] || ''

  # TODO: path for access_type=dropbox
  # TODO: how is path typically specified in dragonfly? leading slash?
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



10
11
12
# File 'lib/dragonfly/dropbox_data_store.rb', line 10

def access_token
  @access_token
end

#access_token_secretObject

Returns the value of attribute access_token_secret.



10
11
12
# File 'lib/dragonfly/dropbox_data_store.rb', line 10

def access_token_secret
  @access_token_secret
end

#access_typeObject

Returns the value of attribute access_type.



10
11
12
# File 'lib/dragonfly/dropbox_data_store.rb', line 10

def access_type
  @access_type
end

#app_keyObject

Returns the value of attribute app_key.



10
11
12
# File 'lib/dragonfly/dropbox_data_store.rb', line 10

def app_key
  @app_key
end

#app_secretObject

Returns the value of attribute app_secret.



10
11
12
# File 'lib/dragonfly/dropbox_data_store.rb', line 10

def app_secret
  @app_secret
end

#root_pathObject

Returns the value of attribute root_path.



10
11
12
# File 'lib/dragonfly/dropbox_data_store.rb', line 10

def root_path
  @root_path
end

#store_metaObject

Returns the value of attribute store_meta.



10
11
12
# File 'lib/dragonfly/dropbox_data_store.rb', line 10

def store_meta
  @store_meta
end

#user_idObject

Returns the value of attribute user_id.



10
11
12
# File 'lib/dragonfly/dropbox_data_store.rb', line 10

def user_id
  @user_id
end

Instance Method Details

#destroy(path) ⇒ Object



47
48
49
50
51
52
# File 'lib/dragonfly/dropbox_data_store.rb', line 47

def destroy(path)
  path = absolute(path)
  # TODO: purge empty directories
  wrap_error { storage.file_delete(meta_path(path)) } if store_meta?
  wrap_error { storage.file_delete(path) }
end

#read(path) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/dragonfly/dropbox_data_store.rb', line 38

def read(path)
  path = absolute(path)
  # TODO: possibly return some of dropbox's native metadata automatically
  wrap_error do
    [ storage.get_file(path),
      store_meta? && YAML.load(storage.get_file(meta_path(path))) ]
  end
end

#storageObject



66
67
68
69
70
71
72
# File 'lib/dragonfly/dropbox_data_store.rb', line 66

def storage
  @storage ||= begin
    session = DropboxSession.new(app_key, app_secret)
    session.set_access_token(access_token, access_token_secret)
    DropboxClient.new(session, access_type)
  end
end

#store_meta?Boolean

TODO: thumbnail data-uri

Returns:

  • (Boolean)


62
63
64
# File 'lib/dragonfly/dropbox_data_store.rb', line 62

def store_meta?
  @store_meta != false # Default to true if not set
end

#url_for(path, opts = {}) ⇒ Object

Only option is “expires” and it’s a boolean



55
56
57
58
# File 'lib/dragonfly/dropbox_data_store.rb', line 55

def url_for(path, opts = {})
  path = absolute(path)
  (opts[:expires] ? storage.media(path) : storage.shares(path))['url']
end

#write(content, opts = {}) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/dragonfly/dropbox_data_store.rb', line 29

def write(content, opts = {})
  # TODO: deal with dropbox vs. app_folder stuff
  # figure out how paths work for each
  path = opts[:path] || absolute(relative_path_for(content.name || 'file'))
  data_path = storage.put_file(path, content.file)['path']
  storage.put_file(meta_path(data_path), YAML.dump(content.meta)) if store_meta?
  relative(data_path)
end