Class: DataURI

Inherits:
Object
  • Object
show all
Defined in:
lib/file_to_data_uri.rb,
lib/file_to_data_uri/version.rb

Overview

DataURI class for generating data URIs from files or file-like objects

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ DataURI

Initialize with a file path or file-like object

Parameters:

  • source (String, #read)

    File path or file-like object



20
21
22
# File 'lib/file_to_data_uri.rb', line 20

def initialize(source)
  @source = source
end

Class Method Details

.convert(source) ⇒ String

Convert a file path or file-like object to a data URI string

Parameters:

  • source (String, #read)

    File path or file-like object

Returns:

  • (String)

    A data URI representation of the file



13
14
15
# File 'lib/file_to_data_uri.rb', line 13

def self.convert(source)
  new(source).to_s
end

Instance Method Details

#to_sString

Convert to data URI string

Returns:

  • (String)

    A data URI representation of the file



27
28
29
30
31
32
33
# File 'lib/file_to_data_uri.rb', line 27

def to_s
  content = read_content
  mime_type = determine_mime_type
  encoded_content = Base64.strict_encode64(content)

  "data:#{mime_type};base64,#{encoded_content}"
end