class Fog::Storage::Akamai::Mock

Public Class Methods

data() click to toggle source
# File lib/fog/akamai/storage.rb, line 67
def self.data
  @data ||= {}
end
new(options = {}) click to toggle source
# File lib/fog/akamai/storage.rb, line 71
def initialize(options = {})
  init(options)
end

Public Instance Methods

data() click to toggle source
# File lib/fog/akamai/storage.rb, line 75
def data
  self.class.data
end
delete(path) click to toggle source
# File lib/fog/akamai/requests/storage/delete.rb, line 21
def delete(path)
  path_guard(path)

  if remove_file(Pathname.new(format_path(path)))
    Excon::Response.new(status: 200)
  else
    fail(Excon::Errors::NotFound, '404 Not Found')
  end
end
dir(path = '') click to toggle source
# File lib/fog/akamai/requests/storage/dir.rb, line 32
def dir(path = '')
  key = format_path(path)
  data.key?(key) ? Excon::Response.new(status: 200, body: data[key]) : fail(Excon::Errors::NotFound, '404 Not Found')
end
download(path) click to toggle source
# File lib/fog/akamai/requests/storage/download.rb, line 20
def download(path)
  path_guard(path)
  formatted_path = format_path(path)
  fail(Excon::Errors::NotFound, '404 Not Found') unless data.key?(formatted_path) && data[formatted_path].key?(:body)
  Excon::Response.new(status: 200, body: data[formatted_path][:body])
end
du(path) click to toggle source
# File lib/fog/akamai/requests/storage/du.rb, line 27
def du(path)
  path_guard(path)

  key = format_path(path)
  directory = data[key]

  if directory
    Excon::Response.new(status: 200, body: { directory: key, files: directory[:files].count.to_s, bytes: directory[:directories].count.to_s })
  else
    fail(Excon::Errors::NotFound, '404 Not Found')
  end
end
mkdir(path) click to toggle source
# File lib/fog/akamai/requests/storage/mkdir.rb, line 22
def mkdir(path)
  path_guard(path)

  path = Pathname.new(format_path(path))
  last_path_basename = ''

  path.ascend do |parent|
    break if parent.nil?

    key = parent.to_s
    update_data(key, last_path_basename)
    last_path_basename = parent.basename.to_s
  end

  Excon::Response.new(headers: { 'Status' => 200 })
end
mtime(path, mtime = DateTime.now.to_time.to_i) click to toggle source
# File lib/fog/akamai/requests/storage/mtime.rb, line 22
def mtime(path, mtime = DateTime.now.to_time.to_i)
  path_guard(path)

  pathname = Pathname.new(format_path(path))
  file = get_file(pathname)

  if file
    file['mtime'] = mtime.to_s
    Excon::Response.new(status: 200)
  else
    fail(Excon::Errors::NotFound, '404 Not Found')
  end
end
rename(source, destination) click to toggle source
# File lib/fog/akamai/requests/storage/rename.rb, line 24
def rename(source, destination)
  path_guard(source)
  path_guard(destination)

  source_pathname = Pathname.new(format_path(source))
  destination_pathname = Pathname.new(format_path(destination))

  if rename_file(source_pathname, destination_pathname)
    move_file(source_pathname, destination_pathname)
    Excon::Response.new(status: 200)
  else
    fail(Excon::Errors::NotFound, '404 Not Found')
  end
end
reset_data() click to toggle source
# File lib/fog/akamai/storage.rb, line 79
def reset_data
  self.class.data.clear
end
rmdir(path) click to toggle source
# File lib/fog/akamai/requests/storage/rmdir.rb, line 19
def rmdir(path)
  path_guard(path)

  formatted_path = format_path(path)
  dir = data[formatted_path]

  if empty?(dir)
    data.delete(formatted_path)
    Excon::Response.new(status: 200)
  else
    fail(Excon::Errors::Conflict, 'Not a empty directory')
  end
