dropbox-api-kilgore5 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ require "dropbox-api/client/raw"
2
+ require "dropbox-api/client/files"
3
+
4
+ module Dropbox
5
+ module API
6
+
7
+ class Client
8
+
9
+ attr_accessor :raw, :connection
10
+
11
+ def initialize(options = {})
12
+ @connection = Dropbox::API::Connection.new(:token => options.delete(:token),
13
+ :secret => options.delete(:secret))
14
+ @raw = Dropbox::API::Raw.new :connection => @connection
15
+ @options = options
16
+ end
17
+
18
+ include Dropbox::API::Client::Files
19
+
20
+ def find(filename)
21
+ data = self.raw.metadata(:path => filename)
22
+ data.delete('contents')
23
+ Dropbox::API::Object.convert(data, self)
24
+ end
25
+
26
+ def ls(path = '')
27
+ Dropbox::API::Dir.init({'path' => path}, self).ls
28
+ end
29
+
30
+ def folder_changed?(path = '', hash = '')
31
+ meta_response = Dropbox::API::Dir.init({'path' => path}, self).folder_meta(path,hash)
32
+ return (meta_response.kind_of? Dropbox::API::Response::NotModified) ? false : meta_response
33
+ end
34
+
35
+ def account
36
+ Dropbox::API::Object.init(self.raw.account, self)
37
+ end
38
+
39
+ def mkdir(path)
40
+ # Remove the characters not allowed by Dropbox
41
+ path = path.gsub(/[\\\:\?\*\<\>\"\|]+/, '')
42
+ response = raw.create_folder :path => path
43
+ Dropbox::API::Dir.init(response, self)
44
+ end
45
+
46
+ def search(term, options = {})
47
+ options[:path] ||= ''
48
+ results = raw.search({ :query => term }.merge(options))
49
+ Dropbox::API::Object.convert(results, self)
50
+ end
51
+
52
+ def delta(cursor=nil)
53
+ entries = []
54
+ has_more = true
55
+ params = cursor ? {:cursor => cursor} : {}
56
+ while has_more
57
+ response = raw.delta(params)
58
+ params[:cursor] = response['cursor']
59
+ has_more = response['has_more']
60
+ entries.push *response['entries']
61
+ end
62
+
63
+ files = entries.map do |entry|
64
+ entry.last || {:is_deleted => true, :path => entry.first}
65
+ end
66
+
67
+ Delta.new(params[:cursor], Dropbox::API::Object.convert(files, self))
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,40 @@
1
+ module Dropbox
2
+ module API
3
+
4
+ class Client
5
+
6
+ module Files
7
+
8
+ def download(path, options = {})
9
+ root = options.delete(:root) || Dropbox::API::Config.mode
10
+ path = Dropbox::API::Util.escape(path)
11
+ url = ['', "files", root, path].compact.join('/')
12
+ connection.get_raw(:content, url)
13
+ end
14
+
15
+ def upload(path, data, options = {})
16
+ root = options.delete(:root) || Dropbox::API::Config.mode
17
+ query = Dropbox::API::Util.query(options)
18
+ path = Dropbox::API::Util.escape(path)
19
+ url = ['', "files_put", root, path].compact.join('/')
20
+ response = connection.put(:content, "#{url}?#{query}", data, {
21
+ 'Content-Type' => "application/octet-stream",
22
+ "Content-Length" => data.length.to_s
23
+ })
24
+ Dropbox::API::File.init(response, self)
25
+ end
26
+
27
+ def copy_from_copy_ref(copy_ref, to, options = {})
28
+ raw.copy({
29
+ :from_copy_ref => copy_ref,
30
+ :to_path => to
31
+ }.merge(options))
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end
40
+
@@ -0,0 +1,51 @@
1
+ module Dropbox
2
+ module API
3
+
4
+ class Raw
5
+
6
+ attr_accessor :connection
7
+
8
+ def initialize(options = {})
9
+ @connection = options[:connection]
10
+ end
11
+
12
+ def self.add_method(method, action, options = {})
13
+ # Add the default root bit, but allow it to be disabled by a config option
14
+ root = options[:root] == false ? '' : "options[:root] ||= Dropbox::API::Config.mode"
15
+ self.class_eval <<-STR
16
+ def #{options[:as] || action}(options = {})
17
+ #{root}
18
+ request(:#{options[:endpoint] || 'main'}, :#{method}, "#{action}", options)
19
+ end
20
+ STR
21
+ end
22
+
23
+ def request(endpoint, method, action, data = {})
24
+ action.sub! ':root', Dropbox::API::Util.escape(data.delete(:root).to_s) if action.match ':root'
25
+ action.sub! ':path', Dropbox::API::Util.escape(data.delete(:path).to_s) if action.match ':path'
26
+ action = Dropbox::API::Util.remove_double_slashes(action)
27
+ connection.send(method, endpoint, action, data)
28
+ end
29
+
30
+ add_method :get, "/account/info", :as => 'account', :root => false
31
+
32
+ add_method :get, "/metadata/:root/:path", :as => 'metadata'
33
+ add_method :post, "/delta", :as => 'delta', :root => false
34
+ add_method :get, "/revisions/:root/:path", :as => 'revisions'
35
+ add_method :post, "/restore/:root/:path", :as => 'restore'
36
+ add_method :get, "/search/:root/:path", :as => 'search'
37
+ add_method :post, "/shares/:root/:path", :as => 'shares'
38
+ add_method :post, "/media/:root/:path", :as => 'media'
39
+
40
+ add_method :get_raw, "/thumbnails/:root/:path", :as => 'thumbnails', :endpoint => :content
41
+
42
+ add_method :post, "/fileops/copy", :as => "copy"
43
+ add_method :get, "/copy_ref/:root/:path", :as => 'copy_ref'
44
+ add_method :post, "/fileops/create_folder", :as => "create_folder"
45
+ add_method :post, "/fileops/delete", :as => "delete"
46
+ add_method :post, "/fileops/move", :as => "move"
47
+
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,34 @@
1
+ require "dropbox-api/connection/requests"
2
+
3
+ module Dropbox
4
+ module API
5
+
6
+ class Connection
7
+
8
+ include Dropbox::API::Connection::Requests
9
+
10
+ attr_accessor :consumers
11
+ attr_accessor :tokens
12
+
13
+ def initialize(options = {})
14
+ @options = options
15
+ @consumers = {}
16
+ @tokens = {}
17
+ Dropbox::API::Config.endpoints.each do |endpoint, url|
18
+ @consumers[endpoint] = Dropbox::API::OAuth.consumer(endpoint)
19
+ @tokens[endpoint] = Dropbox::API::OAuth.access_token(@consumers[endpoint], options)
20
+ end
21
+ end
22
+
23
+ def consumer(endpoint = :main)
24
+ @consumers[endpoint]
25
+ end
26
+
27
+ def token(endpoint = :main)
28
+ @tokens[endpoint]
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,67 @@
1
+ module Dropbox
2
+ module API
3
+
4
+ class Connection
5
+
6
+ module Requests
7
+
8
+ def request(options = {})
9
+ response = yield
10
+ raise Dropbox::API::Error::ConnectionFailed if !response
11
+ status = response.code.to_i
12
+ case status
13
+ when 401
14
+ raise Dropbox::API::Error::Unauthorized
15
+ when 403
16
+ parsed = MultiJson.decode(response.body)
17
+ raise Dropbox::API::Error::Forbidden.new(parsed["error"])
18
+ when 404
19
+ raise Dropbox::API::Error::NotFound
20
+ when 400, 406
21
+ parsed = MultiJson.decode(response.body)
22
+ raise Dropbox::API::Error.new(parsed["error"])
23
+ when 300..399
24
+ raise Dropbox::API::Error::Redirect
25
+ when 500..599
26
+ raise Dropbox::API::Error
27
+ else
28
+ options[:raw] ? response.body : MultiJson.decode(response.body)
29
+ end
30
+ rescue Dropbox::API::Error::Redirect
31
+ return Dropbox::API::Response::NotModified.new
32
+ end
33
+
34
+
35
+
36
+ def get_raw(endpoint, path, data = {}, headers = {})
37
+ query = Dropbox::API::Util.query(data)
38
+ request(:raw => true) do
39
+ token(endpoint).get "#{Dropbox::API::Config.prefix}#{path}?#{URI.parse(URI.encode(query))}", headers
40
+ end
41
+ end
42
+
43
+ def get(endpoint, path, data = {}, headers = {})
44
+ query = Dropbox::API::Util.query(data)
45
+ request do
46
+ token(endpoint).get "#{Dropbox::API::Config.prefix}#{path}?#{URI.parse(URI.encode(query))}", headers
47
+ end
48
+ end
49
+
50
+ def post(endpoint, path, data = {}, headers = {})
51
+ request do
52
+ token(endpoint).post "#{Dropbox::API::Config.prefix}#{path}", data, headers
53
+ end
54
+ end
55
+
56
+ def put(endpoint, path, data = {}, headers = {})
57
+ request do
58
+ token(endpoint).put "#{Dropbox::API::Config.prefix}#{path}", data, headers
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,11 @@
1
+ module Dropbox
2
+ module API
3
+ class Delta
4
+ attr_reader :cursor, :entries
5
+ def initialize(cursor, entries)
6
+ @cursor = cursor
7
+ @entries = entries
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,30 @@
1
+ module Dropbox
2
+ module API
3
+
4
+ class Dir < Dropbox::API::Object
5
+
6
+ include Dropbox::API::Fileops
7
+
8
+ def ls(path_to_list = '')
9
+ data = client.raw.metadata :path => path + path_to_list
10
+ if data['is_dir']
11
+ Dropbox::API::Object.convert(data.delete('contents') || [], client)
12
+ else
13
+ [Dropbox::API::Object.convert(data, client)]
14
+ end
15
+ end
16
+
17
+ def folder_meta(path,hash = '')
18
+ data = client.raw.metadata :path => path, :hash => hash
19
+
20
+ if data.kind_of? Dropbox::API::Error::NotModified
21
+ return Dropbox::API::Error::NotModified
22
+ else
23
+ return data
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,44 @@
1
+ module Dropbox
2
+ module API
3
+
4
+ class File < Dropbox::API::Object
5
+
6
+ include Dropbox::API::Fileops
7
+
8
+ def revisions(options = {})
9
+ response = client.raw.revisions({ :path => self.path }.merge(options))
10
+ Dropbox::API::Object.convert(response, client)
11
+ end
12
+
13
+ def restore(rev, options = {})
14
+ response = client.raw.restore({ :rev => rev, :path => self.path }.merge(options))
15
+ self.update response
16
+ end
17
+
18
+ def share_url(options = {})
19
+ response = client.raw.shares({ :path => self.path }.merge(options))
20
+ Dropbox::API::Object.init(response, client)
21
+ end
22
+
23
+ def direct_url(options = {})
24
+ response = client.raw.media({ :path => self.path }.merge(options))
25
+ Dropbox::API::Object.init(response, client)
26
+ end
27
+
28
+ def thumbnail(options = {})
29
+ client.raw.thumbnails({ :path => self.path }.merge(options))
30
+ end
31
+
32
+ def copy_ref(options = {})
33
+ response = client.raw.copy_ref({ :path => self.path }.merge(options))
34
+ Dropbox::API::Object.init(response, client)
35
+ end
36
+
37
+ def download
38
+ client.download(self.path)
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,28 @@
1
+ module Dropbox
2
+ module API
3
+
4
+ module Fileops
5
+
6
+ def copy(to, options = {})
7
+ response = client.raw.copy({ :from_path => self.path, :to_path => to }.merge(options))
8
+ self.update response
9
+ end
10
+
11
+ def move(to, options = {})
12
+ response = client.raw.move({ :from_path => self.path, :to_path => to }.merge(options))
13
+ self.update response
14
+ end
15
+
16
+ def destroy(options = {})
17
+ response = client.raw.delete({ :path => self.path }.merge(options))
18
+ self.update response
19
+ end
20
+
21
+ def path
22
+ self['path'].sub(/^\//, '')
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,39 @@
1
+ module Dropbox
2
+ module API
3
+
4
+ class Object < Hashie::Mash
5
+ attr_accessor :client
6
+
7
+ def self.init(response, client)
8
+ instance = self.new(response)
9
+ instance.client = client
10
+ instance
11
+ end
12
+
13
+ def self.resolve_class(hash)
14
+ hash['is_dir'] == true ? Dropbox::API::Dir : Dropbox::API::File
15
+ end
16
+
17
+ def self.convert(array_or_object, client)
18
+ if array_or_object.is_a?(Array)
19
+ array_or_object.collect do |item|
20
+ resolve_class(item).init(item, client)
21
+ end
22
+ else
23
+ resolve_class(array_or_object).init(array_or_object, client)
24
+ end
25
+ end
26
+
27
+ # Kill off the ability for recursive conversion
28
+ def deep_update(other_hash)
29
+ other_hash.each_pair do |k,v|
30
+ key = convert_key(k)
31
+ regular_writer(key, convert_value(v, true))
32
+ end
33
+ self
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,48 @@
1
+ module Dropbox
2
+ module API
3
+
4
+ class Tasks
5
+
6
+ extend Rake::DSL if defined? Rake::DSL
7
+
8
+ def self.install
9
+
10
+ namespace :dropbox do
11
+ desc "Authorize wizard for Dropbox API"
12
+ task :authorize do
13
+ require "dropbox-api"
14
+ require "cgi"
15
+ print "Enter consumer key: "
16
+ consumer_key = $stdin.gets.chomp
17
+ print "Enter consumer secret: "
18
+ consumer_secret = $stdin.gets.chomp
19
+
20
+ Dropbox::API::Config.app_key = consumer_key
21
+ Dropbox::API::Config.app_secret = consumer_secret
22
+
23
+ consumer = Dropbox::API::OAuth.consumer(:authorize)
24
+ request_token = consumer.get_request_token
25
+ puts "\nGo to this url and click 'Authorize' to get the token:"
26
+ puts request_token.authorize_url
27
+ query = request_token.authorize_url.split('?').last
28
+ params = CGI.parse(query)
29
+ token = params['oauth_token'].first
30
+ print "\nOnce you authorize the app on Dropbox, press enter... "
31
+ $stdin.gets.chomp
32
+
33
+ access_token = request_token.get_access_token(:oauth_verifier => token)
34
+
35
+ puts "\nAuthorization complete!:\n\n"
36
+ puts " Dropbox::API::Config.app_key = '#{consumer.key}'"
37
+ puts " Dropbox::API::Config.app_secret = '#{consumer.secret}'"
38
+ puts " client = Dropbox::API::Client.new(:token => '#{access_token.token}', :secret => '#{access_token.secret}')"
39
+ puts "\n"
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+ end