Sha256: ee18174b94e2592a5b70acb2f4d0db298214f265beffa08dd6b5f69346783968

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

require 'json'
require 'base64'
require File.dirname(__FILE__) + '/response'


module NSISam
  class FakeClient

    attr_accessor :expire
    attr_reader :host, :port

    def initialize(host="localhost", port="8888")
      @storage = {}
      @host = host
      @port = port
    end

    def download_link_for_file(key)
      "http://#{@host}:#{@port}/file/#{key}"
    end

    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

    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

    def get(key, expected_checksum=nil)
      if @storage.has_key?(key)
        Response.new 'data' => @storage[key]
      else
        raise NSISam::Errors::Client::KeyNotFoundError
      end
    end

    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

    def delete(key)
      if @storage.has_key?(key)
        @storage.delete key
        Response.new 'deleted' => true
      else
        raise NSISam::Errors::Client::KeyNotFoundError
      end
    end

    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

    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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nsisam-0.7.7 lib/nsisam/fake_client.rb
nsisam-0.7.6 lib/nsisam/fake_client.rb
nsisam-0.7.5 lib/nsisam/fake_client.rb