class NSISam::FakeClient

Attributes

expire[RW]
host[R]
port[R]

Public Class Methods

new(host="localhost", port="8888") click to toggle source
# File lib/nsisam/fake_client.rb, line 12
def initialize(host="localhost", port="8888")
  @storage = {}
  @host = host
  @port = port
end

Public Instance Methods

delete(key) click to toggle source
# File lib/nsisam/fake_client.rb, line 51
def delete(key)
  if @storage.has_key?(key)
    @storage.delete key
    Response.new 'deleted' => true
  else
    raise NSISam::Errors::Client::KeyNotFoundError
  end
end
get(key, expected_checksum=nil) click to toggle source
# File lib/nsisam/fake_client.rb, line 34
def get(key, expected_checksum=nil)
  if @storage.has_key?(key)
    Response.new 'data' => @storage[key]
  else
    raise NSISam::Errors::Client::KeyNotFoundError
  end
end
get_file(key, type=:file) click to toggle source
# File lib/nsisam/fake_client.rb, line 42
def get_file(key, type=:file)
  if @storage.has_key?(key)
    response = Hash.new 'data' => Base64.decode64(@storage[key][type.to_s])
    Response.new response
  else
    raise NSISam::Errors::Client::KeyNotFoundError
  end
end
store(data) click to toggle source
# File lib/nsisam/fake_client.rb, line 22
def store(data)
  key = Time.now.nsec.to_s
  @storage[key] = JSON.load(data.to_json) unless @expire
  Response.new 'key' => key, 'checksum' => 0
end
store_file(file, filename, type=:file) click to toggle source
# File lib/nsisam/fake_client.rb, line 28
def store_file(file, filename, type=:file)
  key = Time.now.to_i.to_s
  @storage[key] = {type.to_s => Base64.encode64(file), filename: filename}.to_json unless @expire
  Response.new "key" => key, "checksum" => 0
end
update(key, value) click to toggle source
# File lib/nsisam/fake_client.rb, line 60
def update(key, value)
  if @storage.has_key?(key)
    if @expire
      @storage.delete(key)
    else
      @storage[key] = value
    end
    Response.new 'key' => key, 'checksum' => 0
  else
    raise NSISam::Errors::Client::KeyNotFoundError
  end
end
update_file(key, file, filename) click to toggle source
# File lib/nsisam/fake_client.rb, line 73
def update_file(key, file, filename)
  hash = {file: file, filename: filename}
  @storage[key] = hash
  remove_request_stub(:get, "http://#{@host}:#{@port}/file/#{key}")
  Response.new "key" => key, "checksum" => 0
end