end
stat(path) click to toggle source
# File lib/fog/akamai/requests/storage/stat.rb, line 36
def stat(path)
  path_guard(path)

  path = Pathname.new(format_path(path))
  key = path.split.first.to_s

  response = Excon::Response.new(status: get_status(key, path.basename.to_s), body: get_body(key))

  fail(Excon::Errors::NotFound, '404 Not Found') if response.status == 404
  response
end
upload(path, body) click to toggle source
# File lib/fog/akamai/requests/storage/upload.rb, line 26
def upload(path, body)
  path_and_body_guard(path, body)

  path = Pathname(path)
  dir = path.split.first.to_s

  mkdir(dir)
  add_file(dir, path, body)

  Excon::Response.new(status: 200)
end

Private Instance Methods

add_file(dir, path, body) click to toggle source
# File lib/fog/akamai/requests/storage/upload.rb, line 40
def add_file(dir, path, body)
  data[format_path(dir)][:files] << build_file(body, path)
  data[format_path(path.to_s)] = { body: body }
end
build_directory_node(last_path_basename) click to toggle source
# File lib/fog/akamai/requests/storage/mkdir.rb, line 46
def build_directory_node(last_path_basename)
  { 'type' => 'dir', 'name' => last_path_basename, 'mtime' => DateTime.now.to_time.to_i.to_s }
end
build_file(body, path) click to toggle source
# File lib/fog/akamai/requests/storage/upload.rb, line 45
def build_file(body, path)
  { 'type' => 'file', 'name' => path.basename.to_s, 'mtime' => DateTime.now.to_time.to_i.to_s, 'size' => body.size.to_s }
end
empty?(dir) click to toggle source
# File lib/fog/akamai/requests/storage/rmdir.rb, line 35
def empty?(dir)
  dir[:files].empty? && dir[:directories].empty?
end
get_body(key) click to toggle source
# File lib/fog/akamai/requests/storage/stat.rb, line 60
def get_body(key)
  data[key] if data.key?(key)
end
get_file(pathname) click to toggle source
# File lib/fog/akamai/requests/storage/mtime.rb, line 38
def get_file(pathname)
  dir = data[pathname.split.first.to_s] || { files: [] }
  dir[:files].find { |file_hash| file_hash['name'] == pathname.basename.to_s }
end
get_status(key, basename) click to toggle source
# File lib/fog/akamai/requests/storage/stat.rb, line 50
def get_status(key, basename)
  if data.key?(key) &&
     (data[key][:directories].any? { |dir| dir['name'] == basename } ||
       data[key][:files].any? { |file| file['name'] == basename })
    200
  else
    404
  end
end
move_file(source, destination) click to toggle source
# File lib/fog/akamai/requests/storage/rename.rb, line 47
def move_file(source, destination)
  data[destination.to_s] = data[source.to_s]
  data[source.to_s] = nil
end
remove_file(path) click to toggle source
# File lib/fog/akamai/requests/storage/delete.rb, line 33
def remove_file(path)
  return false unless data.key?(path.to_s)

  data[path.to_s] = nil
  remove_file_from_parent_dir(path)
end
remove_file_from_parent_dir(path) click to toggle source
# File lib/fog/akamai/requests/storage/delete.rb, line 40
def remove_file_from_parent_dir(path)
  data[path.split.first.to_s][:files].reject! { |hash| hash['name'] == path.basename.to_s }
end
rename_file(source, destination) click to toggle source
# File lib/fog/akamai/requests/storage/rename.rb, line 41
def rename_file(source, destination)
  source_dir = data[source.split.first.to_s] || { files: [] }
  source_file = source_dir[:files].find { |file| file['name'] == source.basename.to_s }
  source_file['name'] = destination.basename.to_s if source_file
end
update_data(key, last_path_basename) click to toggle source
# File lib/fog/akamai/requests/storage/mkdir.rb, line 41
def update_data(key, last_path_basename)
  data[key] ||= { directory: key, files: [], directories: [] }
  data[key][:directories] << build_directory_node(last_path_basename) unless last_path_basename.empty?
end