class DynamicImage::Format

Attributes

animated[R]
content_types[R]
extensions[R]
magic_bytes[R]
name[R]
save_options[R]

Public Class Methods

content_type(type) click to toggle source
# File lib/dynamic_image/format.rb, line 34
def content_type(type)
  formats.filter { |f| f.content_types.include?(type) }.first
end
content_types() click to toggle source
# File lib/dynamic_image/format.rb, line 38
def content_types
  formats.flat_map(&:content_types)
end
find(name) click to toggle source
# File lib/dynamic_image/format.rb, line 42
def find(name)
  key = name.to_s.upcase
  key = "JPEG" if key == "JPG"
  registered_formats[key]
end
formats() click to toggle source
# File lib/dynamic_image/format.rb, line 48
def formats
  registered_formats.map { |_, f| f }
end
new(name, options) click to toggle source
# File lib/dynamic_image/format.rb, line 8
def initialize(name, options)
  options = default_options.merge(options)

  @name = name
  @animated = options[:animated]
  @content_types = Array(options[:content_type])
  @extensions = Array(options[:extension])
  @magic_bytes = options[:magic_bytes].map do |s|
    s.dup.force_encoding("binary")
  end
  @save_options = options[:save_options]
end
register(name, **opts) click to toggle source
# File lib/dynamic_image/format.rb, line 52
def register(name, **opts)
  registered_formats[name] = new(name, opts)
end
sniff(bytes) click to toggle source
# File lib/dynamic_image/format.rb, line 56
def sniff(bytes)
  return unless bytes

  formats.each do |format|
    format.magic_bytes.each do |b|
      return format if bytes.start_with?(b)
    end
  end
  nil
end

Private Class Methods

registered_formats() click to toggle source
# File lib/dynamic_image/format.rb, line 69
def registered_formats
  @registered_formats ||= {}
end

Public Instance Methods

animated?() click to toggle source
# File lib/dynamic_image/format.rb, line 21
def animated?
  animated
end
content_type() click to toggle source
# File lib/dynamic_image/format.rb, line 25
def content_type
  content_types.first
end
default_options() click to toggle source
# File lib/dynamic_image/format.rb, line 74
def default_options
  { animated: false, content_type: [], extension: [], magic_bytes: [],
    save_options: {} }
end
extension() click to toggle source
# File lib/dynamic_image/format.rb, line 29
def extension
  extensions.first
end