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.



14
15
16
17
18
# File 'lib/nsisam/fake_client.rb', line 14

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

Instance Attribute Details

#expireObject

Returns the value of attribute expire.



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

def expire
  @expire
end

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#allow_netObject



20
21
22
# File 'lib/nsisam/fake_client.rb', line 20

def allow_net
  WebMock.allow_net_connect!
end

#delete(key) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/nsisam/fake_client.rb', line 61

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


24
25
26
# File 'lib/nsisam/fake_client.rb', line 24

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

#get(key, expected_checksum = nil) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/nsisam/fake_client.rb', line 44

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



52
53
54
55
56
57
58
59
# File 'lib/nsisam/fake_client.rb', line 52

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



28
29
30
31
32
33
34
35
# File 'lib/nsisam/fake_client.rb', line 28

def store(data)
  key = Time.now.nsec.to_s
  @storage[key] = JSON.load(data.to_json) unless @expire
  if data.kind_of?(Hash) and data.has_key?(:file) and data.has_key?(:filename)
    stub_request(:get, "http://#{@host}:#{@port}/file/#{key}").to_return(body: Base64.decode64(data[:file]))
  end
  Response.new 'key' => key, 'checksum' => 0
end

#store_file(file, filename, type = :file) ⇒ Object



37
38
39
40
41
42
# File 'lib/nsisam/fake_client.rb', line 37

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
  stub_request(:get, "http://#{@host}:#{@port}/file/#{key}").to_return(body: file)
  Response.new "key" => key, "checksum" => 0
end

#update(key, value) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nsisam/fake_client.rb', line 70

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



83
84
85
86
87
88
89
# File 'lib/nsisam/fake_client.rb', line 83

def update_file(key, file, filename)
  hash = {file: file, filename: filename}
  @storage[key] = hash
  remove_request_stub(:get, "http://#{@host}:#{@port}/file/#{key}")
  stub_request(:get, "http://#{@host}:#{@port}/file/#{key}").to_return(body: file)
  Response.new "key" => key, "checksum" => 0
end