remote_files 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,16 +13,16 @@ module RemoteFiles
13
13
  end
14
14
 
15
15
  def store!(file)
16
- ::File.open(directory + file.identifier, 'w') do |f|
16
+ (directory + file.identifier).open('w') do |f|
17
17
  f.write(file.content)
18
18
  # what about content-type?
19
19
  end
20
20
  end
21
21
 
22
22
  def retrieve!(identifier)
23
- content = File.new(directory + identifier).read
23
+ content = (directory + identifier).read
24
24
 
25
- ::File.new(identifier,
25
+ RemoteFiles::File.new(identifier,
26
26
  :content => content,
27
27
  :stored_in => [self.identifier]
28
28
  # what about content-type? maybe use the mime-types gem?
@@ -32,16 +32,16 @@ module RemoteFiles
32
32
  end
33
33
 
34
34
  def delete!(identifier)
35
- ::File.delete(directory + identifier)
35
+ (directory + identifier).delete
36
36
  rescue Errno::ENOENT => e
37
37
  end
38
38
 
39
39
  def url(identifier)
40
- "file://localhost/#{directory + identifier}"
40
+ "file://localhost#{directory + identifier}"
41
41
  end
42
42
 
43
43
  def url_matcher
44
- @url_matcher ||= /file:\/\/localhost\/#{directory}\/(.*)/
44
+ @url_matcher ||= /file:\/\/localhost#{directory}\/(.*)/
45
45
  end
46
46
  end
47
47
  end
@@ -1,3 +1,3 @@
1
1
  module RemoteFiles
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
data/lib/remote_files.rb CHANGED
@@ -22,6 +22,30 @@ module RemoteFiles
22
22
  STORES_MAP[store_identifier] = store
23
23
  end
24
24
 
25
+ def self.configure(hash)
26
+ hash.each do |store_identifier, config|
27
+ #symbolize_keys!
28
+ config.each { |name, value| config[name.to_sym] = config.delete(name) }
29
+
30
+ #camelize
31
+ type = config[:type].gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } + 'Store'
32
+
33
+ klass = RemoteFiles.const_get(type) rescue nil
34
+ unless klass
35
+ require "remote_files/#{config[:type]}_store"
36
+ klass = RemoteFiles.const_get(type)
37
+ end
38
+
39
+ config.delete(:type)
40
+
41
+ add_store(store_identifier.to_sym, :class => klass, :primary => !!config.delete(:primary)) do |store|
42
+ config.each do |name, value|
43
+ store[name] = value
44
+ end
45
+ end
46
+ end
47
+ end
48
+
25
49
  def self.stores
26
50
  raise "You need to configure add stores to RemoteFiles using 'RemoteFiles.add_store'" if STORES.empty?
27
51
  STORES
@@ -0,0 +1,82 @@
1
+ require_relative 'test_helper'
2
+ require 'remote_files/file_store'
3
+
4
+ describe RemoteFiles::FileStore do
5
+ before do
6
+ @directory = Pathname.new(File.dirname(__FILE__)) + '../tmp'
7
+
8
+ @store = RemoteFiles::FileStore.new(:file)
9
+ @store[:directory] = @directory
10
+ end
11
+
12
+ describe '#store!' do
13
+ before do
14
+ @file = RemoteFiles::File.new('identifier', :content_type => 'text/plain', :content => 'content')
15
+ end
16
+
17
+ it 'should store the file on disk' do
18
+ @store.store!(@file)
19
+
20
+ file_path = @directory + 'identifier'
21
+ file_path.exist?
22
+
23
+ assert_equal 'content', file_path.read
24
+ end
25
+ end
26
+
27
+ describe '#retrieve!' do
28
+ it 'should return a RemoteFiles::File when found' do
29
+ (@store.directory + 'identifier').open('w') do |f|
30
+ f.write('content')
31
+ end
32
+
33
+ file = @store.retrieve!('identifier')
34
+
35
+ file.must_be_instance_of(RemoteFiles::File)
36
+ file.content.must_equal('content')
37
+ # file.content_type.must_equal('text/plain')
38
+ end
39
+
40
+ it 'should raise a RemoteFiles::NotFoundError when not found' do
41
+ (@store.directory + 'identifier').delete rescue nil
42
+ proc { @store.retrieve!('identifier') }.must_raise(RemoteFiles::NotFoundError)
43
+ end
44
+ end
45
+
46
+ describe '#url' do
47
+ it 'should return a file url' do
48
+ @store.url('identifier').must_equal("file://localhost#{@directory}/identifier")
49
+ end
50
+ end
51
+
52
+ describe '#file_from_url' do
53
+ it 'should create a file if the directory matches' do
54
+ file = @store.file_from_url("file://localhost#{@directory}/identifier")
55
+ assert file
56
+ assert_equal 'identifier', file.identifier
57
+
58
+ file = @store.file_from_url("file://localhost#{@directory}_other/identifier")
59
+ assert !file
60
+
61
+ file = @store.file_from_url('https://s3.amazonaws.com/mem/identifier')
62
+ assert !file
63
+ end
64
+ end
65
+
66
+ describe '#delete!' do
67
+ before do
68
+ (@store.directory + 'identifier').open('w') do |f|
69
+ f.write('content')
70
+ end
71
+ end
72
+
73
+ it 'should destroy the file' do
74
+ assert (@store.directory + 'identifier').exist?
75
+
76
+ @store.delete!('identifier')
77
+
78
+ assert !(@store.directory + 'identifier').exist?
79
+ end
80
+ end
81
+
82
+ end
@@ -111,14 +111,14 @@ describe RemoteFiles::FogStore do
111
111
  before { @store[:provider] = 'AWS' }
112
112
 
113
113
  it 'should create a file if the bucket matches' do
114
- file = @store.file_from_url('http://s3-eu-west-1.amazonaws.com/directory/key/on/s3.txt')
114
+ file = @store.file_from_url('http://s3-eu-west-1.amazonaws.com/directory/key/on/cloud.txt')
115
115
  assert file
116
- assert_equal 'key/on/s3.txt', file.identifier
116
+ assert_equal 'key/on/cloud.txt', file.identifier
117
117
 
118
- file = @store.file_from_url('http://s3-eu-west-1.amazonaws.com/other_bucket/key/on/s3.txt')
118
+ file = @store.file_from_url('http://s3-eu-west-1.amazonaws.com/other_bucket/key/on/cloud.txt')
119
119
  assert !file
120
120
 
121
- file = @store.file_from_url('http://storage.cloudfiles.com/directory/key/on/s3.txt')
121
+ file = @store.file_from_url('http://storage.cloudfiles.com/directory/key/on/cloud.txt')
122
122
  assert !file
123
123
  end
124
124
  end
@@ -127,14 +127,14 @@ describe RemoteFiles::FogStore do
127
127
  before { @store[:provider] = 'Rackspace' }
128
128
 
129
129
  it 'should create a file if the container matches' do
130
- file = @store.file_from_url('http://storage.cloudfiles.com/directory/key/on/s3.txt')
130
+ file = @store.file_from_url('http://storage.cloudfiles.com/directory/key/on/cloud.txt')
131
131
  assert file
132
- assert_equal 'key/on/s3.txt', file.identifier
132
+ assert_equal 'key/on/cloud.txt', file.identifier
133
133
 
134
- file = @store.file_from_url('http://storage.cloudfiles.com/other_container/key/on/s3.txt')
134
+ file = @store.file_from_url('http://storage.cloudfiles.com/other_container/key/on/cloud.txt')
135
135
  assert !file
136
136
 
137
- file = @store.file_from_url('http://s3-eu-west-1.amazonaws.com/directory/key/on/s3.txt')
137
+ file = @store.file_from_url('http://s3-eu-west-1.amazonaws.com/directory/key/on/cloud.txt')
138
138
  assert !file
139
139
  end
140
140
  end
@@ -143,7 +143,7 @@ describe RemoteFiles::FogStore do
143
143
  before { @store[:provider] = 'Google' }
144
144
 
145
145
  it 'should raise a RuntimeError' do
146
- proc { @store.file_from_url('http://s3-eu-west-1.amazonaws.com/directory/key/on/s3.txt') }.must_raise(RuntimeError)
146
+ proc { @store.file_from_url('http://s3-eu-west-1.amazonaws.com/directory/key/on/cloud.txt') }.must_raise(RuntimeError)
147
147
  end
148
148
  end
149
149
  end
@@ -0,0 +1,71 @@
1
+ require_relative 'test_helper'
2
+ require 'remote_files/memory_store'
3
+
4
+ describe RemoteFiles::MemoryStore do
5
+ before do
6
+ @store = RemoteFiles::MemoryStore.new(:mem)
7
+ end
8
+
9
+ describe '#store!' do
10
+ before do
11
+ @file = RemoteFiles::File.new('identifier', :content_type => 'text/plain', :content => 'content')
12
+ end
13
+
14
+ it 'should store the file in the memory' do
15
+ @store.store!(@file)
16
+
17
+ assert_equal({:content_type => 'text/plain', :content => 'content'}, @store.data['identifier'])
18
+ end
19
+ end
20
+
21
+ describe '#retrieve!' do
22
+ it 'should return a RemoteFiles::File when found' do
23
+ @store.data['identifier'] = {:content_type => 'text/plain', :content => 'content'}
24
+
25
+ file = @store.retrieve!('identifier')
26
+
27
+ file.must_be_instance_of(RemoteFiles::File)
28
+ file.content.must_equal('content')
29
+ file.content_type.must_equal('text/plain')
30
+ end
31
+
32
+ it 'should raise a RemoteFiles::NotFoundError when not found' do
33
+ proc { @store.retrieve!('identifier') }.must_raise(RemoteFiles::NotFoundError)
34
+ end
35
+ end
36
+
37
+ describe '#url' do
38
+ it 'should return a fake memory url' do
39
+ @store.url('identifier').must_equal('memory://mem/identifier')
40
+ end
41
+ end
42
+
43
+ describe '#file_from_url' do
44
+ it 'should create a file if the store identifier matches' do
45
+ file = @store.file_from_url('memory://mem/identifier')
46
+ assert file
47
+ assert_equal 'identifier', file.identifier
48
+
49
+ file = @store.file_from_url('memory://other_store/identifier')
50
+ assert !file
51
+
52
+ file = @store.file_from_url('https://s3.amazonaws.com/mem/identifier')
53
+ assert !file
54
+ end
55
+ end
56
+
57
+ describe '#delete!' do
58
+ before do
59
+ @store.data['identifier'] = {:content_type => 'text/plain', :content => 'content'}
60
+ end
61
+
62
+ it 'should destroy the file' do
63
+ assert @store.data['identifier']
64
+
65
+ @store.delete!('identifier')
66
+
67
+ assert !@store.data['identifier']
68
+ end
69
+ end
70
+
71
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remote_files
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.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-09-07 00:00:00.000000000 Z
12
+ date: 2012-09-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fog
@@ -124,8 +124,10 @@ files:
124
124
  - lib/remote_files/resque_job.rb
125
125
  - lib/remote_files/version.rb
126
126
  - lib/remote_files.rb
127
+ - test/file_store_test.rb
127
128
  - test/file_test.rb
128
129
  - test/fog_store_test.rb
130
+ - test/memory_store_test.rb
129
131
  - test/remote_files_test.rb
130
132
  - test/resque_job_test.rb
131
133
  - test/test_helper.rb
@@ -144,7 +146,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
144
146
  version: '0'
145
147
  segments:
146
148
  - 0
147
- hash: -3981899794566552461
149
+ hash: 816766500051184913
148
150
  required_rubygems_version: !ruby/object:Gem::Requirement
149
151
  none: false
150
152
  requirements:
@@ -153,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
155
  version: '0'
154
156
  segments:
155
157
  - 0
156
- hash: -3981899794566552461
158
+ hash: 816766500051184913
157
159
  requirements: []
158
160
  rubyforge_project:
159
161
  rubygems_version: 1.8.24
@@ -163,8 +165,10 @@ summary: The purpose of the library is to implement a simple interface for uploa
163
165
  files to multiple backends and to keep the backends in sync, so that your app will
164
166
  keep working when one backend is down.
165
167
  test_files:
168
+ - test/file_store_test.rb
166
169
  - test/file_test.rb
167
170
  - test/fog_store_test.rb
171
+ - test/memory_store_test.rb
168
172
  - test/remote_files_test.rb
169
173
  - test/resque_job_test.rb
170
174
  - test/test_helper.rb