ruby-egnyte 0.1.6

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.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/Gemfile +5 -0
  4. data/LICENSE.txt +23 -0
  5. data/README.markdown +113 -0
  6. data/Rakefile +4 -0
  7. data/egnyte.gemspec +32 -0
  8. data/includes/cacert.pem +3849 -0
  9. data/lib/egnyte.rb +25 -0
  10. data/lib/egnyte/client.rb +11 -0
  11. data/lib/egnyte/errors.rb +28 -0
  12. data/lib/egnyte/file.rb +51 -0
  13. data/lib/egnyte/folder.rb +112 -0
  14. data/lib/egnyte/folder_structure.rb +13 -0
  15. data/lib/egnyte/group.rb +185 -0
  16. data/lib/egnyte/helper.rb +41 -0
  17. data/lib/egnyte/item.rb +43 -0
  18. data/lib/egnyte/link.rb +109 -0
  19. data/lib/egnyte/permission.rb +181 -0
  20. data/lib/egnyte/session.rb +197 -0
  21. data/lib/egnyte/user.rb +207 -0
  22. data/lib/egnyte/version.rb +3 -0
  23. data/spec/file_spec.rb +71 -0
  24. data/spec/fixtures/folder_listing_no_files.json +12 -0
  25. data/spec/fixtures/group/group_all.json +17 -0
  26. data/spec/fixtures/group/group_by_parameter.json +10 -0
  27. data/spec/fixtures/group/group_by_parameter_empty.json +7 -0
  28. data/spec/fixtures/group/group_create.json +1 -0
  29. data/spec/fixtures/link/link.json +1 -0
  30. data/spec/fixtures/link/link_create.json +1 -0
  31. data/spec/fixtures/link/link_list.json +1 -0
  32. data/spec/fixtures/link/link_list_empty.json +1 -0
  33. data/spec/fixtures/list_file.json +27 -0
  34. data/spec/fixtures/list_folder.json +23 -0
  35. data/spec/fixtures/permission/permission_list.json +1 -0
  36. data/spec/fixtures/user/user_all.json +1007 -0
  37. data/spec/fixtures/user/user_by_email.json +27 -0
  38. data/spec/fixtures/user/user_create.json +20 -0
  39. data/spec/fixtures/user/user_find.json +20 -0
  40. data/spec/fixtures/user/user_update.json +20 -0
  41. data/spec/folder_spec.rb +207 -0
  42. data/spec/group_spec.rb +166 -0
  43. data/spec/helper_spec.rb +30 -0
  44. data/spec/links_spec.rb +156 -0
  45. data/spec/permissions_spec.rb +98 -0
  46. data/spec/spec_helper.rb +17 -0
  47. data/spec/user_spec.rb +212 -0
  48. metadata +260 -0
