Class: Manipulator
- Inherits:
-
Object
- Object
- Manipulator
- Defined in:
- lib/manipulator/manipulator.rb
Overview
Using Manipulator is as simple as creating an instance and calling manipulate.
The connection to S3 is opened using credentials read from AWSCredentials.
Instance Attribute Summary collapse
-
#temp_file_path ⇒ Object
Returns the value of attribute temp_file_path.
Instance Method Summary collapse
-
#cleanup ⇒ Object
Removes the temp file.
-
#connect_to_s3 ⇒ Object
Establishes a connection to S3 if not already connected.
-
#download(bucket, key) ⇒ Object
Downloads the specified key from the S3 bucket to a local temp file.
-
#initialize(options = {}) ⇒ Manipulator
constructor
A new instance of Manipulator.
-
#manipulate(options, &block) ⇒ Object
Specify a S3 key to manipulate (and its bucket).
-
#upload(bucket, key) ⇒ Object
Pushes contents of temp file back to specified bucket, key on S3 Returns the url for the file.
Constructor Details
#initialize(options = {}) ⇒ Manipulator
Returns a new instance of Manipulator.
8 9 10 |
# File 'lib/manipulator/manipulator.rb', line 8 def initialize( = {}) MiniMagick.processor = [:processor] end |
Instance Attribute Details
#temp_file_path ⇒ Object
Returns the value of attribute temp_file_path.
7 8 9 |
# File 'lib/manipulator/manipulator.rb', line 7 def temp_file_path @temp_file_path end |
Instance Method Details
#cleanup ⇒ Object
Removes the temp file
73 74 75 |
# File 'lib/manipulator/manipulator.rb', line 73 def cleanup File.delete(temp_file_path) end |
#connect_to_s3 ⇒ Object
Establishes a connection to S3 if not already connected
13 14 15 16 17 |
# File 'lib/manipulator/manipulator.rb', line 13 def connect_to_s3 unless AWS::S3::Base.connected? AWS::S3::Base.establish_connection!(:access_key_id => AWSCredentials.access_key, :secret_access_key => AWSCredentials.secret_access_key) end end |
#download(bucket, key) ⇒ Object
Downloads the specified key from the S3 bucket to a local temp file
20 21 22 23 24 25 26 |
# File 'lib/manipulator/manipulator.rb', line 20 def download(bucket, key) connect_to_s3 @temp_file_path = File.join(Dir.tmpdir, key.gsub('/', '-')) File.open(temp_file_path, 'w+') do |f| f.puts AWS::S3::S3Object.value(key,bucket) end end |
#manipulate(options, &block) ⇒ Object
Specify a S3 key to manipulate (and its bucket).
Block yields a MiniMagick::Image image instance with access to MiniMagick’s methods Note that you don’t have to chain methods to return the image with all manipulations using MiniMagick: For example, use
m.manipulate('my-bucket', 'my-key') do |img|
img.rotate(90)
img.resize(100x100)
end
Want to do something that MiniMagick doesn’t directly support, like removing all profiles from an image with GraphicsMagick (aka ImageMagick’s strip method) m.manipulate(‘my-bucket’, ‘my-key’ do |img|
`gm mogrify +profile '*' #{manipulator.temp_file_path}`
end
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/manipulator/manipulator.rb', line 50 def manipulate(, &block) download([:bucket], [:key]) begin image = MiniMagick::Image.open(temp_file_path) image. do |i| yield(i) end image.write(temp_file_path) unless [:keep_local] target_key = [:target_key] || [:key] upload([:bucket], target_key) end rescue Exception => e puts e. puts e.backtrace ensure cleanup end end |
#upload(bucket, key) ⇒ Object
Pushes contents of temp file back to specified bucket, key on S3 Returns the url for the file
30 31 32 33 34 |
# File 'lib/manipulator/manipulator.rb', line 30 def upload(bucket, key) connect_to_s3 AWS::S3::S3Object.store(key, File.open(temp_file_path, 'r'), bucket, :access => :public_read) AWS::S3::S3Object.url_for(key, bucket, :authenticated => false) end |