Class: Markdownator::StreamInfo

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#charsetObject (readonly)

Returns the value of attribute charset.



35
36
37
# File 'lib/markdownator/stream_info.rb', line 35

def charset
  @charset
end

#extensionObject (readonly)

Returns the value of attribute extension.



35
36
37
# File 'lib/markdownator/stream_info.rb', line 35

def extension
  @extension
end

#filenameObject (readonly)

Returns the value of attribute filename.



35
36
37
# File 'lib/markdownator/stream_info.rb', line 35

def filename
  @filename
end

#local_pathObject (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

#mimetypeObject (readonly)

Returns the value of attribute mimetype.



35
36
37
# File 'lib/markdownator/stream_info.rb', line 35

def mimetype
  @mimetype
end

#urlObject (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_mimetypeObject

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