Class: ImageVise

Inherits:
Object
  • Object
show all
Defined in:
lib/image_vise.rb

Defined Under Namespace

Classes: AutoOrient, Crop, EllipseStencil, FileResponse, FitCrop, Geom, ImageRequest, Pipeline, RenderEngine, SRGB, Sharpen, StripMetadata

Constant Summary collapse

VERSION =
'0.0.21'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_allowed_host!(hostname) ⇒ Object

Add an allowed host



27
28
29
# File 'lib/image_vise.rb', line 27

def add_allowed_host!(hostname)
  S_MUTEX.synchronize { @allowed_hosts << hostname }
end

.add_operator(operator_name, object_responding_to_new) ⇒ Object

Adds an operator



87
88
89
# File 'lib/image_vise.rb', line 87

def add_operator(operator_name, object_responding_to_new)
  @operators[operator_name.to_s] = object_responding_to_new
end

.add_secret_key!(key) ⇒ Object

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.



56
57
58
59
# File 'lib/image_vise.rb', line 56

def add_secret_key!(key)
  S_MUTEX.synchronize { @keys << key }
  self
end

.allow_filesystem_source!(glob_pattern) ⇒ Object



41
42
43
# File 'lib/image_vise.rb', line 41

def allow_filesystem_source!(glob_pattern)
  S_MUTEX.synchronize { @allowed_glob_patterns << glob_pattern }
end

.allowed_filesystem_sourcesObject



45
46
47
# File 'lib/image_vise.rb', line 45

def allowed_filesystem_sources
  S_MUTEX.synchronize { @allowed_glob_patterns.to_a }
end

.allowed_hostsObject

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



32
33
34
# File 'lib/image_vise.rb', line 32

def allowed_hosts
  S_MUTEX.synchronize { @allowed_hosts.to_a }
end

.call(rack_env) ⇒ Object

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'


116
117
118
# File 'lib/image_vise.rb', line 116

def self.call(rack_env)
  ImageVise::RenderEngine.new.call(rack_env)
end

.defined_operator_namesObject



96
97
98
# File 'lib/image_vise.rb', line 96

def defined_operator_names
  @operators.keys
end

.deny_filesystem_sources!Object



49
50
51
# File 'lib/image_vise.rb', line 49

def deny_filesystem_sources!
  S_MUTEX.synchronize { @allowed_glob_patterns.clear }
end

.destroy(maybe_image) ⇒ Object

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.



128
129
130
131
132
133
# File 'lib/image_vise.rb', line 128

def self.destroy(maybe_image)
  return unless maybe_image
  return unless maybe_image.respond_to?(:destroy!)
  return if maybe_image.destroyed?
  maybe_image.destroy!
end

.image_params(src_url:, secret:) {|p| ... } ⇒ Hash

Generate a set of querystring params for a resized image. Yields a Pipeline object that will receive method calls for adding image operations to a stack.

ImageVise.image_params(src_url: image_url_on_s3, secret: '...') do |p|
   p.center_fit width: 128, height: 128 
   p.elliptic_stencil
end #=> {q: '...', sig: '...'}

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

Yields:

  • (p)

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


79
80
81
82
83
84
# File 'lib/image_vise.rb', line 79

def image_params(src_url:, secret:)
  p = Pipeline.new
  yield(p)
  raise ArgumentError, "Image pipeline has no steps defined" if p.empty?
  ImageRequest.new(src_url: src_url, pipeline: p).to_query_string_params(secret)
end

.operator_from(operator_name) ⇒ Object

Gets an operator by name



92
93
94
# File 'lib/image_vise.rb', line 92

def operator_from(operator_name)
  @operators.fetch(operator_name.to_s)
end

.operator_name_for(operator) ⇒ Object



100
101
102
# File 'lib/image_vise.rb', line 100

def operator_name_for(operator)
  @operators.key(operator.class) or raise "Operator #{operator.inspect} not registered using ImageVise.add_operator"
end

.reset_allowed_hosts!Object

Resets all allowed hosts



22
23
24
# File 'lib/image_vise.rb', line 22

def reset_allowed_hosts!
  S_MUTEX.synchronize { @allowed_hosts.clear }
end

.reset_secret_keys!Object

Removes all set keys



37
38
39
# File 'lib/image_vise.rb', line 37

def reset_secret_keys!
  S_MUTEX.synchronize { @keys.clear }
end

.secret_keysObject

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



62
63
64
65
# File 'lib/image_vise.rb', line 62

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

Instance Method Details

#call(rack_env) ⇒ Object



120
121
122
# File 'lib/image_vise.rb', line 120

def call(rack_env)
  ImageVise::RenderEngine.new.call(rack_env)
end