carrierwave-data-uri 0.0.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6763b9e42a19e7766f8eb11e610dedc2849e5e49
4
- data.tar.gz: 8499f9d8ad15cd880ed7bfba07b03a58d1cfe0f3
3
+ metadata.gz: f0960317b08eac242c66685efc83c7eeb4864867
4
+ data.tar.gz: 2f2ed1296b4c0c41cfb93bdb68746d590e86781c
5
5
  SHA512:
6
- metadata.gz: 4319d0ed174376671fa844abd7092ba414198db1ca2d0f3be136db969cfc9526e7e607247c70091e13163aa988ba2f4019bb58661257e3c72c71fc78b3bfae38
7
- data.tar.gz: df9963c6c0dc6f793df5ffb5fa3a97870b8ff7b8f75e2984d276a97684d16fb9730d1e83f0864ffefc1d085f2106480c82cde8bb52d575f3c57fb3c471abcbcd
6
+ metadata.gz: 38f25bcfd8ae51506b2025202fff28f20ae9539d5724dd381659180a5404967976696c885c61f13d0320c53d621ea2f99a69a53de6e1a812ee9f2aa348e8a3d1
7
+ data.tar.gz: 4638f4f9a4ab5f862407869e078d9e228e8097a32e6bd52293b11e5734c640fd408de3751419818a95e4f9e16e1b15598887c86c28c82b38c30950e67dc2d873
data/README.md CHANGED
@@ -1,7 +1,11 @@
1
- # CarrierWave::DataUri [![Build Status](https://travis-ci.org/timsly/carrierwave-data-uri.svg)](https://travis-ci.org/timsly/carrierwave-data-uri)
1
+ # CarrierWave::DataUri
2
+
3
+ [![Build Status](https://travis-ci.org/timsly/carrierwave-data-uri.svg)](https://travis-ci.org/timsly/carrierwave-data-uri)
2
4
 
3
5
  Carrierwave plugin that allows create image from data uri
4
6
 
7
+ :warning: Please read the [migration notes](#migration-notes) before upgrading to a new major version.
8
+
5
9
  ## Installation
6
10
 
7
11
  Add this line to your application's Gemfile:
@@ -34,6 +38,20 @@ user.avatar_data_uri = 'data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP////8AAP///y
34
38
  user.save
35
39
  ```
36
40
 
41
+ and then retrieve passed data
42
+
43
+ ```
44
+ user.avatar_data_uri # => data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP////8AAP///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw==
45
+ ```
46
+
47
+ In case emtpy/invalid data error won't be raised.
48
+ Loud version should be used instead to raise an error on emtpy/invalid data:
49
+
50
+ ```
51
+ user = User.find 123
52
+ user.avatar_data_uri_loud = '' # will raise `CarrierWave::DataUri::InvalidData` error
53
+ ```
54
+
37
55
  Optionally, to customize the file name, specify the `#{column}_data_filename` and `#{column}_data_mimetype` attributes before the `#{column}_data_uri` attribute.
38
56
 
39
57
  ```ruby
@@ -44,6 +62,12 @@ user.avatar_data_uri = 'data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP////8AAP///y
44
62
  user.save
45
63
  ```
46
64
 
65
+ ## Migration Notes
66
+
67
+ #### Version 0.2.0
68
+
69
+ - Assigning empty data is not raising an error now. Use loud version(`model.field_data_uri_loud =`) instead.
70
+
47
71
  ## Contributing
48
72
 
49
73
  1. Fork it ( https://github.com/[my-github-username]/carrierwave-data-uri/fork )
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'carrierwave'
22
+ spec.add_dependency 'data_uri'
22
23
 
23
24
  spec.add_development_dependency 'bundler', '~> 1.7'
24
25
  spec.add_development_dependency 'rake', '~> 10.0'
@@ -8,10 +8,20 @@ module CarrierWave
8
8
  super
9
9
 
10
10
  class_eval <<-RUBY, __FILE__, __LINE__+1
11
+ attr_reader :#{column}_data_uri
11
12
  attr_accessor :#{column}_data_filename, :#{column}_data_mimetype
12
13
 
14
+ def #{column}_data_uri_loud=(data)
15
+ self.#{column} = Parser.new(data).to_file(
16
+ original_filename: self.#{column}_data_filename,
17
+ content_type: self.#{column}_data_mimetype
18
+ )
19
+ end
20
+
13
21
  def #{column}_data_uri=(data)
14
- self.#{column} = Parser.new(data).to_file original_filename: self.#{column}_data_filename, content_type: self.#{column}_data_mimetype
22
+ @#{column}_data_uri = data
23
+ self.#{column}_data_uri_loud = data
24
+ rescue CarrierWave::DataUri::InvalidData
15
25
  end
16
26
  RUBY
17
27
  end
@@ -1,4 +1,4 @@
1
- require 'base64'
1
+ require 'data_uri'
2
2
 
3
3
  module CarrierWave
4
4
  module DataUri
@@ -6,18 +6,16 @@ module CarrierWave
6
6
  attr_reader :type, :encoder, :data, :extension
7
7
 
8
8
  def initialize(data_uri)
9
- if data_uri.match /^data:(.*?);(.*?),(.*)$/
10
- @type = $1
11
- @encoder = $2
12
- @data = $3
13
- @extension = $1.split('/')[1]
14
- else
15
- raise ArgumentError, 'Cannot parse data'
16
- end
9
+ uri = URI::Data.new data_uri
10
+ @type = uri.content_type
11
+ @extension = @type.split('/')[1]
12
+ @data = uri.data
13
+ rescue URI::InvalidURIError
14
+ raise InvalidData, 'Cannot parse data'
17
15
  end
18
16
 
19
17
  def binary_data
20
- @binary_data ||= Base64.decode64 data
18
+ @data
21
19
  end
22
20
 
23
21
  def to_file(options = {})
@@ -32,5 +30,7 @@ module CarrierWave
32
30
  end
33
31
  end
34
32
  end
33
+
34
+ class InvalidData < StandardError; end
35
35
  end
36
36
  end
@@ -1,7 +1,7 @@
1
1
  module Carrierwave
2
2
  module Data
3
3
  module Uri
4
- VERSION = '0.0.3'
4
+ VERSION = '0.2.0'
5
5
  end
6
6
  end
7
7
  end
@@ -56,4 +56,27 @@ RSpec.describe 'Integration' do
56
56
 
57
57
  expect(image.src.file.filename).to end_with '.jpeg'
58
58
  end
59
+
60
+ it 'should allow to retrieve passed data' do
61
+ image.src_data_uri = gif_data_uri
62
+
63
+ expect(image.src_data_uri).to eql gif_data_uri
64
+ end
65
+
66
+ it 'should not raise an error on assignig empty string' do
67
+ expect{image.src_data_uri = ''}.not_to raise_error
68
+ end
69
+
70
+ it 'should allow to assign data to the loud version of setter but disallow to retrieve passed data' do
71
+ image.src_data_uri_loud = gif_data_uri
72
+ image.save!
73
+
74
+ expect(image[:src]).to be_present
75
+ expect(image.src.file.filename).to end_with '.gif'
76
+ expect(image.src_data_uri).to be_nil
77
+ end
78
+
79
+ it 'should raise an error on assignig empty string to the loud version' do
80
+ expect{image.src_data_uri_loud = ''}.to raise_error CarrierWave::DataUri::InvalidData
81
+ end
59
82
  end
@@ -5,7 +5,7 @@ RSpec.describe CarrierWave::DataUri::Parser do
5
5
 
6
6
  describe 'initialization' do
7
7
  it 'should raise error when data is invalid' do
8
- expect { CarrierWave::DataUri::Parser.new 'invadli_data' }.to raise_error ArgumentError
8
+ expect { CarrierWave::DataUri::Parser.new 'invadli_data' }.to raise_error CarrierWave::DataUri::InvalidData
9
9
  end
10
10
 
11
11
  it 'should parse data on init' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-data-uri
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tima Maslyuchenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-02 00:00:00.000000000 Z
11
+ date: 2017-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carrierwave
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: data_uri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement