cabinet 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cabinet.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cabinet (0.0.1)
5
+ fog (~> 0.3.30)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ builder (3.0.0)
11
+ diff-lcs (1.1.2)
12
+ excon (0.3.6)
13
+ fog (0.3.34)
14
+ builder
15
+ excon (>= 0.3.6)
16
+ formatador (>= 0.0.16)
17
+ json
18
+ mime-types
19
+ net-ssh (>= 2.0.23)
20
+ nokogiri (>= 1.4.4)
21
+ ruby-hmac
22
+ formatador (0.0.16)
23
+ json (1.4.6)
24
+ mime-types (1.16)
25
+ net-ssh (2.0.23)
26
+ nokogiri (1.4.4)
27
+ rspec (2.3.0)
28
+ rspec-core (~> 2.3.0)
29
+ rspec-expectations (~> 2.3.0)
30
+ rspec-mocks (~> 2.3.0)
31
+ rspec-core (2.3.1)
32
+ rspec-expectations (2.3.0)
33
+ diff-lcs (~> 1.1.2)
34
+ rspec-mocks (2.3.0)
35
+ ruby-hmac (0.4.0)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ cabinet!
42
+ fog (~> 0.3.30)
43
+ rspec (~> 2.3.0)
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 by Sebastian von Conrad
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,9 @@
1
+ = Cabinet
2
+
3
+ Cabinet is a gem for managing local and remote files through a nice and Ruby-esque interface.
4
+
5
+ More information is coming soon!
6
+
7
+ == License
8
+
9
+ Cabinet is released under the MIT license, which can be found in LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/cabinet.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cabinet/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cabinet"
7
+ s.version = Cabinet::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Sebastian von Conrad"]
10
+ s.email = ["sebastian@vonconrad.com"]
11
+ s.homepage = "http://github.com/vonconrad/cabinet"
12
+ s.summary = %q{}
13
+ s.description = %q{}
14
+
15
+ s.rubyforge_project = "cabinet"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "fog", "~> 0.3.30"
23
+ s.add_development_dependency "rspec", "~> 2.3.0"
24
+ end
@@ -0,0 +1,86 @@
1
+ require 'fog'
2
+
3
+ module Cabinet
4
+ class Cloud
5
+ attr_accessor :connection, :container
6
+
7
+ def initialize(config)
8
+ root_dir = config.delete(:container)
9
+ self.connection = Fog::Rackspace::Storage.new(config)
10
+ self.container = connection.directories.get(root_dir) || connection.directories.create(:key => root_dir)
11
+ end
12
+
13
+ def get(name)
14
+ file(name).body
15
+ end
16
+
17
+ def list(regexp=/.*/)
18
+ container.files.reload
19
+ container.files.select{|f| f.key.match(regexp)}.to_a.map(&:key)
20
+ end
21
+
22
+ def put(name, content)
23
+ begin
24
+ container.files.create(:key => name, :body => content).content_length == content.length
25
+ rescue
26
+ false
27
+ end
28
+ end
29
+
30
+ def delete(name_or_regexp)
31
+ @file = nil
32
+
33
+ container.files.reload
34
+
35
+ if name_or_regexp.class == Regexp
36
+ container.files.select{|f| f.key.match(name_or_regexp)}.each do |file|
37
+ file.destroy
38
+ end
39
+ else
40
+ container.files.get(name_or_regexp).destroy
41
+ end
42
+
43
+ true
44
+ end
45
+
46
+ def copy_to(klass_or_sym, name)
47
+ if klass_or_sym.is_a? Symbol
48
+ c = case klass_or_sym
49
+ when :local
50
+ Local.new('/tmp')
51
+ end
52
+ else
53
+ c = klass_or_sym
54
+ end
55
+
56
+ c.put(name, get(name))
57
+ end
58
+
59
+ def exists?(name)
60
+ !!file(name)
61
+ end
62
+
63
+ def gzip(name, content)
64
+ name = name.gsub(/(.+?)(\.gz)?$/, '\1.gz')
65
+ local = "/tmp/#{name}"
66
+
67
+ File.open(local, 'w') do |f|
68
+ gz = Zlib::GzipWriter.new(f)
69
+ gz.write content
70
+ gz.close
71
+ end
72
+
73
+ put(name, File.read(local)) and File.unlink(local)
74
+ end
75
+
76
+ private
77
+ def file(name)
78
+ if @file && @file.key == name
79
+ @file
80
+ else
81
+ container.files.reload
82
+ @file = container.files.get(name)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,34 @@
1
+ module Cabinet
2
+ class Local
3
+ attr_accessor :dir
4
+
5
+ def initialize(root_dir)
6
+ self.dir = root_dir.chomp('/') + '/'
7
+ end
8
+
9
+ def get(file)
10
+ raise ArgumentError, "The file #{file} does not exist" unless exists?(file)
11
+ File.read(dir + file)
12
+ end
13
+
14
+ def put(file, content)
15
+ File.open(dir + file, 'wb') {|f| f.write(content)} == content.length
16
+ end
17
+
18
+ def delete(file_or_regexp)
19
+ File.delete *Dir.glob(dir + file_or_regexp)
20
+ end
21
+
22
+ def exists?(file)
23
+ File.exists?(dir + file)
24
+ end
25
+
26
+ def gzip(file, content)
27
+ File.open(dir + file, 'w') do |f|
28
+ gz = Zlib::GzipWriter.new(f)
29
+ gz.write content
30
+ gz.close
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Cabinet
2
+ VERSION = "0.1.0"
3
+ end
data/lib/cabinet.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Cabinet; end
2
+
3
+ dir = Pathname(__FILE__).dirname.expand_path
4
+ require dir + 'cabinet/cloud'
5
+ require dir + 'cabinet/local'
@@ -0,0 +1,74 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Cabinet::Cloud do
4
+ before(:all) do
5
+ credentials = YAML.load_file(File.expand_path(File.join(File.dirname(__FILE__), '..', 'cloud_credentials.yml')))
6
+ @cc = Cabinet::Cloud.new({:container => 'cabinet_test'}.merge(credentials))
7
+ end
8
+
9
+ it "should create file" do
10
+ @cc.put(@@file_name, @@file_content).should == true
11
+ end
12
+
13
+ it "should confirm file exists" do
14
+ @cc.exists?(@@file_name).should == true
15
+ end
16
+
17
+ it "should confirm file does not exist" do
18
+ @cc.exists?(@@file_content).should == false
19
+ end
20
+
21
+ it "should read file" do
22
+ @cc.get(@@file_name).should == @@file_content
23
+ end
24
+
25
+ it "should list files" do
26
+ @cc.list.should include(@@file_name)
27
+ @cc.list(/#{@@file_name}/).should include(@@file_name)
28
+ @cc.list(/#{@@file_content}/).should == []
29
+ end
30
+
31
+ it "should copy file to local filesystem using object" do
32
+ cl = Cabinet::Local.new('/tmp')
33
+ cl.delete(@@file_name) if cl.exists?(@@file_name)
34
+
35
+ @cc.copy_to(cl, @@file_name)
36
+ cl.exists?(@@file_name).should == true
37
+ cl.delete(@@file_name)
38
+ end
39
+
40
+ it "should copy file to local filesystem using symbol" do
41
+ File.unlink("/tmp/#{@@file_name}") if File.exists?("/tmp/#{@@file_name}")
42
+ @cc.copy_to(:local, @@file_name)
43
+ File.exists?("/tmp/#{@@file_name}").should == true
44
+ File.unlink("/tmp/#{@@file_name}")
45
+ end
46
+
47
+ it "should not overwrite file"
48
+
49
+ it "should overwrite file if :force => true"
50
+
51
+ it "should gzip file" do
52
+ gz_file_name = @@file_name + '.gz'
53
+ File.unlink("/tmp/#{gz_file_name}") if File.exists?("/tmp/#{gz_file_name}")
54
+
55
+ @cc.gzip(gz_file_name, @@file_content)
56
+ @cc.copy_to(:local, gz_file_name)
57
+
58
+ Zlib::GzipReader.open("/tmp/#{gz_file_name}") {|gz| gz.read}.should == @@file_content
59
+
60
+ @cc.delete(gz_file_name)
61
+ File.unlink("/tmp/#{gz_file_name}")
62
+ end
63
+
64
+ it "should delete file" do
65
+ @cc.delete(@@file_name)
66
+ @cc.exists?(@@file_name).should == false
67
+ end
68
+
69
+ it "should bulk delete files" do
70
+ (1..3).each {|n| @cc.put("#{@@file_name}.#{n}", @@file_content)}
71
+ @cc.delete(/#{@@file_name}/)
72
+ (1..3).inject([]) {|arr, n| arr << @cc.exists?("#{@@file_name}.#{n}")}.should == [false, false, false]
73
+ end
74
+ end
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Cabinet::Local do
4
+ before(:all) do
5
+ @cl = Cabinet::Local.new('/tmp')
6
+ end
7
+
8
+ it "should create file" do
9
+ @cl.put(@@file_name, @@file_content).should == true
10
+ end
11
+
12
+ it "should confirm file exists" do
13
+ @cl.exists?(@@file_name).should == true
14
+ end
15
+
16
+ it "should confirm file does not exist" do
17
+ @cl.exists?(@@file_content).should == false
18
+ end
19
+
20
+ it "should read file" do
21
+ @cl.get(@@file_name).should == @@file_content
22
+ end
23
+
24
+ it "should list files"
25
+
26
+ it "should not overwrite file"
27
+
28
+ it "should overwrite file if :force => true"
29
+
30
+ it "should gzip file" do
31
+ gz_file_name = @@file_name + '.gz'
32
+ @cl.gzip(gz_file_name, @@file_content)
33
+ Zlib::GzipReader.open("/tmp/#{gz_file_name}") {|gz| gz.read}.should == @@file_content
34
+ @cl.delete(gz_file_name)
35
+ end
36
+
37
+ it "should delete file" do
38
+ @cl.delete(@@file_name)
39
+ @cl.exists?(@@file_name).should == false
40
+ end
41
+
42
+ it "should bulk delete files" do
43
+ (1..3).each {|n| @cl.put("#{@@file_name}.#{n}", @@file_content)}
44
+ @cl.delete("#{@@file_name}*")
45
+ (1..3).inject([]) {|arr, n| arr << @cl.exists?("#{@@file_name}.#{n}")}.should == [false, false, false]
46
+ end
47
+ end
@@ -0,0 +1,2 @@
1
+ :rackspace_username: echann3l
2
+ :rackspace_api_key: 4b529021b4edb4d877581fc82e1ea746
@@ -0,0 +1,8 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'cabinet')
2
+
3
+ RSpec.configure do |config|
4
+ config.before(:suite) do
5
+ @@file_name = 'cabinet.test'
6
+ @@file_content = (0...50).map{('a'..'z').to_a[rand(26)]}.join
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cabinet
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Sebastian von Conrad
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-06 00:00:00 +10:30
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: fog
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 47
30
+ segments:
31
+ - 0
32
+ - 3
33
+ - 30
34
+ version: 0.3.30
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 2
48
+ - 3
49
+ - 0
50
+ version: 2.3.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: ""
54
+ email:
55
+ - sebastian@vonconrad.com
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files: []
61
+
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE
67
+ - README.rdoc
68
+ - Rakefile
69
+ - cabinet.gemspec
70
+ - lib/cabinet.rb
71
+ - lib/cabinet/cloud.rb
72
+ - lib/cabinet/local.rb
73
+ - lib/cabinet/version.rb
74
+ - spec/cabinet/cloud_spec.rb
75
+ - spec/cabinet/local_spec.rb
76
+ - spec/cloud_credentials.yml
77
+ - spec/spec_helper.rb
78
+ has_rdoc: true
79
+ homepage: http://github.com/vonconrad/cabinet
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options: []
84
+
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ requirements: []
106
+
107
+ rubyforge_project: cabinet
108
+ rubygems_version: 1.3.7
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: ""
112
+ test_files:
113
+ - spec/cabinet/cloud_spec.rb
114
+ - spec/cabinet/local_spec.rb
115
+ - spec/cloud_credentials.yml
116
+ - spec/spec_helper.rb