manipulator 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +6 -1
- data/lib/manipulator/manipulator.rb +13 -3
- data/test/test_manipulator.rb +14 -12
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -9,7 +9,7 @@ S3 is great for storing image files, but manipulating images after they've been
|
|
9
9
|
|
10
10
|
After re-implementing this pattern a few times over (and filling up /tmp by forgetting the last step), we decided to roll this into a gem that makes S3 image manipulation as easy as:
|
11
11
|
|
12
|
-
m = Manipulator.new
|
12
|
+
m = Manipulator.new(:processor => :gm) # use GraphicsMagick; defaults to ImageMagick
|
13
13
|
m.manipulate({:bucket => 'my-bucket', :key => 'some/file.jpg'}) do |img|
|
14
14
|
img.rotate(90)
|
15
15
|
img.resize_to_fit(75,75)
|
@@ -19,6 +19,11 @@ The idea is to instantiate a Manipulator and then pass it a bucket name and key
|
|
19
19
|
|
20
20
|
We're using MiniMagick now instead of RMagick to save memory - we found that RMagick was a hog. MiniMagick is a lot more straightforward and has a nice interface for both GraphicsMagick (recommended) and ImageMagick.
|
21
21
|
|
22
|
+
Want to do something that MiniMagick doesn't directly support, like removing all profiles from an image with GraphicsMagick (aka ImageMagick's strip method)?
|
23
|
+
m.manipulate('my-bucket', 'my-key' do |img|
|
24
|
+
`gm mogrify +profile '*' #{manipulator.temp_file_path}`
|
25
|
+
end
|
26
|
+
|
22
27
|
== Install
|
23
28
|
|
24
29
|
$ gem install manipulator
|
@@ -5,6 +5,9 @@ require 'mini_magick'
|
|
5
5
|
# The connection to S3 is opened using credentials read from AWSCredentials.
|
6
6
|
class Manipulator
|
7
7
|
attr_accessor :temp_file_path
|
8
|
+
def initialize(options = {})
|
9
|
+
MiniMagick.processor = options[:processor]
|
10
|
+
end
|
8
11
|
|
9
12
|
# Establishes a connection to S3 if not already connected
|
10
13
|
def connect_to_s3
|
@@ -39,9 +42,12 @@ class Manipulator
|
|
39
42
|
# img.rotate(90)
|
40
43
|
# img.resize(100x100)
|
41
44
|
# end
|
45
|
+
# Want to do something that MiniMagick doesn't directly support, like
|
46
|
+
# removing all profiles from an image with GraphicsMagick (aka ImageMagick's strip method)
|
47
|
+
# m.manipulate('my-bucket', 'my-key' do |img|
|
48
|
+
# `gm mogrify +profile '*' #{manipulator.temp_file_path}`
|
49
|
+
# end
|
42
50
|
def manipulate(options, &block)
|
43
|
-
# TODO: make this configurable - use graphics magick
|
44
|
-
MiniMagick.processor = :gm
|
45
51
|
|
46
52
|
download(options[:bucket], options[:key])
|
47
53
|
begin
|
@@ -50,7 +56,11 @@ class Manipulator
|
|
50
56
|
yield(i)
|
51
57
|
end
|
52
58
|
image.write(temp_file_path)
|
53
|
-
|
59
|
+
|
60
|
+
unless options[:keep_local]
|
61
|
+
target_key = options[:target_key] || options[:key]
|
62
|
+
upload(options[:bucket], target_key)
|
63
|
+
end
|
54
64
|
rescue Exception => e
|
55
65
|
puts e.message
|
56
66
|
puts e.backtrace
|
data/test/test_manipulator.rb
CHANGED
@@ -16,7 +16,7 @@ class TestManipulator < Test::Unit::TestCase
|
|
16
16
|
|
17
17
|
context "#connect_to_s3" do
|
18
18
|
setup do
|
19
|
-
@manipulator = Manipulator.new
|
19
|
+
@manipulator = Manipulator.new(:processor => :gm)
|
20
20
|
end
|
21
21
|
should "use existing connection" do
|
22
22
|
AWS::S3::Base.stubs(:connected?).returns(true)
|
@@ -31,7 +31,7 @@ class TestManipulator < Test::Unit::TestCase
|
|
31
31
|
end
|
32
32
|
context "#download" do
|
33
33
|
setup do
|
34
|
-
@manipulator = Manipulator.new
|
34
|
+
@manipulator = Manipulator.new(:processor => :gm)
|
35
35
|
@manipulator.stubs(:connect_to_s3)
|
36
36
|
AWS::S3::S3Object.stubs(:value).returns('123')
|
37
37
|
end
|
@@ -53,16 +53,15 @@ class TestManipulator < Test::Unit::TestCase
|
|
53
53
|
|
54
54
|
context "#manipulate" do
|
55
55
|
setup do
|
56
|
-
@manipulator = Manipulator.new
|
56
|
+
@manipulator = Manipulator.new(:processor => :gm)
|
57
57
|
@manipulator.stubs(:connect_to_s3)
|
58
58
|
@file = stub_everything('fake file')
|
59
59
|
@manipulator.stubs(:download).returns(@key)
|
60
|
-
@manipulator.stubs(:upload)
|
61
60
|
@manipulator.stubs(:cleanup)
|
62
61
|
@manipulator.stubs(:temp_file_path).returns(@temp_file_path)
|
63
|
-
@
|
64
|
-
|
65
|
-
|
62
|
+
@manipulator.stubs(:upload)
|
63
|
+
@mocked = stub_everything('fake mini_magick instance')
|
64
|
+
MiniMagick::Image.stubs(:open).with(@temp_file_path).returns(@mocked)
|
66
65
|
end
|
67
66
|
|
68
67
|
should "download the file" do
|
@@ -70,7 +69,8 @@ class TestManipulator < Test::Unit::TestCase
|
|
70
69
|
@manipulator.manipulate({:bucket => @bucket, :key => @key}) { |img| img }
|
71
70
|
end
|
72
71
|
should "create an MiniMagick Image instance and yield it to the block" do
|
73
|
-
|
72
|
+
@manipulator.stubs(:upload)
|
73
|
+
MiniMagick::Image.expects(:open).with(@temp_file_path).returns(@mocked)
|
74
74
|
@manipulator.manipulate({:bucket => @bucket, :key => @key}) do |img|
|
75
75
|
assert_equal @mocked, img
|
76
76
|
img
|
@@ -78,7 +78,7 @@ class TestManipulator < Test::Unit::TestCase
|
|
78
78
|
end
|
79
79
|
should "write the returned manipulated image to the temp file" do
|
80
80
|
@mocked.expects(:write).with(@temp_file_path)
|
81
|
-
|
81
|
+
@manipulator.stubs(:upload)
|
82
82
|
@manipulator.manipulate({:bucket => @bucket, :key => @key}) { |img| img }
|
83
83
|
end
|
84
84
|
should "upload the file back to s3" do
|
@@ -103,7 +103,9 @@ class TestManipulator < Test::Unit::TestCase
|
|
103
103
|
@manipulator.manipulate({:bucket => @bucket, :key => @key}) do |img|
|
104
104
|
raise "an error"
|
105
105
|
end
|
106
|
-
rescue
|
106
|
+
rescue Exception => e
|
107
|
+
puts e.message
|
108
|
+
puts e.backtrace
|
107
109
|
end
|
108
110
|
end
|
109
111
|
end
|
@@ -111,7 +113,7 @@ class TestManipulator < Test::Unit::TestCase
|
|
111
113
|
context "#upload" do
|
112
114
|
setup do
|
113
115
|
AWS::S3::S3Object.stubs(:url_for)
|
114
|
-
@manipulator = Manipulator.new
|
116
|
+
@manipulator = Manipulator.new(:processor => :gm)
|
115
117
|
@manipulator.stubs(:temp_file_path).returns(@temp_file_path)
|
116
118
|
@manipulator.stubs(:connect_to_s3)
|
117
119
|
end
|
@@ -124,7 +126,7 @@ class TestManipulator < Test::Unit::TestCase
|
|
124
126
|
|
125
127
|
context "#cleanup" do
|
126
128
|
setup do
|
127
|
-
@manipulator = Manipulator.new
|
129
|
+
@manipulator = Manipulator.new(:processor => :gm)
|
128
130
|
@key = File.join(File.dirname(__FILE__), 'data', 'ham.jpg')
|
129
131
|
@manipulator.stubs(:temp_file_path).returns(@temp_file_path)
|
130
132
|
Manipulator.any_instance.stubs(:download)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: manipulator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 1.0.0
|
10
|
+
version: 1.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jacqui Maher
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-12-
|
19
|
+
date: 2010-12-02 00:00:00 -05:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|