cleversafe 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cleversafe/connection.rb +73 -0
- data/lib/cleversafe/object.rb +34 -0
- data/lib/cleversafe/vault.rb +60 -0
- data/lib/cleversafe.rb +9 -0
- metadata +76 -0
@@ -0,0 +1,73 @@
|
|
1
|
+
module Cleversafe
|
2
|
+
class Connection
|
3
|
+
attr_reader :username
|
4
|
+
attr_accessor :password
|
5
|
+
attr_accessor :host
|
6
|
+
attr_accessor :proto
|
7
|
+
attr_accessor :vault
|
8
|
+
attr_accessor :method
|
9
|
+
|
10
|
+
def initialize(*args)
|
11
|
+
if args[0].is_a?(Hash)
|
12
|
+
options = args[0]
|
13
|
+
@username = options[:username]
|
14
|
+
@password = options[:password]
|
15
|
+
@host = options[:host]
|
16
|
+
@protocol = options[:protocol] || "http"
|
17
|
+
else
|
18
|
+
@username = args[0]
|
19
|
+
@password = args[1]
|
20
|
+
@host = args[2]
|
21
|
+
@protocol = args[3] || "http"
|
22
|
+
end
|
23
|
+
|
24
|
+
@connection ||= begin
|
25
|
+
build_connection
|
26
|
+
end
|
27
|
+
return @connection
|
28
|
+
end
|
29
|
+
|
30
|
+
def build_connection
|
31
|
+
RestClient::Resource.new(base_url, @username, @password)
|
32
|
+
end
|
33
|
+
|
34
|
+
def base_url
|
35
|
+
"#{@protocol}://#{@host}/"
|
36
|
+
end
|
37
|
+
|
38
|
+
def vault(name)
|
39
|
+
Cleversafe::Vault.new(self, name)
|
40
|
+
end
|
41
|
+
|
42
|
+
def vaults
|
43
|
+
vaults = JSON.parse(get(nil))['vaults']
|
44
|
+
vaults.collect{|v| v['vault_name']}
|
45
|
+
end
|
46
|
+
|
47
|
+
def vault_exists?(vault_name)
|
48
|
+
begin
|
49
|
+
response = get(vault_name)
|
50
|
+
true
|
51
|
+
rescue => e
|
52
|
+
false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def get(path, options = {})
|
57
|
+
@connection[path].get options
|
58
|
+
end
|
59
|
+
|
60
|
+
def head(path, options = {})
|
61
|
+
@connection[path].head options
|
62
|
+
end
|
63
|
+
|
64
|
+
def put(path, payload, options = {})
|
65
|
+
@connection[path].put payload, options
|
66
|
+
end
|
67
|
+
|
68
|
+
def delete(path)
|
69
|
+
@connection[path].delete
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Cleversafe
|
2
|
+
class Object
|
3
|
+
|
4
|
+
attr_reader :name
|
5
|
+
attr_reader :vault
|
6
|
+
|
7
|
+
def initialize(vault, objectname = {})
|
8
|
+
@vault = vault.name
|
9
|
+
@name = objectname
|
10
|
+
@connection = vault.connection
|
11
|
+
end
|
12
|
+
|
13
|
+
def data(options ={})
|
14
|
+
begin
|
15
|
+
@connection.get("#{@vault}/#{@name}", options).body
|
16
|
+
rescue RestClient::Exception => e
|
17
|
+
raise "#{e.http_code.to_s}: Object #{@name} does not exist" if (e.http_code.to_s == "404")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def object_metadata
|
22
|
+
begin
|
23
|
+
@object_metadata ||= @connection.head("#{@vault}/#{@name}").headers
|
24
|
+
rescue RestClient::Exception => e
|
25
|
+
raise "#{e.http_code.to_s}: Object #{@name} does not exist" if (e.http_code.to_s == "404")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def etag
|
30
|
+
self.object_metadata[:etag]
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Cleversafe
|
2
|
+
class Vault
|
3
|
+
|
4
|
+
attr_reader :name
|
5
|
+
attr_reader :connection
|
6
|
+
|
7
|
+
def initialize(connection, name)
|
8
|
+
@connection = connection
|
9
|
+
@name = name
|
10
|
+
end
|
11
|
+
|
12
|
+
def vault_metadata
|
13
|
+
@metadata ||= JSON.parse(@connection.get(@name))
|
14
|
+
end
|
15
|
+
|
16
|
+
def bytes_used
|
17
|
+
self.vault_metadata['vault_usage']['used_size']
|
18
|
+
end
|
19
|
+
|
20
|
+
def object(objectname)
|
21
|
+
Cleversafe::Object.new(self, objectname)
|
22
|
+
end
|
23
|
+
|
24
|
+
def objects(params = {})
|
25
|
+
options = {}
|
26
|
+
options['X-Operation'] = "list"
|
27
|
+
options['X-List-Length-Limit'] = params[:limit] if params[:limit]
|
28
|
+
options['X-Start-Id'] = params[:start_id] if params[:start_id]
|
29
|
+
@connection.get(@name, options).split("\n")
|
30
|
+
end
|
31
|
+
|
32
|
+
def object_exists?(objectname)
|
33
|
+
begin
|
34
|
+
response = @connection.head("#{@name}/#{objectname}")
|
35
|
+
true
|
36
|
+
rescue => e
|
37
|
+
false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def create_object(payload, options = {})
|
42
|
+
response = @connection.put("#{@name}", payload, options)
|
43
|
+
digest_header = "x_content_digest"
|
44
|
+
if response.headers[:x_content_digest]
|
45
|
+
{:id => response.body, :x_content_digest => response.headers[:x_content_digest] }
|
46
|
+
else
|
47
|
+
response.body
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def delete_object(objectname)
|
52
|
+
begin
|
53
|
+
@connection.delete("#{@name}/#{objectname}")
|
54
|
+
rescue RestClient::Exception => e
|
55
|
+
raise "#{e.http_code.to_s}: Object #{objectname} does not exist" if (e.http_code.to_s == "404")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
data/lib/cleversafe.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cleversafe
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 1.0.2
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- John Williams
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2013-02-27 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rest-client
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
description:
|
33
|
+
email: john@37signals.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files: []
|
39
|
+
|
40
|
+
files:
|
41
|
+
- lib/cleversafe/connection.rb
|
42
|
+
- lib/cleversafe/object.rb
|
43
|
+
- lib/cleversafe/vault.rb
|
44
|
+
- lib/cleversafe.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage:
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.3.6
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: A Ruby API into Cleversafe's REST interface.
|
75
|
+
test_files: []
|
76
|
+
|