cleversafe 1.0.2 → 1.0.7

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.
@@ -3,10 +3,10 @@ module Cleversafe
3
3
  attr_reader :username
4
4
  attr_accessor :password
5
5
  attr_accessor :host
6
- attr_accessor :proto
6
+ attr_accessor :protocol
7
7
  attr_accessor :vault
8
8
  attr_accessor :method
9
-
9
+
10
10
  def initialize(*args)
11
11
  if args[0].is_a?(Hash)
12
12
  options = args[0]
@@ -14,37 +14,47 @@ module Cleversafe
14
14
  @password = options[:password]
15
15
  @host = options[:host]
16
16
  @protocol = options[:protocol] || "http"
17
+ @open_timeout = options[:open_timeout] || 10
17
18
  else
18
19
  @username = args[0]
19
20
  @password = args[1]
20
21
  @host = args[2]
21
22
  @protocol = args[3] || "http"
23
+ @open_timeout = args[4] || 10
22
24
  end
23
-
25
+
24
26
  @connection ||= begin
25
27
  build_connection
26
28
  end
27
29
  return @connection
28
30
  end
29
-
31
+
30
32
  def build_connection
31
- RestClient::Resource.new(base_url, @username, @password)
33
+ RestClient::Resource.new(base_url, :user => @username, :password => @password,
34
+ :raw_response => true, :open_timeout => @open_timeout)
32
35
  end
33
-
36
+
34
37
  def base_url
35
38
  "#{@protocol}://#{@host}/"
36
39
  end
37
40
 
41
+ def url_for(vault, objectname, options={})
42
+ protocol = options.fetch(:protocol, @protocol)
43
+ host = options.fetch(:host, @host)
44
+
45
+ "#{protocol}://#{host}/#{vault}/#{objectname}"
46
+ end
47
+
38
48
  def vault(name)
39
49
  Cleversafe::Vault.new(self, name)
40
50
  end
41
51
 
42
- def vaults
52
+ def vaults
43
53
  vaults = JSON.parse(get(nil))['vaults']
44
54
  vaults.collect{|v| v['vault_name']}
45
55
  end
46
-
47
- def vault_exists?(vault_name)
56
+
57
+ def vault_exists?(vault_name)
48
58
  begin
49
59
  response = get(vault_name)
50
60
  true
@@ -52,22 +62,22 @@ module Cleversafe
52
62
  false
53
63
  end
54
64
  end
55
-
65
+
56
66
  def get(path, options = {})
57
67
  @connection[path].get options
58
68
  end
59
-
69
+
60
70
  def head(path, options = {})
61
71
  @connection[path].head options
62
72
  end
63
-
73
+
64
74
  def put(path, payload, options = {})
65
75
  @connection[path].put payload, options
66
76
  end
67
-
77
+
68
78
  def delete(path)
69
79
  @connection[path].delete
70
80
  end
71
-
81
+
72
82
  end
73
- end
83
+ end
@@ -0,0 +1,6 @@
1
+ module Cleversafe
2
+ module Error
3
+ class Base < RuntimeError; end
4
+ class NotFound < Base; end
5
+ end
6
+ end
@@ -1,6 +1,8 @@
1
+ require 'fileutils'
2
+
1
3
  module Cleversafe
2
4
  class Object
3
-
5
+
4
6
  attr_reader :name
5
7
  attr_reader :vault
6
8
 
@@ -9,26 +11,71 @@ module Cleversafe
9
11
  @name = objectname
10
12
  @connection = vault.connection
11
13
  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")
14
+
15
+ def url(options={})
16
+ @connection.url_for(@vault, @name, options)
17
+ end
18
+
19
+ def delete
20
+ handle_errors do
21
+ @connection.delete("#{@vault}/#{@name}")
22
+ end
23
+ end
24
+
25
+ def exists?
26
+ metadata
27
+ true
28
+ rescue Error::NotFound
29
+ false
30
+ end
31
+
32
+ def data(options={})
33
+ handle_errors do
34
+ @connection.get("#{@vault}/#{@name}", options).to_s
18
35
  end
