Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dball committed Jan 11, 2011
0 parents commit 6581bf4
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.swp
37 changes: 37 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
= Data URI

== Introduction

Data URIs allow resources to be embedding inside a URI. The URI::Data class
provides support for parsing these URIs using the normal URI.parse method.

== Usage

uri = URI.parse('data:image/gif;base64,...')
uri.content_type # 'image/gif
uri.data # Base64 decoded data

== License:

(The MIT License)

Copyright (c) 2010

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
desc "Build a gem file"
task :build do
system "gem build data_uri.gemspec"
end

task :default => :test
16 changes: 16 additions & 0 deletions data_uri.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Gem::Specification.new do |s|
s.name = "data_uri"
s.version = "0.0.1"
s.author = "Donald Ball"
s.email = "[email protected]"
s.homepage = "http://github.com/dball/data_uri"
s.description = "URI class for parsing data URIs"
s.summary = "A URI class for parsing data URIs as per RFC2397"

s.platform = Gem::Platform::RUBY
s.has_rdoc = true
s.extra_rdoc_files = ["README.rdoc"]

s.require_path = 'lib'
s.files = %w(README.rdoc Rakefile) + Dir.glob("lib/**/*")
end
24 changes: 24 additions & 0 deletions lib/data_uri.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'uri'
require 'base64'

module URI

class Data < Generic

COMPONENT = [:scheme, :opaque].freeze
OPAQUE_REGEXP = /^([^;]+);base64,(.+)$/.freeze

attr_reader :content_type, :data

def initialize(*args)
super(*args)
if OPAQUE_REGEXP.match(@opaque)
@content_type = $1
@data = Base64.decode64($2)
end
end
end

@@schemes['DATA'] = Data

end
28 changes: 28 additions & 0 deletions test/data_uri_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'data_uri'
require 'minitest/autorun'

describe URI::Data do

describe "a valid data URI" do

before do
@base64 = "R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7"
@uri = URI.parse("data:image/gif;base64,#{@base64}")
end

it "should parse as a URI::Data object" do
@uri.class.must_equal URI::Data
end

it "should have a content_type of image/gif" do
@uri.content_type.must_equal 'image/gif'
end

it "should have decoded data" do
require 'base64'
@uri.data.must_equal Base64.decode64(@base64)
end

end

end

0 comments on commit 6581bf4

Please sign in to comment.