Class: Diamant::MimeFile
- Inherits:
-
Object
- Object
- Diamant::MimeFile
- Defined in:
- lib/diamant/mimefile.rb
Overview
MIME aware wrapper for file
Constant Summary collapse
- MIMETYPES =
{ '.gemini' => 'text/gemini', '.gmi' => 'text/gemini', '.txt' => 'text/plain', '.md' => 'text/markdown', '.org' => 'text/org', '.rss' => 'application/rss+xml', '.atom' => 'application/atom+xml', '.xml' => 'application/xml', '.svg' => 'image/svg+xml', '.bmp' => 'image/bmp', '.png' => 'image/png', '.jpg' => 'image/jpeg', '.jpeg' => 'image/jpeg', '.gif' => 'image/gif', '.webp' => 'image/webp', '.mp3' => 'audio/mpeg', '.ogg' => 'audio/ogg' }.freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#category ⇒ Object
readonly
Returns the value of attribute category.
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
Instance Method Summary collapse
-
#initialize(path) ⇒ MimeFile
constructor
A new instance of MimeFile.
-
#validate ⇒ Object
Disable metrics on purpose for big switch rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength.
-
#validate! ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength.
Constructor Details
#initialize(path) ⇒ MimeFile
Returns a new instance of MimeFile.
30 31 32 33 34 35 36 37 |
# File 'lib/diamant/mimefile.rb', line 30 def initialize(path) @path = path @body = File.read path @extension, @content_type = extract_info @category = classify validate! prepare_gem_file if @content_type == 'text/gemini' end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
8 9 10 |
# File 'lib/diamant/mimefile.rb', line 8 def body @body end |
#category ⇒ Object (readonly)
Returns the value of attribute category.
8 9 10 |
# File 'lib/diamant/mimefile.rb', line 8 def category @category end |
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
8 9 10 |
# File 'lib/diamant/mimefile.rb', line 8 def content_type @content_type end |
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
8 9 10 |
# File 'lib/diamant/mimefile.rb', line 8 def extension @extension end |
Instance Method Details
#validate ⇒ Object
Disable metrics on purpose for big switch rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/diamant/mimefile.rb', line 41 def validate return true if @category == :text case @content_type when 'image/jpeg' @body[0..1] == "\xFF\xD8" when 'image/png' @body[0..7] == "\x89PNG\r\n\u001A\n" when 'image/gif' @body[0..2] == 'GIF' when 'image/webp' @body[0..3] == 'RIFF' && @body[8..11] == 'WEBP' when 'image/bmp' @body[0..2] == 'BM' when 'image/svg+xml', 'application/xml', 'application/rss+xml', 'application/atom+xml' @body[0..5] == '<?xml ' when 'audio/mpeg' @body[0..3] == 'ID3' when 'audio/ogg' @body[0..4] == 'OggS' else false end end |
#validate! ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength
68 69 70 71 72 |
# File 'lib/diamant/mimefile.rb', line 68 def validate! return if validate raise MimeError, "#{@path} does not looks like a #{@content_type} file!" end |