dragonfly 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of dragonfly might be problematic. Click here for more details.

data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.4
1
+ 0.3.5
data/dragonfly.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dragonfly}
8
- s.version = "0.3.4"
8
+ s.version = "0.3.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mark Evans"]
12
- s.date = %q{2009-12-16}
12
+ s.date = %q{2009-12-17}
13
13
  s.email = %q{mark@new-bamboo.co.uk}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -61,6 +61,7 @@ Gem::Specification.new do |s|
61
61
  "lib/dragonfly/data_storage/base.rb",
62
62
  "lib/dragonfly/data_storage/base64_data_store.rb",
63
63
  "lib/dragonfly/data_storage/file_data_store.rb",
64
+ "lib/dragonfly/data_storage/s3data_store.rb",
64
65
  "lib/dragonfly/data_storage/transparent_data_store.rb",
65
66
  "lib/dragonfly/delegatable.rb",
66
67
  "lib/dragonfly/delegator.rb",
@@ -94,6 +95,7 @@ Gem::Specification.new do |s|
94
95
  "spec/dragonfly/configurable_spec.rb",
95
96
  "spec/dragonfly/data_storage/data_store_spec.rb",
96
97
  "spec/dragonfly/data_storage/file_data_store_spec.rb",
98
+ "spec/dragonfly/data_storage/s3_data_store_spec.rb",
97
99
  "spec/dragonfly/delegatable_spec.rb",
98
100
  "spec/dragonfly/delegator_spec.rb",
99
101
  "spec/dragonfly/encoding/r_magick_encoder_spec.rb",
@@ -133,6 +135,7 @@ Gem::Specification.new do |s|
133
135
  "spec/dragonfly/configurable_spec.rb",
134
136
  "spec/dragonfly/data_storage/data_store_spec.rb",
135
137
  "spec/dragonfly/data_storage/file_data_store_spec.rb",
138
+ "spec/dragonfly/data_storage/s3_data_store_spec.rb",
136
139
  "spec/dragonfly/delegatable_spec.rb",
137
140
  "spec/dragonfly/delegator_spec.rb",
138
141
  "spec/dragonfly/encoding/r_magick_encoder_spec.rb",
@@ -11,12 +11,7 @@ module Dragonfly
11
11
 
12
12
  def store(temp_object)
13
13
 
14
- suffix = if temp_object.name.blank?
15
- 'file'
16
- else
17
- temp_object.name.sub(/\.[^.]*?$/, '')
18
- end
19
- relative_path = relative_storage_path(suffix)
14
+ relative_path = relative_storage_path(temp_object.basename || 'file')
20
15
 
21
16
  begin
22
17
  while File.exist?(storage_path = absolute_storage_path(relative_path))
@@ -0,0 +1,64 @@
1
+ require 'aws/s3'
2
+
3
+ module Dragonfly
4
+ module DataStorage
5
+
6
+ class S3DataStore < Base
7
+
8
+ include Configurable
9
+
10
+ configurable_attr :bucket_name
11
+ configurable_attr :access_key_id
12
+ configurable_attr :secret_access_key
13
+
14
+ def connect!
15
+ AWS::S3::Base.establish_connection!(
16
+ :access_key_id => access_key_id,
17
+ :secret_access_key => secret_access_key
18
+ )
19
+ end
20
+
21
+ def create_bucket!
22
+ AWS::S3::Bucket.create(bucket_name)
23
+ end
24
+
25
+ def store(temp_object)
26
+ uid = generate_uid(temp_object.basename || 'file')
27
+ ensure_initialized
28
+ AWS::S3::S3Object.store(uid, temp_object.file, bucket_name)
29
+ uid
30
+ end
31
+
32
+ def retrieve(uid)
33
+ ensure_initialized
34
+ AWS::S3::S3Object.value(uid, bucket_name)
35
+ rescue AWS::S3::NoSuchKey => e
36
+ raise DataNotFound, "#{e} - #{uid}"
37
+ end
38
+
39
+ def destroy(uid)
40
+ ensure_initialized
41
+ AWS::S3::S3Object.delete(uid, bucket_name)
42
+ rescue AWS::S3::NoSuchKey => e
43
+ raise DataNotFound, "#{e} - #{uid}"
44
+ end
45
+
46
+ private
47
+
48
+ def ensure_initialized
49
+ unless @initialized
50
+ connect!
51
+ create_bucket!
52
+ @initialized = true
53
+ end
54
+ end
55
+
56
+ def generate_uid(suffix)
57
+ time = Time.now
58
+ "#{time.strftime '%Y/%m/%d/%H/%M/%S'}/#{rand(1000)}/#{suffix}"
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+ end
@@ -17,8 +17,6 @@ module Dragonfly
17
17
  initialize_from_object!(obj)
