class Ocman::Folder

Public Class Methods

create(path) click to toggle source
# File lib/ocman/folder.rb, line 18
def create(path)
  Ocman::Dav.new.mkdir(escape_path(path))
  wait_for_folder(path)
end
create_recursive(path) click to toggle source
# File lib/ocman/folder.rb, line 10
def create_recursive(path)
  path_list(path).each do |path_part|
    next if ignore404 { list(path_part) }

    create(path_part)
  end
end
list(path, options = {}) click to toggle source
# File lib/ocman/folder.rb, line 23
def list(path, options = {})
  accu = []

  Ocman::Dav.new.ls(escape_path(path), options) do |item|
    accu << Hashie::Mash.new(
      path: CGI.unescape(item.uri.to_s.gsub(Ocman::Dav.url(path), '').gsub('/', '')),
      type: item.type.to_s,
      size: item.size
    )
  end

  accu
end

Private Class Methods

escape_path(path) click to toggle source
# File lib/ocman/folder.rb, line 39
def escape_path(path)
  path.split('/').map { |segment| ERB::Util.url_encode(segment) }.join('/')
end
ignore404() { || ... } click to toggle source
# File lib/ocman/folder.rb, line 56
def ignore404
  yield
rescue Net::HTTPServerException => e
  raise unless e.to_s.match?(/404.*Not found/i)

  nil
end
path_list(path) click to toggle source
# File lib/ocman/folder.rb, line 64
def path_list(path)
  path.split('/').reduce([]) do |acc, segment|
    acc << [acc.last, segment].compact.join('/')
  end
end
wait_for_folder(path) click to toggle source
# File lib/ocman/folder.rb, line 43
def wait_for_folder(path)
  wait_time = 1
  retries = 0
  loop do
    raise TimeoutError if retries >= 3
    break if Ocman.list(path)

    sleep wait_time
    wait_time *= 2
    retries += 1
  end
end