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.
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.
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, ) = .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 @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 |
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_types ⇒ Object
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 |
.formats ⇒ Object
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
21 22 23 |
# File 'lib/dynamic_image/format.rb', line 21 def animated? animated end |
#content_type ⇒ Object
25 26 27 |
# File 'lib/dynamic_image/format.rb', line 25 def content_type content_types.first end |
#default_options ⇒ Object
74 75 76 77 |
# File 'lib/dynamic_image/format.rb', line 74 def { animated: false, content_type: [], extension: [], magic_bytes: [], save_options: {} } end |
#extension ⇒ Object
29 30 31 |
# File 'lib/dynamic_image/format.rb', line 29 def extension extensions.first end |