Class: DataStory::DataURI

Inherits:
Object
  • Object
show all
Defined in:
lib/datastory/data_uri.rb

Class Method Summary collapse

Class Method Details

.from_data(file_type, data) ⇒ Object

Public: Generates a data uri from a file type and data.



7
8
9
10
# File 'lib/datastory/data_uri.rb', line 7

def self.from_data(file_type, data)
  encoded_data = Base64.strict_encode64(data)
  "data:#{file_type};base64,%s" % encoded_data
end

.from_file(path) ⇒ Object

Public: Loads the file at file_path and converts it to a data uri.



13
14
15
16
17
18
19
20
21
22
# File 'lib/datastory/data_uri.rb', line 13

def self.from_file(path)
  file_type = `file --mime -b #{path}`
  
  data = nil
  File.open(path, "rb") do |file|
    data = file.read
  end

  from_data(file_type, data)
end