Class: NSISam::FakeClient

Inherits:
Object
  • Object
show all
Defined in:
lib/nsisam/fake_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "localhost", port = "8888") ⇒ FakeClient

Returns a new instance of FakeClient.



12
13
14
15
16
# File 'lib/nsisam/fake_client.rb', line 12

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

Instance Attribute Details

#expireObject

Returns the value of attribute expire.



9
10
11
# File 'lib/nsisam/fake_client.rb', line 9

def expire
  @expire
end

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/nsisam/fake_client.rb', line 10

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/nsisam/fake_client.rb', line 10

def port
  @port
end

Instance Method Details

#delete(key) ⇒ Object



51
52
53
54
55
56
57
58
# 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


18
19
20
# File 'lib/nsisam/fake_client.rb', line 18

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

#get(key, expected_checksum = nil) ⇒ Object



34
35
36
37
38
39
40
# 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) ⇒ Object



42
43
44
45
46
47
48
49
# 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) ⇒ Object



22
23
24
25
26
# 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) ⇒ Object



28
29
30
31
32
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object



73
74
75
76
77
78
# 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