Module: Imagery::Core
- Included in:
- Imagery
- Defined in:
- lib/imagery.rb
Overview
In order to facilitate a plugin architecture, all overridable methods are placed in the ‘Core` module. Imagery::S3 demonstrates overriding in action.
Instance Method Summary collapse
-
#delete ⇒ Object
A very simple and destructive method.
- #initialize(prefix, key = nil, sizes = {}) ⇒ Object
-
#save(io, key = nil) ⇒ Object
Accepts an ‘IO` object, typically taken from an input.
-
#url(file = @original) ⇒ Object
Returns the url for a given size, which defaults to ‘:original`.
Instance Method Details
#delete ⇒ Object
A very simple and destructive method. Deletes the entire folder for the current prefix/key combination.
101 102 103 |
# File 'lib/imagery.rb', line 101 def delete FileUtils.rm_rf(root) end |
#initialize(prefix, key = nil, sizes = {}) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/imagery.rb', line 37 def initialize(prefix, key = nil, sizes = {}) @prefix = prefix.to_s @key = key.to_s @sizes = sizes @original = :original # Used as the filename for the raw image. @ext = :jpg # We default to jpg for the image format. end |
#save(io, key = nil) ⇒ Object
Accepts an ‘IO` object, typically taken from an input.
The second optional ‘key` argument is used when you want to force a new resource, useful in conjunction with cloudfront / high cache scenarios where updating an existing image won’t suffice.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/imagery.rb', line 73 def save(io, key = nil) GM.identify(io) or raise(InvalidImage) # We delete the existing object iff: # 1. A key was passed # 2. The key passed is different from the existing key. delete if key && key != self.key # Now we can assign the new key passed, with the assurance that the # old key has been deleted and won't be used anymore. @key = key.to_s if key # Ensure that the path to all images is created. FileUtils.mkdir_p(root) # Write the original filename as binary using the `IO` object's data. File.open(root(ext(@original)), "wb") { |file| file.write(io.read) } # We resize the original raw image to different sizes which we # defined in the constructor. GraphicsMagick is assumed to exist # within the machine. sizes.each do |size, (resize, extent)| GM.convert root(ext(@original)), root(ext(size)), resize, extent end end |
#url(file = @original) ⇒ Object
Returns the url for a given size, which defaults to ‘:original`.
If the key is nil, a missing path is returned.
48 49 50 51 52 |
# File 'lib/imagery.rb', line 48 def url(file = @original) return "/missing/#{prefix}/#{ext(file)}" if key.to_s.empty? "/#{prefix}/#{key}/#{ext(file)}" end |