dropbox-sdk-v2 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f7aebcfc896301354f9bcfdc1d04ea31e83565ec
4
+ data.tar.gz: 38355d1a956283242d731b3160daabac149281ef
5
+ SHA512:
6
+ metadata.gz: 8e6c399ffb0a1ae9e460dd7a0f120167e92ba6ababee422840dfdfbada64e3901ad6710d19a699673260a1fc9b5b22f39551199f2cad995e67ac39bfd1fef176
7
+ data.tar.gz: 138a4e77f9797b32ecd972f56dee81f8ac5dfae0cece5e4b4ffe747305a4a8551dd8e812fb94bc0833ed0c77bd69fda25daba7e2a8c5c22c5f2466ac368934c4
@@ -0,0 +1,9 @@
1
+ require_relative 'dropbox/client'
2
+ require_relative 'dropbox/errors'
3
+ require_relative 'dropbox/account'
4
+ require_relative 'dropbox/metadata'
5
+
6
+ module Dropbox
7
+ API = 'https://api.dropboxapi.com/2'
8
+ CONTENT_API = 'https://content.dropboxapi.com/2'
9
+ end
@@ -0,0 +1,47 @@
1
+ module Dropbox
2
+ class Account
3
+ attr_reader :account_id, :display_name, :email, :email_verified, :disabled, :profile_photo_url
4
+
5
+ def initialize(attrs={})
6
+ @account_id = attrs['account_id']
7
+ @display_name = attrs['name']['display_name']
8
+ @email = attrs['email']
9
+ @email_verified = attrs['email_verified']
10
+ @disabled = attrs['disabled']
11
+ @profile_photo_url = attrs['profile_photo_url']
12
+ end
13
+ end
14
+
15
+ class BasicAccount < Account
16
+ attr_reader :is_teammate, :team_member_id
17
+
18
+ def initialize(attrs={})
19
+ @is_teammate = attrs.delete('is_teammate')
20
+ @team_member_id = attrs.delete('team_member_id')
21
+ super(attrs)
22
+ end
23
+ end
24
+
25
+ class FullAccount < Account
26
+ attr_reader :locale, :referral_link, :is_paired, :profile_photo_url, :country
27
+
28
+ def initialize(attrs={})
29
+ @locale = attrs.delete('locale')
30
+ @referral_link = attrs.delete('referral_link')
31
+ @is_paired = attrs.delete('is_paired')
32
+ @profile_photo_url = attrs.delete('profile_photo_url')
33
+ @country = attrs.delete('country')
34
+ super(attrs)
35
+ end
36
+ end
37
+
38
+ class SpaceUsage
39
+ attr_reader :used, :allocation, :allocated
40
+
41
+ def initialize(attrs={})
42
+ @used = attrs['used'] # Space used in bytes
43
+ @allocation = attrs['allocation']['.tag'] # The type of allocation
44
+ @allocated = attrs['allocation']['allocated'] # Space allocated in bytes
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,179 @@
1
+ require 'http'
2
+ require 'json'
3
+ require 'time'
4
+
5
+ module Dropbox
6
+ class Client
7
+ def initialize(access_token)
8
+ unless access_token =~ /^[a-z0-9_-]{64}$/i
9
+ raise ClientError.invalid_access_token
10
+ end
11
+
12
+ @access_token = access_token
13
+ end
14
+
15
+ def copy(from, to)
16
+ resp = request('/files/copy', from_path: from, to_path: to)
17
+ parse_tagged_response(resp)
18
+ end
19
+
20
+ def create_folder(path)
21
+ resp = request('/files/create_folder', path: path)
22
+ parse_tagged_response(resp, 'folder')
23
+ end
24
+
25
+ def delete(path)
26
+ resp = request('/files/delete', path: path)
27
+ parse_tagged_response(resp)
28
+ end
29
+
30
+ def download(path)
31
+ resp, body = content_request('/files/download', path: path)
32
+ return parse_tagged_response(resp, 'file'), body
33
+ end
34
+
35
+ def get_account(id)
36
+ resp = request('/users/get_account', account_id: id)
37
+ parse_tagged_response(resp, 'basic_account')
38
+ end
39
+
40
+ # Takes an array of account IDs and returns an array of BasicAccounts
41
+ def get_account_batch(ids)
42
+ resp = request('/users/get_account_batch', account_ids: ids)
43
+ resp.map { |a| parse_tagged_response(a, 'basic_account') }
44
+ end
45
+
46
+ def get_current_account
47
+ resp = request('/users/get_current_account')
48
+ parse_tagged_response(resp, 'full_account')
49
+ end
50
+
51
+ def get_metadata(path)
52
+ resp = request('/files/get_metadata', path: path)
53
+ parse_tagged_response(resp)
54
+ end
55
+
56
+ def get_preview(path)
57
+ resp, body = content_request('/files/get_preview', path: path)
58
+ return parse_tagged_response(resp, 'file'), body
59
+ end
60
+
61
+ def get_space_usage
62
+ resp = request('/users/get_space_usage')
63
+ SpaceUsage.new(resp)
64
+ end
65
+
66
+ def get_temporary_link(path)
67
+ resp = request('/files/get_temporary_link', path: path)
68
+ return parse_tagged_response(resp['metadata'], 'file'), resp['link']
69
+ end
70
+
71
+ def get_thumbnail(path, format='jpeg', size='w64h64')
72
+ resp, body = content_request('/files/get_thumbnail', path: path, format: format, size: size)
73
+ return parse_tagged_response(resp, 'file'), body
74
+ end
75
+
76
+ def list_folder(path)
77
+ resp = request('/files/list_folder', path: path)
78
+ resp['entries'].map { |e| parse_tagged_response(e) }
79
+ end
80
+
81
+ def list_revisions(path)
82
+ resp = request('/files/list_revisions', path: path)
83
+ entries = resp['entries'].map { |e| parse_tagged_response(e, 'file') }
84
+ return entries, resp['is_deleted']
85
+ end
86
+
87
+ def move(from, to)
88
+ resp = request('/files/move', from_path: from, to_path: to)
89
+ parse_tagged_response(resp)
90
+ end
91
+
92
+ def restore(path, rev)
93
+ resp = request('/files/restore', path: path, rev: rev)
94
+ parse_tagged_response(resp, 'file')
95
+ end
96
+
97
+ # Revokes the current access token and returns it
98
+ def revoke_token
99
+ r = HTTP.auth('Bearer ' + @access_token).post(API + '/auth/token/revoke')
100
+ raise APIError.new(r) if r.code != 200
101
+ @access_token
102
+ end
103
+
104
+ def save_url(path, url)
105
+ resp = request('/files/save_url', path: path, url: url)
106
+ case resp['.tag']
107
+ when 'complete'
108
+ parse_tagged_response(resp['complete'], 'file')
109
+ when 'async_job_id'
110
+ resp['async_job_id']
111
+ else
112
+ raise ClientError.unknown_response_type(resp['.tag'])
113
+ end
114
+ end
115
+
116
+ def search(query, path='', max=100)
117
+ resp = request('/files/search', path: path, query: query, max_results: max)
118
+ resp['matches'].map { |m| parse_tagged_response(m['metadata']) }
119
+ end
120
+
121
+ # Body can be a String or an Enumerable
122
+ # Mode can be 'add', 'overwrite', or 'update'
123
+ def upload(path, body, mode='add', autorename=false, client_modified=nil, mute=false)
124
+ client_modified = client_modified.iso8601 if client_modified.is_a?(Time)
125
+ resp = upload_request('/files/upload', body, path: path, mode: mode,
126
+ autorename: autorename, client_modified: client_modified, mute: mute)
127
+ parse_tagged_response(resp, 'file')
128
+ end
129
+
130
+ private
131
+ def parse_tagged_response(resp, tag=resp['.tag'])
132
+ case tag
133
+ when 'file'
134
+ FileMetadata.new(resp)
135
+ when 'folder'
136
+ FolderMetadata.new(resp)
137
+ when 'deleted'
138
+ DeletedMetadata.new(resp)
139
+ when 'basic_account'
140
+ BasicAccount.new(resp)
141
+ when 'full_account'
142
+ FullAccount.new(resp)
143
+ else
144
+ raise ClientError.unknown_response_type(tag)
145
+ end
146
+ end
147
+
148
+ def request(action, data=nil)
149
+ url = API + action
150
+ resp = HTTP.auth('Bearer ' + @access_token)
151
+ .headers(content_type: ('application/json' if data))
152
+ .post(url, json: data)
153
+
154
+ raise APIError.new(resp) if resp.code != 200
155
+ JSON.parse(resp.to_s)
156
+ end
157
+
158
+ def content_request(action, args={})
159
+ url = CONTENT_API + action
160
+ resp = HTTP.auth('Bearer ' + @access_token)
161
+ .headers('Dropbox-API-Arg' => args.to_json).get(url)
162
+
163
+ raise APIError.new(resp) if resp.code != 200
164
+ file = JSON.parse(resp.headers['Dropbox-API-Result'])
165
+ return file, resp.body
166
+ end
167
+
168
+ def upload_request(action, body, args={})
169
+ resp = HTTP.auth('Bearer ' + @access_token).headers({
170
+ 'Content-Type' => 'application/octet-stream',
171
+ 'Dropbox-API-Arg' => args.to_json,
172
+ 'Transfer-Encoding' => ('chunked' unless body.is_a?(String))
173
+ }).post(CONTENT_API + action, body: body)
174
+
175
+ raise APIError.new(resp) if resp.code != 200
176
+ JSON.parse(resp.to_s)
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,39 @@
1
+ require 'json'
2
+
3
+ module Dropbox
4
+ class ClientError < StandardError
5
+ attr_reader :message
6
+
7
+ def self.invalid_access_token
8
+ self.new("Invalid access token")
9
+ end
10
+
11
+ def self.unknown_response_type(str)
12
+ self.new("Unknown response type '#{str}'")
13
+ end
14
+
15
+ def initialize(message=nil)
16
+ @message = message
17
+ end
18
+
19
+ def to_s
20
+ @message.to_s
21
+ end
22
+ end
23
+
24
+ class APIError < StandardError
25
+ attr_reader :message
26
+
27
+ def initialize(response)
28
+ if response.content_type.mime_type == 'application/json'
29
+ @message = JSON.parse(response)['error_summary']
30
+ else
31
+ @message = response
32
+ end
33
+ end
34
+
35
+ def to_s
36
+ @message.to_s
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ require 'time'
2
+
3
+ module Dropbox
4
+ class Metadata
5
+ attr_reader :name, :path_lower, :path_display
6
+
7
+ def initialize(attrs={})
8
+ @name = attrs['name']
9
+ @path_lower = attrs['path_lower']
10
+ @path_display = attrs['path_display']
11
+ end
12
+ end
13
+
14
+ class FileMetadata < Metadata
15
+ attr_reader :id, :client_modified, :server_modified, :rev, :size
16
+
17
+ def initialize(attrs={})
18
+ @id = attrs.delete('id')
19
+ if cm = attrs.delete('client_modified')
20
+ @client_modified = Time.parse(cm)
21
+ end
22
+ @server_modified = Time.parse(attrs.delete('server_modified'))
23
+ @rev = attrs.delete('rev')
24
+ @size = attrs.delete('size')
25
+ super(attrs)
26
+ end
27
+ end
28
+
29
+ class FolderMetadata < Metadata
30
+ attr_reader :id
31
+
32
+ def initialize(attrs={})
33
+ @id = attrs.delete('id')
34
+ super(attrs)
35
+ end
36
+ end
37
+
38
+ class DeletedMetadata < Metadata
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dropbox-sdk-v2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dylan Waits
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: http
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ description: A Ruby library for the new Dropbox API.
42
+ email: dylan@waits.io
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/dropbox.rb
48
+ - lib/dropbox/account.rb
49
+ - lib/dropbox/client.rb
50
+ - lib/dropbox/errors.rb
51
+ - lib/dropbox/metadata.rb
52
+ homepage: https://github.com/waits/dropbox-sdk-ruby
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.1.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.6.3
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Dropbox SDK v2
76
+ test_files: []
77
+ has_rdoc: