storage 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +215 -97
- data/lib/storage.rb +8 -7
- data/lib/storage/config.rb +6 -1
- data/lib/storage/strategies.rb +2 -2
- data/lib/storage/strategies/file_system.rb +6 -0
- data/lib/storage/strategies/s3.rb +47 -23
- data/lib/storage/version.rb +2 -2
- data/spec/spec_helper.rb +4 -3
- data/spec/storage/strategies/file_system_spec.rb +7 -7
- data/spec/storage/strategies/s3_spec.rb +60 -49
- data/spec/storage_spec.rb +17 -10
- data/storage.gemspec +5 -9
- metadata +49 -66
- data/spec/storage/config_spec.rb +0 -10
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fd2bec2d0dd25eba612ea984b394c8fc4322a019
|
4
|
+
data.tar.gz: 4387f67ab04388059956ec41a410d3793ddc2581
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4f248beff7dbb3229a2fbc1f3c7f46f3884c19e5dc7b05696e81c55ec20af960ba506f98d57751f2126bf6e5a19cf092f8499a26c02a460129422edd4d74b506
|
7
|
+
data.tar.gz: b621accb8cd68e00218d2b33430e5146d4f9f3d4c486fee9dc2d341f3fd84da4a47a39d33141f489b0e22cd620c13675d043a8a14dc9e93e6f08dad6786524e6
|
data/Gemfile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
source "
|
1
|
+
source "https://rubygems.org"
|
2
2
|
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,133 +1,251 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
storage (0.
|
5
|
-
|
4
|
+
storage (0.2.0)
|
5
|
+
fog
|
6
6
|
|
7
7
|
GEM
|
8
|
-
remote:
|
8
|
+
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
CFPropertyList (2.3.1)
|
11
|
+
actionmailer (3.2.22)
|
12
|
+
actionpack (= 3.2.22)
|
13
|
+
mail (~> 2.5.4)
|
14
|
+
actionpack (3.2.22)
|
15
|
+
activemodel (= 3.2.22)
|
16
|
+
activesupport (= 3.2.22)
|
16
17
|
builder (~> 3.0.0)
|
17
18
|
erubis (~> 2.7.0)
|
18
|
-
|
19
|
-
rack (~> 1.
|
20
|
-
rack-cache (~> 1.
|
21
|
-
rack-mount (~> 0.8.2)
|
19
|
+
journey (~> 1.0.4)
|
20
|
+
rack (~> 1.4.5)
|
21
|
+
rack-cache (~> 1.2)
|
22
22
|
rack-test (~> 0.6.1)
|
23
|
-
sprockets (~> 2.
|
24
|
-
activemodel (3.
|
25
|
-
activesupport (= 3.
|
23
|
+
sprockets (~> 2.2.1)
|
24
|
+
activemodel (3.2.22)
|
25
|
+
activesupport (= 3.2.22)
|
26
26
|
builder (~> 3.0.0)
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
arel (~> 2.2.1)
|
27
|
+
activerecord (3.2.22)
|
28
|
+
activemodel (= 3.2.22)
|
29
|
+
activesupport (= 3.2.22)
|
30
|
+
arel (~> 3.0.2)
|
32
31
|
tzinfo (~> 0.3.29)
|
33
|
-
activeresource (3.
|
34
|
-
activemodel (= 3.
|
35
|
-
activesupport (= 3.
|
36
|
-
activesupport (3.
|
32
|
+
activeresource (3.2.22)
|
33
|
+
activemodel (= 3.2.22)
|
34
|
+
activesupport (= 3.2.22)
|
35
|
+
activesupport (3.2.22)
|
36
|
+
i18n (~> 0.6, >= 0.6.4)
|
37
37
|
multi_json (~> 1.0)
|
38
|
-
arel (
|
39
|
-
|
38
|
+
arel (3.0.3)
|
39
|
+
ast (2.0.0)
|
40
|
+
astrolabe (1.3.0)
|
41
|
+
parser (>= 2.2.0.pre.3, < 3.0)
|
42
|
+
awesome_print (1.6.1)
|
43
|
+
builder (3.0.4)
|
44
|
+
byebug (4.0.5)
|
45
|
+
columnize (= 0.9.0)
|
46
|
+
coderay (1.1.0)
|
47
|
+
columnize (0.9.0)
|
48
|
+
diff-lcs (1.2.5)
|
49
|
+
erubis (2.7.0)
|
50
|
+
excon (0.45.3)
|
51
|
+
fission (0.5.0)
|
52
|
+
CFPropertyList (~> 2.2)
|
53
|
+
fog (1.31.0)
|
54
|
+
fog-atmos
|
55
|
+
fog-aws (~> 0.0)
|
56
|
+
fog-brightbox (~> 0.4)
|
57
|
+
fog-core (~> 1.30)
|
58
|
+
fog-ecloud
|
59
|
+
fog-google (>= 0.0.2)
|
60
|
+
fog-json
|
61
|
+
fog-local
|
62
|
+
fog-powerdns (>= 0.1.1)
|
63
|
+
fog-profitbricks
|
64
|
+
fog-radosgw (>= 0.0.2)
|
65
|
+
fog-riakcs
|
66
|
+
fog-sakuracloud (>= 0.0.4)
|
67
|
+
fog-serverlove
|
68
|
+
fog-softlayer
|
69
|
+
fog-storm_on_demand
|
70
|
+
fog-terremark
|
71
|
+
fog-vmfusion
|
72
|
+
fog-voxel
|
73
|
+
fog-xml (~> 0.1.1)
|
74
|
+
ipaddress (~> 0.5)
|
75
|
+
nokogiri (~> 1.5, >= 1.5.11)
|
76
|
+
fog-atmos (0.1.0)
|
77
|
+
fog-core
|
78
|
+
fog-xml
|
79
|
+
fog-aws (0.6.0)
|
80
|
+
fog-core (~> 1.27)
|
81
|
+
fog-json (~> 1.0)
|
82
|
+
fog-xml (~> 0.1)
|
83
|
+
ipaddress (~> 0.8)
|
84
|
+
fog-brightbox (0.7.2)
|
85
|
+
fog-core (~> 1.22)
|
86
|
+
fog-json
|
87
|
+
inflecto (~> 0.0.2)
|
88
|
+
fog-core (1.31.1)
|
40
89
|
builder
|
90
|
+
excon (~> 0.45)
|
91
|
+
formatador (~> 0.2)
|
41
92
|
mime-types
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
93
|
+
net-scp (~> 1.1)
|
94
|
+
net-ssh (>= 2.1.3)
|
95
|
+
fog-ecloud (0.2.0)
|
96
|
+
fog-core
|
97
|
+
fog-xml
|
98
|
+
fog-google (0.0.6)
|
99
|
+
fog-core
|
100
|
+
fog-json
|
101
|
+
fog-xml
|
102
|
+
fog-json (1.0.2)
|
103
|
+
fog-core (~> 1.0)
|
104
|
+
multi_json (~> 1.10)
|
105
|
+
fog-local (0.2.1)
|
106
|
+
fog-core (~> 1.27)
|
107
|
+
fog-powerdns (0.1.1)
|
108
|
+
fog-core (~> 1.27)
|
109
|
+
fog-json (~> 1.0)
|
110
|
+
fog-xml (~> 0.1)
|
111
|
+
fog-profitbricks (0.0.3)
|
112
|
+
fog-core
|
113
|
+
fog-xml
|
114
|
+
nokogiri
|
115
|
+
fog-radosgw (0.0.4)
|
116
|
+
fog-core (>= 1.21.0)
|
117
|
+
fog-json
|
118
|
+
fog-xml (>= 0.0.1)
|
119
|
+
fog-riakcs (0.1.0)
|
120
|
+
fog-core
|
121
|
+
fog-json
|
122
|
+
fog-xml
|
123
|
+
fog-sakuracloud (1.0.1)
|
124
|
+
fog-core
|
125
|
+
fog-json
|
126
|
+
fog-serverlove (0.1.2)
|
127
|
+
fog-core
|
128
|
+
fog-json
|
129
|
+
fog-softlayer (0.4.7)
|
130
|
+
fog-core
|
131
|
+
fog-json
|
132
|
+
fog-storm_on_demand (0.1.1)
|
133
|
+
fog-core
|
134
|
+
fog-json
|
135
|
+
fog-terremark (0.1.0)
|
136
|
+
fog-core
|
137
|
+
fog-xml
|
138
|
+
fog-vmfusion (0.1.0)
|
139
|
+
fission
|
140
|
+
fog-core
|
141
|
+
fog-voxel (0.1.0)
|
142
|
+
fog-core
|
143
|
+
fog-xml
|
144
|
+
fog-xml (0.1.2)
|
145
|
+
fog-core
|
146
|
+
nokogiri (~> 1.5, >= 1.5.11)
|
147
|
+
formatador (0.2.5)
|
148
|
+
hike (1.2.3)
|
149
|
+
i18n (0.7.0)
|
150
|
+
inflecto (0.0.2)
|
151
|
+
ipaddress (0.8.0)
|
152
|
+
journey (1.0.4)
|
153
|
+
json (1.8.3)
|
154
|
+
mail (2.5.4)
|
53
155
|
mime-types (~> 1.16)
|
54
156
|
treetop (~> 1.4.8)
|
55
|
-
method_source (0.
|
56
|
-
|
57
|
-
|
58
|
-
multi_json (1.
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
157
|
+
method_source (0.8.2)
|
158
|
+
mime-types (1.25.1)
|
159
|
+
mini_portile (0.6.2)
|
160
|
+
multi_json (1.11.1)
|
161
|
+
net-scp (1.2.1)
|
162
|
+
net-ssh (>= 2.6.5)
|
163
|
+
net-ssh (2.9.2)
|
164
|
+
nokogiri (1.6.6.2)
|
165
|
+
mini_portile (~> 0.6.0)
|
166
|
+
parser (2.3.0.pre.2)
|
167
|
+
ast (>= 1.1, < 3.0)
|
168
|
+
polyglot (0.3.5)
|
169
|
+
pry (0.10.1)
|
170
|
+
coderay (~> 1.1.0)
|
171
|
+
method_source (~> 0.8.1)
|
172
|
+
slop (~> 3.4)
|
173
|
+
pry-byebug (3.1.0)
|
174
|
+
byebug (~> 4.0)
|
175
|
+
pry (~> 0.10)
|
176
|
+
pry-meta (0.0.10)
|
177
|
+
awesome_print
|
178
|
+
pry
|
179
|
+
pry-byebug
|
180
|
+
pry-remote
|
181
|
+
pry-remote (0.1.8)
|
182
|
+
pry (~> 0.9)
|
183
|
+
slop (~> 3.0)
|
184
|
+
rack (1.4.7)
|
185
|
+
rack-cache (1.2)
|
68
186
|
rack (>= 0.4)
|
69
|
-
rack-
|
70
|
-
rack (>= 1.0.0)
|
71
|
-
rack-ssl (1.3.2)
|
187
|
+
rack-ssl (1.3.4)
|
72
188
|
rack
|
73
|
-
rack-test (0.6.
|
189
|
+
rack-test (0.6.3)
|
74
190
|
rack (>= 1.0)
|
75
|
-
rails (3.
|
76
|
-
actionmailer (= 3.
|
77
|
-
actionpack (= 3.
|
78
|
-
activerecord (= 3.
|
79
|
-
activeresource (= 3.
|
80
|
-
activesupport (= 3.
|
191
|
+
rails (3.2.22)
|
192
|
+
actionmailer (= 3.2.22)
|
193
|
+
actionpack (= 3.2.22)
|
194
|
+
activerecord (= 3.2.22)
|
195
|
+
activeresource (= 3.2.22)
|
196
|
+
activesupport (= 3.2.22)
|
81
197
|
bundler (~> 1.0)
|
82
|
-
railties (= 3.
|
83
|
-
railties (3.
|
84
|
-
actionpack (= 3.
|
85
|
-
activesupport (= 3.
|
198
|
+
railties (= 3.2.22)
|
199
|
+
railties (3.2.22)
|
200
|
+
actionpack (= 3.2.22)
|
201
|
+
activesupport (= 3.2.22)
|
86
202
|
rack-ssl (~> 1.3.2)
|
87
203
|
rake (>= 0.8.7)
|
88
204
|
rdoc (~> 3.4)
|
89
|
-
thor (
|
90
|
-
|
91
|
-
|
205
|
+
thor (>= 0.14.6, < 2.0)
|
206
|
+
rainbow (2.0.0)
|
207
|
+
rake (10.4.2)
|
208
|
+
rdoc (3.12.2)
|
92
209
|
json (~> 1.4)
|
93
|
-
rspec (
|
94
|
-
rspec-core (~>
|
95
|
-
rspec-expectations (~>
|
96
|
-
rspec-mocks (~>
|
97
|
-
rspec-core (
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
sexp_processor (3.0.7)
|
109
|
-
slop (2.1.0)
|
110
|
-
sprockets (2.0.2)
|
210
|
+
rspec (3.3.0)
|
211
|
+
rspec-core (~> 3.3.0)
|
212
|
+
rspec-expectations (~> 3.3.0)
|
213
|
+
rspec-mocks (~> 3.3.0)
|
214
|
+
rspec-core (3.3.1)
|
215
|
+
rspec-support (~> 3.3.0)
|
216
|
+
rspec-expectations (3.3.0)
|
217
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
218
|
+
rspec-support (~> 3.3.0)
|
219
|
+
rspec-mocks (3.3.1)
|
220
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
221
|
+
rspec-support (~> 3.3.0)
|
222
|
+
rspec-support (3.3.0)
|
223
|
+
slop (3.6.0)
|
224
|
+
sprockets (2.2.3)
|
111
225
|
hike (~> 1.2)
|
226
|
+
multi_json (~> 1.0)
|
112
227
|
rack (~> 1.0)
|
113
228
|
tilt (~> 1.1, != 1.3.0)
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
229
|
+
thor (0.19.1)
|
230
|
+
tilt (1.4.1)
|
231
|
+
transpec (3.1.0)
|
232
|
+
activesupport (>= 3.0, < 5.0)
|
233
|
+
astrolabe (~> 1.2)
|
234
|
+
bundler (~> 1.3)
|
235
|
+
json (~> 1.8)
|
236
|
+
parser (~> 2.2)
|
237
|
+
rainbow (>= 1.99.1, < 3.0)
|
238
|
+
treetop (1.4.15)
|
118
239
|
polyglot
|
119
240
|
polyglot (>= 0.3.1)
|
120
|
-
tzinfo (0.3.
|
121
|
-
xml-simple (1.1.1)
|
241
|
+
tzinfo (0.3.44)
|
122
242
|
|
123
243
|
PLATFORMS
|
124
244
|
ruby
|
125
245
|
|
126
246
|
DEPENDENCIES
|
127
|
-
|
128
|
-
nokogiri (~> 1.4.4)
|
129
|
-
pry
|
247
|
+
pry-meta
|
130
248
|
rails (~> 3.1)
|
131
|
-
rspec
|
132
|
-
sqlite3 (~> 1.3.3)
|
249
|
+
rspec
|
133
250
|
storage!
|
251
|
+
transpec
|
data/lib/storage.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require "ostruct"
|
2
|
-
require "fileutils"
|
3
|
-
require "
|
4
|
-
require "aws/s3"
|
2
|
+
require "fileutils"
|
3
|
+
require "fog"
|
5
4
|
|
6
5
|
module Storage
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
require "storage/errors"
|
7
|
+
require "storage/config"
|
8
|
+
require "storage/strategies"
|
9
|
+
require "storage/version"
|
10
10
|
|
11
11
|
# Set up the storage options.
|
12
12
|
#
|
@@ -16,8 +16,9 @@ module Storage
|
|
16
16
|
#
|
17
17
|
# Check Storage::Config for available options.
|
18
18
|
#
|
19
|
-
def self.setup
|
19
|
+
def self.setup
|
20
20
|
yield Config
|
21
|
+
strategy.prepare!
|
21
22
|
end
|
22
23
|
|
23
24
|
# A shortcut to the current strategy.
|
data/lib/storage/config.rb
CHANGED
@@ -29,8 +29,13 @@ module Storage
|
|
29
29
|
# Override setter so we can automatically define the strategy class
|
30
30
|
# based on its registered name.
|
31
31
|
def self.strategy=(strategy)
|
32
|
-
@strategy_class =
|
32
|
+
@strategy_class = get_class_for_strategy(strategy)
|
33
33
|
@strategy = strategy
|
34
34
|
end
|
35
|
+
|
36
|
+
def self.get_class_for_strategy(strategy)
|
37
|
+
Storage::Strategies::STRATEGIES[strategy].split('::')
|
38
|
+
.reduce(Object) {|ns, class_name| ns.const_get(class_name) }
|
39
|
+
end
|
35
40
|
end
|
36
41
|
end
|
data/lib/storage/strategies.rb
CHANGED
@@ -3,57 +3,81 @@ module Storage
|
|
3
3
|
module S3
|
4
4
|
extend self
|
5
5
|
|
6
|
+
MissingBucket = Class.new(StandardError)
|
7
|
+
|
8
|
+
def connection
|
9
|
+
@connection ||= Fog::Storage.new({
|
10
|
+
:provider => 'AWS',
|
11
|
+
:aws_access_key_id => Storage::Config.access_key,
|
12
|
+
:aws_secret_access_key => Storage::Config.secret_key
|
13
|
+
})
|
14
|
+
end
|
15
|
+
|
16
|
+
def prepare!
|
17
|
+
disconnect!
|
18
|
+
end
|
19
|
+
|
6
20
|
def connect!
|
7
|
-
AWS::S3::Base.establish_connection!({
|
8
|
-
:access_key_id => Storage::Config.access_key,
|
9
|
-
:secret_access_key => Storage::Config.secret_key
|
10
|
-
}) unless AWS::S3::Base.connected?
|
11
21
|
end
|
12
22
|
|
13
23
|
def disconnect!
|
14
|
-
|
24
|
+
@connection = nil
|
15
25
|
end
|
16
26
|
|
17
27
|
def get(file, options = {})
|
18
|
-
|
28
|
+
expires = options.fetch(:expires, Time.now.to_i + 3600)
|
19
29
|
object = find_object(file, options)
|
20
|
-
|
21
|
-
rescue AWS::S3::NoSuchKey, AWS::S3::NoSuchBucket
|
22
|
-
raise Storage::MissingFileError
|
30
|
+
object.public_url || object.url(expires)
|
23
31
|
end
|
24
32
|
|
25
33
|
def store(file, options = {})
|
26
|
-
connect!
|
27
34
|
object = find_object(file, options) rescue nil
|
35
|
+
raise FileAlreadyExistsError if object
|
28
36
|
|
29
|
-
|
30
|
-
|
31
|
-
bucket = find_bucket_or_create(options[:bucket])
|
37
|
+
bucket = find_bucket_or_create(options.fetch(:bucket))
|
32
38
|
file = File.open(file, "rb") unless file.respond_to?(:read) && !file.kind_of?(Pathname)
|
33
|
-
|
39
|
+
|
40
|
+
create_object(bucket, file, options)
|
34
41
|
end
|
35
42
|
|
36
43
|
def remove(file, options = {})
|
37
|
-
connect!
|
38
44
|
object = find_object(file, options)
|
39
|
-
object.
|
40
|
-
rescue AWS::S3::NoSuchKey, AWS::S3::NoSuchBucket
|
41
|
-
raise Storage::MissingFileError
|
45
|
+
object.destroy
|
42
46
|
end
|
43
47
|
|
44
48
|
def find_bucket(name)
|
45
|
-
|
49
|
+
connection.directories.get(name)
|
50
|
+
end
|
51
|
+
|
52
|
+
def find_bucket!(name)
|
53
|
+
find_bucket(name) || raise(MissingBucket)
|
54
|
+
end
|
55
|
+
|
56
|
+
def create_bucket(name)
|
57
|
+
connection.directories.create(
|
58
|
+
:key => name,
|
59
|
+
:public => false
|
60
|
+
)
|
61
|
+
end
|
62
|
+
|
63
|
+
def create_object(bucket, file, options)
|
64
|
+
bucket.files.create({
|
65
|
+
:key => options.fetch(:name),
|
66
|
+
:body => file,
|
67
|
+
:public => (options[:public] || options[:access] != :public_read)
|
68
|
+
})
|
46
69
|
end
|
47
70
|
|
48
71
|
def find_object(file, options = {})
|
49
72
|
path = options.fetch(:name, file)
|
50
|
-
|
73
|
+
bucket = find_bucket!(options.fetch(:bucket))
|
74
|
+
bucket.files.get(path) || raise(MissingFileError)
|
75
|
+
rescue MissingBucket
|
76
|
+
raise MissingFileError
|
51
77
|
end
|
52
78
|
|
53
79
|
def find_bucket_or_create(name)
|
54
|
-
|
55
|
-
bucket ||= AWS::S3::Bucket.create(name)
|
56
|
-
bucket
|
80
|
+
find_bucket(name) || create_bucket(name)
|
57
81
|
end
|
58
82
|
end
|
59
83
|
end
|
data/lib/storage/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
require "bundler"
|
2
|
-
Bundler.setup
|
1
|
+
require "bundler/setup"
|
3
2
|
Bundler.require(:default, :development)
|
4
3
|
|
5
4
|
require "storage"
|
@@ -9,8 +8,10 @@ TMP = Pathname.new(File.expand_path(File.dirname(__FILE__) + "/tmp"))
|
|
9
8
|
RESOURCES = Pathname.new(File.expand_path(File.dirname(__FILE__) + "/resources"))
|
10
9
|
|
11
10
|
RSpec.configure do |config|
|
12
|
-
|
11
|
+
cleaner = proc do
|
13
12
|
FileUtils.rm_rf(TMP) rescue nil
|
14
13
|
FileUtils.mkdir_p(TMP) rescue nil
|
15
14
|
end
|
15
|
+
|
16
|
+
config.before(:each, &cleaner)
|
16
17
|
end
|
@@ -15,21 +15,21 @@ describe Storage::Strategies::FileSystem do
|
|
15
15
|
handler = File.open(@source)
|
16
16
|
Storage.store(handler, :name => "lorem.txt")
|
17
17
|
|
18
|
-
File.
|
19
|
-
File.read(@destiny).
|
18
|
+
expect(File).to be_file(@destiny)
|
19
|
+
expect(File.read(@destiny)).to eq(File.read(@source))
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should save a file using a path" do
|
23
23
|
Storage.store(@source, :name => "lorem.txt")
|
24
24
|
|
25
|
-
File.
|
26
|
-
File.read(@destiny).
|
25
|
+
expect(File).to be_file(@destiny)
|
26
|
+
expect(File.read(@destiny)).to eq(File.read(@source))
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should remove an existing file" do
|
30
30
|
Storage.store(@source, :name => "lorem.txt")
|
31
|
-
Storage.remove("lorem.txt").
|
32
|
-
File.
|
31
|
+
expect(Storage.remove("lorem.txt")).to be_truthy
|
32
|
+
expect(File).not_to be_file(@destiny)
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should raise when trying to removing an unexesting file" do
|
@@ -40,7 +40,7 @@ describe Storage::Strategies::FileSystem do
|
|
40
40
|
|
41
41
|
it "should retrieve an existing file" do
|
42
42
|
Storage.store(@source, :name => "lorem.txt")
|
43
|
-
Storage.get("lorem.txt").
|
43
|
+
expect(Storage.get("lorem.txt")).to eq(File.expand_path(TMP.join("lorem.txt")))
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should raise when trying to retrieve an unexesting file" do
|
@@ -2,13 +2,13 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe Storage::Strategies::S3 do
|
4
4
|
before do
|
5
|
+
@adapter = Storage::Strategies::S3
|
5
6
|
@source = RESOURCES.join("file.txt")
|
6
7
|
@destiny = TMP.join("lorem.txt")
|
7
|
-
@
|
8
|
+
@connection = double("connection")
|
9
|
+
@bucket = double("bucket")
|
8
10
|
|
9
|
-
|
10
|
-
AWS::S3::Bucket.stub :find => @bucket
|
11
|
-
AWS::S3::S3Object.stub :store
|
11
|
+
allow(@adapter).to receive(:connection).and_return(@connection)
|
12
12
|
|
13
13
|
Storage.setup do |c|
|
14
14
|
c.strategy = :s3
|
@@ -17,84 +17,95 @@ describe Storage::Strategies::S3 do
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
it "should establish connection" do
|
21
|
-
options = {:access_key_id => "abc", :secret_access_key => "123"}
|
22
|
-
AWS::S3::Base.should_receive(:establish_connection!).with(options)
|
23
|
-
|
24
|
-
Storage::Strategies::S3.connect!
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should not reconnect when a connection is already established" do
|
28
|
-
AWS::S3::Base.should_receive(:connected?).and_return(true)
|
29
|
-
AWS::S3::Base.should_not_receive(:establish_connection!)
|
30
|
-
|
31
|
-
Storage::Strategies::S3.connect!
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should disconnect when connection is established" do
|
35
|
-
AWS::S3::Base.should_receive(:connected?).and_return(true)
|
36
|
-
AWS::S3::Base.should_receive(:disconnect!)
|
37
|
-
Storage::Strategies::S3.disconnect!
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should ignore disconnection when connection is not established" do
|
41
|
-
AWS::S3::Base.should_receive(:connected?).and_return(false)
|
42
|
-
AWS::S3::Base.should_not_receive(:disconnect!)
|
43
|
-
Storage::Strategies::S3.disconnect!
|
44
|
-
end
|
45
|
-
|
46
20
|
it "should save a file using file handler" do
|
47
21
|
handler = File.open(@source)
|
48
|
-
|
22
|
+
|
23
|
+
expect(@connection).to receive_message_chain('directories.get').with('files').and_return(@bucket)
|
24
|
+
expect(@adapter).to receive_message_chain('find_object').and_return(nil)
|
25
|
+
expect(@bucket).to receive_message_chain('files.create').with(:key => 'lorem.txt', :body => handler, :public => true)
|
26
|
+
|
49
27
|
Storage.store(handler, :name => "lorem.txt", :bucket => "files")
|
50
28
|
end
|
51
29
|
|
52
30
|
it "should save a file using a path" do
|
53
|
-
|
31
|
+
expect(@connection).to receive_message_chain('directories.get').with('files').and_return(@bucket)
|
32
|
+
expect(@adapter).to receive_message_chain('find_object').and_return(nil)
|
33
|
+
expect(@bucket).to receive_message_chain('files.create').with(:key => 'lorem.txt', :body => kind_of(File), :public => true)
|
34
|
+
|
54
35
|
Storage.store(@source, :name => "lorem.txt", :bucket => "files")
|
55
36
|
end
|
56
37
|
|
57
38
|
it "should remove an existing file" do
|
58
|
-
object =
|
59
|
-
|
60
|
-
|
39
|
+
object = double("object")
|
40
|
+
expect(@connection).to receive_message_chain('directories.get').with('files').and_return(@bucket)
|
41
|
+
expect(@bucket).to receive_message_chain('files.get').with('lorem.txt').and_return(object)
|
42
|
+
expect(object).to receive(:destroy).and_return(true)
|
61
43
|
|
62
|
-
Storage.remove("lorem.txt", :bucket => "files").
|
44
|
+
expect(Storage.remove("lorem.txt", :bucket => "files")).to be_truthy
|
63
45
|
end
|
64
46
|
|
65
47
|
it "should raise when trying to removing an unexesting file" do
|
66
|
-
|
48
|
+
expect(@connection).to receive_message_chain('directories.get').with('files').and_return(@bucket)
|
49
|
+
expect(@bucket).to receive_message_chain('files.get').with('lorem.txt').and_return(nil)
|
67
50
|
|
68
51
|
expect {
|
69
52
|
Storage.remove("lorem.txt", :bucket => "files")
|
70
53
|
}.to raise_error(Storage::MissingFileError)
|
71
54
|
end
|
72
55
|
|
73
|
-
it "should retrieve an existing file" do
|
74
|
-
object =
|
56
|
+
it "should retrieve an existing file (public url)" do
|
57
|
+
object = double("object", public_url: 'PUBLIC_URL')
|
58
|
+
expect(@connection).to receive_message_chain('directories.get').with('files').and_return(@bucket)
|
59
|
+
expect(@bucket).to receive_message_chain('files.get').with('lorem.txt').and_return(object)
|
75
60
|
|
76
|
-
|
77
|
-
|
61
|
+
expect(Storage.get("lorem.txt", :bucket => "files")).to eq('PUBLIC_URL')
|
62
|
+
end
|
78
63
|
|
79
|
-
|
64
|
+
it "should retrieve an existing file (private url)" do
|
65
|
+
object = double("object", public_url: nil)
|
66
|
+
|
67
|
+
expect(object).to receive_message_chain('url').with(Time.now.to_i + 3600).and_return('PRIVATE_URL')
|
68
|
+
expect(@connection).to receive_message_chain('directories.get').with('files').and_return(@bucket)
|
69
|
+
expect(@bucket).to receive_message_chain('files.get').with('lorem.txt').and_return(object)
|
70
|
+
|
71
|
+
expect(Storage.get("lorem.txt", :bucket => "files")).to eq('PRIVATE_URL')
|
80
72
|
end
|
81
73
|
|
82
|
-
it "should raise when trying to retrieve an
|
83
|
-
|
84
|
-
|
74
|
+
it "should raise when trying to retrieve an missing file" do
|
75
|
+
expect(@connection).to receive_message_chain('directories.get').with('files').and_return(@bucket)
|
76
|
+
expect(@bucket).to receive_message_chain('files.get').with('lorem.txt').and_return(nil)
|
85
77
|
|
86
78
|
expect {
|
87
79
|
Storage.get("lorem.txt", :bucket => "files")
|
88
80
|
}.to raise_error(Storage::MissingFileError)
|
89
81
|
end
|
90
82
|
|
83
|
+
it "should raise when trying to retrieve an missing bucket" do
|
84
|
+
expect(@connection).to receive_message_chain('directories.get').with('files').and_return(nil)
|
85
|
+
|
86
|
+
expect {
|
87
|
+
Storage.get("lorem.txt", :bucket => "files")
|
88
|
+
}.to raise_error(Storage::MissingFileError)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should create a bucket and trying to store a file on a missing bucket" do
|
92
|
+
expect(@adapter).to receive_message_chain('find_object').and_return(nil)
|
93
|
+
allow(@connection).to receive_message_chain('directories.get').and_return(nil)
|
94
|
+
expect(@connection).to receive_message_chain('directories.create').with(key: 'files', public: false).and_return(@bucket)
|
95
|
+
|
96
|
+
@bucket.as_null_object
|
97
|
+
|
98
|
+
Storage.store(@source, :name => 'lorem.txt', :bucket => "files")
|
99
|
+
end
|
100
|
+
|
91
101
|
it "should raise when saving a file that already exists" do
|
92
|
-
object =
|
93
|
-
|
94
|
-
|
102
|
+
object = double("object")
|
103
|
+
|
104
|
+
expect(@connection).to receive_message_chain('directories.get').with('files').and_return(@bucket)
|
105
|
+
expect(@bucket).to receive_message_chain('files.get').with('lorem.txt').and_return(object)
|
95
106
|
|
96
107
|
expect {
|
97
|
-
Storage.store(@source,
|
108
|
+
Storage.store(@source, :name => 'lorem.txt', :bucket => 'files')
|
98
109
|
}.to raise_error(Storage::FileAlreadyExistsError)
|
99
110
|
end
|
100
111
|
end
|
data/spec/storage_spec.rb
CHANGED
@@ -2,44 +2,51 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe Storage do
|
4
4
|
it "should return the strategy" do
|
5
|
-
@strategy =
|
5
|
+
@strategy = double("strategy")
|
6
6
|
Storage::Config.strategy_class = @strategy
|
7
7
|
|
8
|
-
Storage.strategy.
|
8
|
+
expect(Storage.strategy).to be(@strategy)
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should return the config" do
|
12
|
-
Storage::Strategies::S3.
|
12
|
+
allow(Storage::Strategies::S3).to receive :prepare!
|
13
13
|
|
14
14
|
Storage.setup do |config|
|
15
15
|
config.strategy = :s3
|
16
|
-
config.
|
16
|
+
expect(config).to be(Storage::Config)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
+
it "should set strategy class based on its name" do
|
21
|
+
Storage::Config.strategy_class = nil
|
22
|
+
Storage::Config.strategy = :s3
|
23
|
+
|
24
|
+
expect(Storage::Config.strategy_class).to eq(Storage::Strategies::S3)
|
25
|
+
end
|
26
|
+
|
20
27
|
it "prepare strategy after setting its configuration" do
|
21
|
-
Storage::Strategies::S3.
|
28
|
+
expect(Storage::Strategies::S3).to receive(:prepare!).once
|
22
29
|
Storage.setup {|config| config.strategy = :s3}
|
23
30
|
end
|
24
31
|
|
25
32
|
context "delegation" do
|
26
33
|
before do
|
27
|
-
@strategy =
|
28
|
-
Storage.
|
34
|
+
@strategy = double("strategy")
|
35
|
+
expect(Storage).to receive(:strategy).and_return(@strategy)
|
29
36
|
end
|
30
37
|
|
31
38
|
it "should delegate save method" do
|
32
|
-
@strategy.
|
39
|
+
expect(@strategy).to receive(:store).with("some/file")
|
33
40
|
Storage.store "some/file"
|
34
41
|
end
|
35
42
|
|
36
43
|
it "should delegate destroy method" do
|
37
|
-
@strategy.
|
44
|
+
expect(@strategy).to receive(:remove).with("some/file")
|
38
45
|
Storage.remove "some/file"
|
39
46
|
end
|
40
47
|
|
41
48
|
it "should delegate get method" do
|
42
|
-
@strategy.
|
49
|
+
expect(@strategy).to receive(:get).with("some/file")
|
43
50
|
Storage.get "some/file"
|
44
51
|
end
|
45
52
|
end
|
data/storage.gemspec
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
|
2
|
-
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "storage/version"
|
1
|
+
require "./lib/storage/version"
|
4
2
|
|
5
3
|
Gem::Specification.new do |s|
|
6
4
|
s.name = "storage"
|
@@ -17,12 +15,10 @@ Gem::Specification.new do |s|
|
|
17
15
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
16
|
s.require_paths = ["lib"]
|
19
17
|
|
20
|
-
s.add_dependency "
|
18
|
+
s.add_dependency "fog"
|
21
19
|
|
22
20
|
s.add_development_dependency "rails" , "~> 3.1"
|
23
|
-
s.add_development_dependency "
|
24
|
-
s.add_development_dependency "
|
25
|
-
s.add_development_dependency "
|
26
|
-
s.add_development_dependency "sqlite3" , "~> 1.3.3"
|
27
|
-
s.add_development_dependency "pry"
|
21
|
+
s.add_development_dependency "rspec"
|
22
|
+
s.add_development_dependency "transpec"
|
23
|
+
s.add_development_dependency "pry-meta"
|
28
24
|
end
|
metadata
CHANGED
@@ -1,94 +1,86 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: storage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Nando Vieira
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-06-26 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement:
|
17
|
-
none: false
|
14
|
+
name: fog
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version: 0
|
19
|
+
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
25
27
|
- !ruby/object:Gem::Dependency
|
26
28
|
name: rails
|
27
|
-
requirement:
|
28
|
-
none: false
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
29
30
|
requirements:
|
30
|
-
- - ~>
|
31
|
+
- - "~>"
|
31
32
|
- !ruby/object:Gem::Version
|
32
33
|
version: '3.1'
|
33
34
|
type: :development
|
34
35
|
prerelease: false
|
35
|
-
version_requirements:
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: fakeweb
|
38
|
-
requirement: &70300994120320 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
37
|
requirements:
|
41
|
-
- - ~>
|
38
|
+
- - "~>"
|
42
39
|
- !ruby/object:Gem::Version
|
43
|
-
version:
|
44
|
-
type: :development
|
45
|
-
prerelease: false
|
46
|
-
version_requirements: *70300994120320
|
40
|
+
version: '3.1'
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
|
-
name: rspec
|
49
|
-
requirement:
|
50
|
-
none: false
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
51
44
|
requirements:
|
52
|
-
- -
|
45
|
+
- - ">="
|
53
46
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
47
|
+
version: '0'
|
55
48
|
type: :development
|
56
49
|
prerelease: false
|
57
|
-
version_requirements:
|
58
|
-
- !ruby/object:Gem::Dependency
|
59
|
-
name: nokogiri
|
60
|
-
requirement: &70300994117700 !ruby/object:Gem::Requirement
|
61
|
-
none: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
62
51
|
requirements:
|
63
|
-
- -
|
52
|
+
- - ">="
|
64
53
|
- !ruby/object:Gem::Version
|
65
|
-
version:
|
66
|
-
type: :development
|
67
|
-
prerelease: false
|
68
|
-
version_requirements: *70300994117700
|
54
|
+
version: '0'
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
71
|
-
requirement:
|
72
|
-
none: false
|
56
|
+
name: transpec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
73
58
|
requirements:
|
74
|
-
- -
|
59
|
+
- - ">="
|
75
60
|
- !ruby/object:Gem::Version
|
76
|
-
version:
|
61
|
+
version: '0'
|
77
62
|
type: :development
|
78
63
|
prerelease: false
|
79
|
-
version_requirements:
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
80
69
|
- !ruby/object:Gem::Dependency
|
81
|
-
name: pry
|
82
|
-
requirement:
|
83
|
-
none: false
|
70
|
+
name: pry-meta
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
84
72
|
requirements:
|
85
|
-
- -
|
73
|
+
- - ">="
|
86
74
|
- !ruby/object:Gem::Version
|
87
75
|
version: '0'
|
88
76
|
type: :development
|
89
77
|
prerelease: false
|
90
|
-
version_requirements:
|
91
|
-
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: 'This gem provides a simple API for multiple storage backends. Supported
|
92
84
|
storages: Amazon S3 and FileSystem.'
|
93
85
|
email:
|
94
86
|
- fnando.vieira@gmail.com
|
@@ -96,8 +88,8 @@ executables: []
|
|
96
88
|
extensions: []
|
97
89
|
extra_rdoc_files: []
|
98
90
|
files:
|
99
|
-
- .gitignore
|
100
|
-
- .rspec
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
101
93
|
- Gemfile
|
102
94
|
- Gemfile.lock
|
103
95
|
- README.rdoc
|
@@ -111,46 +103,37 @@ files:
|
|
111
103
|
- lib/storage/version.rb
|
112
104
|
- spec/resources/file.txt
|
113
105
|
- spec/spec_helper.rb
|
114
|
-
- spec/storage/config_spec.rb
|
115
106
|
- spec/storage/strategies/file_system_spec.rb
|
116
107
|
- spec/storage/strategies/s3_spec.rb
|
117
108
|
- spec/storage_spec.rb
|
118
109
|
- storage.gemspec
|
119
110
|
homepage: http://github.com/fnando/storage
|
120
111
|
licenses: []
|
112
|
+
metadata: {}
|
121
113
|
post_install_message:
|
122
114
|
rdoc_options: []
|
123
115
|
require_paths:
|
124
116
|
- lib
|
125
117
|
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
-
none: false
|
127
118
|
requirements:
|
128
|
-
- -
|
119
|
+
- - ">="
|
129
120
|
- !ruby/object:Gem::Version
|
130
121
|
version: '0'
|
131
|
-
segments:
|
132
|
-
- 0
|
133
|
-
hash: -2661529604855007369
|
134
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
-
none: false
|
136
123
|
requirements:
|
137
|
-
- -
|
124
|
+
- - ">="
|
138
125
|
- !ruby/object:Gem::Version
|
139
126
|
version: '0'
|
140
|
-
segments:
|
141
|
-
- 0
|
142
|
-
hash: -2661529604855007369
|
143
127
|
requirements: []
|
144
128
|
rubyforge_project:
|
145
|
-
rubygems_version:
|
129
|
+
rubygems_version: 2.4.6
|
146
130
|
signing_key:
|
147
|
-
specification_version:
|
148
|
-
summary:
|
131
|
+
specification_version: 4
|
132
|
+
summary: 'This gem provides a simple API for multiple storage backends. Supported
|
149
133
|
storages: Amazon S3 and FileSystem.'
|
150
134
|
test_files:
|
151
135
|
- spec/resources/file.txt
|
152
136
|
- spec/spec_helper.rb
|
153
|
-
- spec/storage/config_spec.rb
|
154
137
|
- spec/storage/strategies/file_system_spec.rb
|
155
138
|
- spec/storage/strategies/s3_spec.rb
|
156
139
|
- spec/storage_spec.rb
|
data/spec/storage/config_spec.rb
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Storage::Config do
|
4
|
-
it "should set strategy class based on its name" do
|
5
|
-
Storage::Config.strategy_class = nil
|
6
|
-
Storage::Config.strategy = :s3
|
7
|
-
|
8
|
-
Storage::Config.strategy_class.should == Storage::Strategies::S3
|
9
|
-
end
|
10
|
-
end
|