18
18
  end
19
19
 
20
- attr_accessor :name
21
-
22
20
  def modify_self!(obj)
23
21
  unless obj == self
24
22
  reset!
@@ -35,11 +33,12 @@ module Dragonfly
35
33
  if @tempfile
36
34
  @tempfile
37
35
  elsif initialized_tempfile
36
+ initialized_tempfile.open
38
37
  @tempfile = initialized_tempfile
39
38
  elsif initialized_data
40
39
  tempfile = Tempfile.new('dragonfly')
41
40
  tempfile.write(initialized_data)
42
- tempfile.close
41
+ tempfile.open
43
42
  @tempfile = tempfile
44
43
  elsif initialized_file
45
44
  @tempfile = copy_to_tempfile(initialized_file)
@@ -60,6 +59,17 @@ module Dragonfly
60
59
  end
61
60
  end
62
61
 
62
+ attr_writer :name
63
+
64
+ def name
65
+ @name unless @name.blank?
66
+ end
67
+
68
+ def basename
69
+ return unless name
70
+ name.sub(/\.[^.]+$/,'')
71
+ end
72
+
63
73
  def ext
64
74
  return unless name
65
75
  bits = name.split('.')
@@ -123,7 +133,6 @@ module Dragonfly
123
133
 
124
134
  def copy_to_tempfile(file)
125
135
  tempfile = Tempfile.new('dragonfly')
126
- tempfile.close
127
136
  FileUtils.cp File.expand_path(file.path), tempfile.path
128
137
  tempfile
129
138
  end
@@ -10,7 +10,8 @@ describe "data_store", :shared => true do
10
10
 
11
11
  describe "store" do
12
12
  it "should return a unique identifier for each storage" do
13
- @data_store.store(@temp_object).should_not == @data_store.store(@temp_object)
13
+ temp_object2 = Dragonfly::TempObject.new('gollum')
14
+ @data_store.store(@temp_object).should_not == @data_store.store(temp_object2)
14
15
  end
15
16
  end
16
17
 
@@ -37,11 +38,6 @@ describe "data_store", :shared => true do
37
38
  }.should raise_error(Dragonfly::DataStorage::DataNotFound)
38
39
  end
39
40
 
40
- it "should raise an error if the data doesn't exist" do
41
- lambda{
42
- @data_store.destroy('gooble/gubbub')
43
- }.should raise_error(Dragonfly::DataStorage::DataNotFound)
44
- end
45
41
  end
46
42
 
47
43
  end
@@ -110,7 +110,13 @@ describe Dragonfly::DataStorage::FileDataStore do
110
110
  end
111
111
 
112
112
  describe "destroying" do
113
-
113
+
114
+ it "should raise an error if the data doesn't exist" do
115
+ lambda{
116
+ @data_store.destroy('gooble/gubbub')
117
+ }.should raise_error(Dragonfly::DataStorage::DataNotFound)
118
+ end
119
+
114
120
  it "should prune empty directories when destroying" do
115
121
  uid = @data_store.store(@temp_object)
116
122
  @data_store.destroy(uid)
