Class: DynamicImage::Format
- Inherits:
-
Object
- Object
- DynamicImage::Format
- Defined in:
- lib/dynamic_image/format.rb
Instance Attribute Summary collapse
-
#animated ⇒ Object
readonly
Returns the value of attribute animated.
-
#content_types ⇒ Object
readonly
Returns the value of attribute content_types.
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
-
#magic_bytes ⇒ Object
readonly
Returns the value of attribute magic_bytes.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#save_options ⇒ Object
readonly
Returns the value of attribute save_options.
-
#signature ⇒ Object
readonly
Returns the value of attribute signature.
Class Method Summary collapse
- .content_type(type) ⇒ Object
- .content_types ⇒ Object
- .find(name) ⇒ Object
- .formats ⇒ Object
- .register(name, **opts) ⇒ Object
- .sniff(bytes) ⇒ Object
Instance Method Summary collapse
- #animated? ⇒ Boolean
- #content_type ⇒ Object
- #default_options ⇒ Object
- #extension ⇒ Object
-
#initialize(name, options) ⇒ Format
constructor
A new instance of Format.
- #matches?(bytes) ⇒ Boolean
Constructor Details
#initialize(name, options) ⇒ Format
Returns a new instance of Format.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/dynamic_image/format.rb', line 8 def initialize(name, ) = .merge() @name = name @animated = [:animated] @content_types = Array([:content_type]) @extensions = Array([:extension]) @magic_bytes = [:magic_bytes].map do |s| s.dup.force_encoding("binary") end @signature = [:signature] @save_options = [:save_options] end |
Instance Attribute Details
#animated ⇒ Object (readonly)
Returns the value of attribute animated.
5 6 7 |
# File 'lib/dynamic_image/format.rb', line 5 def animated @animated end |
#content_types ⇒ Object (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 |
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
5 6 7 |
# File 'lib/dynamic_image/format.rb', line 5 def extensions @extensions end |
#magic_bytes ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/dynamic_image/format.rb', line 5 def name @name end |
#save_options ⇒ Object (readonly)
Returns the value of attribute save_options.
5 6 7 |
# File 'lib/dynamic_image/format.rb', line 5 def @save_options end |
#signature ⇒ Object (readonly)
Returns the value of attribute signature.
5 6 7 |
# File 'lib/dynamic_image/format.rb', line 5 def signature @signature end |
Class Method Details
.content_type(type) ⇒ Object
41 42 43 |
# File 'lib/dynamic_image/format.rb', line 41 def content_type(type) formats.filter { |f| f.content_types.include?(type) }.first end |
.content_types ⇒ Object
45 46 47 |
# File 'lib/dynamic_image/format.rb', line 45 def content_types formats.flat_map(&:content_types) end |
.find(name) ⇒ Object
49 50 51 52 53 |
# File 'lib/dynamic_image/format.rb', line 49 def find(name) key = name.to_s.upcase key = "JPEG" if key == "JPG" registered_formats[key] end |
.formats ⇒ Object
55 56 57 |
# File 'lib/dynamic_image/format.rb', line 55 def formats registered_formats.map { |_, f| f } end |
.register(name, **opts) ⇒ Object
59 60 61 |
# File 'lib/dynamic_image/format.rb', line 59 def register(name, **opts) registered_formats[name] = new(name, opts) end |
.sniff(bytes) ⇒ Object
63 64 65 66 67 |
# File 'lib/dynamic_image/format.rb', line 63 def sniff(bytes) return unless bytes formats.find { |format| format.matches?(bytes) } end |
Instance Method Details
#animated? ⇒ Boolean
22 23 24 |
# File 'lib/dynamic_image/format.rb', line 22 def animated? animated end |
#content_type ⇒ Object
32 33 34 |
# File 'lib/dynamic_image/format.rb', line 32 def content_type content_types.first end |
#default_options ⇒ Object
76 77 78 79 |
# File 'lib/dynamic_image/format.rb', line 76 def { animated: false, content_type: [], extension: [], magic_bytes: [], signature: nil, save_options: {} } end |
#extension ⇒ Object
36 37 38 |
# File 'lib/dynamic_image/format.rb', line 36 def extension extensions.first end |
#matches?(bytes) ⇒ Boolean
26 27 28 29 30 |
# File 'lib/dynamic_image/format.rb', line 26 def matches?(bytes) return false unless magic_bytes.any? { |b| bytes.start_with?(b) } signature.nil? || signature.call(bytes) end |