boxr 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,58 +1,58 @@
1
1
  module Boxr
2
- class Client
3
-
4
- def folder_collaborations(folder_id)
5
- uri = "#{FOLDERS_URI}/#{folder_id}/collaborations"
6
- collaborations, response = get(uri)
7
- collaborations['entries']
8
- end
9
-
10
- #make sure 'role' value is a string as Box has role values with spaces and dashes; e.g. 'previewer uploader'
11
- def add_collaboration(folder_id, accessible_by, role, fields: [], notify: nil)
12
- query = build_fields_query(fields, COLLABORATION_FIELDS_QUERY)
13
- query[:notify] = :notify unless notify.nil?
14
-
15
- attributes = {item: {id: folder_id, type: :folder}}
16
- attributes[:accessible_by] = accessible_by
17
- attributes[:role] = role
18
-
19
- collaboration, response = post(COLLABORATIONS_URI, attributes, query: query)
20
- collaboration
21
- end
22
-
23
- def edit_collaboration(collaboration_id, role: nil, status: nil)
24
- uri = "#{COLLABORATIONS_URI}/#{collaboration_id}"
25
- attributes = {}
26
- attributes[:role] = role unless role.nil?
27
- attributes[:status] = status unless status.nil?
28
-
29
- updated_collaboration, response = put(uri, attributes)
30
- updated_collaboration
31
- end
32
-
33
- def remove_collaboration(collaboration_id)
34
- uri = "#{COLLABORATIONS_URI}/#{collaboration_id}"
35
- result, response = delete(uri)
36
- result
37
- end
38
-
39
- def collaboration(collaboration_id, fields: [], status: nil)
40
- uri = "#{COLLABORATIONS_URI}/#{collaboration_id}"
41
-
42
- query = build_fields_query(fields, COLLABORATION_FIELDS_QUERY)
43
- query[:status] = status unless status.nil?
44
-
45
- collaboration, response = get(uri, query: query)
46
- collaboration
47
- end
48
-
49
- #these are pending collaborations for the current user; use the As-User Header to request for different users
50
- def pending_collaborations
51
- query = {status: :pending}
52
- pending_collaborations, response = get(COLLABORATIONS_URI, query: query)
53
- pending_collaborations['entries']
54
- end
55
-
56
-
57
- end
2
+ class Client
3
+
4
+ def folder_collaborations(folder_id)
5
+ uri = "#{FOLDERS_URI}/#{folder_id}/collaborations"
6
+ collaborations, response = get(uri)
7
+ collaborations['entries']
8
+ end
9
+
10
+ #make sure 'role' value is a string as Box has role values with spaces and dashes; e.g. 'previewer uploader'
11
+ def add_collaboration(folder_id, accessible_by, role, fields: [], notify: nil)
12
+ query = build_fields_query(fields, COLLABORATION_FIELDS_QUERY)
13
+ query[:notify] = :notify unless notify.nil?
14
+
15
+ attributes = {item: {id: folder_id, type: :folder}}
16
+ attributes[:accessible_by] = accessible_by
17
+ attributes[:role] = role
18
+
19
+ collaboration, response = post(COLLABORATIONS_URI, attributes, query: query)
20
+ collaboration
21
+ end
22
+
23
+ def edit_collaboration(collaboration_id, role: nil, status: nil)
24
+ uri = "#{COLLABORATIONS_URI}/#{collaboration_id}"
25
+ attributes = {}
26
+ attributes[:role] = role unless role.nil?
27
+ attributes[:status] = status unless status.nil?
28
+
29
+ updated_collaboration, response = put(uri, attributes)
30
+ updated_collaboration
31
+ end
32
+
33
+ def remove_collaboration(collaboration_id)
34
+ uri = "#{COLLABORATIONS_URI}/#{collaboration_id}"
35
+ result, response = delete(uri)
36
+ result
37
+ end
38
+
39
+ def collaboration(collaboration_id, fields: [], status: nil)
40
+ uri = "#{COLLABORATIONS_URI}/#{collaboration_id}"
41
+
42
+ query = build_fields_query(fields, COLLABORATION_FIELDS_QUERY)
43
+ query[:status] = status unless status.nil?
44
+
45
+ collaboration, response = get(uri, query: query)
46
+ collaboration
47
+ end
48
+
49
+ #these are pending collaborations for the current user; use the As-User Header to request for different users
50
+ def pending_collaborations
51
+ query = {status: :pending}
52
+ pending_collaborations, response = get(COLLABORATIONS_URI, query: query)
53
+ pending_collaborations['entries']
54
+ end
55
+
56
+
57
+ end
58
58
  end
@@ -1,52 +1,52 @@
1
1
  module Boxr
2
- class Client
2
+ class Client
3
3
 
4
- def file_comments(file_id, fields: [])
5
- uri = "#{FILES_URI}/#{file_id}/comments"
6
- query = build_fields_query(fields, COMMENT_FIELDS_QUERY)
4
+ def file_comments(file_id, fields: [])
5
+ uri = "#{FILES_URI}/#{file_id}/comments"
6
+ query = build_fields_query(fields, COMMENT_FIELDS_QUERY)
7
7
 
8
- comments = get_with_pagination uri, query: query
9
- end
8
+ comments = get_with_pagination uri, query: query
9
+ end
10
10
 
11
- def add_comment_to_file(file_id, message: nil, tagged_message: nil)
12
- add_comment(:file, file_id, message, tagged_message)
13
- end
11
+ def add_comment_to_file(file_id, message: nil, tagged_message: nil)
12
+ add_comment(:file, file_id, message, tagged_message)
13
+ end
14
14
 
15
- def reply_to_comment(comment_id, message: nil, tagged_message: nil)
16
- add_comment(:comment, comment_id, message, tagged_message)
17
- end
15
+ def reply_to_comment(comment_id, message: nil, tagged_message: nil)
16
+ add_comment(:comment, comment_id, message, tagged_message)
17
+ end
18
18
 
19
- def change_comment(comment_id, message)
20
- uri = "#{COMMENTS_URI}/#{comment_id}"
21
- attributes = {message: message}
22
- updated_comment, response = put uri, attributes
23
- updated_comment
24
- end
19
+ def change_comment(comment_id, message)
20
+ uri = "#{COMMENTS_URI}/#{comment_id}"
21
+ attributes = {message: message}
22
+ updated_comment, response = put uri, attributes
23
+ updated_comment
24
+ end
25
25
 
26
- def comment(comment_id, fields: [])
27
- uri ="#{COMMENTS_URI}/#{comment_id}"
28
- comment, response = get uri
29
- comment
30
- end
26
+ def comment(comment_id, fields: [])
27
+ uri ="#{COMMENTS_URI}/#{comment_id}"
28
+ comment, response = get uri
29
+ comment
30
+ end
31
31
 
32
- def delete_comment(comment_id)
33
- uri = "#{COMMENTS_URI}/#{comment_id}"
34
- result, response = delete uri
35
- result
36
- end
32
+ def delete_comment(comment_id)
33
+ uri = "#{COMMENTS_URI}/#{comment_id}"
34
+ result, response = delete uri
35
+ result
36
+ end
37
37
 
38
38
 
39
- private
39
+ private
40
40
 
41
- def add_comment(type, id, message, tagged_message)
42
- uri = COMMENTS_URI
43
- attributes = {item: {type: type, id: id}}
44
- attributes[:message] = message unless message.nil?
45
- attributes[:tagged_message] = tagged_message unless tagged_message.nil?
41
+ def add_comment(type, id, message, tagged_message)
42
+ uri = COMMENTS_URI
43
+ attributes = {item: {type: type, id: id}}
44
+ attributes[:message] = message unless message.nil?
45
+ attributes[:tagged_message] = tagged_message unless tagged_message.nil?
46
46
 
47
- new_comment, response = post uri, attributes
48
- new_comment
49
- end
47
+ new_comment, response = post uri, attributes
48
+ new_comment
49
+ end
50
50
 
51
- end
51
+ end
52
52
  end
@@ -1,22 +1,22 @@
1
1
  module Boxr
2
- class Client
2
+ class Client
3
3
 
