class Ftpmock::Cache

Attributes

configuration[R]
credentials[R]

Public Class Methods

new(configuration, credentials) click to toggle source
# File lib/ftpmock/core/cache.rb, line 5
def initialize(configuration, credentials)
  @configuration = configuration
  @credentials = credentials
  @chdir = nil
end

Public Instance Methods

_alert(tag, content, color = :green) click to toggle source
# File lib/ftpmock/core/cache.rb, line 19
def _alert(tag, content, color = :green)
  configuration.verbose && VerboseUtils.alert(tag, content, color)
end
_chdir_alert(dirname, old_chdir, new_chdir) click to toggle source
# File lib/ftpmock/core/cache.rb, line 33
def _chdir_alert(dirname, old_chdir, new_chdir)
  tag = "ftpmock.cache.chdir '#{dirname}'"

  _alert tag, "changed from '#{old_chdir}' to '#{new_chdir}'", :green
end
_get_alert_hit(tag, localfile) click to toggle source
# File lib/ftpmock/core/cache.rb, line 99
def _get_alert_hit(tag, localfile)
  _alert tag, "hit! (#{localfile})"
end
_get_alert_miss(tag) click to toggle source
# File lib/ftpmock/core/cache.rb, line 103
def _get_alert_miss(tag)
  _alert tag, 'miss!', :yellow
end
_get_alert_storing(tag, localfile) click to toggle source
# File lib/ftpmock/core/cache.rb, line 107
def _get_alert_storing(tag, localfile)
  _alert tag, "storing #{localfile}"
end
_get_raise_localfile_not_fetched(remotefile, localfile) click to toggle source
# File lib/ftpmock/core/cache.rb, line 111
def _get_raise_localfile_not_fetched(remotefile, localfile)
  msg = "FTP GET '#{remotefile}' should have created '#{localfile}'"
  msg = "#{msg}, but didn't."
  raise GetNotFetched, msg
end
_get_tag_for(remotefile) click to toggle source
# File lib/ftpmock/core/cache.rb, line 93
def _get_tag_for(remotefile)
  tag = "ftpmock.cache.get '#{remotefile}'"
  tag += ", chdir: '#{chdir}'" if chdir
  tag
end
_list_alert_hit(tag, list) click to toggle source
# File lib/ftpmock/core/cache.rb, line 62
def _list_alert_hit(tag, list)
  list.nil? || _alert(tag, "hit! (#{list.size} lines)")
end
_list_alert_miss(tag) click to toggle source
# File lib/ftpmock/core/cache.rb, line 66
def _list_alert_miss(tag)
  _alert tag, 'miss!', :yellow
end
_list_alert_storing(tag, list) click to toggle source
# File lib/ftpmock/core/cache.rb, line 70
def _list_alert_storing(tag, list)
  _alert tag, "storing #{list.size} lines!"
end
_list_tag_for(key) click to toggle source
# File lib/ftpmock/core/cache.rb, line 56
def _list_tag_for(key)
  tag = "ftpmock.cache.list '#{key}'"
  tag += ", chdir: '#{chdir}'" if chdir
  tag
end
_put_alert_exists(tag) click to toggle source
# File lib/ftpmock/core/cache.rb, line 142
def _put_alert_exists(tag)
  _alert tag, 'file exists?'
end
_put_alert_hit(tag, size) click to toggle source
# File lib/ftpmock/core/cache.rb, line 162
def _put_alert_hit(tag, size)
  color = size.zero? ? :green : :red
  _alert tag, "hit! (#{size} differing lines)", color
end
_put_alert_miss(tag) click to toggle source
# File lib/ftpmock/core/cache.rb, line 146
def _put_alert_miss(tag)
  _alert tag, 'miss!', :yellow
end
_put_alert_storing(tag, remotefile) click to toggle source
# File lib/ftpmock/core/cache.rb, line 150
def _put_alert_storing(tag, remotefile)
  _alert tag, "storing #{remotefile}"
