Class: GetThemAll::DropboxStorage

Inherits:
Storage
  • Object
show all
Defined in:
lib/get_them_all/storage/dropbox_storage.rb

Instance Method Summary collapse

Methods inherited from Storage

#build_destpath

Constructor Details

#initialize(options = {}) ⇒ DropboxStorage

Constructor

Parameters:

  • options (Hash) (defaults to: {})

    options

Options Hash (options):

  • :session (Array)

    The session array

  • :timeout (Integer)

    The timeout for dropbox requests.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/get_them_all/storage/dropbox_storage.rb', line 16

def initialize(options = {})
  super

  session_data = options.delete(:session)
  raise "session missing" unless session_data
  
  # default to 30s timeout
  @timeout = options.delete(:timeout) || 30

  consumer_key, consumer_secret, authorized, token, token_secret, ssl, mode = session_data

  @session = Dropbox::Session.new(consumer_key, consumer_secret, :ssl => ssl, :already_authorized => authorized)
  @session.set_access_token(token, token_secret)
  @session.mode = mode.to_sym

  @queue = GirlFriday::WorkQueue.new(:dropbox_upload, :size => 1) do |msg|
    # puts "Uploading file #{msg[:path]}..."
    write(msg[:path], msg[:data], false, msg[:deferrable])
    # puts "Upload ok."
  end
end

Instance Method Details

#exist?(path) ⇒ Boolean

Check if the remote file/folder exists, there may be a better way but it works.

Returns:

  • (Boolean)

    true if the remote path exists



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/get_them_all/storage/dropbox_storage.rb', line 54

def exist?(path)
  destpath = build_destpath(path)

   = @session.(destpath)
  if .respond_to?(:is_deleted) && .is_deleted
    false
  else
    true
  end
  
rescue Dropbox::FileNotFoundError
  false
rescue => err
  show_error(err)
  false
end

#read(path) ⇒ String

Read file’s content and return it.

Returns:

  • (String)

    file’s content



108
109
110
111
112
113
114
# File 'lib/get_them_all/storage/dropbox_storage.rb', line 108

def read(path)
  destpath = build_destpath(path)
  @session.download(destpath)
rescue => err
  show_error(err)
  raise ReadError, "cannot read file: #{err}"
end

#working?Boolean

Can we exit or is there still some work to do ?

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/get_them_all/storage/dropbox_storage.rb', line 43

def working?
  s = @queue.status['dropbox_upload']
  (s[:backlog] > 0) || (s[:busy] > 0)
end

#write(path, data, delay = true, deferrable = EM::DefaultDeferrable.new) ⇒ Object

Write data in a remote file. Folders in the path will be automatically created as needed.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/get_them_all/storage/dropbox_storage.rb', line 76

def write(path, data, delay = true, deferrable = EM::DefaultDeferrable.new)
  retries = 0

  if delay
    @queue.push(:path => path, :data => data, :deferrable => deferrable)
  else
    deferrable.timeout(@timeout)
    
    destpath = build_destpath(path)
    filename, dirname = File.basename(destpath), File.dirname(destpath)
    @session.upload(StringIO.new(data), dirname, :as => filename)
    EM::next_tick{ deferrable.succeed }
  end
  
  deferrable
rescue => err
  show_error(err)
  if retries < 4
    # puts "[#{retries}] Upload error, retrying: #{err}"
    retries += 1
    retry
  else
    EM::next_tick{ deferrable.fail }
    raise WriteError, "cannot write file: #{err}"
  end
end