Class: URI::Data

Inherits:
Generic
  • Object
show all
Defined in:
lib/data_uri/uri.rb,
lib/data_uri/open_uri.rb

Constant Summary collapse

COMPONENT =
[:scheme, :opaque]
MIME_TYPE_RE =
%r{^([-\w.+]+/[-\w.+]*)}
MIME_PARAM_RE =
/^;([-\w.+]+)=([^;,]+)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Data

Returns a new instance of Data.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/data_uri/uri.rb', line 9

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

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



7
8
9
# File 'lib/data_uri/uri.rb', line 7

def content_type
  @content_type
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/data_uri/uri.rb', line 7

def data
  @data
end

Class Method Details

.build(arg) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/data_uri/uri.rb', line 44

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

Instance Method Details

#openObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/data_uri/open_uri.rb', line 5

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