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.



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

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.



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

def data
  @data
end

#encoderObject (readonly)

Returns the value of attribute encoder.



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

def encoder
  @encoder
end

#extensionObject (readonly)

Returns the value of attribute extension.



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

def extension
  @extension
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#binary_dataObject



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

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

#to_file(options = {}) ⇒ Object



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

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