Sha256: 6ce65362a5e2a86256f5b2a41c89c11a4e6b996d2f3cb0e41b76f21b83ac58a8

Contents?: true

Size: 797 Bytes

Versions: 2

Compression:

Stored size: 797 Bytes

Contents

# frozen_string_literal: true

require_relative "uri/version"
require "uri/version"
require "base64"

module Strict
  module Data
    module Uri
      Error = Class.new ArgumentError
      InvalidEncoder = Class.new Error
      InvalidDataUri = Class.new Error

      def self.encode(data, type = "text/plain")
        "data:#{type};base64,#{Base64.strict_encode64(data).rstrip}"
      end

      def self.decode(uri)
        raise InvalidDataUri, "invalid data URI: #{uri.inspect}" unless uri.match(/^data:(.*?);(.*?),(.*)$/)

        encoder = Regexp.last_match(2)
        data = Regexp.last_match(3)

        case encoder
        when "base64" then Base64.strict_decode64(data)
        else raise InvalidEncoder, "missing or invalid encoder: #{encoder}"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
strict-data-uri-0.1.1 lib/strict/data/uri.rb
strict-data-uri-0.1.0 lib/strict/data/uri.rb