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 +4 -4
- data/README.md +25 -1
- data/carrierwave-data-uri.gemspec +1 -0
- data/lib/carrierwave_data_uri/mount.rb +11 -1
- data/lib/carrierwave_data_uri/parser.rb +10 -10
- data/lib/carrierwave_data_uri/version.rb +1 -1
- data/spec/integration_spec.rb +23 -0
- data/spec/parser_spec.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f0960317b08eac242c66685efc83c7eeb4864867
|
|
4
|
+
data.tar.gz: 2f2ed1296b4c0c41cfb93bdb68746d590e86781c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 38f25bcfd8ae51506b2025202fff28f20ae9539d5724dd381659180a5404967976696c885c61f13d0320c53d621ea2f99a69a53de6e1a812ee9f2aa348e8a3d1
|
|
7
|
+
data.tar.gz: 4638f4f9a4ab5f862407869e078d9e228e8097a32e6bd52293b11e5734c640fd408de3751419818a95e4f9e16e1b15598887c86c28c82b38c30950e67dc2d873
|
data/README.md
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
# CarrierWave::DataUri
|
|
1
|
+
# CarrierWave::DataUri
|
|
2
|
+
|
|
3
|
+
[](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 )
|
|
@@ -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
|
-
|
|
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 '
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
@
|
|
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
|
data/spec/integration_spec.rb
CHANGED
|
@@ -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
|
data/spec/parser_spec.rb
CHANGED
|
@@ -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
|
|
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
|
|
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:
|
|
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
|