module DataURL
Utilities for generating data URLs for use in HTML, CSS, etc.
Public Class Methods
generate(mime_type:, content:)
click to toggle source
Generate a data URL. @param mime_type [String] media type of the data to encode @param content [String] content to encode @return [String] RFC 2397-compliant data URL.
# File lib/dataurl.rb, line 9 def self.generate(mime_type:, content:) "data:#{mime_type};base64,#{[content].pack('m0')}" end
guess_mime(filename)
click to toggle source
Guess the MIME type of a file using mailcap's /etc/mime.types. @param filename [String] filename to check against mime.types @return [String] media type or nil if it could not be detected
# File lib/dataurl.rb, line 16 def self.guess_mime(filename) extension = File.extname(filename)[1..-1].downcase File.foreach('/etc/mime.types') do |line| match = /\A([^#\s]+)\s+([^#]+)/.match(line) return match[1] if match && match[2].split.include?(extension) end nil end