4
- def user_events(stream_position: 0, stream_type: :all, limit: 100)
5
- query = {stream_position: stream_position, stream_type: stream_type, limit: limit}
6
-
7
- events, response = get(EVENTS_URI, query: query)
8
- Hashie::Mash.new({events: events["entries"], chunk_size: events["chunk_size"], next_stream_position: events["next_stream_position"]})
9
- end
4
+ def user_events(stream_position: 0, stream_type: :all, limit: 100)
5
+ query = {stream_position: stream_position, stream_type: stream_type, limit: limit}
6
+
7
+ events, response = get(EVENTS_URI, query: query)
8
+ Hashie::Mash.new({events: events["entries"], chunk_size: events["chunk_size"], next_stream_position: events["next_stream_position"]})
9
+ end
10
10
 
11
- def enterprise_events(stream_position: 0, limit: 100, event_type: nil, created_after: nil, created_before: nil)
12
- query = {stream_position: stream_position, stream_type: :admin_logs, limit: limit}
13
- query['event_type'] = event_type unless event_type.nil?
14
- query['created_after'] = created_after.to_datetime.rfc3339 unless created_after.nil?
15
- query['created_before'] = created_before.to_datetime.rfc3339 unless created_before.nil?
11
+ def enterprise_events(stream_position: 0, limit: 100, event_type: nil, created_after: nil, created_before: nil)
12
+ query = {stream_position: stream_position, stream_type: :admin_logs, limit: limit}
13
+ query['event_type'] = event_type unless event_type.nil?
14
+ query['created_after'] = created_after.to_datetime.rfc3339 unless created_after.nil?
15
+ query['created_before'] = created_before.to_datetime.rfc3339 unless created_before.nil?
16
16
 
17
- events, response = get(EVENTS_URI, query: query)
18
- Hashie::Mash.new({events: events["entries"], chunk_size: events["chunk_size"], next_stream_position: events["next_stream_position"]})
19
- end
17
+ events, response = get(EVENTS_URI, query: query)
18
+ Hashie::Mash.new({events: events["entries"], chunk_size: events["chunk_size"], next_stream_position: events["next_stream_position"]})
19
+ end
20
20
 
21
- end
21
+ end
22
22
  end
@@ -1,44 +1,44 @@
1
1
  module Boxr
2
2
 
3
- class BoxrException < Exception
3
+ class BoxrException < Exception
4
4
 
5
- attr_reader :response_body, :type, :status, :code, :help_uri, :box_message, :boxr_message, :request_id
5
+ attr_reader :response_body, :type, :status, :code, :help_uri, :box_message, :boxr_message, :request_id
6
6
 
7
- def initialize(status: nil, body: nil, header: nil, boxr_message: nil)
8
- @status = status
9
- @response_body = body
10
- @header = header
11
- @boxr_message = boxr_message
7
+ def initialize(status: nil, body: nil, header: nil, boxr_message: nil)
8
+ @status = status
9
+ @response_body = body
10
+ @header = header
11
+ @boxr_message = boxr_message
12
12
 
13
- if(body)
14
- body_json = Oj.load(body)
15
- if body_json
16
- @type = body_json["type"]
17
- @box_status = body_json["status"]
18
- @code = body_json["code"]
19
- @help_uri = body_json["help_uri"]
20
- @box_message = body_json["message"]
21
- @request_id = body_json["request_id"]
22
- end
23
- end
24
- end
13
+ if(body)
14
+ body_json = Oj.load(body)
15
+ if body_json
16
+ @type = body_json["type"]
17
+ @box_status = body_json["status"]
18
+ @code = body_json["code"]
19
+ @help_uri = body_json["help_uri"]
20
+ @box_message = body_json["message"]
21
+ @request_id = body_json["request_id"]
22
+ end
23
+ end
24
+ end
25
25
 
26
- def message
27
- auth_header = @header['WWW-Authenticate'] unless @header.nil?
28
- if(auth_header && auth_header != [])
29
- "#{@status}: #{auth_header}"
30
- elsif(@box_message)
31
- "#{@status}: #{@box_message}"
32
- elsif(@boxr_message)
33
- @boxr_message
34
- else
35
- "#{@status}: #{@response_body}"
36
- end
37
- end
26
+ def message
27
+ auth_header = @header['WWW-Authenticate'][0] unless @header.nil?
28
+ if(auth_header && auth_header != [])
29
+ "#{@status}: #{auth_header}"
30
+ elsif(@box_message)
31
+ "#{@status}: #{@boxr_message}"
32
+ elsif(@boxr_message)
33
+ @boxr_message
34
+ else
35
+ "#{@status}: #{@response_body}"
36
+ end
37
+ end
38
38
 
