module Shrine::Plugins::DetermineMimeType::ClassMethods

Public Instance Methods

determine_mime_type(io) click to toggle source

Determines the MIME type of the IO object by calling the specified analyzer.

# File lib/shrine/plugins/determine_mime_type.rb, line 25
def determine_mime_type(io)
  analyzer = opts[:determine_mime_type][:analyzer]

  analyzer = mime_type_analyzer(analyzer) if analyzer.is_a?(Symbol)
  args     = if analyzer.is_a?(Proc)
      [io, mime_type_analyzers].take(analyzer.arity.abs)
    else
      [io, opts[:determine_mime_type][:analyzer_options]]
    end

  mime_type = instrument_mime_type(io) { analyzer.call(*args) }
  io.rewind

  mime_type
end
Also aliased as: mime_type
mime_type(io)
Alias for: determine_mime_type
mime_type_analyzer(name) click to toggle source

Returns callable mime type analyzer object.

# File lib/shrine/plugins/determine_mime_type.rb, line 52
def mime_type_analyzer(name)
  MimeTypeAnalyzer.new(name)
end
mime_type_analyzers() click to toggle source

Returns a hash of built-in MIME type analyzers, where keys are analyzer names and values are ‘#call`-able objects which accepts the IO object.

# File lib/shrine/plugins/determine_mime_type.rb, line 45
def mime_type_analyzers
  @mime_type_analyzers ||= MimeTypeAnalyzer::SUPPORTED_TOOLS.inject({}) do |hash, tool|
    hash.merge!(tool => mime_type_analyzer(tool))
  end
end

Private Instance Methods

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

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

# File lib/shrine/plugins/determine_mime_type.rb, line 59
def instrument_mime_type(io, &block)
  return yield unless respond_to?(:instrument)

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