data-uri-0.1.0/0000755000175000017500000000000013432121723013752 5ustar utkarsh2102utkarsh2102data-uri-0.1.0/lib/0000755000175000017500000000000013432121723014520 5ustar utkarsh2102utkarsh2102data-uri-0.1.0/lib/data_uri.rb0000644000175000017500000000011213432121723016627 0ustar utkarsh2102utkarsh2102require 'uri' require 'base64' require 'stringio' require 'data_uri/uri' data-uri-0.1.0/lib/data_uri/0000755000175000017500000000000013432121723016310 5ustar utkarsh2102utkarsh2102data-uri-0.1.0/lib/data_uri/open_uri.rb0000644000175000017500000000046113432121723020456 0ustar utkarsh2102utkarsh2102module 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 data-uri-0.1.0/lib/data_uri/uri.rb0000644000175000017500000000346413432121723017443 0ustar utkarsh2102utkarsh2102module URI class Data < Generic COMPONENT = [:scheme, :opaque].freeze MIME_TYPE_RE = %r{^([-\w.+]+/[-\w.+]*)}.freeze MIME_PARAM_RE = /^;([-\w.+]+)=([^;,]+)/.freeze attr_reader :content_type, :data def initialize(*args) if args.length == 1 uri = args.first.to_s unless uri.match(/^data:/) raise URI::InvalidURIError.new('Invalid Data URI: ' + args.first.inspect) end @scheme = 'data' @opaque = uri[5 .. -1] else super(*args) end @data = @opaque if md = MIME_TYPE_RE.match(@data) @content_type = md[1] @data = @data[@content_type.length .. -1] end @content_type ||= 'text/plain' @mime_params = {} while md = MIME_PARAM_RE.match(@data) @mime_params[md[1]] = md[2] @data = @data[md[0].length .. -1] end if base64 = /^;base64/.match(@data) @data = @data[7 .. -1] end unless /^,/.match(@data) raise URI::InvalidURIError.new('Invalid data URI') end @data = @data[1 .. -1] @data = base64 ? Base64.decode64(@data) : URI.decode(@data) if @data.respond_to?(:force_encoding) && charset = @mime_params['charset'] @data.force_encoding(charset) end end def self.build(arg) data = nil content_type = nil case arg when IO data = arg when Hash data = arg[:data] content_type = arg[:content_type] end raise 'Invalid build argument: ' + arg.inspect unless data if !content_type && data.respond_to?(:content_type) content_type = data.content_type end new('data', nil, nil, nil, nil, nil, "#{content_type};base64,#{Base64.encode64(data.read).chop}", nil, nil) end end @@schemes['DATA'] = Data end data-uri-0.1.0/Rakefile0000644000175000017500000000022513432121723015416 0ustar utkarsh2102utkarsh2102require 'rake/testtask' Rake::TestTask.new task :default => :test desc "Build a gem file" task :build do system "gem build data_uri.gemspec" end data-uri-0.1.0/data_uri.gemspec0000644000175000017500000000273713432121723017120 0ustar utkarsh2102utkarsh2102######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: data_uri 0.1.0 ruby lib Gem::Specification.new do |s| s.name = "data_uri".freeze s.version = "0.1.0" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["Donald Ball".freeze] s.date = "2014-02-18" s.description = "URI class for parsing data URIs".freeze s.email = "donald.ball@gmail.com".freeze s.extra_rdoc_files = ["README.rdoc".freeze] s.files = ["README.rdoc".freeze, "Rakefile".freeze, "lib/data_uri.rb".freeze, "lib/data_uri/open_uri.rb".freeze, "lib/data_uri/uri.rb".freeze] s.homepage = "http://github.com/dball/data_uri".freeze s.rubygems_version = "2.7.6".freeze s.summary = "A URI class for parsing data URIs as per RFC2397".freeze if s.respond_to? :specification_version then s.specification_version = 3 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 0"]) else s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) end else s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) end end data-uri-0.1.0/README.rdoc0000644000175000017500000000717713432121723015574 0ustar utkarsh2102utkarsh2102= Data URI {Build Status}[https://travis-ci.org/dball/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. I wrote it to support embedding binary data inside JSON messages in a relatively reasonable way. If you find some other use for it, please drop me a line. == Usage require 'data_uri' uri = URI::Data.new('data:image/gif;base64,...') uri.content_type # image/gif uri.data # Base64 decoded data Data URIs can play nicely with open-uri: require 'open-uri' require 'data_uri/open_uri' open(uri) do |io| io.content_type # image/gif io.read # decoded data end There is no support for creating data URI strings, but it would be trivial to add if anyone's interested. == Features & Limitations URI.parse knows about URI::Data, but unfortunately, its regexp for splitting URIs into components maxes out at 92 characters for an opaque URI, which is far too small to be useful for data URIs. It accepts URIs with charset MIME parameters: data:text/html;charset=utf-8,... and if the String object has a force_encoding method, as in ruby-1.9.x, it will call it with the value of the charset parameter on the decoded data. Other MIME parameters are parsed and stored, but are not exposed. The base64 pseudo-parameter must appear immediately prior to the data, as per the RFC. Google Chrome apparently misbehaves in this regard, so if this causes grief to anyone, yell and I'll relax the constraint. If base64 coding is not specified, the data are URI decoded. Some non-authoritative documentation about data URIs state that whitespace is allowed and ignored within them. The RFC seems to contradict this interpretation, but support seems to be widespread in the browser world. URI::Data cannot parse URIs containing whitespace. If this causes grief to anyone, yell and I'll either add a class method to URI::Data for explicit relaxed parsing, and/or figure out how to convince URI::Generic to be more open minded about the kinds of URIs it allows. Even when using the open-uri support file, Kernel.open will not accept string data URIs, only URI::Data objects. Open-uri is opinionated about the formats of the URIs it will try to open, and it's ambiguous to me at least if data URIs can necessarily be distinguished from filesystem paths on all platforms. Again, if this causes undue pain and suffering, we can commit minor violence to open-uri to convince it to see reason. == License: (The MIT License) Copyright (c) 2010 Donald Ball 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.