@@ -0,0 +1,25 @@
1
+ require 'uri'
2
+ require 'json'
3
+ require 'oauth2'
4
+ require 'open-uri'
5
+ require 'net/https'
6
+ require 'mime/types'
7
+ require 'net/http/post/multipart'
8
+
9
+ require "egnyte/version"
10
+ require "egnyte/helper"
11
+ require "egnyte/errors"
12
+ require "egnyte/session"
13
+ require "egnyte/client"
14
+ require "egnyte/item"
15
+ require "egnyte/folder"
16
+ require "egnyte/file"
17
+ require "egnyte/folder_structure"
18
+ require "egnyte/user"
19
+ require "egnyte/group"
20
+ require "egnyte/link"
21
+ require "egnyte/permission"
22
+
23
+ module Egnyte
24
+ EGNYTE_DOMAIN = "egnyte.com"
25
+ end
@@ -0,0 +1,11 @@
1
+ module Egnyte
2
+
3
+ class Client
4
+
5
+ def initialize(session)
6
+ @session = session
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,28 @@
1
+ module Egnyte
2
+ class EgnyteError < StandardError
3
+ def initialize(data)
4
+ @data = data
5
+ end
6
+
7
+ def [](key)
8
+ @data[key]
9
+ end
10
+ end
11
+
12
+ class UnsupportedAuthStrategy < StandardError; end
13
+ class InvalidParameters < StandardError; end
14
+ class FileExpected < StandardError; end
15
+ class FolderExpected < StandardError; end
16
+ class RecordNotFound < EgnyteError; end
17
+ class RequestError < EgnyteError; end
18
+ class BadRequest < EgnyteError; end
19
+ class NotAuthorized < EgnyteError; end
20
+ class InsufficientPermissions < EgnyteError; end
21
+ class DuplicateRecordExists < EgnyteError; end
22
+ class FileSizeExceedsLimit < EgnyteError; end
23
+ class ClientIdRequired < EgnyteError; end
24
+ class DomainRequired < EgnyteError; end
25
+ class OAuthUsernameRequired < StandardError; end
26
+ class OAuthPasswordRequired < StandardError; end
27
+ class MissingAttribute < EgnyteError; end
28
+ end
@@ -0,0 +1,51 @@
1
+ module Egnyte
2
+
3
+ class Client
4
+
5
+ def file(path)
6
+ File::find(@session, path)
7
+ end
8
+
9
+ end
10
+
11
+ class File < Item
12
+
13
+ def download
14
+ stream.read
15
+ end
16
+
17
+ def download_version(entry_id)
18
+ stream(:entry_id => entry_id).read
19
+ end
20
+
21
+ # use opts to provide lambdas
22
+ # to track the streaming download:
23
+ #
24
+ # :content_length_proc
25
+ # :progress_proc
26
+ def stream( opts={} )
27
+ file_content_path = "#{fs_path('fs-content')}#{Egnyte::Helper.normalize_path(path)}"
28
+ file_content_path += "?entry_id=#{opts[:entry_id]}" if opts[:entry_id]
29
+ @session.streaming_download(file_content_path, opts )
30
+ end
31
+
32
+ def delete
33
+ @session.delete("#{fs_path}#{path}")
34
+ end
35
+
36
+ def self.find(session, path)
37
+ path = Egnyte::Helper.normalize_path(path)
38
+
39
+ file = File.new({
40
+ 'path' => path
41
+ }, session)
42
+
43
+ parsed_body = session.get("#{file.fs_path}#{path}")
44
+
45
+ raise FileExpected if parsed_body['is_folder']
46
+
47
+ file.update_data(parsed_body)
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,112 @@
1
+ module Egnyte
2
+
3
+ class Client
4
+ def folder(path='Shared')
5
+ Folder::find(@session, path)
6
+ end
7
+
8
+ def create_folder(path)
9
+ Folder::create(@session, path)
10
+ end
11
+
12
+ def delete_folder(path)
13
+ Folder::delete(@session, path)
14
+ end
15
+ end
16
+
17
+ class Folder < Item
18
+ def create(path)
19
+ path = Egnyte::Helper.normalize_path(path)
20
+ new_folder_path = "#{self.path}/#{path}"
21
+ Egnyte::Folder.create(@session, new_folder_path)
22
+ end
23
+
24
+ def self.create(session, path)
25
+ path = Egnyte::Helper.normalize_path(path)
26
+ session.post("#{Egnyte::Item.fs_path(session)}#{path}", JSON.dump({
27
+ action: 'add_folder'
28
+ }))
29
+
30
+ Folder.new({
31
+ 'path' => path,
32
+ 'folders' => [],
33
+ 'is_folder' => true,
34
+ 'name' => path.split('/').pop
35
+ }, session)
36
+ end
37
+
38
+ def delete
39
+ Egnyte::Folder.delete(@session, path)
40
+ end
41
+
42
+ def self.delete(session, path)
43
+ session.delete("#{Egnyte::Item.fs_path(session)}/#{path}")
44
+ end
45
+
46
+ def upload(filename, content)
47
+ resp = @session.multipart_post("#{fs_path('fs-content')}#{path}/#{filename}", filename, content, false)
48
+
49
+ content.rewind # to calculate size, rewind content stream.
50
+
51
+ File.new({
52
+ 'is_folder' => false,
53
+ 'entry_id' => resp['ETag'],
54
+ 'checksum' => resp['X-Sha512-Checksum'],
55
+ 'last_modified' => resp['Last-Modified'],
56
+ 'name' => filename,
57
+ 'size' => content.size
58
+ }, @session)
59
+ end
60
+
61
+ def files
62
+ create_objects(File, 'files')
63
+ end
64
+
65
+ def folders
66
+ create_objects(Folder, 'folders')
67
+ end
68
+
69
+ def self.find(session, path)
70
+ path = Egnyte::Helper.normalize_path(path)
71
+
72
+ folder = Folder.new({
73
+ 'path' => path
74
+ }, session)
75
+
76
+ parsed_body = session.get("#{folder.fs_path}#{path}")
77
+
78
+ raise FolderExpected unless parsed_body['is_folder']
79
+
80
+ folder.update_data(parsed_body)
81
+ end
82
+
83
+ def permissions(params=nil)
84
+ Egnyte::Permission.folder_permissions(@session, @data['path'])
85
+ end
86
+
87
+ def inherited_permissions(params=nil)
88
+ Egnyte::Permission.inherited_permissions(@session, @data['path'])
89
+ end
90
+
91
+ def explicit_permissions(params=nil)
92
+ Egnyte::Permission.explicit_permissions(@session, @data['path'])
93
+ end
94
+
95
+ def apply_permissions(permission_object)
96
+ Egnyte::Permission.apply(@session, permission_object, @data['path'])
97
+ end
98
+
99
+ private
100
+
101
+ def create_objects(klass, key)
102
+ return [] unless @data[key]
103
+ @data[key].map do |data|
104
+ data = data.merge({
105
+ 'path' => "#{path}/#{data['name']}"
106
+ })
107
+ klass.new(data, @session)
108
+ end
109
+ end
110
+
111
+ end
112
+ end
@@ -0,0 +1,13 @@
1
+ module Egnyte
2
+ class FolderStructure
3
+ def self.traverse_dfs(folder, session, order=:preorder, max_depth=nil, current_depth=1, &block)
4
+ yield folder if order == :preorder
5
+ folder.folders = folder.folders.each do |f|
6
+ f = Egnyte::Folder.find(session, f.path)
7
+ traverse_dfs(f, session, order, max_depth, current_depth+1, &block) unless !max_depth.nil? and current_depth >= max_depth
8
+ f
9
+ end
10
+ yield folder if order == :postorder
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,185 @@
1
+ module Egnyte
2
+
3
+ class Client
4
+
5
+ def groups
6
+ Group::all(@session)
7
+ end
8
+
9
+ def groups_where(params)
10
+ Group::where(@session, params)
11
+ end
12
+
13
+ def search_groups(search_string)
14
+ Group::search(@session, search_string)
15
+ end
16
+
17
+ def group(id)
18
+ Group::find(@session, id)
19
+ end
20
+
21
+ def group_by_name(name)
22
+ Group::where(@session, {:displayName => name}).first
23
+ end
24
+
25
+ def create_group(params)
26
+ Group::create(@session, params)
27
+ end
28
+
29
+ def delete_group(id)
30
+ Group::delete(@session, id)
31
+ end
32
+
33
+ end
34
+
35
+ class Group
36
+
37
+ @@required_attributes = ['displayName']
38
+ attr_accessor :displayName, :members
39
+ attr_reader :id
40
+
41
+ def initialize(session, params)
42
+ @session = session
43
+ if params.is_a? String
44
+ @displayName = params
45
+ elsif params.is_a? Hash
46
+ params.each do |k,v|
47
+ if k == "name"
48
+ @displayName = v
49
+ else
50
+ instance_variable_set("@#{k}", v)
51
+ end
52
+ end
53
+ end
54
+ @members = [] if @members.nil?
55
+ end
56
+
57
+ def self.all(session)
58
+ self.where(session)
59
+ end
60
+
61
+ def self.create(session, params)
62
+ group = self.new(session, params)
63
+ group.save
64
+ end
65
+
66
+ def name
67
+ @displayName
68
+ end
69
+
70
+ def name=(name)
71
+ @displayName = name
72
+ end
73
+
74
+ def self.find(session, id)
75
+ response = session.get("#{self.group_path(session)}/#{id}", return_parsed_response=true)
76
+ self.new(session, response)
77
+ end
78
+
79
+ def self.find_by_name(session, displayName)
80
+ self.where(session, {:displayName => displayName}).first
81
+ end
82
+
83
+ def self.where(session, params=nil)
84
+ startIndex = 1
85
+ group_count = nil
86
+ itemsPerPage = 100
87
+ group_list = []
88
+ base_url = self.group_path(session)
89
+ base_url += Egnyte::Helper.params_to_filter_string(params) if params
90
+ while startIndex == 1 || group_count > startIndex
91
+ url = base_url
92
+ url += params.nil? ? '?' : '&'
93
+ url += "startIndex=#{startIndex}&count=#{itemsPerPage}"
94
+ parsed_body = session.get(url)
95
+ parsed_body["resources"].each do |group_hash|
96
+ group_list << self.new(session, group_hash)
97
+ end
98
+ group_count = parsed_body["totalResults"]
99
+ startIndex += itemsPerPage
100
+ end
101
+ group_list
102
+ end
103
+
104
+ def self.search(session, search_string)
105
+ group_list = self.all(session)
106
+ result_list = []
107
+ group_list.each do |user|
108
+ catch(:found) do
109
+ user.instance_variables.each do |ivar|
110
+ value = user.instance_variable_get(ivar).to_s
111
+ if value.match(search_string)
112
+ result_list << user
113
+ throw :found
114
+ end
115
+ end
116
+ end
117
+ end
118
+ result_list
119
+ end
120
+
121
+ def save
122
+ raise Egnyte::MissingAttribute.new(missing_attributes) unless valid?
123
+ response = ''
124
+ if @id.nil? or @id.to_s.empty?
125
+ response = @session.post(group_path, to_json_for_api_call)
126
+ @id = response['id']
127
+ else
128
+ response = @session.put("#{group_path}/#{@id}", to_json_for_api_call)
129
+ end
130
+ Egnyte::Group.new(@session, response)
131
+ end
132
+
133
+ def delete
134
+ Egnyte::Group.delete(@session, @id)
135
+ end
136
+
137
+ def self.delete(session, id)
138
+ session.delete("#{self.group_path(session)}/#{id}", return_parsed_response=true)
139
+ end
140
+
141
+ def valid?
142
+ return missing_attributes.size < 1
143
+ end
144
+
145
+ def missing_attributes
146
+ missing = @@required_attributes.collect do |param|
147
+ param unless instance_variable_get("@#{param}")
148
+ end
149
+ missing.compact
150
+ end
151
+
152
+ def to_hash
153
+ hash = to_hash_for_api_call
154
+ hash[:id] = @id
155
+ hash
156
+ end
157
+
158
+ def to_json
159
+ to_hash.to_json
160
+ end
161
+
162
+ def to_hash_for_api_call
163
+ hash = {}
164
+ hash[:displayName] = @displayName
165
+ hash[:members] = []
166
+ @members.each do |group_member|
167
+ hash[:members] << {"value" => group_member}
168
+ end
169
+ hash
170
+ end
171
+
172
+ def to_json_for_api_call
173
+ to_hash_for_api_call.to_json
174
+ end
175
+
176
+ def group_path
177
+ Egnyte::Group.group_path(@session)
178
+ end
179
+
180
+ def self.group_path(session)
181
+ "https://#{session.domain}.#{EGNYTE_DOMAIN}/#{session.api}/v2/groups"
182
+ end
183
+
184
+ end
185
+ end
@@ -0,0 +1,41 @@
1
+ module Egnyte
2
+ class Helper
3
+
4
+ # removes leading and trailing '/'
5
+ # from folder and file names.
6
+ def self.normalize_path(path)
7
+ path.gsub(/(^\/)|(\/$)/, '')
8
+ end
9
+
10
+ def self.params_to_s(params)
11
+ str = ''
12
+ if params
13
+ str = "?"
14
+ params.each_with_index do |(k,v),i|
15
+ v.split('|') if v.instance_of? Array
16
+ str += "#{k}=#{v}"
17
+ str += "&" unless i == params.size - 1
18
+ end
19
+ end
20
+ return str
21
+ end
22
+
23
+ def self.params_to_filter_string(params)
24
+ str = ''
25
+ if params
26
+ str = "?"
27
+ params.each_with_index do |(k,v),i|
28
+ str += "filter="
29
+ str += "#{k} eq \"#{v}\""
30
+ str += "&" unless i == params.size - 1
31
+ end
32
+ end
33
+ return str
34
+ end
35
+
36
+ def self.encode_url(url)
37
+ URI.encode(url).gsub("[","%5B").gsub("]","%5D")
38
+ end
39
+
40
+ end
41
+ end