class Shrine::Derivation::Generate

Public Instance Methods

call(file = nil) click to toggle source
# File lib/shrine/plugins/derivation_endpoint.rb, line 580
def call(file = nil)
  derivative = generate(file)
  derivative = normalize(derivative)
  derivative
end

Private Instance Methods

derivation_block() click to toggle source
# File lib/shrine/plugins/derivation_endpoint.rb, line 638
def derivation_block
  shrine_class.derivations[name] or fail Derivation::NotFound, "derivation #{name.inspect} is not defined"
end
derive(*args) click to toggle source

Calls the derivation block.

# File lib/shrine/plugins/derivation_endpoint.rb, line 600
def derive(*args)
  instrument_derivation do
    derivation.instance_exec(*args, &derivation_block)
  end
end
download_source(&block) click to toggle source

Downloads the source uploaded file from the storage.

# File lib/shrine/plugins/derivation_endpoint.rb, line 632
def download_source(&block)
  source.download(**download_options, &block)
rescue Shrine::FileNotFound
  raise Derivation::SourceNotFound, "source file \"#{source.id}\" was not found on storage :#{source.storage_key}"
end
generate(file) click to toggle source

Determines how to call the derivation block. If a file object is given, passes that as the source file, otherwise downloads the source uploaded file.

# File lib/shrine/plugins/derivation_endpoint.rb, line 591
def generate(file)
  if download
    with_downloaded(file) { |file| derive(file, *args) }
  else
    derive(*args)
  end
end
instrument_derivation() { || ... } click to toggle source

Sends a ‘derivation.shrine` event for instrumentation plugin.

# File lib/shrine/plugins/derivation_endpoint.rb, line 607
def instrument_derivation(&block)
  return yield unless shrine_class.respond_to?(:instrument)

  shrine_class.instrument(:derivation, { derivation: derivation }, &block)
end
normalize(file) click to toggle source

Massages the derivation result, ensuring it’s opened in binary mode, rewinded and flushed to disk.

# File lib/shrine/plugins/derivation_endpoint.rb, line 615
def normalize(file)
  unless file.is_a?(File) || file.is_a?(Tempfile)
    fail Error, "expected File or Tempfile object as derivation result, got #{file.inspect}"
  end

  file.open if file.is_a?(Tempfile) # refresh file descriptor
  file.binmode                      # ensure binary mode
  file
end
with_downloaded(file) { |file| ... } click to toggle source
# File lib/shrine/plugins/derivation_endpoint.rb, line 625
def with_downloaded(file, &block)
  return yield(file) if file

  download_source(&block)
end