Module: Shrine::Plugins::DetermineMimeType::ClassMethods

Defined in:
lib/shrine/plugins/determine_mime_type.rb

Instance Method Summary collapse

Instance Method Details

#determine_mime_type(io) ⇒ Object Also known as: mime_type

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


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

#mime_type_analyzer(name) ⇒ Object

Returns callable mime type analyzer object.


52
53
54
# File 'lib/shrine/plugins/determine_mime_type.rb', line 52

def mime_type_analyzer(name)
  MimeTypeAnalyzer.new(name)
end

#mime_type_analyzersObject

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.


45
46
47
48
49
# 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