nsisam 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.2
1
+ 0.6.3
data/lib/nsisam/client.rb CHANGED
@@ -10,6 +10,8 @@ require File.dirname(__FILE__) + '/response'
10
10
  module NSISam
11
11
  class Client
12
12
 
13
+ attr_accessor :expire
14
+
13
15
  # Initialize a client to a SAM node hosted at a specific url
14
16
  #
15
17
  # @param [Hash] optional hash with user, password, host and port of the SAM node
@@ -23,6 +25,7 @@ module NSISam
23
25
  @password = params[:password]
24
26
  @host = params[:host]
25
27
  @port = params[:port]
28
+ @expire = params[:expire]
26
29
  end
27
30
 
28
31
  # Store a given data in SAM
@@ -37,8 +40,9 @@ module NSISam
37
40
  # @example
38
41
  # nsisam.store("something")
39
42
  def store(data)
40
- request_data = {:value => data}.to_json
41
- request = prepare_request :PUT, request_data
43
+ request_data = {:value => data}
44
+ request_data[:expire] = @expire if @expire
45
+ request = prepare_request :PUT, request_data.to_json
42
46
  Response.new(execute_request(request))
43
47
  end
44
48
 
@@ -130,8 +134,9 @@ module NSISam
130
134
  # @example
131
135
  # nsisam.update("my key", "my value")
132
136
  def update(key, value)
133
- request_data = {:key => key, :value => value}.to_json
134
- request = prepare_request :POST, request_data
137
+ request_data = {:key => key, :value => value}
138
+ request_data[:expire] = @expire if @expire
139
+ request = prepare_request :POST, request_data.to_json
135
140
  Response.new(execute_request(request))
136
141
  end
137
142
 
@@ -46,11 +46,22 @@ module NSISam
46
46
  @port
47
47
  end
48
48
 
49
+ # Set the default {NSISam::Client} expire time
50
+ #
51
+ #
52
+ # @param [Fixnum] expire time to set
53
+ #
54
+ # @return [Fixnum] the expire set
55
+ def expire(expire = nil)
56
+ @expire = expire unless expire.nil?
57
+ @expire
58
+ end
59
+
49
60
  # See how are the settings
50
61
  #
51
62
  # @return [Hash] actual settings
52
63
  def settings
53
- {user: @user, password: @password, host: @host, port: @port}
64
+ {user: @user, password: @password, host: @host, port: @port, expire: @expire}
54
65
  end
55
66
  end
56
67
  end
@@ -17,7 +17,7 @@ module NSISam
17
17
  content_type :json
18
18
  incoming = JSON.parse(request.body.read)
19
19
  key = generate_key
20
- storage[key] = incoming['value']
20
+ storage[key] = incoming['value'] unless incoming.has_key? 'expire'
21
21
  { key: key, checksum: "0" }.to_json
22
22
  end
23
23
 
@@ -44,9 +44,14 @@ module NSISam
44
44
  post "/" do
45
45
  content_type :json
46
46
  incoming = JSON.parse(request.body.read)
47
+ expire = incoming['expire']
47
48
  key = incoming["key"]
48
49
  return 404 unless storage.has_key?(key)
49
- storage[key] = incoming['value']
50
+ if incoming.has_key? 'expire'
51
+ storage.delete(key)
52
+ else
53
+ storage[key] = incoming['value']
54
+ end
50
55
  { key: key, checksum: 0 }.to_json
51
56
  end
52
57
  end
data/nsisam.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "nsisam"
8
- s.version = "0.6.2"
8
+ s.version = "0.6.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Douglas Camata"]
12
- s.date = "2012-08-31"
12
+ s.date = "2012-09-04"
13
13
  s.description = "A simple gem to access a SAM node. For more info about SAM\n visit www.github.com/nsi-iff/sam_buildout."
14
14
  s.email = "d.camata@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -23,14 +23,21 @@ describe "NSISam::Client::Configuration" do
23
23
  Configuration.port.should == '8888'
24
24
  end
25
25
 
26
+ it "set and return default expire time" do
27
+ Configuration.expire 5
28
+ Configuration.expire.should == 5
29
+ end
30
+
26
31
  it "return a hash of attributes" do
27
32
  Configuration.instance_eval do
28
33
  user "why"
29
34
  password "chunky"
30
35
  host "localhost"
31
36
  port "8888"
37
+ expire 8
32
38
  end
33
39
  Configuration.settings.should == {user: "why", password: "chunky",
34
- host: "localhost", port: "8888"}
40
+ host: "localhost", port: "8888",
41
+ expire: 8}
35
42
  end
36
43
  end
data/spec/nsisam_spec.rb CHANGED
@@ -4,11 +4,15 @@ require 'base64'
4
4
  describe NSISam do
5
5
  before :all do
6
6
  fake_options = { user: 'test', password: 'test', host: 'localhost',
7
- port: '8888' }
7
+ port: '9888' }
8
8
  @options = integration_options || fake_options
9
9
  @nsisam = NSISam::Client.new(@options)
10
10
  @keys = Array.new
11
- @fake_sam = NSISam::FakeServerManager.new.start_server unless integrating?
11
+ @fake_sam = NSISam::FakeServerManager.new.start_server(9888) unless integrating?
12
+ end
13
+
14
+ before :each do
15
+ @nsisam.expire = false
12
16
  end
13
17
 
14
18
  after :all do
@@ -33,6 +37,14 @@ describe NSISam do
33
37
  response.should respond_to("checksum")
34
38
  end
35
39
 
40
+ it "can store a value with an expire time" do
41
+ @nsisam.expire = 2
42
+ response = @nsisam.store('teste')
43
+ sleep(3)
44
+ expect { @nsisam.get(response.key) }.to raise_error(NSISam::Errors::Client::KeyNotFoundError)
45
+ @nsisam.expire = false
46
+ end
47
+
36
48
  context "file" do
37
49
  it "encodes content before storing" do
38
50
  Base64.should_receive(:encode64).with(file_content).
@@ -107,6 +119,14 @@ describe NSISam do
107
119
  @nsisam.get(key).data.should == 'updated'
108
120
  end
109
121
 
122
+ it "can update values with an expire time to the new value" do
123
+ response = @nsisam.store('test')
124
+ @nsisam.expire = 2
125
+ @nsisam.update(response.key, 'test 2')
126
+ sleep(3)
127
+ expect { @nsisam.get(response.key) }.to raise_error(NSISam::Errors::Client::KeyNotFoundError)
128
+ end
129
+
110
130
  it "raises error when key not found" do
111
131
  expect { @nsisam.update("dont exist ruby is fast", "foo") }.to raise_error(NSISam::Errors::Client::KeyNotFoundError)
112
132
  end
@@ -156,6 +176,7 @@ describe NSISam do
156
176
  password "chunky"
157
177
  host "localhost"
158
178
  port "8888"
179
+ expire false
159
180
  end
160
181
  end
161
182
 
@@ -165,14 +186,16 @@ describe NSISam do
165
186
  sam.instance_variable_get(:@password).should == "chunky"
166
187
  sam.instance_variable_get(:@host).should == "localhost"
167
188
  sam.instance_variable_get(:@port).should == "8888"
189
+ sam.instance_variable_get(:@expire).should == false
168
190
  end
169
191
 
170
192
  it "by initialize parameters" do
171
- sam = NSISam::Client.new(user: 'luckystiff', password: 'bacon', host: 'why.com', port: '9999')
193
+ sam = NSISam::Client.new(user: 'luckystiff', password: 'bacon', host: 'why.com', port: '9999', expire: 5)
172
194
  sam.instance_variable_get(:@user).should == "luckystiff"
173
195
  sam.instance_variable_get(:@password).should == "bacon"
174
196
  sam.instance_variable_get(:@host).should == "why.com"
175
197
  sam.instance_variable_get(:@port).should == "9999"
198
+ sam.instance_variable_get(:@expire).should == 5
176
199
  end
177
200
  end
178
201
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nsisam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-31 00:00:00.000000000 Z
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -172,7 +172,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
172
172
  version: '0'
173
173
  segments:
174
174
  - 0
175
- hash: -3250131280174348984
175
+ hash: -2000685296802539724
176
176
  required_rubygems_version: !ruby/object:Gem::Requirement
177
177
  none: false
178
178
  requirements: