nsisam 0.5.4 → 0.6.0
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/.travis.yml +16 -0
- data/README.rdoc +16 -1
- data/VERSION +1 -1
- data/lib/nsisam/client.rb +72 -12
- data/lib/nsisam/fake_client.rb +17 -0
- data/lib/nsisam/fake_server.rb +21 -8
- data/lib/nsisam/response.rb +21 -0
- data/lib/nsisam.rb +1 -0
- data/nsisam.gemspec +11 -4
- data/spec/fake_client_spec.rb +14 -1
- data/spec/integration.yml.example +4 -0
- data/spec/nsisam_spec.rb +93 -25
- data/spec/response_spec.rb +21 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/support/core_extensions.rb +21 -0
- data/spec/support/files.rb +11 -0
- data/spec/support/integration.rb +26 -0
- metadata +11 -4
data/.travis.yml
ADDED
data/README.rdoc
CHANGED
@@ -1,8 +1,24 @@
|
|
1
1
|
= nsisam
|
2
2
|
|
3
|
+
{<img src="https://secure.travis-ci.org/nsi-iff/nsisam-ruby.png"/>}[http://travis-ci.org/nsi-iff/nsisam-ruby]
|
4
|
+
|
3
5
|
Just a simple and lightweight package to access a SAM node using Ruby. It also includes a simple fake client to
|
4
6
|
be used in your application when a real server is not available to receive requests.
|
5
7
|
|
8
|
+
== How to test
|
9
|
+
|
10
|
+
Just run:
|
11
|
+
|
12
|
+
rake spec
|
13
|
+
|
14
|
+
|
15
|
+
By default, all tests are run against a fake server. If you want to use a real SAM, you must create a
|
16
|
+
spec/integration.yml file (there is a example file in spec/integration.yml.example) and point it to your
|
17
|
+
SAM instance. You must also set the NSI_SAM_INTEGRATION environment variable to any value:
|
18
|
+
|
19
|
+
NSI_SAM_INTEGRATION=1 rake spec
|
20
|
+
|
21
|
+
|
6
22
|
== Contributing to nsisam
|
7
23
|
|
8
24
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
@@ -17,4 +33,3 @@ be used in your application when a real server is not available to receive reque
|
|
17
33
|
|
18
34
|
Copyright (c) 2012 Douglas Camata. See LICENSE.txt for
|
19
35
|
further details.
|
20
|
-
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/lib/nsisam/client.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require "json"
|
2
2
|
require "net/http"
|
3
3
|
require "digest"
|
4
|
+
require "base64"
|
4
5
|
|
5
6
|
require File.dirname(__FILE__) + '/configuration'
|
6
7
|
require File.dirname(__FILE__) + '/errors'
|
8
|
+
require File.dirname(__FILE__) + '/response'
|
7
9
|
|
8
10
|
module NSISam
|
9
11
|
class Client
|
@@ -37,7 +39,23 @@ module NSISam
|
|
37
39
|
def store(data)
|
38
40
|
request_data = {:value => data}.to_json
|
39
41
|
request = prepare_request :PUT, request_data
|
40
|
-
execute_request(request)
|
42
|
+
Response.new(execute_request(request))
|
43
|
+
end
|
44
|
+
|
45
|
+
# Store a file in SAM. If the file will be used by other NSI's service
|
46
|
+
# you should pass an additional 'type' parameter.
|
47
|
+
#
|
48
|
+
# @param [Object] file_content json serializable object
|
49
|
+
# @param [Symbol] type of the file to be stored. Can be either :doc and :video.
|
50
|
+
# @return [Response] object with access to the key and the sha512 checkum of the stored data
|
51
|
+
#
|
52
|
+
# @raise [NSISam::Errors::Client::AuthenticationError] when user and password doesn't match
|
53
|
+
#
|
54
|
+
# @example
|
55
|
+
# nsisam.store_file(File.read("foo.txt"))
|
56
|
+
# nsisam.store_file(File.read("foo.txt"), :video)
|
57
|
+
def store_file(file_content, type=:file)
|
58
|
+
store(type => Base64.encode64(file_content))
|
41
59
|
end
|
42
60
|
|
43
61
|
# Delete data at a given SAM key
|
@@ -54,16 +72,13 @@ module NSISam
|
|
54
72
|
def delete(key)
|
55
73
|
request_data = {:key => key}.to_json
|
56
74
|
request = prepare_request :DELETE, request_data
|
57
|
-
execute_request(request)
|
75
|
+
Response.new(execute_request(request))
|
58
76
|
end
|
59
77
|
|
60
78
|
# Recover data stored at a given SAM key
|
61
79
|
#
|
62
80
|
# @param [String] key of the value to acess
|
63
|
-
# @return [
|
64
|
-
# * "from_user" [String] the user who stored the value
|
65
|
-
# * "date" [String] the date when the value was stored
|
66
|
-
# * "data" [String, Hash, Array] the data stored at that key
|
81
|
+
# @return [Response] response object holding the file and some metadata
|
67
82
|
#
|
68
83
|
# @raise [NSISam::Errors::Client::KeyNotFoundError] when the key doesn't exists
|
69
84
|
# @raise [NSISam::Errors::Client::AuthenticationError] when user and password doesn't match
|
@@ -75,16 +90,38 @@ module NSISam
|
|
75
90
|
request = prepare_request :GET, request_data
|
76
91
|
response = execute_request(request)
|
77
92
|
verify_checksum(response["data"], expected_checksum) unless expected_checksum.nil?
|
78
|
-
response
|
93
|
+
Response.new(response)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Recover a file stored at a given SAM key
|
97
|
+
#
|
98
|
+
# @param [String] key of the file to access
|
99
|
+
# @param [Symbol] type of the file to be recovered. Can be either :doc and :video.
|
100
|
+
# @return [Response] response object holding the file and some metadata
|
101
|
+
#
|
102
|
+
# @raise [NSISam::Errors::Client::KeyNotFoundError] when the key doesn't exists
|
103
|
+
# @raise [NSISam::Errors::Client::AuthenticationError] when user and password doesn't match
|
104
|
+
#
|
105
|
+
# @note Use of wrong "type" parameter can generate errors.
|
106
|
+
#
|
107
|
+
# @example
|
108
|
+
# nsisam.get_file("some key")
|
109
|
+
# nsisam.store_file("test", :doc) # stored at key 'test_key'
|
110
|
+
# nsisam.get_file("test_key", :doc)
|
111
|
+
def get_file(key, type=:file, expected_checksum = nil)
|
112
|
+
response = get(key, expected_checksum)
|
113
|
+
Response.new(
|
114
|
+
'key' => response.key,
|
115
|
+
'checksum' => response.checksum,
|
116
|
+
'data' => Base64.decode64(response.data[type.to_s]),
|
117
|
+
'deleted' => response.deleted?)
|
79
118
|
end
|
80
119
|
|
81
120
|
# Update data stored at a given SAM key
|
82
121
|
#
|
83
122
|
# @param [String] key of the data to update
|
84
123
|
# @param [String, Hash, Array] data to be stored at the key
|
85
|
-
# @return [
|
86
|
-
# * "key" [String] just to value key again
|
87
|
-
# * "checksum" [String] the new sha512 checksum of the key's data
|
124
|
+
# @return [Response] response object holding the file and some metadata
|
88
125
|
#
|
89
126
|
# @raise [NSISam::Errors::Client::KeyNotFoundError] when the key doesn't exists
|
90
127
|
# @raise [NSISam::Errors::Client::AuthenticationError] when user and password doesn't match
|
@@ -94,7 +131,26 @@ module NSISam
|
|
94
131
|
def update(key, value)
|
95
132
|
request_data = {:key => key, :value => value}.to_json
|
96
133
|
request = prepare_request :POST, request_data
|
97
|
-
execute_request(request)
|
134
|
+
Response.new(execute_request(request))
|
135
|
+
end
|
136
|
+
|
137
|
+
# Update file stored at a given SAM key
|
138
|
+
#
|
139
|
+
# @param [String] key of the file to update
|
140
|
+
# @param [Symbol] type of the file to be recovered. Can be either :doc and :video.
|
141
|
+
# @param [String] new_content content of the new file
|
142
|
+
# @return [Response] response object holding the file and some metadata
|
143
|
+
#
|
144
|
+
# @raise [NSISam::Errors::Client::KeyNotFoundError] when the key doesn't exists
|
145
|
+
# @raise [NSISam::Errors::Client::AuthenticationError] when user and password doesn't match
|
146
|
+
#
|
147
|
+
# @example
|
148
|
+
# nsisam.update_file("my key", "my value")
|
149
|
+
# nsisam.update_file("my key", "my value", :video)
|
150
|
+
# nsisam.update_file("my key", "my value", :doc)
|
151
|
+
def update_file(key, type=:file, new_content)
|
152
|
+
encoded = Base64.encode64(new_content)
|
153
|
+
update(key, type => encoded)
|
98
154
|
end
|
99
155
|
|
100
156
|
# Pre-configure the NSISam module with default params for the NSISam::Client
|
@@ -112,6 +168,10 @@ module NSISam
|
|
112
168
|
Configuration.instance_eval(&block)
|
113
169
|
end
|
114
170
|
|
171
|
+
def download_link_for_file(key)
|
172
|
+
"http://#{@host}:#{@port}/file/#{key}"
|
173
|
+
end
|
174
|
+
|
115
175
|
private
|
116
176
|
|
117
177
|
def prepare_request(verb, body)
|
@@ -127,6 +187,7 @@ module NSISam
|
|
127
187
|
response = Net::HTTP.start @host, @port do |http|
|
128
188
|
http.request(request)
|
129
189
|
end
|
190
|
+
response
|
130
191
|
rescue Errno::ECONNREFUSED => e
|
131
192
|
raise NSISam::Errors::Client::ConnectionRefusedError
|
132
193
|
else
|
@@ -141,6 +202,5 @@ module NSISam
|
|
141
202
|
sha512_checksum = Digest::SHA512.hexdigest(data)
|
142
203
|
raise NSISam::Errors::Client::ChecksumMismatchError unless sha512_checksum == expected_checksum
|
143
204
|
end
|
144
|
-
|
145
205
|
end
|
146
206
|
end
|
data/lib/nsisam/fake_client.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'base64'
|
3
|
+
require File.dirname(__FILE__) + '/response'
|
2
4
|
|
3
5
|
module NSISam
|
4
6
|
class FakeClient
|
@@ -12,6 +14,12 @@ module NSISam
|
|
12
14
|
{'key' => key, 'checksum' => 0}
|
13
15
|
end
|
14
16
|
|
17
|
+
def store_file(file, type=:file)
|
18
|
+
key = Time.now.to_i.to_s
|
19
|
+
@storage[key] = {type.to_s => Base64.encode64(file)}.to_json
|
20
|
+
Response.new "key" => key, "checksum" => 0
|
21
|
+
end
|
22
|
+
|
15
23
|
def get(key, expected_checksum=nil)
|
16
24
|
if @storage.has_key?(key)
|
17
25
|
{'data' => @storage[key]}
|
@@ -20,6 +28,15 @@ module NSISam
|
|
20
28
|
end
|
21
29
|
end
|
22
30
|
|
31
|
+
def get_file(key, type=:file)
|
32
|
+
if @storage.has_key?(key)
|
33
|
+
response = Hash.new 'data' => Base64.decode64(@storage[key][type.to_s])
|
34
|
+
Response.new response
|
35
|
+
else
|
36
|
+
raise NSISam::Errors::Client::KeyNotFoundError
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
23
40
|
def delete(key)
|
24
41
|
if @storage.has_key?(key)
|
25
42
|
@storage.delete key
|
data/lib/nsisam/fake_server.rb
CHANGED
@@ -5,36 +5,49 @@ require "thread"
|
|
5
5
|
|
6
6
|
module NSISam
|
7
7
|
class Server < Sinatra::Application
|
8
|
+
def storage
|
9
|
+
@@storage ||= {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def generate_key
|
13
|
+
rand.to_s
|
14
|
+
end
|
8
15
|
|
9
16
|
put "/" do
|
10
17
|
content_type :json
|
11
18
|
incoming = JSON.parse(request.body.read)
|
12
|
-
|
19
|
+
key = generate_key
|
20
|
+
storage[key] = incoming['value']
|
21
|
+
{ key: key, checksum: "0" }.to_json
|
13
22
|
end
|
14
23
|
|
15
24
|
get "/" do
|
16
25
|
content_type :json
|
17
26
|
incoming = JSON.parse(request.body.read)
|
18
|
-
|
27
|
+
key = incoming["key"]
|
28
|
+
return 404 unless storage.has_key?(key)
|
19
29
|
{
|
20
30
|
metadata: "this is the metadata",
|
21
|
-
data:
|
31
|
+
data: storage[key]
|
22
32
|
}.to_json
|
23
33
|
end
|
24
34
|
|
25
35
|
delete "/" do
|
26
36
|
content_type :json
|
27
37
|
incoming = JSON.parse(request.body.read)
|
28
|
-
|
29
|
-
|
30
|
-
|
38
|
+
key = incoming["key"]
|
39
|
+
return 404 unless storage.has_key?(key)
|
40
|
+
storage.delete(key)
|
41
|
+
{ deleted: true }.to_json
|
31
42
|
end
|
32
43
|
|
33
44
|
post "/" do
|
34
45
|
content_type :json
|
35
46
|
incoming = JSON.parse(request.body.read)
|
36
|
-
|
37
|
-
|
47
|
+
key = incoming["key"]
|
48
|
+
return 404 unless storage.has_key?(key)
|
49
|
+
storage[key] = incoming['value']
|
50
|
+
{ key: key, checksum: 0 }.to_json
|
38
51
|
end
|
39
52
|
end
|
40
53
|
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module NSISam
|
2
|
+
# @attr [String] key The key of the stored data/file
|
3
|
+
# @attr [String] checksum The checksum of the stored data/file
|
4
|
+
# @attr [Hash, String, Array] data The stored object
|
5
|
+
class Response
|
6
|
+
def initialize(hash)
|
7
|
+
@key, @checksum, @data, @deleted = hash.values_at(
|
8
|
+
'key', 'checksum', 'data', 'deleted')
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :key, :checksum, :data
|
12
|
+
|
13
|
+
# Check if some data was deleted sucessfully
|
14
|
+
#
|
15
|
+
# @return [Boolean] was the object deleted?
|
16
|
+
#
|
17
|
+
def deleted?
|
18
|
+
!!@deleted
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/nsisam.rb
CHANGED
@@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/nsisam/configuration'
|
|
3
3
|
require File.dirname(__FILE__) + '/nsisam/errors'
|
4
4
|
require File.dirname(__FILE__) + '/nsisam/fake_server'
|
5
5
|
require File.dirname(__FILE__) + '/nsisam/fake_client'
|
6
|
+
require File.dirname(__FILE__) + '/nsisam/response'
|
6
7
|
|
7
8
|
module NSISam
|
8
9
|
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.
|
8
|
+
s.version = "0.6.0"
|
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-
|
12
|
+
s.date = "2012-08-29"
|
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 = [
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".rspec",
|
22
|
+
".travis.yml",
|
22
23
|
"Gemfile",
|
23
24
|
"LICENSE.txt",
|
24
25
|
"README.rdoc",
|
@@ -30,16 +31,22 @@ Gem::Specification.new do |s|
|
|
30
31
|
"lib/nsisam/errors.rb",
|
31
32
|
"lib/nsisam/fake_client.rb",
|
32
33
|
"lib/nsisam/fake_server.rb",
|
34
|
+
"lib/nsisam/response.rb",
|
33
35
|
"nsisam.gemspec",
|
34
36
|
"spec/configuration_spec.rb",
|
35
37
|
"spec/fake_client_spec.rb",
|
38
|
+
"spec/integration.yml.example",
|
36
39
|
"spec/nsisam_spec.rb",
|
37
|
-
"spec/
|
40
|
+
"spec/response_spec.rb",
|
41
|
+
"spec/spec_helper.rb",
|
42
|
+
"spec/support/core_extensions.rb",
|
43
|
+
"spec/support/files.rb",
|
44
|
+
"spec/support/integration.rb"
|
38
45
|
]
|
39
46
|
s.homepage = "http://github.com/nsi-iff/nsisam-ruby"
|
40
47
|
s.licenses = ["MIT"]
|
41
48
|
s.require_paths = ["lib"]
|
42
|
-
s.rubygems_version = "1.8.
|
49
|
+
s.rubygems_version = "1.8.24"
|
43
50
|
s.summary = "A simple gem to access a SAM service."
|
44
51
|
|
45
52
|
if s.respond_to? :specification_version then
|
data/spec/fake_client_spec.rb
CHANGED
@@ -12,6 +12,13 @@ describe "NSISam::FakeClient" do
|
|
12
12
|
response.should have_key("checksum")
|
13
13
|
end
|
14
14
|
|
15
|
+
it "can store a file" do
|
16
|
+
response = @nsisam.store_file("some file not in base64")
|
17
|
+
response.should_not be_nil
|
18
|
+
response.key.should_not be_nil
|
19
|
+
response.checksum.should_not be_nil
|
20
|
+
end
|
21
|
+
|
15
22
|
it "can delete a stored value" do
|
16
23
|
resp = @nsisam.store("delete this")
|
17
24
|
response = @nsisam.delete(resp["key"])
|
@@ -35,6 +42,12 @@ describe "NSISam::FakeClient" do
|
|
35
42
|
response["data"].should == "retrieve this"
|
36
43
|
end
|
37
44
|
|
45
|
+
it "can retrieve a stored file" do
|
46
|
+
resp = @nsisam.store_file("file not in base64")
|
47
|
+
response = @nsisam.get_file(resp.key)
|
48
|
+
response.data.should_not be_nil
|
49
|
+
end
|
50
|
+
|
38
51
|
it "can update values in keys already stored" do
|
39
52
|
resp = @nsisam.store("update this")
|
40
53
|
response = @nsisam.update(resp['key'], "updated")
|
@@ -42,4 +55,4 @@ describe "NSISam::FakeClient" do
|
|
42
55
|
@nsisam.get(response['key'])['data'].should == "updated"
|
43
56
|
response.should have_key("checksum")
|
44
57
|
end
|
45
|
-
end
|
58
|
+
end
|
data/spec/nsisam_spec.rb
CHANGED
@@ -1,22 +1,26 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'base64'
|
2
3
|
|
3
4
|
describe NSISam do
|
4
|
-
|
5
5
|
before :all do
|
6
|
-
|
7
|
-
|
6
|
+
fake_options = { user: 'test', password: 'test', host: 'localhost',
|
7
|
+
port: '8888' }
|
8
|
+
@options = integration_options || fake_options
|
9
|
+
@nsisam = NSISam::Client.new(@options)
|
8
10
|
@keys = Array.new
|
9
|
-
@fake_sam = NSISam::FakeServerManager.new.start_server
|
11
|
+
@fake_sam = NSISam::FakeServerManager.new.start_server unless integrating?
|
10
12
|
end
|
11
13
|
|
12
14
|
after :all do
|
13
|
-
@fake_sam.stop_server
|
15
|
+
@fake_sam.stop_server unless integrating?
|
14
16
|
end
|
15
17
|
|
18
|
+
let(:file_content) { example_file_content }
|
19
|
+
|
16
20
|
context "cannot connect to server" do
|
17
21
|
it "throws error if couldn't connect to the server" do
|
18
22
|
sam = NSISam::Client.new user: 'test', password: 'test',
|
19
|
-
|
23
|
+
host: 'localhost', port: '4000'
|
20
24
|
expect { sam.store('anything') }.to raise_error(NSISam::Errors::Client::ConnectionRefusedError)
|
21
25
|
end
|
22
26
|
end
|
@@ -24,17 +28,26 @@ describe NSISam do
|
|
24
28
|
context "storing" do
|
25
29
|
it "can store a value in SAM" do
|
26
30
|
response = @nsisam.store("something")
|
27
|
-
response.
|
28
|
-
response.should
|
29
|
-
|
31
|
+
response.should respond_to("key")
|
32
|
+
response.should respond_to("checksum")
|
33
|
+
end
|
34
|
+
|
35
|
+
context "file" do
|
36
|
+
it "encodes content before storing" do
|
37
|
+
Base64.should_receive(:encode64).with(file_content).
|
38
|
+
and_return(:dummy_value)
|
39
|
+
@nsisam.should_receive(:store).with(file: :dummy_value).
|
40
|
+
and_return(:dummy_result)
|
41
|
+
@nsisam.store_file(file_content).should == :dummy_result
|
42
|
+
end
|
30
43
|
end
|
31
44
|
end
|
32
45
|
|
33
46
|
context "deleting" do
|
34
47
|
it "can delete a stored value" do
|
35
|
-
@nsisam.store("delete this")
|
36
|
-
response = @nsisam.delete(
|
37
|
-
response
|
48
|
+
key = @nsisam.store("delete this").key
|
49
|
+
response = @nsisam.delete(key)
|
50
|
+
response.should be_deleted
|
38
51
|
end
|
39
52
|
|
40
53
|
it "raises error when key not found" do
|
@@ -44,40 +57,95 @@ describe NSISam do
|
|
44
57
|
|
45
58
|
context "retrieving" do
|
46
59
|
it "can retrieve a stored value" do
|
47
|
-
@nsisam.store("retrieve this")
|
48
|
-
response = @nsisam.get(
|
49
|
-
response
|
60
|
+
key = @nsisam.store("retrieve this").key
|
61
|
+
response = @nsisam.get(key)
|
62
|
+
response.data.should == "retrieve this"
|
50
63
|
end
|
51
64
|
|
52
65
|
it "can retrieve a stored value and automaticly verify its checksum" do
|
53
|
-
@nsisam.should_receive(:verify_checksum).with('
|
54
|
-
@nsisam.store("retrieve this")
|
55
|
-
response = @nsisam.get(
|
56
|
-
response
|
66
|
+
@nsisam.should_receive(:verify_checksum).with('retrieve this', 0).and_return(0)
|
67
|
+
key = @nsisam.store("retrieve this").key
|
68
|
+
response = @nsisam.get(key, 0)
|
69
|
+
response.data.should == "retrieve this"
|
57
70
|
end
|
58
71
|
|
59
72
|
it "raises errors when expected checksum doesn't match the calculated one" do
|
60
73
|
wrong_checksum = 333
|
61
|
-
@nsisam.store("retrieve this")
|
62
|
-
expect { @nsisam.get(
|
74
|
+
key = @nsisam.store("retrieve this").key
|
75
|
+
expect { @nsisam.get(key, 333) }.to raise_error(NSISam::Errors::Client::ChecksumMismatchError)
|
63
76
|
end
|
64
77
|
|
65
78
|
it "raises error when key not found" do
|
66
79
|
expect { @nsisam.get("i dont exist") }.to raise_error(NSISam::Errors::Client::KeyNotFoundError)
|
67
80
|
end
|
81
|
+
|
82
|
+
context 'file' do
|
83
|
+
it 'decodes content after retrieving' do
|
84
|
+
@nsisam.should_receive(:get).with(:key, nil).
|
85
|
+
and_return(stub(key: 'key', checksum: 999,
|
86
|
+
data: { 'file' => :dummy_value }, deleted?: true))
|
87
|
+
Base64.should_receive(:decode64).with(:dummy_value).
|
88
|
+
and_return(:decoded_dummy)
|
89
|
+
response = @nsisam.get_file(:key)
|
90
|
+
response.data.should == :decoded_dummy
|
91
|
+
end
|
92
|
+
|
93
|
+
it "can generate a direct link to download any file" do
|
94
|
+
link = @nsisam.download_link_for_file('some_key')
|
95
|
+
link.should == "http://#{@options[:host]}:#{@options[:port]}/file/some_key"
|
96
|
+
end
|
97
|
+
end
|
68
98
|
end
|
69
99
|
|
70
100
|
context "updating" do
|
71
101
|
it "can update values in keys already stored" do
|
72
|
-
@nsisam.store("update this")
|
73
|
-
response = @nsisam.update(
|
74
|
-
response
|
75
|
-
response.
|
102
|
+
key = @nsisam.store("update this").key
|
103
|
+
response = @nsisam.update(key, "updated")
|
104
|
+
response.key.should == key
|
105
|
+
response.checksum.should_not be_nil
|
106
|
+
@nsisam.get(key).data.should == 'updated'
|
76
107
|
end
|
77
108
|
|
78
109
|
it "raises error when key not found" do
|
79
110
|
expect { @nsisam.update("dont exist ruby is fast", "foo") }.to raise_error(NSISam::Errors::Client::KeyNotFoundError)
|
80
111
|
end
|
112
|
+
|
113
|
+
context 'file' do
|
114
|
+
it 'encodes content before updating' do
|
115
|
+
key = @nsisam.store_file(file_content).key
|
116
|
+
Base64.should_receive(:encode64).with(:dummy_content).
|
117
|
+
and_return(:dummy_content)
|
118
|
+
@nsisam.should_receive(:update).with(key, file: :dummy_content).
|
119
|
+
and_return(:dummy_result)
|
120
|
+
@nsisam.update_file(key, :dummy_content).should == :dummy_result
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'file storage without mocking' do
|
126
|
+
it 'stores, retrieves and updates files' do
|
127
|
+
updated_file_content = file_content + 'anything ha!'
|
128
|
+
key = @nsisam.store_file(file_content).key
|
129
|
+
@nsisam.get_file(key).data.should == file_content
|
130
|
+
@nsisam.update_file(key, updated_file_content)
|
131
|
+
@nsisam.get_file(key).data.should == updated_file_content
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'stores, retrieves and updates documents for other nsi-services' do
|
135
|
+
updated_file_content = file_content + 'anything ha!'
|
136
|
+
key = @nsisam.store_file(file_content, :doc).key
|
137
|
+
@nsisam.get_file(key, :doc).data.should == file_content
|
138
|
+
@nsisam.update_file(key, :doc, updated_file_content)
|
139
|
+
@nsisam.get_file(key, :doc).data.should == updated_file_content
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'stores, retrieves and updates videos for other nsi-services' do
|
143
|
+
updated_file_content = file_content + 'anything ha!'
|
144
|
+
key = @nsisam.store_file(file_content, :video).key
|
145
|
+
@nsisam.get_file(key, :video).data.should == file_content
|
146
|
+
@nsisam.update_file(key, :video, updated_file_content)
|
147
|
+
@nsisam.get_file(key, :video).data.should == updated_file_content
|
148
|
+
end
|
81
149
|
end
|
82
150
|
|
83
151
|
context "get configuration" do
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe NSISam::Response do
|
4
|
+
let(:response) do
|
5
|
+
response = NSISam::Response.new('key' => 'key value',
|
6
|
+
'checksum' => 'checksum value',
|
7
|
+
'data' => 'data value',
|
8
|
+
'deleted' => true)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'retrieves response data as methods' do
|
12
|
+
response.key.should == 'key value'
|
13
|
+
response.checksum.should == 'checksum value'
|
14
|
+
response.data.should == 'data value'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'retrieves deleted as boolean method' do
|
18
|
+
response.should be_deleted
|
19
|
+
NSISam::Response.new('deleted' => false).should_not be_deleted
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
# stolen from active_support 3.2.6 lib/active_support/core_ext/hash/keys.rb
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
# Return a new hash with all keys converted to symbols, as long as
|
5
|
+
# they respond to +to_sym+.
|
6
|
+
#
|
7
|
+
# { 'name' => 'Rob', 'years' => '28' }.symbolize_keys
|
8
|
+
# #=> { :name => "Rob", :years => "28" }
|
9
|
+
def symbolize_keys
|
10
|
+
dup.symbolize_keys!
|
11
|
+
end
|
12
|
+
|
13
|
+
# Destructively convert all keys to symbols, as long as they respond
|
14
|
+
# to +to_sym+. Same as +symbolize_keys+, but modifies +self+.
|
15
|
+
def symbolize_keys!
|
16
|
+
keys.each do |key|
|
17
|
+
self[(key.to_sym rescue key) || key] = delete(key)
|
18
|
+
end
|
19
|
+
self
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module NSISam
|
4
|
+
module Test
|
5
|
+
module Helpers
|
6
|
+
def integration_options
|
7
|
+
YAML::load(File.open(config_file)).symbolize_keys if integrating?
|
8
|
+
end
|
9
|
+
|
10
|
+
def integrating?
|
11
|
+
ENV['NSI_SAM_INTEGRATION'] && config_file_exists?
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
|
16
|
+
def config_file_exists?
|
17
|
+
File.exists?(config_file)
|
18
|
+
end
|
19
|
+
|
20
|
+
def config_file
|
21
|
+
File.expand_path(File.join(File.dirname(__FILE__), '..',
|
22
|
+
'integration.yml'))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
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.
|
4
|
+
version: 0.6.0
|
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-
|
12
|
+
date: 2012-08-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
@@ -134,6 +134,7 @@ extra_rdoc_files:
|
|
134
134
|
files:
|
135
135
|
- .document
|
136
136
|
- .rspec
|
137
|
+
- .travis.yml
|
137
138
|
- Gemfile
|
138
139
|
- LICENSE.txt
|
139
140
|
- README.rdoc
|
@@ -145,11 +146,17 @@ files:
|
|
145
146
|
- lib/nsisam/errors.rb
|
146
147
|
- lib/nsisam/fake_client.rb
|
147
148
|
- lib/nsisam/fake_server.rb
|
149
|
+
- lib/nsisam/response.rb
|
148
150
|
- nsisam.gemspec
|
149
151
|
- spec/configuration_spec.rb
|
150
152
|
- spec/fake_client_spec.rb
|
153
|
+
- spec/integration.yml.example
|
151
154
|
- spec/nsisam_spec.rb
|
155
|
+
- spec/response_spec.rb
|
152
156
|
- spec/spec_helper.rb
|
157
|
+
- spec/support/core_extensions.rb
|
158
|
+
- spec/support/files.rb
|
159
|
+
- spec/support/integration.rb
|
153
160
|
homepage: http://github.com/nsi-iff/nsisam-ruby
|
154
161
|
licenses:
|
155
162
|
- MIT
|
@@ -165,7 +172,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
165
172
|
version: '0'
|
166
173
|
segments:
|
167
174
|
- 0
|
168
|
-
hash:
|
175
|
+
hash: 2483391004084390575
|
169
176
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
177
|
none: false
|
171
178
|
requirements:
|
@@ -174,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
174
181
|
version: '0'
|
175
182
|
requirements: []
|
176
183
|
rubyforge_project:
|
177
|
-
rubygems_version: 1.8.
|
184
|
+
rubygems_version: 1.8.24
|
178
185
|
signing_key:
|
179
186
|
specification_version: 3
|
180
187
|
summary: A simple gem to access a SAM service.
|