Class: Files::Folder

Inherits:
Object
  • Object
show all
Defined in:
lib/files.com/models/folder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Folder

Returns a new instance of Folder.



109
110
111
112
113
# File 'lib/files.com/models/folder.rb', line 109

def initialize(*args)
  @attributes = (args[0].is_a?(Hash) && args[0]) || {}
  @options = (args[1].is_a?(Hash) && args[1]) || {}
  @attributes['path'] = args[0] if args[0].is_a?(String)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/files.com/models/folder.rb', line 5

def attributes
  @attributes
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/files.com/models/folder.rb', line 5

def options
  @options
end

#posObject



133
134
135
# File 'lib/files.com/models/folder.rb', line 133

def pos
  @pos ||= 0
end

Class Method Details

.[](path) ⇒ Object



6
7
8
9
# File 'lib/files.com/models/folder.rb', line 6

def self.[](path)
  subfolders = find_recursive(path.split("*").first || "")
  subfolders.select { |subfolder| ::File.fnmatch(path, "/" + subfolder) }
end

.chdir(path, &block) ⇒ Object

Raises:

  • (Errno::ENOENT)


11
12
13
14
15
16
17
18
19
# File 'lib/files.com/models/folder.rb', line 11

def self.chdir(path, &block)
  raise Errno::ENOENT.new(path) if exist? path

  if block
    yield path
  else
    @@path ||= path
  end
end

.children(path, _encoding = "") ⇒ Object



21
22
23
# File 'lib/files.com/models/folder.rb', line 21

def self.children(path, _encoding = "")
  Folder.new(path).contents.map(&:path)
end

.chroot(*args) ⇒ Object



25
# File 'lib/files.com/models/folder.rb', line 25

def self.chroot(*args); end

.create(path, params = {}, options = {}) ⇒ Object

Create folder



361
362
363
364
365
366
367
368
369
# File 'lib/files.com/models/folder.rb', line 361

def self.create(path, params = {}, options = {})
  params ||= {}
  params[:path] = path
  raise InvalidParameterError.new("Bad parameter: path must be an String") if params.dig(:path) and !params.dig(:path).is_a?(String)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  response, options = Api.send_request("/folders/#{Addressable::URI.encode_component(params[:path])}", :post, params, options)
  File.new(response.data, options)
end

.delete(path, params = {}, options = {}) ⇒ Object



27
28
29
# File 'lib/files.com/models/folder.rb', line 27

def self.delete(path, params = {}, options = {})
  File.delete(path, params, options)
end

.destroy(path, params = {}, options = {}) ⇒ Object



31
32
33
# File 'lib/files.com/models/folder.rb', line 31

def self.destroy(path, params = {}, options = {})
  File.destroy(path, params, options)
end

.each_child(path, _encoding = "", &block) ⇒ Object



35
36
37
# File 'lib/files.com/models/folder.rb', line 35

def self.each_child(path, _encoding = "", &block)
  Folder.new(path).each(&block)
end

.empty?(*_args) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/files.com/models/folder.rb', line 39

def self.empty?(*_args)
  Folder.new(path).contents.empty?
end

.entries(path) ⇒ Object



43
44
45
# File 'lib/files.com/models/folder.rb', line 43

def self.entries(path)
  list(path)
end

.exist?(*args) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/files.com/models/folder.rb', line 47

def self.exist?(*args)
  File.exist?(*args)
end

.find(path, params = {}, options = {}) ⇒ Object



51
52
53
# File 'lib/files.com/models/folder.rb', line 51

def self.find(path, params = {}, options = {})
  File.find(path, params, options)
end

.find_recursive(path, type = "dir") ⇒ Object



55
56
57
58
59
# File 'lib/files.com/models/folder.rb', line 55

def self.find_recursive(path, type = "dir")
  return path if type == "file"

  list(path).map { |c| find_recursive(c.path, c.type) }.flatten.compact
end

.foreach(path, _encoding) ⇒ Object



65
66
67
# File 'lib/files.com/models/folder.rb', line 65

def self.foreach(path, _encoding)
  list(path, {}).each { |x| yield x }
end

.get(path, params = {}, options = {}) ⇒ Object



61
62
63
# File 'lib/files.com/models/folder.rb', line 61

def self.get(path, params = {}, options = {})
  File.get(path, params, options)
end

.getwd(*_args) ⇒ Object



69
70
71
# File 'lib/files.com/models/folder.rb', line 69

def self.getwd(*_args)
  pwd
end

.glob(*args) ⇒ Object



73
74
75
# File 'lib/files.com/models/folder.rb', line 73

def self.glob(*args)
  send(:[], *args)
end

.home(*_args) ⇒ Object



77
78
79
# File 'lib/files.com/models/folder.rb', line 77

def self.home(*_args)
  ""
end

.list_for(path, params = {}, options = {}) ⇒ Object

Parameters:

page - integer - Current page number.
per_page - integer - Number of records to show per page.  (Max: 10,000, 1,000 or less is recommended).
action - string - Action to take.  Can be `count`, `count_nrs` (non recursive), `size`, `permissions`, or blank.
path (required) - string - Path to operate on.
cursor - string - Send cursor to resume an existing list from the point at which you left off.  Get a cursor from an existing list via the X-Files-Cursor header.
filter - string - If specified, will to filter folders/files list by this string.  Wildcards of `*` and `?` are acceptable here.
preview_size - string - Request a preview size.  Can be `small` (default), `large`, `xlarge`, or `pdf`.
search - string - If `search_all` is `true`, provide the search string here.  Otherwise, this parameter acts like an alias of `filter`.
search_all - boolean - Search entire site?
with_priority_color - boolean - Include file priority color information?


343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/files.com/models/folder.rb', line 343

def self.list_for(path, params = {}, options = {})
  params ||= {}
  params[:path] = path
  raise InvalidParameterError.new("Bad parameter: page must be an Integer") if params.dig(:page) and !params.dig(:page).is_a?(Integer)
  raise InvalidParameterError.new("Bad parameter: per_page must be an Integer") if params.dig(:per_page) and !params.dig(:per_page).is_a?(Integer)
  raise InvalidParameterError.new("Bad parameter: action must be an String") if params.dig(:action) and !params.dig(:action).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: path must be an String") if params.dig(:path) and !params.dig(:path).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: cursor must be an String") if params.dig(:cursor) and !params.dig(:cursor).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: filter must be an String") if params.dig(:filter) and !params.dig(:filter).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: preview_size must be an String") if params.dig(:preview_size) and !params.dig(:preview_size).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: search must be an String") if params.dig(:search) and !params.dig(:search).is_a?(String)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  response, options = Api.send_request("/folders/#{Addressable::URI.encode_component(params[:path])}", :get, params, options)
  response.data.map { |object| File.new(object, options) }
end

.mkdir(path, params = {}, options = {}) ⇒ Object



81
82
83
# File 'lib/files.com/models/folder.rb', line 81

def self.mkdir(path, params = {}, options = {})
  create(path, params, options)
end

.open(*args, &block) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/files.com/models/folder.rb', line 85

def self.open(*args, &block)
  if block.nil?
    new *args
  else
    yield new *args
  end
end

.pwd(*_args) ⇒ Object



93
94
95
# File 'lib/files.com/models/folder.rb', line 93

def self.pwd(*_args)
  @@path ||= ""
end

.rmdir(path) ⇒ Object



97
98
99
# File 'lib/files.com/models/folder.rb', line 97

def self.rmdir(path)
  delete(path)
end


101
102
103
# File 'lib/files.com/models/folder.rb', line 101

def self.unlink(path)
  delete(path)
end

.update(path, params = {}, options = {}) ⇒ Object



105
106
107
# File 'lib/files.com/models/folder.rb', line 105

def self.update(path, params = {}, options = {})
  File.update(path, params, options)
end

Instance Method Details

#close(*args) ⇒ Object



115
# File 'lib/files.com/models/folder.rb', line 115

def close(*args); end

#contentsObject



125
126
127
# File 'lib/files.com/models/folder.rb', line 125

def contents
  @contents ||= Folder.list(path, {}, @options)
end

#crc32Object

string - File CRC32 checksum. This is sometimes delayed, so if you get a blank response, wait and try again.



223
224
225
# File 'lib/files.com/models/folder.rb', line 223

def crc32
  @attributes[:crc32]
end

#crc32=(value) ⇒ Object



227
228
229
# File 'lib/files.com/models/folder.rb', line 227

def crc32=(value)
  @attributes[:crc32] = value
end

#create(params = {}) ⇒ Object

Create folder



313
314
315
316
317
318
319
320
321
# File 'lib/files.com/models/folder.rb', line 313

def create(params = {})
  params ||= {}
  params[:path] = @attributes[:path]
  raise MissingParameterError.new("Current object doesn't have a path") unless @attributes[:path]
  raise InvalidParameterError.new("Bad parameter: path must be an String") if params.dig(:path) and !params.dig(:path).is_a?(String)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  Api.send_request("/folders/#{Addressable::URI.encode_component(params[:path])}", :post, params, @options)
end

#display_nameObject

string - File/Folder display name



178
179
180
# File 'lib/files.com/models/folder.rb', line 178

def display_name
  @attributes[:display_name]
end

#display_name=(value) ⇒ Object



182
183
184
# File 'lib/files.com/models/folder.rb', line 182

def display_name=(value)
  @attributes[:display_name] = value
end

#download_uriObject

string - Link to download file. Provided only in response to a download request.



277
278
279
# File 'lib/files.com/models/folder.rb', line 277

def download_uri
  @attributes[:download_uri]
end

#download_uri=(value) ⇒ Object



281
282
283
# File 'lib/files.com/models/folder.rb', line 281

def download_uri=(value)
  @attributes[:download_uri] = value
end

#eachObject



117
118
119
# File 'lib/files.com/models/folder.rb', line 117

def each
  contents.each { |x| yield x }
end

#filenoObject



121
122
123
# File 'lib/files.com/models/folder.rb', line 121

def fileno
  stats.id
end

#idObject

int64 - File/Folder ID



160
161
162
# File 'lib/files.com/models/folder.rb', line 160

def id
  @attributes[:id]
end

#id=(value) ⇒ Object



164
165
166
# File 'lib/files.com/models/folder.rb', line 164

def id=(value)
  @attributes[:id] = value
end

#md5Object

string - File MD5 checksum. This is sometimes delayed, so if you get a blank response, wait and try again.



232
233
234
# File 'lib/files.com/models/folder.rb', line 232

def md5
  @attributes[:md5]
end

#md5=(value) ⇒ Object



236
237
238
# File 'lib/files.com/models/folder.rb', line 236

def md5=(value)
  @attributes[:md5] = value
end

#mime_typeObject

string - MIME Type. This is determined by the filename extension and is not stored separately internally.



241
242
243
# File 'lib/files.com/models/folder.rb', line 241

def mime_type
  @attributes[:mime_type]
end

#mime_type=(value) ⇒ Object



245
246
247
# File 'lib/files.com/models/folder.rb', line 245

def mime_type=(value)
  @attributes[:mime_type] = value
end

#mtimeObject

date-time - File last modified date/time, according to the server. This is the timestamp of the last Files.com operation of the file, regardless of what modified timestamp was sent.



205
206
207
# File 'lib/files.com/models/folder.rb', line 205

def mtime
  @attributes[:mtime]
end

#mtime=(value) ⇒ Object



209
210
211
# File 'lib/files.com/models/folder.rb', line 209

def mtime=(value)
  @attributes[:mtime] = value
end

#pathObject

string - File/Folder path This must be slash-delimited, but it must neither start nor end with a slash. Maximum of 5000 characters.



169
170
171
# File 'lib/files.com/models/folder.rb', line 169

def path
  @attributes[:path]
end

#path=(value) ⇒ Object



173
174
175
# File 'lib/files.com/models/folder.rb', line 173

def path=(value)
  @attributes[:path] = value
end

#permissionsObject

string - A short string representing the current user’s permissions. Can be ‘r`,`w`,`p`, or any combination



259
260
261
# File 'lib/files.com/models/folder.rb', line 259

def permissions
  @attributes[:permissions]
end

#permissions=(value) ⇒ Object



263
264
265
# File 'lib/files.com/models/folder.rb', line 263

def permissions=(value)
  @attributes[:permissions] = value
end

#previewObject

File preview



304
305
306
# File 'lib/files.com/models/folder.rb', line 304

def preview
  @attributes[:preview]
end

#preview=(value) ⇒ Object



308
309
310
# File 'lib/files.com/models/folder.rb', line 308

def preview=(value)
  @attributes[:preview] = value
end

#preview_idObject

int64 - File preview ID



295
296
297
# File 'lib/files.com/models/folder.rb', line 295

def preview_id
  @attributes[:preview_id]
end

#preview_id=(value) ⇒ Object



299
300
301
# File 'lib/files.com/models/folder.rb', line 299

def preview_id=(value)
  @attributes[:preview_id] = value
end

#priority_colorObject

string - Bookmark/priority color of file/folder



286
287
288
# File 'lib/files.com/models/folder.rb', line 286

def priority_color
  @attributes[:priority_color]
end

#priority_color=(value) ⇒ Object



290
291
292
# File 'lib/files.com/models/folder.rb', line 290

def priority_color=(value)
  @attributes[:priority_color] = value
end

#provided_mtimeObject

date-time - File last modified date/time, according to the client who set it. Files.com allows desktop, FTP, SFTP, and WebDAV clients to set modified at times. This allows Desktop<->Cloud syncing to preserve modified at times.



214
215
216
# File 'lib/files.com/models/folder.rb', line 214

def provided_mtime
  @attributes[:provided_mtime]
end

#provided_mtime=(value) ⇒ Object



218
219
220
# File 'lib/files.com/models/folder.rb', line 218

def provided_mtime=(value)
  @attributes[:provided_mtime] = value
end

#read(*_args) ⇒ Object



139
140
141
# File 'lib/files.com/models/folder.rb', line 139

def read(*_args)
  contents[@pos]
end

#regionObject

string - Region location



250
251
252
# File 'lib/files.com/models/folder.rb', line 250

def region
  @attributes[:region]
end

#region=(value) ⇒ Object



254
255
256
# File 'lib/files.com/models/folder.rb', line 254

def region=(value)
  @attributes[:region] = value
end

#rewindObject



143
144
145
# File 'lib/files.com/models/folder.rb', line 143

def rewind
  @pos = 0
end

#saveObject



323
324
325
326
327
328
329
330
# File 'lib/files.com/models/folder.rb', line 323

def save
  if @attributes[:path]
    raise NotImplementedError.new("The Folder object doesn't support updates.")
  else
    new_obj = Folder.create(@attributes, @options)
    @attributes = new_obj.attributes
  end
end

#seek(pos) ⇒ Object



147
148
149
# File 'lib/files.com/models/folder.rb', line 147

def seek(pos)
  @pos = pos
end

#sizeObject

int64 - File/Folder size



196
197
198
# File 'lib/files.com/models/folder.rb', line 196

def size
  @attributes[:size]
end

#size=(value) ⇒ Object



200
201
202
# File 'lib/files.com/models/folder.rb', line 200

def size=(value)
  @attributes[:size] = value
end

#statsObject



129
130
131
# File 'lib/files.com/models/folder.rb', line 129

def stats
  @stats ||= File.download(@filename, { "action": "stat" }, @options)
end

#subfolders_locked=(value) ⇒ Object



272
273
274
# File 'lib/files.com/models/folder.rb', line 272

def subfolders_locked=(value)
  @attributes[:subfolders_locked?] = value
end

#subfolders_locked?Boolean

boolean - Are subfolders locked and unable to be modified?

Returns:

  • (Boolean)


268
269
270
# File 'lib/files.com/models/folder.rb', line 268

def subfolders_locked?
  @attributes[:subfolders_locked?]
end

#tellObject



151
152
153
# File 'lib/files.com/models/folder.rb', line 151

def tell
  @pos
end

#to_pathObject



155
156
157
# File 'lib/files.com/models/folder.rb', line 155

def to_path
  path
end

#typeObject

string - Type: ‘directory` or `file`.



187
188
189
# File 'lib/files.com/models/folder.rb', line 187

def type
  @attributes[:type]
end

#type=(value) ⇒ Object



191
192
193
# File 'lib/files.com/models/folder.rb', line 191

def type=(value)
  @attributes[:type] = value
end