Module: DataURI

Defined in:
lib/data-uri.rb,
lib/data-uri/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.decode(uri) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/data-uri.rb', line 9

def self.decode(uri)
  if uri.match(%r{^data:(.*?);(.*?),(.*)$})
    type = $1
    encoder = $2
    data = $3
    if encoder == "base64"
      return Base64.decode64(data)
    end
  end

  raise "Illegal format error: #{uri.inspect}"
end

.encode(data, type = "text/plain") ⇒ Object



5
6
7
# File 'lib/data-uri.rb', line 5

def self.encode(data, type="text/plain")
  "data:" + type + ";base64," + Base64.encode64(data).rstrip
end