forked from dball/data_uri
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6581bf4
Showing
6 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |