remote_files 1.4.2 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,10 @@ module RemoteFiles
14
14
  options[name] = value
15
15
  end
16
16
 
17
+ def directory_name
18
+ raise "You need to implement #{self.class.name}:#directory_name"
19
+ end
20
+
17
21
  def store!(file)
18
22
  raise "You need to implement #{self.class.name}#store!"
19
23
  end
@@ -114,8 +114,7 @@ module RemoteFiles
114
114
  end
115
115
 
116
116
  def synchronize!(file)
117
- file.missing_stores.each do |store_identifier|
118
- store = lookup_store(store_identifier)
117
+ file.missing_stores.each do |store|
119
118
  store.store!(file)
120
119
  file.stored_in << store.identifier
121
120
  end
@@ -37,8 +37,12 @@ module RemoteFiles
37
37
  missing_stores.empty?
38
38
  end
39
39
 
40
+ def stores
41
+ @stored_in.map { |store_id| configuration.lookup_store(store_id) }
42
+ end
43
+
40
44
  def missing_stores
41
- configuration.stores.map(&:identifier) - @stored_in
45
+ configuration.stores - stores
42
46
  end
43
47
 
44
48
  def url(store_identifier = nil)
@@ -55,6 +59,21 @@ module RemoteFiles
55
59
  url(prioritized_stores[0])
56
60
  end
57
61
 
62
+ def retrieve!
63
+ stores.each do |store|
64
+ begin
65
+ file = store.retrieve!(identifier)
66
+ next unless file
67
+ @content = file.content
68
+ @content_type = file.content_type
69
+ return true
70
+ rescue Error => e
71
+ end
72
+ end
73
+
74
+ raise NotFoundError
75
+ end
76
+
58
77
  def store!
59
78
  configuration.store!(self)
60
79
  end
@@ -43,5 +43,9 @@ module RemoteFiles
43
43
  def url_matcher
44
44
  @url_matcher ||= /file:\/\/localhost#{directory}\/(.*)/
45
45
  end
46
+
47
+ def directory_name
48
+ options[:directory].to_s
49
+ end
46
50
  end
47
51
  end
@@ -35,9 +35,9 @@ module RemoteFiles
35
35
  def url(identifier)
36
36
  case options[:provider]
37
37
  when 'AWS'
38
- "https://s3.amazonaws.com/#{options[:directory]}/#{Fog::AWS.escape(identifier)}"
38
+ "https://s3.amazonaws.com/#{directory_name}/#{Fog::AWS.escape(identifier)}"
39
39
  when 'Rackspace'
40
- "https://storage.cloudfiles.com/#{options[:directory]}/#{Fog::Rackspace.escape(identifier, '/')}"
40
+ "https://storage.cloudfiles.com/#{directory_name}/#{Fog::Rackspace.escape(identifier, '/')}"
41
41
  else
42
42
  raise "#{self.class.name}#url was not implemented for the #{options[:provider]} provider"
43
43
  end
@@ -46,9 +46,9 @@ module RemoteFiles
46
46
  def url_matcher
47
47
  @url_matcher ||= case options[:provider]
48
48
  when 'AWS'
49
- /https?:\/\/s3[^\.]*.amazonaws.com\/#{options[:directory]}\/(.*)/
49
+ /https?:\/\/s3[^\.]*.amazonaws.com\/#{directory_name}\/(.*)/
50
50
  when 'Rackspace'
51
- /https?:\/\/storage.cloudfiles.com\/#{options[:directory]}\/(.*)/
51
+ /https?:\/\/storage.cloudfiles.com\/#{directory_name}\/(.*)/
52
52
  else
53
53
  raise "#{self.class.name}#url_matcher was not implemented for the #{options[:provider]} provider"
54
54
  end
@@ -65,6 +65,10 @@ module RemoteFiles
65
65
  @connection ||= Fog::Storage.new(connection_options)
66
66
  end
67
67
 
68
+ def directory_name
69
+ options[:directory]
70
+ end
71
+
68
72
  def directory
69
73
  @directory ||= lookup_directory || create_directory
70
74
  end
@@ -72,12 +76,12 @@ module RemoteFiles
72
76
  protected
73
77
 
74
78
  def lookup_directory
75
- connection.directories.get(options[:directory])
79
+ connection.directories.get(directory_name)
76
80
  end