@@ -0,0 +1,50 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+ require File.dirname(__FILE__) + '/data_store_spec'
3
+
4
+ describe Dragonfly::DataStorage::S3DataStore do
5
+
6
+ # Uncomment this to test it with an actual internet connection
7
+ # describe "common data_store behaviour" do
8
+ #
9
+ # before(:each) do
10
+ # @data_store = Dragonfly::DataStorage::S3DataStore.new
11
+ # @data_store.configure do |d|
12
+ # d.bucket_name = 'dragonfly_test'
13
+ # d.access_key_id = 'xxxxxxxxxx'
14
+ # d.secret_access_key = 'xxxxxxxxxx'
15
+ # end
16
+ # end
17
+ #
18
+ # it_should_behave_like 'data_store'
19
+ #
20
+ # end
21
+
22
+ describe "specific s3_data_store behaviour" do
23
+ before(:each) do
24
+ @data_store = Dragonfly::DataStorage::S3DataStore.new
25
+ @data_store.configure do |d|
26
+ d.bucket_name = 'dragonfly_test'
27
+ d.access_key_id = 'my_key_id'
28
+ d.secret_access_key = 'my_secret_access_key'
29
+ end
30
+ @temp_object = Dragonfly::TempObject.new('gollum')
31
+ AWS::S3::Base.stub!(:establish_connection!).with(
32
+ :access_key_id => @data_store.access_key_id,
33
+ :secret_access_key => @data_store.secret_access_key
34
+ )
35
+ AWS::S3::Bucket.stub!(:create).with(@data_store.bucket_name)
36
+ AWS::S3::S3Object.stub!(:store).with(anything, anything, @data_store.bucket_name)
37
+ AWS::S3::S3Object.stub!(:value).with(anything, @data_store.bucket_name)
38
+ AWS::S3::S3Object.stub!(:delete).with(anything, @data_store.bucket_name)
39
+ end
40
+
41
+
42
+ describe "store" do
43
+ it "should return a unique identifier for each storage" do
44
+ temp_object2 = Dragonfly::TempObject.new('gollum')
45
+ @data_store.store(@temp_object).should_not == @data_store.store(temp_object2)
46
+ end
47
+ end
48
+ end
49
+
50
+ end
@@ -7,7 +7,7 @@ describe Dragonfly::TempObject do
7
7
  def new_tempfile(data = File.read(SAMPLES_DIR + '/round.gif'))
8
8
  tempfile = Tempfile.new('test')
9
9
  tempfile.write(data)
10
- tempfile.close
10
+ tempfile.rewind
11
11
  tempfile
12
12
  end
13
13
 
@@ -73,12 +73,12 @@ describe Dragonfly::TempObject do
73
73
  end
74
74
  end
75
75
  describe "file" do
76
- it "should lazily create a closed tempfile" do
76
+ it "should lazily create an unclosed tempfile" do
77
77
  @temp_object.file.should be_a(Tempfile)
78
- @temp_object.file.should be_closed
78
+ @temp_object.file.should_not be_closed
79
79
  end
80
80
  it "should contain the correct data" do
81
- @temp_object.file.open.read.should == @gif_string
81
+ @temp_object.file.read.should == @gif_string
82
82
  end
83
83
  end
84
84
  describe "each" do
@@ -102,9 +102,9 @@ describe Dragonfly::TempObject do
102
102
  end
103
103
  end
104
104
  describe "file" do
105
- it "should return the closed tempfile" do
105
+ it "should return the unclosed tempfile" do
106
106
  @temp_object.file.should be_a(Tempfile)
107
- @temp_object.file.should be_closed
107
+ @temp_object.file.should_not be_closed
108
108
  @temp_object.file.path.should == @tempfile.path
109
109
  end
110
110
  end
@@ -131,12 +131,12 @@ describe Dragonfly::TempObject do
131
131
  end
132
132
  end
133
133
  describe "file" do
134
- it "should lazily return a closed tempfile" do
134
+ it "should lazily return an unclosed tempfile" do
135
135
  @temp_object.file.should be_a(Tempfile)