end
_put_cached(tag, path, localfile, remotefile) click to toggle source
# File lib/ftpmock/core/cache.rb, line 154
def _put_cached(tag, path, localfile, remotefile)
  diff = PutHelper.compare(path, localfile, remotefile)

  _put_alert_hit(tag, diff.size)

  diff.any? && _put_raise_localfile_differs(remotefile, localfile, diff)
end
_put_raise_localfile_differs(remotefile, localfile, diff) click to toggle source
# File lib/ftpmock/core/cache.rb, line 173
def _put_raise_localfile_differs(remotefile, localfile, diff)
  spaces = ' ' * 20
  VerboseUtils.puts
  VerboseUtils.puts ColorUtils.highlight "#{spaces}Diffy Begin#{spaces}"
  VerboseUtils.puts diff
  VerboseUtils.puts ColorUtils.highlight "#{spaces} Diffy End #{spaces}"
  VerboseUtils.puts

  msg = "FTP PUT '#{remotefile}' has failed because"
  msg = "#{msg} '#{localfile}' contents don't match #{path}/#{remotefile}"
  raise PutLocalDiffersFromCache, msg
end
_put_raise_not_found(remotefile, localfile) click to toggle source
# File lib/ftpmock/core/cache.rb, line 167
def _put_raise_not_found(remotefile, localfile)
  msg = "FTP PUT '#{remotefile}' has failed"
  msg = "#{msg} because '#{localfile}' does not exist."
  raise PutFileNotFound, msg
end
_put_tag_for(localfile) click to toggle source
# File lib/ftpmock/core/cache.rb, line 136
def _put_tag_for(localfile)
  tag = "ftpmock.cache.put '#{localfile}'"
  tag += ", chdir: '#{chdir}'" if chdir
  tag
end
chdir(dirname = nil) click to toggle source
# File lib/ftpmock/core/cache.rb, line 23
def chdir(dirname = nil)
  return @chdir if dirname.nil?

  original = @chdir
  @chdir = PathHelper.join(@chdir, dirname)

  _chdir_alert(dirname, original, @chdir)
  @chdir
end
get(remotefile, localfile = File.basename(remotefile)) { || ... } click to toggle source
# File lib/ftpmock/core/cache.rb, line 74
def get(remotefile, localfile = File.basename(remotefile))
  tag = _get_tag_for(remotefile)

  if GetHelper.read(path, chdir, remotefile, localfile)
    _get_alert_hit(tag, localfile)
    return
  end

  _get_alert_miss(tag)
  yield

  if GetHelper.fetched?(localfile)
    _get_alert_storing(tag, localfile)
    GetHelper.write(path, chdir, remotefile, localfile)
  else
    _get_raise_localfile_not_fetched(remotefile, localfile)
  end
end
list(*args) { || ... } click to toggle source
# File lib/ftpmock/core/cache.rb, line 39
def list(*args)
  key = args.join(',')
  tag = _list_tag_for(key)

  list = ListHelper.read(path, chdir, key)
  _list_alert_hit(tag, list)
  return list if list

  _list_alert_miss(tag)
  list = yield

  _list_alert_storing(tag, list)
  ListHelper.write(path, chdir, key, list)

  list
end
path() click to toggle source
# File lib/ftpmock/core/cache.rb, line 11
def path
  @path ||= Pathname("#{configuration.path}/#{path_dir}")
end
path_dir() click to toggle source
# File lib/ftpmock/core/cache.rb, line 15
def path_dir
  StringUtils.parameterize(credentials.map(&:to_s).join('_'))
end
put(localfile, remotefile = File.basename(localfile)) { || ... } click to toggle source
# File lib/ftpmock/core/cache.rb, line 117
def put(localfile, remotefile = File.basename(localfile))
  tag = _put_tag_for(localfile)

  _put_alert_exists(tag)
  PutHelper.exist?(localfile) || _put_raise_not_found(remotefile, localfile)

  if PutHelper.cached?(path, remotefile)
    _put_cached(tag, path, localfile, remotefile)
  else
    _put_alert_miss(tag)
    yield

    _put_alert_storing(tag, remotefile)
    PutHelper.write(path, localfile, remotefile)
  end

  true
end