kirel-data-uri 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/data-uri.gemspec +46 -0
- data/lib/data-uri.rb +83 -0
- data/test/data_uri_test.rb +44 -0
- data/test/test_helper.rb +10 -0
- metadata +64 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Daniel Kirsch
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "data-uri"
|
8
|
+
gem.summary = %Q{Provides data: uri scheme and methods to open and read the data.}
|
9
|
+
gem.email = "danishkirel@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/danishkirel/data-uri"
|
11
|
+
gem.authors = ["Daniel Kirsch"]
|
12
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
13
|
+
end
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'rake/testtask'
|
19
|
+
Rake::TestTask.new(:test) do |test|
|
20
|
+
test.libs << 'lib' << 'test'
|
21
|
+
test.pattern = 'test/**/*_test.rb'
|
22
|
+
test.verbose = true
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'rcov/rcovtask'
|
27
|
+
Rcov::RcovTask.new do |test|
|
28
|
+
test.libs << 'test'
|
29
|
+
test.pattern = 'test/**/*_test.rb'
|
30
|
+
test.verbose = true
|
31
|
+
end
|
32
|
+
rescue LoadError
|
33
|
+
task :rcov do
|
34
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
task :default => :test
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
if File.exist?('VERSION.yml')
|
44
|
+
config = YAML.load(File.read('VERSION.yml'))
|
45
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
46
|
+
else
|
47
|
+
version = ""
|
48
|
+
end
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "data-uri #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
55
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.2
|
data/data-uri.gemspec
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{data-uri}
|
5
|
+
s.version = "0.0.2"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Daniel Kirsch"]
|
9
|
+
s.date = %q{2009-07-08}
|
10
|
+
s.email = %q{danishkirel@gmail.com}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION",
|
22
|
+
"data-uri.gemspec",
|
23
|
+
"lib/data-uri.rb",
|
24
|
+
"test/data_uri_test.rb",
|
25
|
+
"test/test_helper.rb"
|
26
|
+
]
|
27
|
+
s.homepage = %q{http://github.com/danishkirel/data-uri}
|
28
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
s.rubygems_version = %q{1.3.4}
|
31
|
+
s.summary = %q{Provides data: uri scheme and methods to open and read the data.}
|
32
|
+
s.test_files = [
|
33
|
+
"test/data_uri_test.rb",
|
34
|
+
"test/test_helper.rb"
|
35
|
+
]
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
42
|
+
else
|
43
|
+
end
|
44
|
+
else
|
45
|
+
end
|
46
|
+
end
|
data/lib/data-uri.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'base64'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
module OpenData
|
6
|
+
|
7
|
+
module Meta
|
8
|
+
def Meta.init(obj, src) # :nodoc:
|
9
|
+
obj.extend Meta
|
10
|
+
obj.instance_eval {
|
11
|
+
@content_type = src.content_type
|
12
|
+
@meta = {}
|
13
|
+
}
|
14
|
+
end
|
15
|
+
attr_reader :content_type, :meta
|
16
|
+
# TODO charset content_encoding
|
17
|
+
end
|
18
|
+
|
19
|
+
module OpenRead
|
20
|
+
def read
|
21
|
+
s = data.dup
|
22
|
+
Meta.init(s, self) unless s.respond_to? :meta
|
23
|
+
s
|
24
|
+
end
|
25
|
+
|
26
|
+
def open *rest, &block
|
27
|
+
io = StringIO.new data
|
28
|
+
Meta.init(io, self) unless io.respond_to? :meta
|
29
|
+
if block_given?
|
30
|
+
begin
|
31
|
+
yield io
|
32
|
+
ensure
|
33
|
+
io.close
|
34
|
+
end
|
35
|
+
else
|
36
|
+
io
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
module URI
|
44
|
+
|
45
|
+
# copied from http://necronomicorp.com/lab/1 and extended
|
46
|
+
class Data < Generic
|
47
|
+
|
48
|
+
def initialize(scheme, userinfo, host, port,
|
49
|
+
registry, path, opaque, query, fragment)
|
50
|
+
super
|
51
|
+
|
52
|
+
notdata, data = opaque.split(",", 2)
|
53
|
+
match = URI.decode(notdata).match(/^(.*?)(;base64)?$/)
|
54
|
+
|
55
|
+
@mediatype = match[1]
|
56
|
+
if @mediatype.empty?
|
57
|
+
@mediatype = "text/plain;charset=US-ASCII"
|
58
|
+
@content_type = "text/plain"
|
59
|
+
@charset = "US-ASCII"
|
60
|
+
elsif @mediatype[0] == ?;
|
61
|
+
@mediatype = "text/plain#{@mediatype}"
|
62
|
+
@content_type = "text/plain"
|
63
|
+
# TODO charset, other params (params hash?)
|
64
|
+
else
|
65
|
+
m = @mediatype.split(';')
|
66
|
+
@content_type = m.first
|
67
|
+
# TODO charset, other params (params hash?)
|
68
|
+
end
|
69
|
+
|
70
|
+
@data = unless match[2].nil?
|
71
|
+
Base64.decode64(data)
|
72
|
+
else
|
73
|
+
URI.decode(data)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
attr_reader :content_type, :data
|
78
|
+
|
79
|
+
include OpenData::OpenRead
|
80
|
+
|
81
|
+
end
|
82
|
+
@@schemes["DATA"] = Data
|
83
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DataUriTest < Test::Unit::TestCase
|
4
|
+
should "parse" do
|
5
|
+
uris = %w(
|
6
|
+
data:text/plain;charset=iso-8859-7,%be%fa%be
|
7
|
+
data:application/vnd-xxx-query,select_vcount,fcol_from_fieldtable/local
|
8
|
+
data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7
|
9
|
+
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IAAAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1JREFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jqch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0vr4MkhoXe0rZigAAAABJRU5ErkJggg==
|
10
|
+
data:,A%20brief%20note
|
11
|
+
)
|
12
|
+
uris.each do |uri|
|
13
|
+
assert(URI::Data === URI.parse(uri), "D'oh!")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
should "give StringIO" do
|
18
|
+
assert(StringIO === URI.parse('data:text/plain;charset=iso-8859-7,%be%fa%be').open)
|
19
|
+
end
|
20
|
+
|
21
|
+
should "give String" do
|
22
|
+
assert(String === URI.parse('data:text/plain;charset=iso-8859-7,%be%fa%be').read)
|
23
|
+
end
|
24
|
+
|
25
|
+
should "extend" do
|
26
|
+
assert(OpenData::Meta === URI.parse('data:text/plain;charset=iso-8859-7,%be%fa%be').open)
|
27
|
+
assert(OpenData::Meta === URI.parse('data:text/plain;charset=iso-8859-7,%be%fa%be').read)
|
28
|
+
end
|
29
|
+
|
30
|
+
context "parsed data uri" do
|
31
|
+
setup do
|
32
|
+
@uri = URI.parse('data:text/plain;charset=iso-8859-7,%be%fa%be')
|
33
|
+
end
|
34
|
+
|
35
|
+
should "have correct content type in meta" do
|
36
|
+
assert_equal("text/plain", @uri.read.content_type)
|
37
|
+
@uri.open do |io|
|
38
|
+
assert_equal("text/plain", io.content_type)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kirel-data-uri
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Kirsch
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-08 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: danishkirel@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- data-uri.gemspec
|
33
|
+
- lib/data-uri.rb
|
34
|
+
- test/data_uri_test.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
has_rdoc: false
|
37
|
+
homepage: http://github.com/danishkirel/data-uri
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --charset=UTF-8
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: "Provides data: uri scheme and methods to open and read the data."
|
62
|
+
test_files:
|
63
|
+
- test/data_uri_test.rb
|
64
|
+
- test/test_helper.rb
|