imagery 0.0.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +36 -63
- data/imagery.gemspec +8 -71
- data/lib/imagery.rb +176 -18
- data/lib/imagery/faking.rb +2 -2
- data/lib/imagery/s3.rb +68 -128
- data/lib/imagery/test.rb +5 -5
- data/test/faking.rb +71 -0
- data/test/helper.rb +28 -9
- data/test/imagery.rb +172 -0
- data/test/s3.rb +109 -0
- metadata +22 -91
- data/.document +0 -5
- data/.gitignore +0 -24
- data/Rakefile +0 -56
- data/VERSION +0 -1
- data/lib/imagery/missing.rb +0 -30
- data/lib/imagery/model.rb +0 -217
- data/test/fixtures/lake.jpg +0 -0
- data/test/test_imagery.rb +0 -172
- data/test/test_missing.rb +0 -30
- data/test/test_with_s3.rb +0 -167
data/test/fixtures/lake.jpg
DELETED
Binary file
|
data/test/test_imagery.rb
DELETED
@@ -1,172 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
require 'benchmark'
|
3
|
-
|
4
|
-
class Imagery::Model
|
5
|
-
include Imagery::Faking
|
6
|
-
end
|
7
|
-
|
8
|
-
class TestImagery < Test::Unit::TestCase
|
9
|
-
Photo = Class.new(Struct.new(:id))
|
10
|
-
|
11
|
-
test "optionally yields" do
|
12
|
-
imagery = Imagery.new(Photo.new(1001)) do |i|
|
13
|
-
i.sizes = { medium: ["50x50"] }
|
14
|
-
end
|
15
|
-
|
16
|
-
assert_equal({ medium: ["50x50"], original: ["1920x1200>"] }, imagery.sizes)
|
17
|
-
end
|
18
|
-
|
19
|
-
test "namespace" do
|
20
|
-
assert_equal 'photo', Imagery.new(Photo.new(1001)).namespace
|
21
|
-
end
|
22
|
-
|
23
|
-
test "using a Symbol as a namespace" do
|
24
|
-
assert_equal 'photo', Imagery.new(Photo.new(1001), 1001, :photo).namespace
|
25
|
-
end
|
26
|
-
|
27
|
-
test "key" do
|
28
|
-
assert_equal '1001', Imagery.new(Photo.new(1001)).key
|
29
|
-
end
|
30
|
-
|
31
|
-
test "root_path when root not defined" do
|
32
|
-
imagery = Imagery.new(Photo.new(1001))
|
33
|
-
|
34
|
-
assert_raise Imagery::Model::UndefinedRoot do
|
35
|
-
imagery.file
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
test "root_path when no ROOT_DIR" do
|
40
|
-
imagery = Imagery.new(Photo.new(1001))
|
41
|
-
imagery.root = '/'
|
42
|
-
assert_equal "/public/system/photo/1001/tmp", imagery.send(:tmp)
|
43
|
-
end
|
44
|
-
|
45
|
-
test "root_path when ROOT_DIR defined on imagery" do
|
46
|
-
Imagery::ROOT_DIR = '/root/dir'
|
47
|
-
|
48
|
-
imagery = Imagery.new(Photo.new(1001))
|
49
|
-
assert_equal "/root/dir/public/system/photo/1001/tmp", imagery.send(:tmp)
|
50
|
-
|
51
|
-
Imagery.send :remove_const, :ROOT_DIR
|
52
|
-
end
|
53
|
-
|
54
|
-
test "root_path when ROOT_DIR defined on object" do
|
55
|
-
Object::ROOT_DIR = '/root/dir'
|
56
|
-
|
57
|
-
imagery = Imagery.new(Photo.new(1001))
|
58
|
-
assert_equal "/root/dir/public/system/photo/1001/tmp", imagery.send(:tmp)
|
59
|
-
|
60
|
-
Object.send :remove_const, :ROOT_DIR
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "file" do
|
64
|
-
setup do
|
65
|
-
@imagery = Imagery.new(Photo.new(1001))
|
66
|
-
@imagery.root = '/r/d'
|
67
|
-
@imagery.sizes = { :thumb => ['10x10'] }
|
68
|
-
end
|
69
|
-
|
70
|
-
test "file by default" do
|
71
|
-
assert_equal "/r/d/public/system/photo/1001/original.png", @imagery.file
|
72
|
-
end
|
73
|
-
|
74
|
-
test "file given a size" do
|
75
|
-
assert_equal "/r/d/public/system/photo/1001/thumb.png", @imagery.file(:thumb)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
describe "convert" do
|
80
|
-
setup do
|
81
|
-
@imagery = Imagery.new(Photo.new(1001))
|
82
|
-
@imagery.root = '/r/d'
|
83
|
-
@imagery.sizes = {
|
84
|
-
:thumb => ["56x56^"],
|
85
|
-
:small => ["56x56^", "56x56"]
|
86
|
-
}
|
87
|
-
end
|
88
|
-
|
89
|
-
test "default resize" do
|
90
|
-
expected =
|
91
|
-
"convert /r/d/public/system/photo/1001/tmp -thumbnail '56x56^' " +
|
92
|
-
"-gravity center /r/d/public/system/photo/1001/thumb.png"
|
93
|
-
|
94
|
-
assert_equal expected, @imagery.send(:cmd, :thumb),
|
95
|
-
end
|
96
|
-
|
97
|
-
test "resize with extent param" do
|
98
|
-
expected =
|
99
|
-
"convert /r/d/public/system/photo/1001/tmp -thumbnail '56x56^' " +
|
100
|
-
"-gravity center -extent 56x56 /r/d/public/system/photo/1001/small.png"
|
101
|
-
|
102
|
-
assert_equal expected, @imagery.send(:cmd, :small)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
describe "saving" do
|
107
|
-
setup do
|
108
|
-
@imagery = Imagery.new(Photo.new(1001))
|
109
|
-
@imagery.root = '/tmp'
|
110
|
-
@imagery.sizes = {
|
111
|
-
:thumb => ["56x56^"],
|
112
|
-
:small => ["100x100^", "100x100"],
|
113
|
-
:large => ["200x200>", "200x200"]
|
114
|
-
}
|
115
|
-
end
|
116
|
-
|
117
|
-
teardown do
|
118
|
-
FileUtils.rm_rf '/tmp/public'
|
119
|
-
end
|
120
|
-
|
121
|
-
test "writes all the different geometry sizes normally" do
|
122
|
-
assert @imagery.save(File.open(FIXTURES + '/lake.jpg'))
|
123
|
-
|
124
|
-
assert File.exist?(@imagery.file)
|
125
|
-
assert File.exist?(@imagery.file(:thumb))
|
126
|
-
assert File.exist?(@imagery.file(:small))
|
127
|
-
assert File.exist?(@imagery.file(:large))
|
128
|
-
end
|
129
|
-
|
130
|
-
test "able to delete them" do
|
131
|
-
@imagery.save(File.open(FIXTURES + '/lake.jpg'))
|
132
|
-
@imagery.delete
|
133
|
-
|
134
|
-
assert ! File.exist?(@imagery.file)
|
135
|
-
assert ! File.exist?(@imagery.file(:thumb))
|
136
|
-
assert ! File.exist?(@imagery.file(:small))
|
137
|
-
assert ! File.exist?(@imagery.file(:large))
|
138
|
-
end
|
139
|
-
|
140
|
-
test "when mode == :fake" do
|
141
|
-
time = Benchmark.realtime {
|
142
|
-
Imagery::Model.faked {
|
143
|
-
assert @imagery.save(File.open(FIXTURES + '/lake.jpg'))
|
144
|
-
}
|
145
|
-
}
|
146
|
-
|
147
|
-
assert time < 0.01, "Faked context should run in less than 0.01 secs"
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
describe "url" do
|
152
|
-
setup do
|
153
|
-
@imagery = Imagery.new(Photo.new(1001))
|
154
|
-
@imagery.root = '/tmp'
|
155
|
-
@imagery.sizes = { :small => ["40x40"] }
|
156
|
-
end
|
157
|
-
|
158
|
-
test "original" do
|
159
|
-
assert_equal '/system/photo/1001/original.png', @imagery.url
|
160
|
-
end
|
161
|
-
|
162
|
-
test "small" do
|
163
|
-
assert_equal '/system/photo/1001/small.png', @imagery.url(:small)
|
164
|
-
end
|
165
|
-
|
166
|
-
test "non-existent" do
|
167
|
-
assert_raise Imagery::Model::UnknownSize do
|
168
|
-
@imagery.url(:foo)
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
172
|
-
end
|
data/test/test_missing.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class MissingTest < Test::Unit::TestCase
|
4
|
-
Photo = Class.new(Struct.new(:id))
|
5
|
-
|
6
|
-
test "adding it using extend" do
|
7
|
-
imagery = Imagery.new(Photo.new(1001))
|
8
|
-
imagery.extend Imagery::Missing
|
9
|
-
imagery.existing = ""
|
10
|
-
assert_equal '/missing/photo/original.png', imagery.url
|
11
|
-
end
|
12
|
-
|
13
|
-
class WithMissing < Imagery::Model
|
14
|
-
include Imagery::Missing
|
15
|
-
end
|
16
|
-
|
17
|
-
test "adding it using include" do
|
18
|
-
imagery = WithMissing.new(Photo.new(1001))
|
19
|
-
imagery.existing = ""
|
20
|
-
|
21
|
-
assert_equal '/missing/photo/original.png', imagery.url
|
22
|
-
end
|
23
|
-
|
24
|
-
test "still returns as normal when not missing" do
|
25
|
-
imagery = WithMissing.new(Photo.new(1001))
|
26
|
-
imagery.root = '/tmp'
|
27
|
-
imagery.existing = 'lake.jpg'
|
28
|
-
assert_equal '/system/photo/1001/original.png', imagery.url
|
29
|
-
end
|
30
|
-
end
|
data/test/test_with_s3.rb
DELETED
@@ -1,167 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class WithS3Test < Test::Unit::TestCase
|
4
|
-
class WithS3 < Imagery::Model
|
5
|
-
include Imagery::Faking
|
6
|
-
include Imagery::S3
|
7
|
-
|
8
|
-
s3_bucket "tmp-bucket-name"
|
9
|
-
end
|
10
|
-
|
11
|
-
class NoBucket < Imagery::Model
|
12
|
-
include Imagery::Faking
|
13
|
-
include Imagery::S3
|
14
|
-
end
|
15
|
-
|
16
|
-
class WithS3CustomHost < Imagery::Model
|
17
|
-
include Imagery::Faking
|
18
|
-
include Imagery::S3
|
19
|
-
|
20
|
-
s3_bucket "tmp-bucket-name"
|
21
|
-
s3_host "http://test.host"
|
22
|
-
end
|
23
|
-
|
24
|
-
|
25
|
-
SuperSecretPhoto = Class.new(Struct.new(:id))
|
26
|
-
|
27
|
-
test "default urls" do
|
28
|
-
imagery = WithS3.new(SuperSecretPhoto.new(1001))
|
29
|
-
imagery.root = '/tmp'
|
30
|
-
imagery.sizes = {
|
31
|
-
:thumb => ["56x56^"],
|
32
|
-
:small => ["100x100^", "100x100"],
|
33
|
-
:large => ["200x200>", "200x200"]
|
34
|
-
}
|
35
|
-
|
36
|
-
s = 'http://s3.amazonaws.com/tmp-bucket-name/supersecretphoto/1001/%s.png'
|
37
|
-
|
38
|
-
assert_equal s % 'original', imagery.url
|
39
|
-
assert_equal s % 'original', imagery.url(:original)
|
40
|
-
assert_equal s % 'thumb', imagery.url(:thumb)
|
41
|
-
assert_equal s % 'small', imagery.url(:small)
|
42
|
-
assert_equal s % 'large', imagery.url(:large)
|
43
|
-
end
|
44
|
-
|
45
|
-
test "customized s3_host urls" do
|
46
|
-
imagery = WithS3CustomHost.new(SuperSecretPhoto.new(1001))
|
47
|
-
imagery.root = '/tmp'
|
48
|
-
imagery.sizes = {
|
49
|
-
:thumb => ["56x56^"],
|
50
|
-
:small => ["100x100^", "100x100"],
|
51
|
-
:large => ["200x200>", "200x200"]
|
52
|
-
}
|
53
|
-
|
54
|
-
s = 'http://test.host/tmp-bucket-name/supersecretphoto/1001/%s.png'
|
55
|
-
|
56
|
-
assert_equal s % 'original', imagery.url
|
57
|
-
assert_equal s % 'original', imagery.url(:original)
|
58
|
-
assert_equal s % 'thumb', imagery.url(:thumb)
|
59
|
-
assert_equal s % 'small', imagery.url(:small)
|
60
|
-
assert_equal s % 'large', imagery.url(:large)
|
61
|
-
end
|
62
|
-
|
63
|
-
test "url when no bucket" do
|
64
|
-
imagery = NoBucket.new(SuperSecretPhoto.new(1001))
|
65
|
-
imagery.sizes = {
|
66
|
-
:thumb => ["56x56^"],
|
67
|
-
:small => ["100x100^", "100x100"],
|
68
|
-
:large => ["200x200>", "200x200"]
|
69
|
-
}
|
70
|
-
|
71
|
-
assert_raise Imagery::S3::UndefinedBucket do
|
72
|
-
imagery.url
|
73
|
-
end
|
74
|
-
|
75
|
-
begin
|
76
|
-
imagery.url
|
77
|
-
rescue Imagery::S3::UndefinedBucket => e
|
78
|
-
assert_equal Imagery::S3::Configs::BUCKET_ERROR_MSG, e.message
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
test "urls with a distribution domain" do
|
83
|
-
imagery = WithS3.new(SuperSecretPhoto.new(1001))
|
84
|
-
imagery.root = '/tmp'
|
85
|
-
imagery.sizes = {
|
86
|
-
:thumb => ["56x56^"],
|
87
|
-
:small => ["100x100^", "100x100"],
|
88
|
-
:large => ["200x200>", "200x200"]
|
89
|
-
}
|
90
|
-
|
91
|
-
s = 'http://assets.site.com/supersecretphoto/1001/%s.png'
|
92
|
-
|
93
|
-
WithS3.s3_distribution_domain 'http://assets.site.com'
|
94
|
-
|
95
|
-
assert_equal s % 'original', imagery.url
|
96
|
-
assert_equal s % 'original', imagery.url(:original)
|
97
|
-
assert_equal s % 'thumb', imagery.url(:thumb)
|
98
|
-
assert_equal s % 'small', imagery.url(:small)
|
99
|
-
assert_equal s % 'large', imagery.url(:large)
|
100
|
-
|
101
|
-
WithS3.s3_distribution_domain = nil
|
102
|
-
end
|
103
|
-
|
104
|
-
test "saving and storing" do
|
105
|
-
imagery = WithS3.new(SuperSecretPhoto.new(1001))
|
106
|
-
imagery.root = '/tmp'
|
107
|
-
imagery.sizes = {
|
108
|
-
:thumb => ["56x56^"],
|
109
|
-
:small => ["100x100^", "100x100"],
|
110
|
-
:large => ["200x200>", "200x200"]
|
111
|
-
}
|
112
|
-
|
113
|
-
File.stubs(:open).returns(:contents)
|
114
|
-
|
115
|
-
Imagery::S3::Gateway.expects(:store).with() { |key, io, bucket, hash|
|
116
|
-
key == "supersecretphoto/1001/thumb.png" && bucket == 'tmp-bucket-name' &&
|
117
|
-
hash[:access] == :public_read && hash[:content_type] == 'image/png' &&
|
118
|
-
io == :contents
|
119
|
-
}
|
120
|
-
|
121
|
-
Imagery::S3::Gateway.expects(:store).with() { |key, io, bucket, hash|
|
122
|
-
key == "supersecretphoto/1001/small.png" && bucket == 'tmp-bucket-name' &&
|
123
|
-
hash[:access] == :public_read && hash[:content_type] == 'image/png' &&
|
124
|
-
io == :contents
|
125
|
-
}
|
126
|
-
|
127
|
-
Imagery::S3::Gateway.expects(:store).with() { |key, io, bucket, hash|
|
128
|
-
key == "supersecretphoto/1001/large.png" && bucket == 'tmp-bucket-name' &&
|
129
|
-
hash[:access] == :public_read && hash[:content_type] == 'image/png' &&
|
130
|
-
io == :contents
|
131
|
-
}
|
132
|
-
|
133
|
-
Imagery::S3::Gateway.expects(:store).with() { |key, io, bucket, hash|
|
134
|
-
key == "supersecretphoto/1001/original.png" && bucket == 'tmp-bucket-name' &&
|
135
|
-
hash[:access] == :public_read && hash[:content_type] == 'image/png' &&
|
136
|
-
io == :contents
|
137
|
-
}
|
138
|
-
|
139
|
-
WithS3.faked do
|
140
|
-
assert imagery.save(File.open(FIXTURES + '/lake.jpg'))
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
test "deleting" do
|
145
|
-
imagery = WithS3.new(SuperSecretPhoto.new(1001))
|
146
|
-
imagery.root = '/tmp'
|
147
|
-
imagery.sizes = {
|
148
|
-
:thumb => ["56x56^"],
|
149
|
-
:small => ["100x100^", "100x100"],
|
150
|
-
:large => ["200x200>", "200x200"]
|
151
|
-
}
|
152
|
-
|
153
|
-
Imagery::S3::Gateway.expects(:delete).
|
154
|
-
with('supersecretphoto/1001/thumb.png', 'tmp-bucket-name')
|
155
|
-
|
156
|
-
Imagery::S3::Gateway.expects(:delete).
|
157
|
-
with('supersecretphoto/1001/small.png', 'tmp-bucket-name')
|
158
|
-
|
159
|
-
Imagery::S3::Gateway.expects(:delete).
|
160
|
-
with('supersecretphoto/1001/large.png', 'tmp-bucket-name')
|
161
|
-
|
162
|
-
Imagery::S3::Gateway.expects(:delete).
|
163
|
-
with('supersecretphoto/1001/original.png', 'tmp-bucket-name')
|
164
|
-
|
165
|
-
assert imagery.delete
|
166
|
-
end
|
167
|
-
end
|