136
- @temp_object.file.should be_closed
136
+ @temp_object.file.should_not be_closed
137
137
  end
138
138
  it "should contain the correct data" do
139
- @temp_object.file.open.read.should == @file.read
139
+ @temp_object.file.read.should == @file.read
140
140
  end
141
141
  end
142
142
  describe "each" do
@@ -191,7 +191,7 @@ describe Dragonfly::TempObject do
191
191
  end
192
192
  it "should modify itself when the new object is a tempfile" do
193
193
  tempfile = new_tempfile
194
- data = tempfile.open.read
194
+ data = tempfile.read
195
195
  @temp_object.modify_self!(tempfile)
196
196
  @temp_object.data.should == data
197
197
  end
@@ -237,6 +237,11 @@ describe Dragonfly::TempObject do
237
237
  temp_object = Dragonfly::TempObject.new(file)
238
238
  temp_object.name.should == 'round.gif'
239
239
  end
240
+ it "should still be nil if set to empty string" do
241
+ temp_object = Dragonfly::TempObject.new('sdf')
242
+ temp_object.name = ''
243
+ temp_object.name.should be_nil
244
+ end
240
245
  end
241
246
 
242
247
  describe "ext" do
@@ -257,6 +262,24 @@ describe Dragonfly::TempObject do
257
262
  end
258
263
  end
259
264
 
265
+ describe "basename" do
266
+ before(:each) do
267
+ @temp_object = Dragonfly::TempObject.new('asfsadf')
268
+ end
269
+ it "should use the correct basename from name" do
270
+ @temp_object.name = 'hello.there.mate'
271
+ @temp_object.basename.should == 'hello.there'
272
+ end
273
+ it "should be the name if it has no ext" do
274
+ @temp_object.name = 'hello'
275
+ @temp_object.basename.should == 'hello'
276
+ end
277
+ it "should be nil if name is nil" do
278
+ @temp_object.name = nil
279
+ @temp_object.basename.should be_nil
280
+ end
281
+ end
282
+
260
283
  describe "to_file" do
261
284
 
262
285
  describe "common behaviour for to_file", :shared => true do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dragonfly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Evans
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-16 00:00:00 +00:00
12
+ date: 2009-12-17 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -77,6 +77,7 @@ files:
77
77
  - lib/dragonfly/data_storage/base.rb
78
78
  - lib/dragonfly/data_storage/base64_data_store.rb
79
79
  - lib/dragonfly/data_storage/file_data_store.rb
80
+ - lib/dragonfly/data_storage/s3data_store.rb
80
81
  - lib/dragonfly/data_storage/transparent_data_store.rb
81
82
  - lib/dragonfly/delegatable.rb
82
83
  - lib/dragonfly/delegator.rb
@@ -110,6 +111,7 @@ files:
110
111
  - spec/dragonfly/configurable_spec.rb
111
112
  - spec/dragonfly/data_storage/data_store_spec.rb
112
113
  - spec/dragonfly/data_storage/file_data_store_spec.rb
114
+ - spec/dragonfly/data_storage/s3_data_store_spec.rb
113
115
  - spec/dragonfly/delegatable_spec.rb
114
116
  - spec/dragonfly/delegator_spec.rb
115
117
  - spec/dragonfly/encoding/r_magick_encoder_spec.rb
@@ -171,6 +173,7 @@ test_files:
171
173
  - spec/dragonfly/configurable_spec.rb
172
174
  - spec/dragonfly/data_storage/data_store_spec.rb
173
175
  - spec/dragonfly/data_storage/file_data_store_spec.rb
176
+ - spec/dragonfly/data_storage/s3_data_store_spec.rb
174
177
  - spec/dragonfly/delegatable_spec.rb
175
178
  - spec/dragonfly/delegator_spec.rb
176
179
  - spec/dragonfly/encoding/r_magick_encoder_spec.rb