Class: Markdownator::StreamInfo
- Inherits:
-
Object
- Object
- Markdownator::StreamInfo
- Defined in:
- lib/markdownator/stream_info.rb
Overview
Immutable value object describing a stream of bytes to be converted.
It carries the hints (extension, mimetype, charset, filename, url, local_path) that converters use to decide whether they can handle a given input.
Constant Summary collapse
- EXTENSION_TO_MIMETYPE =
Maps a lower-case file extension (without the dot) to a mimetype.
{ "txt" => "text/plain", "text" => "text/plain", "md" => "text/markdown", "markdown" => "text/markdown", "html" => "text/html", "htm" => "text/html", "csv" => "text/csv", "json" => "application/json", "xml" => "application/xml", "pdf" => "application/pdf", "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation", "epub" => "application/epub+zip", "zip" => "application/zip", "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "png" => "image/png", "gif" => "image/gif", "tif" => "image/tiff", "tiff" => "image/tiff" }.freeze
Instance Attribute Summary collapse
-
#charset ⇒ Object
readonly
Returns the value of attribute charset.
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#local_path ⇒ Object
readonly
Returns the value of attribute local_path.
-
#mimetype ⇒ Object
readonly
Returns the value of attribute mimetype.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#copy_with(**overrides) ⇒ Object
Returns a new StreamInfo with the given attributes overridden, filling in any attribute not provided from the current instance.
-
#guessed_mimetype ⇒ Object
Best-effort mimetype: the explicit one, otherwise derived from extension.
-
#initialize(mimetype: nil, extension: nil, charset: nil, filename: nil, url: nil, local_path: nil) ⇒ StreamInfo
constructor
A new instance of StreamInfo.
Constructor Details
#initialize(mimetype: nil, extension: nil, charset: nil, filename: nil, url: nil, local_path: nil) ⇒ StreamInfo
Returns a new instance of StreamInfo.
37 38 39 40 41 42 43 44 45 |
# File 'lib/markdownator/stream_info.rb', line 37 def initialize(mimetype: nil, extension: nil, charset: nil, filename: nil, url: nil, local_path: nil) @mimetype = mimetype @extension = normalize_extension(extension) @charset = charset @filename = filename @url = url @local_path = local_path freeze end |
Instance Attribute Details
#charset ⇒ Object (readonly)
Returns the value of attribute charset.
35 36 37 |
# File 'lib/markdownator/stream_info.rb', line 35 def charset @charset end |
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
35 36 37 |
# File 'lib/markdownator/stream_info.rb', line 35 def extension @extension end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
35 36 37 |
# File 'lib/markdownator/stream_info.rb', line 35 def filename @filename end |
#local_path ⇒ Object (readonly)
Returns the value of attribute local_path.
35 36 37 |
# File 'lib/markdownator/stream_info.rb', line 35 def local_path @local_path end |
#mimetype ⇒ Object (readonly)
Returns the value of attribute mimetype.
35 36 37 |
# File 'lib/markdownator/stream_info.rb', line 35 def mimetype @mimetype end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
35 36 37 |
# File 'lib/markdownator/stream_info.rb', line 35 def url @url end |
Instance Method Details
#copy_with(**overrides) ⇒ Object
Returns a new StreamInfo with the given attributes overridden, filling in any attribute not provided from the current instance.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/markdownator/stream_info.rb', line 49 def copy_with(**overrides) self.class.new( mimetype: overrides.fetch(:mimetype, mimetype), extension: overrides.fetch(:extension, extension), charset: overrides.fetch(:charset, charset), filename: overrides.fetch(:filename, filename), url: overrides.fetch(:url, url), local_path: overrides.fetch(:local_path, local_path) ) end |
#guessed_mimetype ⇒ Object
Best-effort mimetype: the explicit one, otherwise derived from extension.
61 62 63 |
# File 'lib/markdownator/stream_info.rb', line 61 def guessed_mimetype mimetype || EXTENSION_TO_MIMETYPE[extension] end |