19
36
  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")
37
+
38
+ def open(options={})
39
+ handle_errors do
40
+ response = @connection.get("#{@vault}/#{@name}", options)
41
+ begin
42
+ yield response.file.open
43
+ ensure
44
+ response.file.unlink
45
+ end
26
46
  end
27
47
  end
28
-
48
+
49
+ def write_to(filename, options={})
50
+ handle_errors do
51
+ response = @connection.get("#{@vault}/#{@name}", options)
52
+ FileUtils.cp(response.file.path, filename)
53
+ FileUtils.rm(response.file.path)
54
+ end
55
+ end
56
+
57
+ def metadata
58
+ @metadata ||= handle_errors { @connection.head("#{@vault}/#{@name}").headers }
59
+ end
60
+
29
61
  def etag
30
- self.object_metadata[:etag]
62
+ metadata[:etag]
63
+ end
64
+
65
+ def size
66
+ metadata[:content_length].to_i
67
+ end
68
+
69
+ private
70
+
71
+ def handle_errors
72
+ yield
73
+ rescue RestClient::Exception => e
74
+ if (e.http_code.to_s == "404")
75
+ raise Error::NotFound, "object `#{@name}' does not exist", caller[0..-2]
76
+ else
77
+ raise
78
+ end
31
79
  end
32
-
33
80
  end
34
- end
81
+ end
@@ -1,60 +1,45 @@
1
1
  module Cleversafe
2
2
  class Vault
3
-
3
+
4
4
  attr_reader :name
5
5
  attr_reader :connection
6
-
6
+
7
7
  def initialize(connection, name)
8
8
  @connection = connection
9
9
  @name = name
10
10
  end
11
-
12
- def vault_metadata
11
+
12
+ def metadata
13
13
  @metadata ||= JSON.parse(@connection.get(@name))
14
14
  end
15
-
15
+
16
16
  def bytes_used
17
- self.vault_metadata['vault_usage']['used_size']
17
+ metadata['vault_usage']['used_size']
18
18
  end
19
-
19
+
20
20
  def object(objectname)
21
21
  Cleversafe::Object.new(self, objectname)
22
22
  end
23
-
23
+ alias [] object
24
+
24
25
  def objects(params = {})
25
26
  options = {}
26
27
  options['X-Operation'] = "list"
27
28
  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")
29
+ options['X-Start-Id'] = params[:start_id] if params[:start_id]
30
+ @connection.get(@name, options).to_s.split("\n")
30
31
  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
-
32
+
41
33
  def create_object(payload, options = {})
42
34
  response = @connection.put("#{@name}", payload, options)
43
- digest_header = "x_content_digest"
35
+ id = response.to_s.strip
36
+
44
37
  if response.headers[:x_content_digest]
45
- {:id => response.body, :x_content_digest => response.headers[:x_content_digest] }
38
+ {:id => id, :x_content_digest => response.headers[:x_content_digest] }
46
39
  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")
40
+ id
56
41
  end
57
42
  end
58
-
43
+
59
44
  end
60
- end
45
+ end
data/lib/cleversafe.rb CHANGED
@@ -4,6 +4,7 @@
4
4
  require 'json'
5
5
 
6
6
  $:.unshift(File.dirname(__FILE__))
7
+ require 'cleversafe/errors'
7
8
  require 'cleversafe/vault'
8
9
  require 'cleversafe/object'
9
- require 'cleversafe/connection'
10
+ require 'cleversafe/connection'
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 2
9
- version: 1.0.2
8
+ - 7
9
+ version: 1.0.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Williams
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2013-02-27 00:00:00 -06:00
17
+ date: 2013-04-16 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -39,6 +39,7 @@ extra_rdoc_files: []
39
39
 
40
40
  files:
41
41
  - lib/cleversafe/connection.rb
42
+ - lib/cleversafe/errors.rb
42
43
  - lib/cleversafe/object.rb
43
44
  - lib/cleversafe/vault.rb
44
45
  - lib/cleversafe.rb