imagery 1.0.0.rc1 → 1.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "imagery"
3
- s.version = "1.0.0.rc1"
3
+ s.version = "1.0.0.rc2"
4
4
  s.summary = "POROS + GraphicsMagick."
5
5
  s.description = "Clean & light interface around GraphicsMagick."
6
6
  s.authors = ["Cyril David"]
@@ -13,7 +13,7 @@ class Imagery
13
13
  attr :prefix
14
14
 
15
15
  # A unique id for the image.
16
- attr :key
16
+ attr :id
17
17
 
18
18
  # A hash of name => tuple pairs. The name describes the size, e.g. `small`.
19
19
  #
@@ -32,9 +32,9 @@ class Imagery
32
32
  # are placed in the `Core` module. Imagery::S3 demonstrates overriding
33
33
  # in action.
34
34
  module Core
35
- def initialize(prefix, key = nil, sizes = {})
35
+ def initialize(prefix, id = nil, sizes = {})
36
36
  @prefix = prefix.to_s
37
- @key = key.to_s
37
+ @id = id.to_s if id
38
38
  @sizes = sizes
39
39
  @original = :original # Used as the filename for the raw image.
40
40
  @ext = :jpg # We default to jpg for the image format.
@@ -42,16 +42,16 @@ class Imagery
42
42
 
43
43
  # Returns the url for a given size, which defaults to `:original`.
44
44
  #
45
- # If the key is nil, a missing path is returned.
45
+ # If the id is nil, a missing path is returned.
46
46
  def url(file = @original)
47
- return "/missing/#{prefix}/#{ext(file)}" if key.to_s.empty?
47
+ return "/missing/#{prefix}/#{ext(file)}" if id.to_s.empty?
48
48
 
49
- "/#{prefix}/#{key}/#{ext(file)}"
49
+ "/#{prefix}/#{id}/#{ext(file)}"
50
50
  end
51
51
 
52
52
  # Accepts an `IO` object, typically taken from an input[type=file].
53
53
  #
54
- # The second optional `key` argument is used when you want to force
54
+ # The second optional `id` argument is used when you want to force
55
55
  # a new resource, useful in conjunction with cloudfront / high cache
56
56
  # scenarios where updating an existing image won't suffice.
57
57
  #
@@ -68,17 +68,18 @@ class Imagery
68
68
  # { original: im.url, thumb: im.url(:thumb) }.to_json
69
69
  # end
70
70
  #
71
- def save(io, key = nil)
71
+ def save(io, id = nil)
72
72
  GM.identify(io) or raise(InvalidImage)
73
73
 
74
74
  # We delete the existing object iff:
75
- # 1. A key was passed
76
- # 2. The key passed is different from the existing key.
77
- delete if key && key != self.key
75
+ # 1. An id was passed
76
+ # 2. We have an existing id already.
77
+ # 3. The id passed is different from the existing id.
78
+ delete if id && self.id && id != self.id
78
79
 
79
- # Now we can assign the new key passed, with the assurance that the
80
- # old key has been deleted and won't be used anymore.
81
- @key = key.to_s if key
80
+ # Now we can assign the new id passed, with the assurance that the
81
+ # old id has been deleted and won't be used anymore.
82
+ @id = id.to_s if id
82
83
 
83
84
  # Ensure that the path to all images is created.
84
85
  FileUtils.mkdir_p(root)
@@ -95,8 +96,10 @@ class Imagery
95
96
  end
96
97
 
97
98
  # A very simple and destructive method. Deletes the entire folder
98
- # for the current prefix/key combination.
99
+ # for the current prefix/id combination.
99
100
  def delete
101
+ return if not id
102
+
100
103
  FileUtils.rm_rf(root)
101
104
  end
102
105
  end
@@ -109,7 +112,7 @@ class Imagery
109
112
  end
110
113
 
111
114
  def root(*args)
112
- self.class.root(prefix, key, *args)
115
+ self.class.root(prefix, id, *args)
113
116
  end
114
117
 
115
118
  def self.root(*args)
@@ -4,7 +4,7 @@ class Imagery
4
4
  module S3
5
5
  def self.included(imagery)
6
6
  imagery.extend Config
7
-
7
+
8
8
  # Set the default host for amazon S3. You can also set this
9
9
  # to https://s3.amazon.com if you want to force secure connections
10
10
  # on a global scale.
@@ -36,7 +36,7 @@ class Imagery
36
36
 
37
37
  @keys = [@original] + sizes.keys
38
38
  end
39
-
39
+
40
40
  # If you specify a distribution domain (i.e. a cloudfront domain,
41
41
  # or even an S3 domain with a prefix), that distribution domain is
42
42
  # used.
@@ -49,8 +49,8 @@ class Imagery
49
49
  "#{self.class.s3_host}/#{self.class.s3_bucket}#{super}"
50
50
  end
51
51
  end
52
-
53
- # Returns the complete S3 key used for this object. The S3 key
52
+
53
+ # Returns the complete S3 id used for this object. The S3 id
54
54
  # is simply composed of the prefix and filename, e.g.
55
55
  #
56
56
  # - photos/1001/original.jpg
@@ -58,9 +58,9 @@ class Imagery
58
58
  # - photos/1001/tiny.jpg
59
59
  #
60
60
  def s3_key(file)
