cleversafe 1.0.8 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,13 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cleversafe (1.0.7)
4
+ cleversafe (1.1.1)
5
+ json
5
6
  rest-client (~> 1.6.7)
6
7
 
7
8
  GEM
8
9
  remote: http://rubygems.org/
9
10
  specs:
10
- mime-types (1.22)
11
+ json (1.7.7)
12
+ mime-types (1.23)
13
+ minitest (4.5.0)
14
+ rake (10.0.3)
11
15
  rest-client (1.6.7)
12
16
  mime-types (>= 1.16)
13
17
 
@@ -16,3 +20,5 @@ PLATFORMS
16
20
 
17
21
  DEPENDENCIES
18
22
  cleversafe!
23
+ minitest
24
+ rake
data/Rakefile CHANGED
@@ -1,4 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rake'
1
4
  require 'rake/testtask'
2
5
 
3
- Rake::TestTask.new
4
- task :default => :test
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs << 'test/lib'
10
+ t.pattern = 'test/*_test.rb'
11
+ end
data/cleversafe.gemspec CHANGED
@@ -3,9 +3,13 @@ Gem::Specification.new do |s|
3
3
  s.summary = "A Ruby API into Cleversafe's REST interface."
4
4
  s.author = "John Williams"
5
5
  s.email = "john@37signals.com"
6
- s.version = "1.0.8"
6
+ s.version = "1.1.3"
7
7
 
8
+ s.add_dependency 'json'
8
9
  s.add_dependency 'rest-client', '~> 1.6.7'
9
10
 
11
+ s.add_development_dependency 'rake'
12
+ s.add_development_dependency 'minitest'
13
+
10
14
  s.files = Dir["#{File.dirname(__FILE__)}/**/*"]
11
15
  end
data/lib/cleversafe.rb CHANGED
@@ -1,7 +1,13 @@
1
- require 'rest-client'
2
- require 'json'
3
-
4
1
  require 'cleversafe/errors'
5
2
  require 'cleversafe/vault'
6
3
  require 'cleversafe/object'
7
4
  require 'cleversafe/connection'
5
+
6
+ require 'rest-client'
7
+
8
+ # Monkeypatch RestClient to prevent it from closing IO objects after they're
9
+ # uploaded, which it really shouldn't do.
10
+ class RestClient::Payload::Streamed
11
+ def close
12
+ end
13
+ end
@@ -1,33 +1,17 @@
1
+ require 'json'
2
+
1
3
  module Cleversafe
2
4
  class Connection
3
- attr_reader :username, :password, :host, :protocol, :open_timeout
4
-
5
- def initialize(*args)
6
- if args[0].is_a?(Hash)
7
- options = args[0]
8
- @username = options[:username]
9
- @password = options[:password]
10
- @host = options[:host]
11
- @protocol = options[:protocol] || "http"
12
- @open_timeout = options[:open_timeout] || 10
13
- else
14
- @username = args[0]
15
- @password = args[1]
16
- @host = args[2]
17
- @protocol = args[3] || "http"
18
- @open_timeout = args[4] || 10
19
- end
5
+ def initialize(url, options = {})
6
+ @http = build_http_client(url, options)
20
7
  end
21
8
 
22
- def base_url
23
- "#{protocol}://#{host}/"
9
+ def url
10
+ @http.url
24
11
  end
25
12
 
26
- def url_for(vault, objectname, options={})
27
- protocol = options.fetch(:protocol, protocol)
28
- host = options.fetch(:host, host)
29
-
30
- "#{protocol}://#{host}/#{vault}/#{objectname}"
13
+ def url_for(*paths)
14
+ [ url, *paths ].join('/')
31
15
  end
32
16
 
33
17
  def vault(name)
@@ -35,41 +19,39 @@ module Cleversafe
35
19
  end
36
20
 
37
21
  def vaults
38
- vaults = JSON.parse(get(nil))['vaults']
39
- vaults.collect{|v| v['vault_name']}
22
+ @vaults ||= begin
23
+ data = JSON.parse get.to_s
24
+ data['vaults'].map { |v| v['vault_name'] }
25
+ end
40
26
  end
41
27
 
42
- def vault_exists?(vault_name)
43
- get(vault_name)
28
+ def ping
29
+ head
44
30
  true
45
- rescue RestClient::ResourceNotFound
31
+ rescue RestClient::Exception
46
32
  false
47
33
  end
48
34
 
49
- def get(path, options = {})
50
- connection[path].get options
35
+ def head(path = '', options = {})
36
+ @http[path].head options
51
37
  end
52
38
 
53
- def head(path, options = {})
54
- connection[path].head options
39
+ def get(path = '', options = {})
40
+ @http[path].get options
55
41
  end
56
42
 
57
43
  def put(path, payload, options = {})
58
- connection[path].put payload, options
44
+ @http[path].put payload, options
59
45
  end
60
46
 
61
47
  def delete(path)
62
- connection[path].delete
48
+ @http[path].delete
63
49
  end
64
50
 
65
51
  private
66
- def connection
67
- @connection ||= RestClient::Resource.new(base_url,
68
- :user => username,
69
- :password => password,
70
- :open_timeout => open_timeout,
71
- :raw_response => true
72
- )
52
+ def build_http_client(url, options = {})
53
+ defaults = { :open_timeout => 0.5 }
54
+ RestClient::Resource.new(url, defaults.merge(options).merge(:raw_response => true))
73
55
  end
