Class: DynamicImage::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_image/format.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Format

Returns a new instance of Format.



8
9
10
11
12
13
14
15
16
17
18
19
# 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

Instance Attribute Details

#animatedObject (readonly)

Returns the value of attribute animated.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def animated
  @animated
end

#content_typesObject (readonly)

Returns the value of attribute content_types.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def content_types
  @content_types
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def extensions
  @extensions
end

#magic_bytesObject (readonly)

Returns the value of attribute magic_bytes.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def magic_bytes
  @magic_bytes
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def name
  @name
end

#save_optionsObject (readonly)

Returns the value of attribute save_options.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def save_options
  @save_options
end

Class Method Details

.content_type(type) ⇒ Object



34
35
36
# File 'lib/dynamic_image/format.rb', line 34

def content_type(type)
  formats.filter { |f| f.content_types.include?(type) }.first
end

.content_typesObject



38
39
40
# File 'lib/dynamic_image/format.rb', line 38

def content_types
  formats.flat_map(&:content_types)
end

.find(name) ⇒ Object



42
43
44
45
46
# 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

.formatsObject



48
49
50
# File 'lib/dynamic_image/format.rb', line 48

def formats
  registered_formats.map { |_, f| f }
end

.register(name, **opts) ⇒ Object



52
53
54
# File 'lib/dynamic_image/format.rb', line 52

def register(name, **opts)
  registered_formats[name] = new(name, opts)
end

.sniff(bytes) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# 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

Instance Method Details

#animated?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/dynamic_image/format.rb', line 21

def animated?
  animated
end

#content_typeObject



25
26
27
# File 'lib/dynamic_image/format.rb', line 25

def content_type
  content_types.first
end

#default_optionsObject



74
75
76
77
# File 'lib/dynamic_image/format.rb', line 74

def default_options
  { animated: false, content_type: [], extension: [], magic_bytes: [],
    save_options: {} }
end

#extensionObject



29
30
31
# File 'lib/dynamic_image/format.rb', line 29

def extension
  extensions.first
end