77
81
 
78
82
  def create_directory
79
83
  connection.directories.new(
80
- :key => options[:directory],
84
+ :key => directory_name,
81
85
  :public => options[:public]
82
86
  ).tap do |dir|
83
87
  dir.save
@@ -34,6 +34,10 @@ module RemoteFiles
34
34
  )
35
35
  end
36
36
 
37
+ def directory_name
38
+ self.identifier.to_s
39
+ end
40
+
37
41
  def delete!(identifier)
38
42
  data.delete(identifier)
39
43
  end
@@ -1,3 +1,3 @@
1
1
  module RemoteFiles
2
- VERSION = '1.4.2'
2
+ VERSION = '1.5.0'
3
3
  end
@@ -79,4 +79,10 @@ describe RemoteFiles::FileStore do
79
79
  end
80
80
  end
81
81
 
82
+ describe '#directory_name' do
83
+ it 'returns the name of the directory' do
84
+ @store.directory_name.must_equal @directory.to_s
85
+ end
86
+ end
87
+
82
88
  end
data/test/file_test.rb CHANGED
@@ -37,10 +37,17 @@ describe RemoteFiles::File do
37
37
  end
38
38
  end
39
39
 
40
+ describe '#stores' do
41
+ it 'should give an array of stores where the file is stored' do
42
+ @file.stored_in.replace([:s3])
43
+ @file.stores.must_equal([@s3])
44
+ end
45
+ end
46
+
40
47
  describe '#missing_stores' do
41
- it 'should give an array of store identifiers where the file is not stored' do
48
+ it 'should give an array of stores where the file is not stored' do
42
49
  @file.stored_in.replace([:s3])
43
- @file.missing_stores.must_equal([:cf])
50
+ @file.missing_stores.must_equal([@cf])
44
51
  end
45
52
  end
46
53
 
@@ -103,4 +110,39 @@ describe RemoteFiles::File do
103
110
  @file.delete!
104
111
  end
105
112
  end
113
+
114
+ describe '#retrieve!' do
115
+ before do
116
+ @file_with_content = RemoteFiles::File.new('identifier', :content => 'content', :content_type => 'content_type')
117
+
118
+ @store = stub
119
+ @file.stubs(:stores).returns([@store])
120
+ end
121
+
122
+ describe 'when the file is found' do
123
+ before do
124
+ @store.expects(:retrieve!).returns(@file_with_content)
125
+ end
126
+
127
+ it 'fills in the content and content_type' do
128
+ @file.content.must_be_nil
129
+ @file.content_type.must_be_nil
130
+
131
+ @file.retrieve!
132
+
133
+ @file.content.must_equal 'content'
134
+ @file.content_type.must_equal 'content_type'
135
+ end
136
+ end
137
+
138
+ describe 'when the file is not found' do
139
+ before do
140
+ @store.expects(:retrieve!).returns(nil)
141
+ end
142
+
143
+ it 'raises a NotFoundError' do
144
+ proc { @file.retrieve! }.must_raise(RemoteFiles::NotFoundError)
145
+ end
146
+ end
147
+ end
106
148
  end
@@ -174,4 +174,10 @@ describe RemoteFiles::FogStore do
174
174
  end
175
175
  end
176
176
 
177
+ describe '#directory_name' do
178
+ it 'returns the name of the directory' do
179
+ @store.directory_name.must_equal 'directory'
180
+ end
181
+ end
182
+
177
183
  end
@@ -68,4 +68,11 @@ describe RemoteFiles::MemoryStore do
68
68
  end
69
69
  end
70
70
 
71
+
72
+ describe '#directory_name' do
73
+ it 'returns the store identifier' do
74
+ @store.directory_name.must_equal 'mem'
75
+ end
76
+ end
77
+
71
78
  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.4.2
4
+ version: 1.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -148,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
148
  version: '0'
149
149
  segments:
150
150
  - 0
151
- hash: -343498225094597126
151
+ hash: 708350776970309851
152
152
  required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  none: false
154
154
  requirements:
@@ -157,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
157
  version: '0'
158
158
  segments:
159
159
  - 0
160
- hash: -343498225094597126
160
+ hash: 708350776970309851
161
161
  requirements: []
162
162
  rubyforge_project:
163
163
  rubygems_version: 1.8.24