Class: CarrierWave::DataUri::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave_data_uri/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_uri) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
13
14
15
16
17
18
# File 'lib/carrierwave_data_uri/parser.rb', line 9

def initialize(data_uri)
  if data_uri.match /^data:(.*?);(.*?),(.*)$/
    @type = $1
    @encoder = $2
    @data = $3
    @extension = $1.split('/')[1]
  else
    raise ArgumentError, 'Cannot parse data'
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/carrierwave_data_uri/parser.rb', line 7

def data
  @data
end

#encoderObject (readonly)

Returns the value of attribute encoder.



7
8
9
# File 'lib/carrierwave_data_uri/parser.rb', line 7

def encoder
  @encoder
end

#extensionObject (readonly)

Returns the value of attribute extension.



7
8
9
# File 'lib/carrierwave_data_uri/parser.rb', line 7

def extension
  @extension
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/carrierwave_data_uri/parser.rb', line 7

def type
  @type
end

Instance Method Details

#binary_dataObject



20
21
22
# File 'lib/carrierwave_data_uri/parser.rb', line 20

def binary_data
  @binary_data ||= Base64.decode64 data
end

#to_fileObject



24
25
26
27
28
29
30
31
32
# File 'lib/carrierwave_data_uri/parser.rb', line 24

def to_file
  @file ||= begin
    file = Tempfile.new ['data_uri_upload', ".#{extension}"]
    file.binmode
    file << binary_data
    file.rewind
    file
  end
end