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.
Expand valid data URIs, open-uri support
- Loading branch information
Showing
5 changed files
with
117 additions
and
23 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 |
---|---|---|
@@ -1,24 +1,7 @@ | ||
require 'uri' | ||
require 'base64' | ||
require 'stringio' | ||
require 'rubygems' | ||
require 'mime/types' | ||
|
||
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 | ||
require 'data_uri/uri' |
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,22 @@ | ||
module URI | ||
|
||
class Data | ||
|
||
def open | ||
io = StringIO.new(data) | ||
OpenURI::Meta.init(io) | ||
io.meta_add_field('content-type', content_type) | ||
if block_given? | ||
begin | ||
yield io | ||
ensure | ||
io.close | ||
end | ||
else | ||
io | ||
end | ||
end | ||
|
||
end | ||
|
||
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,33 @@ | ||
module URI | ||
|
||
class Data < Generic | ||
|
||
COMPONENT = [:scheme, :opaque].freeze | ||
|
||
attr_reader :content_type, :data | ||
|
||
def initialize(*args) | ||
super(*args) | ||
@data = @opaque | ||
if md = MIME::Type::MEDIA_TYPE_RE.match(@data) | ||
offset = md.offset(0) | ||
if offset[0] == 0 | ||
@content_type = md[0] | ||
@data = @data[offset[1] .. -1] | ||
end | ||
end | ||
@content_type ||= 'text/plain' | ||
if base64 = /^;base64/.match(@data) | ||
@data = @data[7 .. -1] | ||
end | ||
unless /^,/.match(@data) | ||
raise 'Invalid data URI' | ||
end | ||
@data = @data[1 .. -1] | ||
@data = base64 ? Base64.decode64(@data) : URI.decode(@data) | ||
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,36 @@ | ||
require 'data_uri' | ||
require 'open-uri' | ||
require 'data_uri/open_uri' | ||
require 'minitest/autorun' | ||
|
||
describe URI::Data do | ||
|
||
describe "a valid data URI" do | ||
|
||
before do | ||
@base64 = "R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAQAICTAEAOw==" | ||
@uri = URI.parse("data:image/gif;base64,#{@base64}") | ||
@data = Base64.decode64(@base64) | ||
end | ||
|
||
it "should open" do | ||
@uri.open.read.must_equal @data | ||
end | ||
|
||
it "should open with a block" do | ||
@uri.open do |io| | ||
io.read.must_equal @data | ||
end | ||
end | ||
|
||
it "should have content_type on opened IO" do | ||
@uri.open.content_type.must_equal 'image/gif' | ||
end | ||
|
||
it "should open on Kernel.open" do | ||
open(@uri).read.must_equal @uri.data | ||
end | ||
|
||
end | ||
|
||
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