61
- "#{prefix}/#{key}/#{ext(file)}"
61
+ "#{prefix}/#{id}/#{ext(file)}"
62
62
  end
63
-
63
+
64
64
  # Deletes all keys defined for this object, which includes `:original`
65
65
  # and all keys in `sizes`.
66
66
  def delete
@@ -70,11 +70,11 @@ class Imagery
70
70
  Gateway.delete(s3_key(file), self.class.s3_bucket)
71
71
  end
72
72
  end
73
-
73
+
74
74
  # Save the object as we normall would, but also upload all resulting
75
75
  # files to S3. We set the proper content type and Cache-Control setting
76
76
  # optimized for a cloudfront setup.
77
- def save(io, key = nil)
77
+ def save(io, id = nil)
78
78
  super
79
79
 
80
80
  keys.each do |file|
@@ -87,18 +87,18 @@ class Imagery
87
87
  )
88
88
  end
89
89
  end
90
-
90
+
91
91
  # Provides a convenience wrapper around AWS::S3::S3Object and
92
92
  # serves as an auto-connect module.
93
93
  module Gateway
94
94
  def self.store(*args)
95
95
  execute(:store, *args)
96
96
  end
97
-
97
+
98
98
  def self.delete(*args)
99
99
  execute(:delete, *args)
100
100
  end
101
-
101
+
102
102
  private
103
103
  def self.execute(command, *args)
104
104
  begin
@@ -6,18 +6,18 @@ test "defining with a prefix" do
6
6
  assert_equal "avatar", i.prefix
7
7
  end
8
8
 
9
- test "defining with a key" do
9
+ test "defining with a id" do
10
10
  i = Imagery.new(:avatar, "1001")
11
11
 
12
12
  assert_equal "avatar", i.prefix
13
- assert_equal "1001", i.key
13
+ assert_equal "1001", i.id
14
14
  end
15
15
 
16
16
  test "defining with sizes" do
17
17
  i = Imagery.new(:avatar, "1001", small: ["100x100"])
18
18
 
19
19
  assert_equal "avatar", i.prefix
20
- assert_equal "1001", i.key
20
+ assert_equal "1001", i.id
21
21
  assert_equal({ small: ["100x100"] }, i.sizes)
22
22
  end
23
23
 
@@ -41,18 +41,18 @@ test "allows override of the default Dir.pwd" do
41
41
  end
42
42
  end
43
43
 
44
- test "url when missing key" do
44
+ test "url when missing id" do
45
45
  i = Imagery.new(:avatar)
46
46
 
47
47
  assert_equal "/missing/avatar/original.jpg", i.url
48
48
  end
49
49
 
50
- test "url with a key" do
50
+ test "url with a id" do
51
51
  i = Imagery.new(:avatar, "1001")
52
52
  assert_equal "/avatar/1001/original.jpg", i.url
53
53
  end
54
54
 
55
- test "url with a key and a file" do
55
+ test "url with a id and a file" do
56
56
  i = Imagery.new(:avatar, "1001")
57
57
  assert_equal "/avatar/1001/small.jpg", i.url(:small)
58
58
  end
@@ -73,7 +73,7 @@ scope do
73
73
  assert_equal "1024x768", resolution(im.root("original.jpg"))
74
74
  end
75
75
 
76
- test "saving and specifying the key" do |im, io|
76
+ test "saving and specifying the id" do |im, io|
77
77
  assert im.save(io, "GUID")
78
78
  assert File.exist?(Imagery.root("avatar/GUID/original.jpg"))
79
79
  end
@@ -173,6 +173,27 @@ scope do
173
173
  assert File.exist?(s)
174
174
  end
175
175
 
176
+ test "no deletion when existing id is nil" do |_, io|
177
+ im = Imagery.new(:avatar, nil, small: ["100x100^", "100x100"])
178
+
179
+ def im.delete
180
+ raise RuntimeError
181
+ end
182
+
183
+ begin
184
+ im.save(io, 1)
185
+ rescue RuntimeError => ex
186
+ end
187
+
188
+ assert_equal nil, ex
189
+ end
190
+
191
+ test "deletion not possible when id is nil" do |_, io|
192
+ im = Imagery.new(:avatar, nil, small: ["100x100^", "100x100"])
193
+
194
+ assert_equal nil, im.delete
195
+ end
196
+
176
197
  test "inherting imagery" do |im, io|
177
198
  class Photo < Imagery
178
199
  end
@@ -78,7 +78,7 @@ scope do
78
78
  assert cmds.empty?
79
79
  end
80
80
 
81
- test "doesn't delete when passing same key" do |im, io|
81
+ test "doesn't delete when passing same id" do |im, io|
82
82
  im.save(io, "1001")
83
83
 
84
84
  cmds = Imagery::S3::Gateway.commands
@@ -90,7 +90,7 @@ scope do
90
90
  assert cmds.empty?
91
91
  end
92
92
 
93
- test "deletes when passing a different key" do |im, io|
93
+ test "deletes when passing a different id" do |im, io|
94
94
  im.save(io, "1002")
95
95
 
96
96
  cmds = Imagery::S3::Gateway.commands
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imagery
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc1
4
+ version: 1.0.0.rc2
5
5
  prerelease: 6
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-07-03 00:00:00.000000000 Z
12
+ date: 2012-07-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cutest