74
56
  end
75
57
  end
@@ -5,7 +5,8 @@ module Cleversafe
5
5
 
6
6
  attr_reader :connection, :vault, :name
7
7
 
8
- def initialize(vault, name = {})
8
+ def initialize(vault, name)
9
+ raise ArgumentError, "name is required" unless name =~ /\S/
9
10
  @connection = vault.connection
10
11
  @vault = vault.name
11
12
  @name = name
@@ -15,8 +16,8 @@ module Cleversafe
15
16
  "#{vault}/#{name}"
16
17
  end
17
18
 
18
- def url(options={})
19
- connection.url_for(vault, name, options)
19
+ def url
20
+ connection.url_for(vault, name)
20
21
  end
21
22
 
22
23
  def delete
@@ -33,30 +34,23 @@ module Cleversafe
33
34
  end
34
35
 
35
36
  def data(options={})
36
- handle_errors do
37
- connection.get(key, options).to_s
38
- end
37
+ open(options) { |io| io.read }
39
38
  end
40
39
 
41
40
  def open(options={})
42
41
  handle_errors do
43
42
  response = connection.get(key, options)
44
43
  begin
45
- yield response.file.open
44
+ file = response.file
45
+ file.open
46
+ file.binmode
47
+ yield file
46
48
  ensure
47
- response.file.unlink
49
+ file.unlink
48
50
  end
49
51
  end
50
52
  end
51
53
 
52
- def write_to(filename, options={})
53
- handle_errors do
54
- response = connection.get(key, options)
55
- FileUtils.cp(response.file.path, filename)
56
- FileUtils.rm(response.file.path)
57
- end
58
- end
59
-
60
54
  def metadata
61
55
  @metadata ||= handle_errors do
62
56
  connection.head(key).headers
@@ -8,15 +8,15 @@ module Cleversafe
8
8
  end
9
9
 
10
10
  def metadata
11
- @metadata ||= JSON.parse(connection.get(name))
11
+ @metadata ||= JSON.parse connection.get(name).to_s
12
12
  end
13
13
 
14
14
  def bytes_used
15
15
  metadata['vault_usage']['used_size']
16
16
  end
17
17
 
18
- def object(objectname)
19
- Cleversafe::Object.new(self, objectname)
18
+ def object(key)
19
+ Cleversafe::Object.new(self, key)
20
20
  end
21
21
  alias [] object
22
22
 
@@ -38,6 +38,5 @@ module Cleversafe
38
38
  id
39
39
  end
40
40
  end
41
-
42
41
  end
43
42
  end
@@ -0,0 +1,7 @@
1
+ require 'cleversafe/test_helper'
2
+
3
+ class ConnectionTest < MiniTest::Unit::TestCase
4
+ def test_truth
5
+ assert true
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'cleversafe'
4
+
5
+ require 'minitest/unit'
6
+
7
+ MiniTest::Unit.autorun
@@ -0,0 +1,14 @@
1
+ require 'cleversafe/test_helper'
2
+
3
+ class ObjectTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ connection = Cleversafe::Connection.new('http://test.host')
6
+ @vault = Cleversafe::Vault.new(connection, 'test_vault')
7
+ end
8
+
9
+ def test_name_is_required
10
+ assert_raises ArgumentError do
11
+ Cleversafe::Object.new(@vault, '')
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ require 'cleversafe/test_helper'
2
+
3
+ class VaultTest < MiniTest::Unit::TestCase
4
+ def test_truth
5
+ assert true
6
+ end
7
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 0
8
- - 8
9
- version: 1.0.8
7
+ - 1
8
+ - 3
9
+ version: 1.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Williams
@@ -14,13 +14,25 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2013-04-16 00:00:00 -05:00
17
+ date: 2013-05-14 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: rest-client
21
+ name: json
22
22
  prerelease: false
23
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
+ - !ruby/object:Gem::Dependency
33
+ name: rest-client
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
24
36
  requirements:
25
37
  - - ~>
26
38
  - !ruby/object:Gem::Version
@@ -30,7 +42,31 @@ dependencies:
30
42
  - 7
31
43
  version: 1.6.7
32
44
  type: :runtime
33
- version_requirements: *id001
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ type: :development
57
+ version_requirements: *id003
58
+ - !ruby/object:Gem::Dependency
59
+ name: minitest
60
+ prerelease: false
61
+ requirement: &id004 !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ type: :development
69
+ version_requirements: *id004
34
70
  description:
35
71
  email: john@37signals.com
36
72
  executables: []
@@ -40,7 +76,6 @@ extensions: []
40
76
  extra_rdoc_files: []
41
77
 
42
78
  files:
43
- - ./cleversafe-1.0.7.gem
44
79
  - ./cleversafe.gemspec
45
80
  - ./Gemfile
46
81
  - ./Gemfile.lock
@@ -51,6 +86,10 @@ files:
51
86
  - ./lib/cleversafe.rb
52
87
  - ./Rakefile
53
88
  - ./README.md
89
+ - ./test/connection_test.rb
90
+ - ./test/lib/cleversafe/test_helper.rb
91
+ - ./test/object_test.rb
92
+ - ./test/vault_test.rb
54
93
  has_rdoc: true
55
94
  homepage:
56
95
  licenses: []
data/cleversafe-1.0.7.gem DELETED
Binary file