boxr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ module Boxr
2
+ class Client
3
+
4
+ def add_comment_to_file(file_id, message: nil, tagged_message: nil)
5
+ add_comment(:file, file_id, message, tagged_message)
6
+ end
7
+
8
+ def reply_to_comment(comment_id, message: nil, tagged_message: nil)
9
+ add_comment(:comment, comment_id, message, tagged_message)
10
+ end
11
+
12
+ def change_comment(comment_id, message)
13
+ uri = "#{COMMENTS_URI}/#{comment_id}"
14
+ attributes = {message: message}
15
+ updated_comment, response = put uri, attributes
16
+ updated_comment
17
+ end
18
+
19
+ def comment_info(comment_id, fields: [])
20
+ uri ="#{COMMENTS_URI}/#{comment_id}"
21
+ comment, response = get uri
22
+ comment
23
+ end
24
+
25
+ def delete_comment(comment_id)
26
+ uri = "#{COMMENTS_URI}/#{comment_id}"
27
+ result, response = delete uri
28
+ result
29
+ end
30
+
31
+
32
+ private
33
+
34
+ def add_comment(type, id, message, tagged_message)
35
+ uri = COMMENTS_URI
36
+ attributes = {item: {type: type, id: id}}
37
+ attributes[:message] = message unless message.nil?
38
+ attributes[:tagged_message] = tagged_message unless tagged_message.nil?
39
+
40
+ new_comment, response = post uri, attributes
41
+ new_comment
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,39 @@
1
+ module Boxr
2
+
3
+ class BoxrException < Exception
4
+
5
+ attr_reader :response_body, :type, :status, :code, :help_uri, :box_message, :request_id
6
+
7
+ def initialize(status,body,header)
8
+ @status = status
9
+ @response_body = body
10
+ @header = header
11
+
12
+ body_json = Oj.load(body)
13
+ if body_json
14
+ @type = body_json["type"]
15
+ @box_status = body_json["status"]
16
+ @code = body_json["code"]
17
+ @help_uri = body_json["help_uri"]
18
+ @box_message = body_json["message"]
19
+ @request_id = body_json["request_id"]
20
+ end
21
+ end
22
+
23
+ def message
24
+ auth_header = @header['WWW-Authenticate']
25
+ if(auth_header)
26
+ "#{@status}: #{auth_header}"
27
+ elsif(!@box_message.blank?)
28
+ "#{@status}: #{@box_message}"
29
+ else
30
+ "#{@status}: #{@response_body}"
31
+ end
32
+ end
33
+
34
+ def to_s
35
+ message
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,200 @@
1
+ module Boxr
2
+ class Client
3
+
4
+ def file_info(file_id, fields: [])
5
+ uri = "#{FILES_URI}/#{file_id}"
6
+ query = build_fields_query(fields, FOLDER_AND_FILE_FIELDS_QUERY)
7
+ file, response = get uri, query: query
8
+ file
9
+ end
10
+
11
+ def update_file_info(file_id, name: nil, description: nil, parent_id: nil, shared_link: nil, tags: nil, if_match: nil)
12
+ uri = "#{FILES_URI}/#{file_id}"
13
+
14
+ attributes = {}
15
+ attributes[:name] = name unless name.nil?
16
+ attributes[:description] = description unless description.nil?
17
+ attributes[:parent_id] = {id: parent_id} unless parent_id.nil?
18
+ attributes[:shared_link] = shared_link unless shared_link.nil?
19
+ attributes[:tags] = tags unless tags.nil?
20
+
21
+ updated_file, response = put uri, attributes, if_match: if_match
22
+ updated_file
23
+ end
24
+
25
+ def download_file(file_id, version: nil)
26
+ begin
27
+ uri = "#{FILES_URI}/#{file_id}/content"
28
+ query = {}
29
+ query[:version] = version unless version.nil?
30
+ body_json, response = get uri, query: query, success_codes: [302,202]
31
+
32
+ if(response.status==302)
33
+ location = response.header['Location'][0]
34
+ file, response = get location, process_response: false
35
+ elsif(response.status==202)
36
+ retry_after_seconds = response.header['Retry-After'][0]
37
+ sleep retry_after_seconds.to_i
38
+ end
39
+ end until file
40
+
41
+ file
42
+ end
43
+
44
+ def upload_file(path_to_file, parent_id, content_created_at: nil, content_modified_at: nil,
45
+ preflight_check: true, send_content_md5: true)
46
+
47
+ preflight_check(path_to_file, parent_id) if preflight_check
48
+
49
+ file_info = nil
50
+ response = nil
51
+
52
+ File.open(path_to_file) do |file|
53
+ content_md5 = send_content_md5 ? Digest::SHA1.file(file).hexdigest : nil
54
+ attributes = {filename: file, parent_id: parent_id}
55
+ attributes[:content_created_at] = content_created_at.to_datetime.rfc3339 unless content_created_at.nil?
56
+ attributes[:content_modified_at] = content_modified_at.to_datetime.rfc3339 unless content_modified_at.nil?
57
+ file_info, response = post FILES_UPLOAD_URI, attributes, process_body: false, content_md5: content_md5
58
+ end
59
+
60
+ file_info["entries"][0]
61
+ end
62
+
63
+ def delete_file(file_id, if_match: nil)
64
+ uri = "#{FILES_URI}/#{file_id}"
65
+ result, response = delete uri, if_match: if_match
66
+ result
67
+ end
68
+
69
+ def upload_new_version_of_file(path_to_file, file_id, content_modified_at: nil, send_content_md5: true,
70
+ preflight_check: true, if_match: nil)
71
+
72
+ preflight_check_new_version_of_file(path_to_file, file_id) if preflight_check
73
+
74
+ uri = "#{UPLOAD_URI}/files/#{file_id}/content"
75
+ file_info = nil
76
+ response = nil
77
+
78
+ File.open(path_to_file) do |file|
79
+ content_md5 = send_content_md5 ? Digest::SHA1.file(file).hexdigest : nil
80
+ attributes = {filename: file}
81
+ attributes[:content_modified_at] = content_modified_at.to_datetime.rfc3339 unless content_modified_at.nil?
82
+ file_info, response = post uri, attributes, process_body: false, content_md5: content_md5, if_match: if_match
83
+ end
84
+
85
+ file_info["entries"][0]
86
+ end
87
+
88
+ def versions_of_file(file_id)
89
+ uri = "#{FILES_URI}/#{file_id}/versions"
90
+ versions, response = get uri
91
+ versions["entries"]
92
+ end
93
+
94
+ def download_old_version_of_file
95
+
96
+ end
97
+
98
+ def promote_old_version_of_file(file_id, file_version_id)
99
+ uri = "#{FILES_URI}/#{file_id}/versions/current"
100
+ attributes = {:type => 'file_version', :id => file_version_id}
101
+ new_version, res = post uri, attributes
102
+ new_version
103
+ end
104
+
105
+ def delete_old_version_of_file(file_id, file_version_id, if_match: nil)
106
+ uri = "#{FILES_URI}/#{file_id}/versions/#{file_version_id}"
107
+ body_json, res = delete uri, if_match: if_match
108
+ body_json
109
+ end
110
+
111
+ def copy_file(file_id, parent_id, name: nil)
112
+ uri = "#{FILES_URI}/#{file_id}/copy"
113
+ attributes = {:parent => {:id => parent_id}}
114
+ attributes[:name] = name unless name.nil?
115
+ new_file, res = post uri, attributes
116
+ new_file
117
+ end
118
+
119
+ def thumbnail(file_id, min_height: nil, min_width: nil, max_height: nil, max_width: nil)
120
+ uri = "#{FILES_URI}/#{file_id}/thumbnail.png"
121
+ query = {}
122
+ query[:min_height] = min_height unless min_height.nil?
123
+ query[:min_width] = min_width unless min_width.nil?
124
+ query[:max_height] = max_height unless max_height.nil?
125
+ query[:max_width] = max_width unless max_width.nil?
126
+ body, response = get uri, query: query, success_codes: [302,202,200], process_response: false
127
+
128
+ if(response.status==202 || response.status==302)
129
+ location = response.header['Location'][0]
130
+ thumbnail, response = get location, process_response: false
131
+ else #200
132
+ thumbnail = body
133
+ end
134
+
135
+ thumbnail
136
+ end
137
+
138
+ def create_shared_link_for_file(file_id, access: nil, unshared_at: nil, can_download: nil, can_preview: nil)
139
+ uri = "#{FILES_URI}/#{file_id}"
140
+ create_shared_link(uri, file_id, access, unshared_at, can_download, can_preview)
141
+ end
142
+
143
+ def disable_shared_link_for_file(file_id)
144
+ uri = "#{FILES_URI}/#{file_id}"
145
+ disable_shared_link(uri, file_id)
146
+ end
147
+
148
+ def trashed_file(file_id)
149
+ uri = "#{FILES_URI}/#{file_id}/trash"
150
+ trashed_file, response = get uri
151
+ trashed_file
152
+ end
153
+
154
+ def delete_trashed_file(file_id)
155
+ uri = "#{FILES_URI}/#{file_id}/trash"
156
+
157
+ result, response = delete uri
158
+ result
159
+ end
160
+
161
+ def restore_trashed_file(file_id, name: nil, parent_id: nil)
162
+ uri = "#{FILES_URI}/#{file_id}"
163
+ restore_trashed_item(uri, name, parent_id)
164
+ end
165
+
166
+ def file_comments(file_id, fields: [])
167
+ uri = "#{FILES_URI}/#{file_id}/comments"
168
+ query = build_fields_query(fields, COMMENT_FIELDS_QUERY)
169
+
170
+ comments = get_with_pagination uri, query: query
171
+ end
172
+
173
+ def file_tasks(file_id, fields: [])
174
+ uri = "#{FILES_URI}/#{file_id}/tasks"
175
+ query = build_fields_query(fields, TASK_FIELDS_QUERY)
176
+
177
+ tasks, response = get uri, query: query
178
+ tasks["entries"]
179
+ end
180
+
181
+
182
+ private
183
+
184
+ def preflight_check(path_to_file, parent_id)
185
+ size = File.size(path_to_file)
186
+
187
+ #TODO: need to make sure that figuring out the filename from the path_to_file works for people using Winblows
188
+ filename = File.basename(path_to_file)
189
+ attributes = {"name" => filename, "parent" => {"id" => "#{parent_id}"}, "size" => size}
190
+ body_json, res = options "#{FILES_URI}/content", attributes
191
+ end
192
+
193
+ def preflight_check_new_version_of_file(path_to_file, file_id)
194
+ size = File.size(path_to_file)
195
+ attributes = {"size" => size}
196
+ body_json, res = options "#{FILES_URI}/#{file_id}/content", attributes
197
+ end
198
+
199
+ end
200
+ end
@@ -0,0 +1,105 @@
1
+ module Boxr
2
+ class Client
3
+
4
+ def folder_items(folder_id, fields: [])
5
+ query = build_fields_query(fields, FOLDER_AND_FILE_FIELDS_QUERY)
6
+ uri = "#{FOLDERS_URI}/#{folder_id}/items"
7
+
8
+ items = get_with_pagination uri, query: query, limit: FOLDER_ITEMS_LIMIT
9
+ end
10
+
11
+ def create_folder(name, parent_id)
12
+ uri = "#{FOLDERS_URI}"
13
+ attributes = {:name => name, :parent => {:id => parent_id}}
14
+
15
+ created_folder, response = post uri, attributes
16
+ created_folder
17
+ end
18
+
19
+ def folder_info(folder_id, fields: [])
20
+ query = build_fields_query(fields, FOLDER_AND_FILE_FIELDS_QUERY)
21
+ uri = "#{FOLDERS_URI}/#{folder_id}"
22
+
23
+ folder, response = get uri, query: query
24
+ folder
25
+ end
26
+
27
+ def update_folder_info(folder_id, name: nil, description: nil, parent_id: nil, shared_link: nil,
28
+ folder_upload_email: nil, owned_by_id: nil, sync_state: nil, tags: nil,
29
+ can_non_owners_invite: nil, if_match: nil)
30
+ uri = "#{FOLDERS_URI}/#{folder_id}"
31
+
32
+ attributes = {}
33
+ attributes[:name] = name unless name.nil?
34
+ attributes[:description] = description unless description.nil?
35
+ attributes[:parent_id] = {id: parent_id} unless parent_id.nil?
36
+ attributes[:shared_link] = shared_link unless shared_link.nil?
37
+ attributes[:folder_upload_email] = folder_upload_email unless folder_upload_email.nil?
38
+ attributes[:owned_by_id] = {owned_by: owned_by_id} unless owned_by_id.nil?
39
+ attributes[:sync_state] = sync_state unless sync_state.nil?
40
+ attributes[:tags] = tags unless tags.nil?
41
+ attributes[:can_non_owners_invite] = can_non_owners_invite unless can_non_owners_invite.nil?
42
+
43
+ updated_folder, response = put uri, attributes, if_match: if_match
44
+ updated_folder
45
+ end
46
+
47
+ def delete_folder(folder_id, recursive: false, if_match: nil)
48
+ uri = "#{FOLDERS_URI}/#{folder_id}"
49
+ query = {:recursive => recursive}
50
+
51
+ result, response = delete uri, query, if_match: if_match
52
+ result
53
+ end
54
+
55
+ def copy_folder(folder_id, dest_folder_id, name: nil)
56
+ uri = "#{FOLDERS_URI}/#{folder_id}/copy"
57
+ attributes = {:parent => {:id => dest_folder_id}}
58
+ attributes[:name] = name unless name.nil?
59
+
60
+ new_folder, response = post uri, attributes
61
+ new_folder
62
+ end
63
+
64
+ def create_shared_link_for_folder(folder_id, access: nil, unshared_at: nil, can_download: nil, can_preview: nil)
65
+ uri = "#{FOLDERS_URI}/#{folder_id}"
66
+ create_shared_link(uri, folder_id, access, unshared_at, can_download, can_preview)
67
+ end
68
+
69
+ def disable_shared_link_for_folder(folder_id)
70
+ uri = "#{FOLDERS_URI}/#{folder_id}"
71
+ disable_shared_link(uri, folder_id)
72
+ end
73
+
74
+ def folder_collaborations(folder_id)
75
+ uri = "#{FOLDERS_URI}/#{folder_id}/collaborations"
76
+ collaborations, response = get uri
77
+ collaborations
78
+ end
79
+
80
+ def trash(fields: [])
81
+ uri = "#{FOLDERS_URI}/trash/items"
82
+ query = build_fields_query(fields, FOLDER_AND_FILE_FIELDS_QUERY)
83
+
84
+ items = get_with_pagination uri, query: query, limit: FOLDER_ITEMS_LIMIT
85
+ end
86
+
87
+ def trashed_folder(folder_id)
88
+ uri = "#{FOLDERS_URI}/#{folder_id}/trash"
89
+ item, response = get uri
90
+ item
91
+ end
92
+
93
+ def delete_trashed_folder(folder_id)
94
+ uri = "#{FOLDERS_URI}/#{folder_id}/trash"
95
+ result, response = delete uri
96
+ result
97
+ end
98
+
99
+ def restore_trashed_folder(folder_id, name: nil, parent_id: nil)
100
+ uri = "#{FOLDERS_URI}/#{folder_id}"
101
+ restore_trashed_item(uri, name, parent_id)
102
+ end
103
+
104
+ end
105
+ end
@@ -0,0 +1,76 @@
1
+ module Boxr
2
+ class Client
3
+
4
+ def groups(fields: [])
5
+ query = build_fields_query(fields, GROUP_FIELDS_QUERY)
6
+ groups = get_with_pagination(GROUPS_URI, query: query)
7
+ end
8
+
9
+ def create_group(name)
10
+ attributes = {name: name}
11
+ new_group, response = post(GROUPS_URI, attributes)
12
+ new_group
13
+ end
14
+
15
+ def update_group(group_id, name)
16
+ uri = "#{GROUPS_URI}/#{group_id}"
17
+ attributes = {name: name}
18
+
19
+ updated_group, response = put(uri, attributes)
20
+ updated_group
21
+ end
22
+
23
+ def delete_group(group_id)
24
+ uri = "#{GROUPS_URI}/#{group_id}"
25
+ result, response = delete(uri)
26
+ result
27
+ end
28
+
29
+ def group_memberships(group_id)
30
+ uri = "#{GROUPS_URI}/#{group_id}/memberships"
31
+ memberships = get_with_pagination(uri)
32
+ end
33
+
34
+ def group_memberships_for_user(user_id)
35
+ uri = "#{USERS_URI}/#{user_id}/memberships"
36
+ memberships = get_with_pagination(uri)
37
+ end
38
+
39
+ def group_memberships_for_me
40
+ uri = "#{USERS_URI}/me/memberships"
41
+ memberships = get_with_pagination(uri)
42
+ end
43
+
44
+ def group_membership(membership_id)
45
+ uri = "#{GROUP_MEMBERSHIPS_URI}/#{membership_id}"
46
+ membership, response = get(uri)
47
+ membership
48
+ end
49
+
50
+ def add_user_to_group(user_id, group_id, role: nil)
51
+ attributes = {user: {id: user_id}, group: {id: group_id}}
52
+ attributes[:role] = role unless role.nil?
53
+ membership, response = post(GROUP_MEMBERSHIPS_URI, attributes)
54
+ membership
55
+ end
56
+
57
+ def update_group_membership(membership_id, role)
58
+ uri = "#{GROUP_MEMBERSHIPS_URI}/#{membership_id}"
59
+ attributes = {role: role}
60
+ updated_membership, response = put(uri, attributes)
61
+ updated_membership
62
+ end
63
+
64
+ def delete_group_membership(membership_id)
65
+ uri = "#{GROUP_MEMBERSHIPS_URI}/#{membership_id}"
66
+ result, response = delete(uri)
67
+ result
68
+ end
69
+
70
+ def group_collaborations(group_id)
71
+ uri = "#{GROUPS_URI}/#{group_id}/collaborations"
72
+ collaborations = get_with_pagination(uri)
73
+ end
74
+
75
+ end
76
+ end