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(options = {}) ⇒ Object
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
6 7 8 |
# File 'lib/carrierwave_data_uri/parser.rb', line 6 def data @data end |
#encoder ⇒ Object (readonly)
Returns the value of attribute encoder.
6 7 8 |
# File 'lib/carrierwave_data_uri/parser.rb', line 6 def encoder @encoder end |
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
6 7 8 |
# File 'lib/carrierwave_data_uri/parser.rb', line 6 def extension @extension end |
#type ⇒ Object (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_data ⇒ Object
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( = {}) @file ||= begin file = Tempfile.new ['data_uri_upload', ".#{extension}"] file.binmode file << binary_data file.rewind file.original_filename = [:original_filename] file.content_type = [:content_type] file end end |