Class: CarrierWave::DataUri::Parser
- Inherits:
-
Object
- Object
- CarrierWave::DataUri::Parser
- Defined in:
- lib/carrierwave_data_uri/parser.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#encoder ⇒ Object
readonly
Returns the value of attribute encoder.
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #binary_data ⇒ Object
-
#initialize(data_uri) ⇒ Parser
constructor
A new instance of Parser.
- #to_file ⇒ Object
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/carrierwave_data_uri/parser.rb', line 7 def data @data end |
#encoder ⇒ Object (readonly)
Returns the value of attribute encoder.
7 8 9 |
# File 'lib/carrierwave_data_uri/parser.rb', line 7 def encoder @encoder end |
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
7 8 9 |
# File 'lib/carrierwave_data_uri/parser.rb', line 7 def extension @extension end |
#type ⇒ Object (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_data ⇒ Object
20 21 22 |
# File 'lib/carrierwave_data_uri/parser.rb', line 20 def binary_data @binary_data ||= Base64.decode64 data end |
#to_file ⇒ Object
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 |