class ImageVise

Constants

DEFAULT_CACHE_LIFETIME

The default cache liftime is 30 days, and will be used if no custom lifetime is set.

DEFAULT_MAXIMUM_SOURCE_FILE_SIZE

The default limit on how large may a file loaded for processing be, in bytes. This is in addition to the constraints on the file format.

S_MUTEX
VERSION

Public Class Methods

add_allowed_host!(hostname) click to toggle source

Add an allowed host

# File lib/image_vise.rb, line 39
def add_allowed_host!(hostname)
  S_MUTEX.synchronize { @allowed_hosts << hostname }
end
add_operator(operator_name, object_responding_to_new) click to toggle source

Adds an operator

# File lib/image_vise.rb, line 114
def add_operator(operator_name, object_responding_to_new)
  @operators[operator_name.to_s] = object_responding_to_new
end
add_secret_key!(key) click to toggle source

Adds a key against which the parameters are going to be verified. Multiple applications may have their own different keys, so we need to have multiple keys.

# File lib/image_vise.rb, line 83
def add_secret_key!(key)
  S_MUTEX.synchronize { @keys << key }
  self
end
allow_filesystem_source!(glob_pattern) click to toggle source
# File lib/image_vise.rb, line 53
def allow_filesystem_source!(glob_pattern)
  S_MUTEX.synchronize { @allowed_glob_patterns << glob_pattern }
end
allowed_filesystem_sources() click to toggle source
# File lib/image_vise.rb, line 57
def allowed_filesystem_sources
  S_MUTEX.synchronize { @allowed_glob_patterns.to_a }
end
allowed_hosts() click to toggle source

Returns both the allowed hosts added at runtime and the ones set in the constant

# File lib/image_vise.rb, line 44
def allowed_hosts
  S_MUTEX.synchronize { @allowed_hosts.to_a }
end
cache_lifetime_seconds() click to toggle source
# File lib/image_vise.rb, line 72
def cache_lifetime_seconds
  S_MUTEX.synchronize { @cache_lifetime }
end
cache_lifetime_seconds=(length) click to toggle source
# File lib/image_vise.rb, line 65
def cache_lifetime_seconds=(length)
  Integer(length)
  S_MUTEX.synchronize { @cache_lifetime = length.to_i }
rescue => e
  raise ArgumentError, "The custom cache lifetime value must be an integer"
end
call(rack_env) click to toggle source

Made available since the object that is used with `mount()` in Rails has to, by itself, to respond to `call`.

Thanks to this method you can do this:

mount ImageVise => '/thumbnails'

instead of having to do

mount ImageVise.new => '/thumbnails'
# File lib/image_vise.rb, line 153
def self.call(rack_env)
  ImageVise::RenderEngine.new.call(rack_env)
end
defined_operator_names() click to toggle source
# File lib/image_vise.rb, line 123
def defined_operator_names
  @operators.keys
end
deny_filesystem_sources!() click to toggle source
# File lib/image_vise.rb, line 61
def deny_filesystem_sources!
  S_MUTEX.synchronize { @allowed_glob_patterns.clear }
end
destroy(maybe_image) click to toggle source

Used as a shorthand to force-destroy Magick images in ensure() blocks. Since ensure blocks sometimes deal with variables in inconsistent states (variable in scope but not yet set to an image) we take the possibility of nils into account. We also deal with Magick::Image objects that already have been destroyed in a clean manner.

# File lib/image_vise.rb, line 165
def self.destroy(maybe_image)
  return unless maybe_image
  return unless maybe_image.respond_to?(:destroy!)
  return if maybe_image.destroyed?
  Measurometer.instrument('image_vise.image_destroy_dealloc') do
    maybe_image.destroy!
  end
end
fetcher_for(scheme) click to toggle source
# File lib/image_vise.rb, line 131
def fetcher_for(scheme)
  S_MUTEX.synchronize { @fetchers[scheme.to_s] or raise "No fetcher registered for #{scheme}" }
end
image_path(src_url:, secret:) { |p| ... } click to toggle source

Generate a path for a resized image. Yields a Pipeline object that will receive method calls for adding image operations to a stack.

ImageVise.image_path(src_url: image_url_on_s3, secret: '...') do |p|
   p.center_fit width: 128, height: 128
   p.elliptic_stencil
end #=> "/abcdef/xyz123"

The query string elements can be then passed on to RenderEngine for validation and execution.

@yield {ImageVise::Pipeline} @return [String]

# File lib/image_vise.rb, line 106
def image_path(src_url:, secret:)
  p = Pipeline.new
  yield(p)
  raise ArgumentError, "Image pipeline has no steps defined" if p.empty?
  ImageRequest.new(src_url: URI(src_url), pipeline: p).to_path_params(secret)
end
operator_from(operator_name) click to toggle source

Gets an operator by name

# File lib/image_vise.rb, line 119
def operator_from(operator_name)
  @operators.fetch(operator_name.to_s)
end
operator_name_for(operator) click to toggle source
# File lib/image_vise.rb, line 135
def operator_name_for(operator)
  S_MUTEX.synchronize do
    @operators.key(operator.class) or raise "Operator #{operator.inspect} not registered using ImageVise.add_operator"
  end
end
register_fetcher(scheme, fetcher) click to toggle source
# File lib/image_vise.rb, line 127
def register_fetcher(scheme, fetcher)
  S_MUTEX.synchronize { @fetchers[scheme.to_s] = fetcher }
end
reset_allowed_hosts!() click to toggle source

Resets all allowed hosts

# File lib/image_vise.rb, line 34
def reset_allowed_hosts!
  S_MUTEX.synchronize { @allowed_hosts.clear }
end
reset_cache_lifetime_seconds!() click to toggle source
# File lib/image_vise.rb, line 76
def reset_cache_lifetime_seconds!
  S_MUTEX.synchronize { @cache_lifetime = DEFAULT_CACHE_LIFETIME }
end
reset_secret_keys!() click to toggle source

Removes all set keys

# File lib/image_vise.rb, line 49
def reset_secret_keys!
  S_MUTEX.synchronize { @keys.clear }
end
secret_keys() click to toggle source

Returns the array of defined keys or raises an exception if no keys have been set yet

# File lib/image_vise.rb, line 89
def secret_keys
  keys = S_MUTEX.synchronize { @keys.any? && @keys.to_a }
  keys or raise "No keys set, add a key using `ImageVise.add_secret_key!(key)'"
end

Public Instance Methods

call(rack_env) click to toggle source
# File lib/image_vise.rb, line 157
def call(rack_env)
  ImageVise::RenderEngine.new.call(rack_env)
end