module Shrine::Plugins::Blurhash::ClassMethods

Public Instance Methods

compute_blurhash(io) click to toggle source
# File lib/shrine/plugins/blurhash.rb, line 39
def compute_blurhash(io)
  extractor = opts[:blurhash][:extractor]
  extractor = pixels_extractor(extractor) if extractor.is_a?(Symbol)

  resize_to = opts[:blurhash][:resize_to]
  args = [io, resize_to, pixels_extractors].take(extractor.arity.abs)

  blurhash = instrument_blurhash(io) do
    pixels = extractor.call(*args)

    components = opts[:blurhash][:components]
    ::Blurhash.encode(pixels[:width], pixels[:height], pixels[:pixels], x_comp: components[0], y_comp: components[1])
  end

  io.rewind

  blurhash
end
pixels_extractor(name) click to toggle source

Returns callable pixels extractor object.

# File lib/shrine/plugins/blurhash.rb, line 68
def pixels_extractor(name)
  on_error = opts[:blurhash][:on_error]

  PixelsExtractor.new(name, on_error: on_error).method(:call)
end
pixels_extractors() click to toggle source

Returns a hash of built-in pixels extractors, where keys are extractors names and values are `#call`-able objects which accepts the IO object.

# File lib/shrine/plugins/blurhash.rb, line 61
def pixels_extractors
  @pixels_extractors ||= PixelsExtractor::SUPPORTED_EXTRACTORS.inject({}) do |hash, tool|
    hash.merge!(tool => pixels_extractor(tool))
  end
end

Private Instance Methods

instrument_blurhash(io) { || ... } click to toggle source

Sends a `blurhash.shrine` event for instrumentation plugin.

# File lib/shrine/plugins/blurhash.rb, line 77
def instrument_blurhash(io, &block)
  return yield unless respond_to?(:instrument)

  instrument(:blurhash, io: io, &block)
end