class FakeConsul::Server

Public Class Methods

new() click to toggle source
# File lib/fake_consul/server.rb, line 7
def initialize
  restore!
end

Public Instance Methods

clear() click to toggle source

Clear current data and delete backing marshalling file

Calls superclass method
# File lib/fake_consul/server.rb, line 61
def clear
  super
  return unless File.exist?(db_file)
  File.delete(db_file)
end
delete(key, options = nil) click to toggle source

Fake delete

Performs no http requests but deletes data from local hash

@param key [String] @param options [Hash] unused/unimplemented @return [Boolean] true :trollface:

Calls superclass method
# File lib/fake_consul/server.rb, line 52
def delete(key, options = nil)
  super(key)
  compact
  persist!
  true
end
get(key, options = nil, not_found = :reject, found = :return) click to toggle source

Fake get

Performs no http requests but stores data in local hash

@param key [String] @param options [Hash] @options recurse [Boolean] wether to list all keys starting with this prefix @param not_found [Symbol] unused/unimplemented @param found [Symbol] not unused/unimplemented @return [Array<Hash>] e.g. [{key: 'foo', value: 'bar'}]

# File lib/fake_consul/server.rb, line 21
def get(key, options = nil, not_found = :reject, found = :return)
  options ||= {}

  if options[:recurse]
    find_keys_recursive(key)
  else
    consul_export_format(key)
  end
end
put(key, value, options = nil) click to toggle source

Fake put

Performs no http requests but retrieves data from local hash

@param key [String] @param options [Hash] unused/unimplemented @return [Boolean] true :trollface:

# File lib/fake_consul/server.rb, line 38
def put(key, value, options = nil)
  self[key] = value
  compact
  persist!
  true
end

Private Instance Methods

compact() click to toggle source

Remove all keys that are nil

# File lib/fake_consul/server.rb, line 115
def compact
  delete_if { |k, v| v.nil? }
end
consul_export_format(keys) click to toggle source

Returns the keys in the following format:

[{key: `key`, value: 'bar'}]

@return [Array<Hash>]

# File lib/fake_consul/server.rb, line 97
def consul_export_format(keys)
  Array(keys).map do |key|
    {'key' => key, 'value' => self[key]}
  end
end
db_file() click to toggle source

Path to marshalled file

@return [String]

# File lib/fake_consul/server.rb, line 90
def db_file
  "#{Dir.tmpdir}#{File::SEPARATOR}.fake_consul.m"
end
find_keys_recursive(key) click to toggle source

Returns all keys that begin with the supplied `key`.

@return [Array<Hash>] e.g. [{key: 'foo', value: 'bar'}]

# File lib/fake_consul/server.rb, line 106
def find_keys_recursive(key)
  self.keys.select do |_key|
    _key.to_s.start_with?(key.to_s)
  end.map do |_key|
    consul_export_format(_key)
  end.flatten
end
persist!() click to toggle source

Persist current data to marshalled file

# File lib/fake_consul/server.rb, line 70
def persist!
  File.open(db_file, 'w+') do |f|
    Marshal.dump(self, f)
  end
end
restore!() click to toggle source

Restore hash from marshalled data

# File lib/fake_consul/server.rb, line 77
def restore!
  return unless File.exist?(db_file)

  File.open(db_file) do |f|
    restored_data = Marshal.load(f)
    self.clear
    self.merge!(restored_data)
  end
end