Class: Datapimp::Clients::Dropbox

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/datapimp/clients/dropbox.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/datapimp/clients/dropbox.rb', line 96

def method_missing meth, *args, &block
  if api.respond_to?(meth)
    return api.send(meth, *args, &block)
  end

  super
end

Class Method Details

.client(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/datapimp/clients/dropbox.rb', line 14

def self.client(options={})
  require 'dropbox-api' unless defined?(::Dropbox::API)
  @client ||= begin
                ::Dropbox::API::Config.app_key    = options.fetch(:dropbox_app_key) { Datapimp.config.dropbox_app_key }
                ::Dropbox::API::Config.app_secret = options.fetch(:dropbox_app_secret) { Datapimp.config.dropbox_app_secret }
                ::Dropbox::API::Config.mode       = options.fetch(:dropbox_app_type) { Datapimp.config.dropbox_app_type }
                instance.with_options(options)
              end
end

.method_missing(meth, *args, &block) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/datapimp/clients/dropbox.rb', line 6

def self.method_missing(meth, *args, &block)
  if client.respond_to?(meth)
    return client.send(meth, *args, &block)
  end

  super
end

Instance Method Details

#apiObject



29
30
31
32
33
34
35
# File 'lib/datapimp/clients/dropbox.rb', line 29

def api
  @api ||= begin
             token = options.fetch(:token) { Datapimp.config.dropbox_client_token }
             secret = options.fetch(:secret) { Datapimp.config.dropbox_client_secret }
             ::Dropbox::API::Client.new(token: token  , secret: secret)
           end
end

#authorize(token, secret) ⇒ Object



104
105
106
107
108
109
# File 'lib/datapimp/clients/dropbox.rb', line 104

def authorize(token, secret)
  @api = nil if @api
  options[:token] = token
  options[:secret] = secret
  self
end

#create_site_folder(folder, allow_overwrite = false) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/datapimp/clients/dropbox.rb', line 67

def create_site_folder(folder, allow_overwrite=false)
  found = find(folder)

  unless (!found || (found && !allow_overwrite))
    api.mkdir("/#{folder}")
  end
end

#dropbox_app_keyObject



124
125
126
# File 'lib/datapimp/clients/dropbox.rb', line 124

def dropbox_app_key
  Datapimp.config.dropbox_app_key.to_s
end

#dropbox_app_secretObject



128
129
130
# File 'lib/datapimp/clients/dropbox.rb', line 128

def dropbox_app_secret
  Datapimp.config.dropbox_app_secret.to_s
end

#find(path) ⇒ Object



37
38
39
40
41
# File 'lib/datapimp/clients/dropbox.rb', line 37

def find(path)
  api.find(path)
rescue ::Dropbox::API::Error::NotFound
  nil
end

#interactive_setup(options = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/datapimp/clients/dropbox.rb', line 136

def interactive_setup(options={})
  if requires_setup?
    if dropbox_app_key.length == 0
      if answer = options[:dropbox_app_key] || ask("What is the dropbox app key?", String)
        Datapimp.config.set("dropbox_app_key", answer)
      end
      if answer = options[:dropbox_app_secret] || ask("What is the dropbox app secret?", String)
        Datapimp.config.set("dropbox_app_secret", answer)
      end
    end
  end

  raise 'Missing dropbox application values' if requires_setup?

  ::Dropbox::API::Config.app_key    = Datapimp.config.dropbox_app_key
  ::Dropbox::API::Config.app_secret = Datapimp.config.dropbox_app_secret

  consumer = ::Dropbox::API::OAuth.consumer(:authorize)
  request_token = consumer.get_request_token
  puts "\nGo to this url and click 'Authorize' to get the token:"
  puts request_token.authorize_url
  Launchy.open(request_token.authorize_url)

  query  = request_token.authorize_url.split('?').last
  params = CGI.parse(query)
  token  = params['oauth_token'].first
  print "\nOnce you authorize the app on Dropbox, press enter... "
  STDIN.gets.chomp

  access_token  = request_token.get_access_token(:oauth_verifier => token)

  Datapimp.config.set 'dropbox_client_token', access_token.token
  Datapimp.config.set 'dropbox_client_secret', access_token.secret

  puts "\nAuthorization complete!:\n\n"
  puts "  Dropbox::API::Config.app_key    = '#{consumer.key}'"
  puts "  Dropbox::API::Config.app_secret = '#{consumer.secret}'"
  puts "  client = Dropbox::API::Client.new(:token  => '#{access_token.token}', :secret => '#{access_token.secret}')"
  puts "\n"
end

#optionsObject



111
112
113
# File 'lib/datapimp/clients/dropbox.rb', line 111

def options
  @options ||= {}
end

#push_from(local_path, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/datapimp/clients/dropbox.rb', line 43

def push_from(local_path, options={})
  path_prefix = options.fetch(:prefix)
  root        = options[:root]

  unless find(path_prefix)
    api.mkdir(path_prefix)
  end

  uploader = lambda do |node|
    next if node.to_s == ".DS_Store"

    if node.directory?
      Array(node.children).each(&uploader)
    elsif node.file?
      relative = node.relative_path_from(local_path)
      target = "#{path_prefix}/#{relative}"
      target = target.gsub(/^\//,'')
      api.upload(target, node.read)
    end
  end

  Pathname(local_path).children.each(&uploader)
end

#requires_setup?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/datapimp/clients/dropbox.rb', line 120

def requires_setup?
  !(dropbox_app_key.length > 0 && dropbox_app_secret.length > 0)
end

#sandboxed_app?Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/datapimp/clients/dropbox.rb', line 24

def sandboxed_app?
  entry = api.ls.first
  entry && entry.root == "app_folder"
end

#setup(options = {}) ⇒ Object



132
133
134
# File 'lib/datapimp/clients/dropbox.rb', line 132

def setup(options={})
  interactive_setup(options)
end

#sync(local_path, remote_path, options = {}) ⇒ Object



75
76
77
# File 'lib/datapimp/clients/dropbox.rb', line 75

def sync(local_path, remote_path, options={})
  sync_folders(local_path, remote_path, options)
end

#sync_folders(local_path, remote_path, options = {}) ⇒ Object

Instructs Datapimp to create a syncable folder between the local and remote path, and optionally modifies the middleman config for the site



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/datapimp/clients/dropbox.rb', line 82

def sync_folders(local_path, remote_path, options={})
  app = options[:app]

  folder = Datapimp::Sync::Folder.new(local_path: local_path,
                                      remote_path: remote_path,
                                      app: app).synced

  if !!options[:append_config] == true
    Datapimp.append_config(folder.config_line)
  end

  folder
end

#with_options(opts = {}) ⇒ Object



115
116
117
118
# File 'lib/datapimp/clients/dropbox.rb', line 115

def with_options(opts={})
  options.merge!(opts)
  self
end