39
- def to_s
40
- message
41
- end
42
- end
39
+ def to_s
40
+ message
41
+ end
42
+ end
43
43
 
44
44
  end
@@ -1,211 +1,211 @@
1
1
  module Boxr
2
- class Client
3
-
4
- def file_id(path)
5
- if(path.start_with?('/'))
6
- path = path.slice(1..-1)
7
- end
8
-
9
- path_items = path.split('/')
10
- file_name = path_items.slice!(-1)
11
-
12
- folder_id = folder_id(path_items.join('/'))
13
-
14
- files = folder_items(folder_id, fields: [:id, :name])
15
-
16
- begin
17
- files.select{|f| f.name == file_name}.first.id
18
- rescue
19
- raise BoxrException.new(boxr_message: "File not found: '#{file_name}'")
20
- end
21
- end
22
-
23
- def file(file_id, fields: [])
24
- uri = "#{FILES_URI}/#{file_id}"
25
- query = build_fields_query(fields, FOLDER_AND_FILE_FIELDS_QUERY)
26
- file, response = get uri, query: query
27
- file
28
- end
29
-
30
- def update_file(file_id, name: nil, description: nil, parent_id: nil, shared_link: nil, tags: nil, if_match: nil)
31
- uri = "#{FILES_URI}/#{file_id}"
32
-
33
- attributes = {}
34
- attributes[:name] = name unless name.nil?
35
- attributes[:description] = description unless description.nil?
36
- attributes[:parent_id] = {id: parent_id} unless parent_id.nil?
37
- attributes[:shared_link] = shared_link unless shared_link.nil?
38
- attributes[:tags] = tags unless tags.nil?
39
-
40
- updated_file, response = put uri, attributes, if_match: if_match
41
- updated_file
42
- end
43
-
44
- def download_file(file_id, version: nil, follow_redirect: true)
45
- begin
46
- uri = "#{FILES_URI}/#{file_id}/content"
47
- query = {}
48
- query[:version] = version unless version.nil?
49
- body_json, response = get uri, query: query, success_codes: [302,202]
50
-
51
- if(response.status==302)
52
- location = response.header['Location'][0]
53
-
54
- if(follow_redirect)
55
- file, response = get location, process_response: false
56
- else
57
- return location #simply return the url
58
- end
59
- elsif(response.status==202)
60
- retry_after_seconds = response.header['Retry-After'][0]
61
- sleep retry_after_seconds.to_i
62
- end
63
- end until file
64
-
65
- file
66
- end
67
-
68
- def download_url(file_id, version: nil)
69
- download_file(file_id, version: version, follow_redirect: false)
70
- end
71
-
72
- def upload_file(path_to_file, parent_id, content_created_at: nil, content_modified_at: nil,
73
- preflight_check: true, send_content_md5: true)
74
-
75
- preflight_check(path_to_file, parent_id) if preflight_check
76
-
77
- file_info = nil
78
- response = nil
79
-
80
- File.open(path_to_file) do |file|
81
- content_md5 = send_content_md5 ? Digest::SHA1.file(file).hexdigest : nil
82
- attributes = {filename: file, parent_id: parent_id}
83
- attributes[:content_created_at] = content_created_at.to_datetime.rfc3339 unless content_created_at.nil?
84
- attributes[:content_modified_at] = content_modified_at.to_datetime.rfc3339 unless content_modified_at.nil?
85
- file_info, response = post FILES_UPLOAD_URI, attributes, process_body: false, content_md5: content_md5
86
- end
87
-
88
- file_info["entries"][0]
89
- end
90
-
91
- def delete_file(file_id, if_match: nil)
92
- uri = "#{FILES_URI}/#{file_id}"
93
- result, response = delete uri, if_match: if_match
94
- result
95
- end
96
-
97
- def upload_new_version_of_file(path_to_file, file_id, content_modified_at: nil, send_content_md5: true,
98
- preflight_check: true, if_match: nil)
99
-
100
- preflight_check_new_version_of_file(path_to_file, file_id) if preflight_check
101
-
102
- uri = "#{UPLOAD_URI}/files/#{file_id}/content"
103
- file_info = nil
104
- response = nil
105
-
106
- File.open(path_to_file) do |file|
107
- content_md5 = send_content_md5 ? Digest::SHA1.file(file).hexdigest : nil
108
- attributes = {filename: file}
109
- attributes[:content_modified_at] = content_modified_at.to_datetime.rfc3339 unless content_modified_at.nil?
110
- file_info, response = post uri, attributes, process_body: false, content_md5: content_md5, if_match: if_match
111
- end
112
-
113
- file_info["entries"][0]
114
- end
115
-
116
- def versions_of_file(file_id)
117
- uri = "#{FILES_URI}/#{file_id}/versions"
118
- versions, response = get uri
119
- versions["entries"]
120
- end
121
-
122
- def promote_old_version_of_file(file_id, file_version_id)
123
- uri = "#{FILES_URI}/#{file_id}/versions/current"
124
- attributes = {:type => 'file_version', :id => file_version_id}
125
- new_version, res = post uri, attributes
126
- new_version
127
- end
128
-
129
- def delete_old_version_of_file(file_id, file_version_id, if_match: nil)
130
- uri = "#{FILES_URI}/#{file_id}/versions/#{file_version_id}"
131
- result, response = delete uri, if_match: if_match
132
- result
133
- end
134
-
135
- def copy_file(file_id, parent_id, name: nil)
136
- uri = "#{FILES_URI}/#{file_id}/copy"
137
- attributes = {:parent => {:id => parent_id}}
138
- attributes[:name] = name unless name.nil?
139
- new_file, res = post uri, attributes
140
- new_file
141
- end
142
-
143
- def thumbnail(file_id, min_height: nil, min_width: nil, max_height: nil, max_width: nil)
144
- uri = "#{FILES_URI}/#{file_id}/thumbnail.png"
145
- query = {}
146
- query[:min_height] = min_height unless min_height.nil?
147
- query[:min_width] = min_width unless min_width.nil?
148
- query[:max_height] = max_height unless max_height.nil?
149
- query[:max_width] = max_width unless max_width.nil?
150
- body, response = get uri, query: query, success_codes: [302,202,200], process_response: false
151
-
152
- if(response.status==202 || response.status==302)
153
- location = response.header['Location'][0]
154
- thumbnail, response = get location, process_response: false
155
- else #200
156
- thumbnail = body
157
- end
158
-
159
- thumbnail
160
- end
161
-
162
- def create_shared_link_for_file(file_id, access: nil, unshared_at: nil, can_download: nil, can_preview: nil)
163
- uri = "#{FILES_URI}/#{file_id}"
164
- create_shared_link(uri, file_id, access, unshared_at, can_download, can_preview)
165
- end
166
-
167
- def disable_shared_link_for_file(file_id)
168
- uri = "#{FILES_URI}/#{file_id}"
169
- disable_shared_link(uri, file_id)
170
- end
171
-
172
- def trashed_file(file_id, fields: [])
173
- uri = "#{FILES_URI}/#{file_id}/trash"
174
- query = build_fields_query(fields, FOLDER_AND_FILE_FIELDS_QUERY)
175
-
176
- trashed_file, response = get uri, query: query
177
- trashed_file
178
- end
179
-
180
- def delete_trashed_file(file_id)
181
- uri = "#{FILES_URI}/#{file_id}/trash"
182
-
183
- result, response = delete uri
184
- result
185
- end
186
-
187
- def restore_trashed_file(file_id, name: nil, parent_id: nil)
188
- uri = "#{FILES_URI}/#{file_id}"
189
- restore_trashed_item(uri, name, parent_id)
190
- end
191
-
192
-
193
- private
194
-
195
- def preflight_check(path_to_file, parent_id)
196
- size = File.size(path_to_file)
197
-
198
- #TODO: need to make sure that figuring out the filename from the path_to_file works for people using Winblows
199
- filename = File.basename(path_to_file)
200
- attributes = {"name" => filename, "parent" => {"id" => "#{parent_id}"}, "size" => size}
201
- body_json, res = options "#{FILES_URI}/content", attributes
202
- end
203
-
204
- def preflight_check_new_version_of_file(path_to_file, file_id)
205
- size = File.size(path_to_file)
206
- attributes = {"size" => size}
207
- body_json, res = options "#{FILES_URI}/#{file_id}/content", attributes
208
- end
209
-
210
- end
2
+ class Client
3
+
4
+ def file_id(path)
5
+ if(path.start_with?('/'))
6
+ path = path.slice(1..-1)
7
+ end
8
+
9
+ path_items = path.split('/')
10
+ file_name = path_items.slice!(-1)
11
+
12
+ folder_id = folder_id(path_items.join('/'))
13
+
14
+ files = folder_items(folder_id, fields: [:id, :name])
15
+
16
+ begin
17
+ files.select{|f| f.name == file_name}.first.id
18
+ rescue
19
+ raise BoxrException.new(boxr_message: "File not found: '#{file_name}'")
20
+ end
21
+ end
22
+
23
+ def file(file_id, fields: [])
24
+ uri = "#{FILES_URI}/#{file_id}"
25
+ query = build_fields_query(fields, FOLDER_AND_FILE_FIELDS_QUERY)
26
+ file, response = get uri, query: query
27
+ file
28
+ end
29
+
30
+ def update_file(file_id, name: nil, description: nil, parent_id: nil, shared_link: nil, tags: nil, if_match: nil)
31
+ uri = "#{FILES_URI}/#{file_id}"
32
+
33
+ attributes = {}
34
+ attributes[:name] = name unless name.nil?
35
+ attributes[:description] = description unless description.nil?
36
+ attributes[:parent_id] = {id: parent_id} unless parent_id.nil?
37
+ attributes[:shared_link] = shared_link unless shared_link.nil?
38
+ attributes[:tags] = tags unless tags.nil?
39
+
40
+ updated_file, response = put uri, attributes, if_match: if_match
41
+ updated_file
42
+ end
43
+
44
+ def download_file(file_id, version: nil, follow_redirect: true)
45
+ begin
46
+ uri = "#{FILES_URI}/#{file_id}/content"
47
+ query = {}
48
+ query[:version] = version unless version.nil?
49
+ body_json, response = get uri, query: query, success_codes: [302,202]
50
+
51
+ if(response.status==302)
52
+ location = response.header['Location'][0]
53
+
54
+ if(follow_redirect)
55
+ file, response = get location, process_response: false
56
+ else
57
+ return location #simply return the url
58
+ end
59
+ elsif(response.status==202)
60
+ retry_after_seconds = response.header['Retry-After'][0]
61
+ sleep retry_after_seconds.to_i
62
+ end
63
+ end until file
64
+
65
+ file
66
+ end
67
+
68
+ def download_url(file_id, version: nil)
69
+ download_file(file_id, version: version, follow_redirect: false)
70
+ end
71
+
72
+ def upload_file(path_to_file, parent_id, content_created_at: nil, content_modified_at: nil,
73
+ preflight_check: true, send_content_md5: true)
74
+
75
+ preflight_check(path_to_file, parent_id) if preflight_check
76
+
77
+ file_info = nil
78
+ response = nil
79
+
80
+ File.open(path_to_file) do |file|
81
+ content_md5 = send_content_md5 ? Digest::SHA1.file(file).hexdigest : nil
82
+ attributes = {filename: file, parent_id: parent_id}
83
+ attributes[:content_created_at] = content_created_at.to_datetime.rfc3339 unless content_created_at.nil?
84
+ attributes[:content_modified_at] = content_modified_at.to_datetime.rfc3339 unless content_modified_at.nil?
85
+ file_info, response = post FILES_UPLOAD_URI, attributes, process_body: false, content_md5: content_md5
86
+ end
87
+
88
+ file_info["entries"][0]
89
+ end
90
+
91
+ def delete_file(file_id, if_match: nil)
92
+ uri = "#{FILES_URI}/#{file_id}"
93
+ result, response = delete uri, if_match: if_match
94
+ result
95
+ end
96
+
97
+ def upload_new_version_of_file(path_to_file, file_id, content_modified_at: nil, send_content_md5: true,
98
+ preflight_check: true, if_match: nil)
99
+
100
+ preflight_check_new_version_of_file(path_to_file, file_id) if preflight_check
101
+
102
+ uri = "#{UPLOAD_URI}/files/#{file_id}/content"
103
+ file_info = nil
104
+ response = nil
105
+
106
+ File.open(path_to_file) do |file|
107
+ content_md5 = send_content_md5 ? Digest::SHA1.file(file).hexdigest : nil
108
+ attributes = {filename: file}
109
+ attributes[:content_modified_at] = content_modified_at.to_datetime.rfc3339 unless content_modified_at.nil?
110
+ file_info, response = post uri, attributes, process_body: false, content_md5: content_md5, if_match: if_match
111
+ end
112
+
113
+ file_info["entries"][0]
114
+ end
115
+
116
+ def versions_of_file(file_id)
117
+ uri = "#{FILES_URI}/#{file_id}/versions"
118
+ versions, response = get uri
119
+ versions["entries"]
120
+ end
121
+
122
+ def promote_old_version_of_file(file_id, file_version_id)
123
+ uri = "#{FILES_URI}/#{file_id}/versions/current"
124
+ attributes = {:type => 'file_version', :id => file_version_id}
125
+ new_version, res = post uri, attributes
126
+ new_version
127
+ end
128
+
129
+ def delete_old_version_of_file(file_id, file_version_id, if_match: nil)
130
+ uri = "#{FILES_URI}/#{file_id}/versions/#{file_version_id}"
131
+ result, response = delete uri, if_match: if_match
132
+ result
133
+ end
134
+
135
+ def copy_file(file_id, parent_id, name: nil)
136
+ uri = "#{FILES_URI}/#{file_id}/copy"
137
+ attributes = {:parent => {:id => parent_id}}
138
+ attributes[:name] = name unless name.nil?
139
+ new_file, res = post uri, attributes
140
+ new_file
141
+ end
142
+
143
+ def thumbnail(file_id, min_height: nil, min_width: nil, max_height: nil, max_width: nil)
144
+ uri = "#{FILES_URI}/#{file_id}/thumbnail.png"
145
+ query = {}
146
+ query[:min_height] = min_height unless min_height.nil?
147
+ query[:min_width] = min_width unless min_width.nil?
148
+ query[:max_height] = max_height unless max_height.nil?
149
+ query[:max_width] = max_width unless max_width.nil?
150
+ body, response = get uri, query: query, success_codes: [302,202,200], process_response: false
151
+
152
+ if(response.status==202 || response.status==302)
153
+ location = response.header['Location'][0]
154
+ thumbnail, response = get location, process_response: false
155
+ else #200
156
+ thumbnail = body
157
+ end
158
+
159
+ thumbnail
160
+ end
161
+
162
+ def create_shared_link_for_file(file_id, access: nil, unshared_at: nil, can_download: nil, can_preview: nil)
163
+ uri = "#{FILES_URI}/#{file_id}"
164
+ create_shared_link(uri, file_id, access, unshared_at, can_download, can_preview)
165
+ end
166
+
167
+ def disable_shared_link_for_file(file_id)
168
+ uri = "#{FILES_URI}/#{file_id}"
169
+ disable_shared_link(uri, file_id)
170
+ end
171
+
172
+ def trashed_file(file_id, fields: [])
173
+ uri = "#{FILES_URI}/#{file_id}/trash"
174
+ query = build_fields_query(fields, FOLDER_AND_FILE_FIELDS_QUERY)
175
+
176
+ trashed_file, response = get uri, query: query
177
+ trashed_file
178
+ end
179
+
180
+ def delete_trashed_file(file_id)
181
+ uri = "#{FILES_URI}/#{file_id}/trash"
182
+
183
+ result, response = delete uri
184
+ result
185
+ end
186
+
187
+ def restore_trashed_file(file_id, name: nil, parent_id: nil)
188
+ uri = "#{FILES_URI}/#{file_id}"
189
+ restore_trashed_item(uri, name, parent_id)
190
+ end
191
+
192
+
193
+ private
194
+
195
+ def preflight_check(path_to_file, parent_id)
196
+ size = File.size(path_to_file)
197
+
198
+ #TODO: need to make sure that figuring out the filename from the path_to_file works for people using Winblows
199
+ filename = File.basename(path_to_file)
200
+ attributes = {"name" => filename, "parent" => {"id" => "#{parent_id}"}, "size" => size}
201
+ body_json, res = options "#{FILES_URI}/content", attributes
202
+ end
203
+
204
+ def preflight_check_new_version_of_file(path_to_file, file_id)
205
+ size = File.size(path_to_file)
206
+ attributes = {"size" => size}
207
+ body_json, res = options "#{FILES_URI}/#{file_id}/content", attributes
208
+ end
209
+
210
+ end
211
211
  end