carrierwave-base64 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d249b3b150b00ce5d437c1800800c7ccad72c8fb
4
- data.tar.gz: 9c9b87841ced55ecc87b887b3de826af16100262
3
+ metadata.gz: 6cf760a50a5b061dd501f3ef738c93e11deef874
4
+ data.tar.gz: b934e247e3419fbc7adcbaf7bb66d2f15a1442e0
5
5
  SHA512:
6
- metadata.gz: 6cf7446b85f54a298aae229b152382c2597fbde5b3ffb229bbe181aa3875bffce41affbddfa1cfdcee3f8351fca0a6298d3c482fe6beaf4736812312406c1b73
7
- data.tar.gz: e6e6ac3219ac7116d0e8373e244eb63b8806cb2fbb141cdd1758f39a78d789d44333563060b36909dff0fd6c0bdcd16ecd01674ffa6aa087face8f17f6f1a523
6
+ metadata.gz: c5e14194c1a0b7e76d32c708de7f46c675bdb9f2cd642f6cc2d75956e9c1ea71d9a4e8f7251e29ab978fb2571ccf26fb63bf50b658a16bb0e255aeadc2f85309
7
+ data.tar.gz: cda44171485b7081c9b005ac372a1a88b0a8cdf7dc0a8fb92392593532353ea1818205f060ec6c379b6614ad70df43e26a4329de21624d769574c189e6295124
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ Documentation:
2
+ Enabled: false
data/.travis.yml CHANGED
@@ -2,14 +2,20 @@ language: ruby
2
2
 
3
3
  rvm:
4
4
  - 2.0.0
5
- - 2.1.5
6
- - 2.2.2
5
+ - 2.1.9
6
+ - 2.2.5
7
+ - 2.3.1
7
8
  - ruby-head
8
9
 
10
+ matrix:
11
+ allow_failures:
12
+ - rvm: ruby-head
13
+
9
14
  gemfile:
10
15
  - Gemfile
11
16
 
12
17
  sudo: false
13
18
 
14
- notifications:
15
- email: false
19
+ script:
20
+ - bundle exec rspec
21
+ - bundle exec rubocop
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # carrierwave-base64 changelog
2
+
3
+ ## 2.3.0
4
+
5
+ - Added `:file_name` option for `mount_base64_uploader` method. All base64 uploads for this attribute will use the given filename for the stored file. The `:file_name` option should not contain the file extention (it will be taken from the content type of base64 string). (@HarenBroog, thanks for the idea)
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in carrierwave-base64.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -7,7 +7,7 @@ Upload files encoded as base64 to carrierwave.
7
7
 
8
8
  This small gem can be useful for API's that interact with mobile devices.
9
9
 
10
- As of version 2.2.0, this gem requires Ruby 2.0 or higher
10
+ As of version 2.3.0, this gem requires Ruby 2.0 or higher
11
11
 
12
12
  ## Installation
13
13
 
@@ -33,6 +33,16 @@ mount_base64_uploader :image, ImageUploader
33
33
 
34
34
  Now you can also upload files by passing an encoded base64 string to the attribute.
35
35
 
36
+ ## Setting the file name
37
+
38
+ To set the file name for the uploaded files, use the `:file_name` option:
39
+
40
+ ```ruby
41
+ mount_base64_uploader :image, ImageUploader, file_name: 'userpic'
42
+ ```
43
+
44
+ This value will be used, when naming stored files. Extention will be taked from the base64 content-type spec.
45
+
36
46
  ## Data format
37
47
 
38
48
  The string with the encoded data, should be prefixed with Data URI scheme format:
data/Rakefile CHANGED
@@ -1,10 +1,8 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
 
3
- require "rspec/core/rake_task"
3
+ require 'rspec/core/rake_task'
4
4
 
5
- desc "Run all examples"
6
- RSpec::Core::RakeTask.new(:spec) do |t|
7
- t.rspec_opts = %w[--color]
8
- end
5
+ desc 'Run all examples'
6
+ RSpec::Core::RakeTask.new(:spec)
9
7
 
10
8
  task default: [:spec]
@@ -1,31 +1,33 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path("../lib", __FILE__)
2
+ lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
- require "carrierwave/base64/version"
5
+ require 'carrierwave/base64/version'
6
6
 
7
7
  Gem::Specification.new do |spec|
8
- spec.name = "carrierwave-base64"
8
+ spec.name = 'carrierwave-base64'
9
9
  spec.version = Carrierwave::Base64::VERSION
10
- spec.authors = ["Yury Lebedev"]
11
- spec.email = ["lebedev.yurii@gmail.com"]
12
- spec.summary = %q{Upload images encoded as base64 to carrierwave.}
13
- spec.description = %q{This gem can be useful, if you need to upload files to your API from mobile devises.}
14
- spec.homepage = "https://github.com/lebedev-yury/carrierwave-base64"
15
- spec.license = "MIT"
10
+ spec.authors = ['Yury Lebedev']
11
+ spec.email = ['lebedev.yurii@gmail.com']
12
+ spec.summary = 'Upload images encoded as base64 to carrierwave.'
13
+ spec.description = 'This gem can be useful, if you need to upload files '\
14
+ 'to your API from mobile devises.'
15
+ spec.homepage = 'https://github.com/lebedev-yury/carrierwave-base64'
16
+ spec.license = 'MIT'
16
17
 
17
18
  spec.files = `git ls-files -z`.split("\x0")
18
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
- spec.require_paths = ["lib"]
21
+ spec.require_paths = ['lib']
21
22
 
22
- spec.add_dependency "carrierwave", [">= 0.8.0", "< 0.12.0"]
23
+ spec.add_dependency 'carrierwave', ['>= 0.8.0', '< 0.12.0']
23
24
 
24
- spec.add_development_dependency "rails", ">= 3.2.0"
25
- spec.add_development_dependency "sqlite3"
26
- spec.add_development_dependency "bundler", "~> 1.7"
27
- spec.add_development_dependency "rake", ["~> 10.0"]
28
- spec.add_development_dependency "rspec", ["~> 2.14"]
29
- spec.add_development_dependency "sham_rack"
30
- spec.add_development_dependency "pry"
25
+ spec.add_development_dependency 'rails', '>= 3.2.0'
26
+ spec.add_development_dependency 'sqlite3'
27
+ spec.add_development_dependency 'bundler', '~> 1.7'
28
+ spec.add_development_dependency 'rake', ['~> 10.0']
29
+ spec.add_development_dependency 'rspec', ['~> 2.14']
30
+ spec.add_development_dependency 'sham_rack'
31
+ spec.add_development_dependency 'pry'
32
+ spec.add_development_dependency 'rubocop'
31
33
  end
@@ -1,6 +1,6 @@
1
- require "carrierwave/base64/version"
2
- require "carrierwave/base64/base64_string_io"
3
- require "carrierwave/base64/adapter"
1
+ require 'carrierwave/base64/version'
2
+ require 'carrierwave/base64/base64_string_io'
3
+ require 'carrierwave/base64/adapter'
4
4
 
5
5
  module Carrierwave
6
6
  module Base64
@@ -10,7 +10,8 @@ module Carrierwave
10
10
  end
11
11
 
12
12
  ActiveSupport.on_load :mongoid do
13
- Mongoid::Document::ClassMethods.send :include, Carrierwave::Base64::Adapter
13
+ Mongoid::Document::ClassMethods.send :include,
14
+ Carrierwave::Base64::Adapter
14
15
  end
15
16
  end
16
17
  end
@@ -7,11 +7,12 @@ module Carrierwave
7
7
  define_method "#{attribute}=" do |data|
8
8
  send "#{attribute}_will_change!" if data.present?
9
9
 
10
- if data.present? && data.is_a?(String) && data.strip.start_with?("data")
11
- super(Carrierwave::Base64::Base64StringIO.new(data.strip))
12
- else
13
- super(data)
14
- end
10
+ super(data) unless data.is_a?(String) &&
11
+ data.strip.start_with?('data')
12
+
13
+ super(Carrierwave::Base64::Base64StringIO.new(
14
+ data.strip, options[:file_name] || 'file'
15
+ ))
15
16
  end
16
17
  end
17
18
  end
@@ -3,14 +3,15 @@ module Carrierwave
3
3
  class Base64StringIO < StringIO
4
4
  class ArgumentError < StandardError; end
5
5
 
6
- attr_accessor :file_format
6
+ attr_accessor :file_format, :file_name
7
7
 
8
- def initialize(encoded_file)
9
- description, encoded_bytes = encoded_file.split(",")
8
+ def initialize(encoded_file, file_name)
9
+ description, encoded_bytes = encoded_file.split(',')
10
10
 
11
11
  raise ArgumentError unless encoded_bytes
12
- raise ArgumentError if encoded_bytes.eql?("(null)")
12
+ raise ArgumentError if encoded_bytes.eql?('(null)')
13
13
 
14
+ @file_name = file_name
14
15
  @file_format = get_file_format description
15
16
  bytes = ::Base64.decode64 encoded_bytes
16
17
 
@@ -18,7 +19,7 @@ module Carrierwave
18
19
  end
19
20
 
20
21
  def original_filename
21
- File.basename("file.#{@file_format}")
22
+ File.basename("#{@file_name}.#{@file_format}")
22
23
  end
23
24
 
24
25
  private
@@ -1,5 +1,5 @@
1
1
  module Carrierwave
2
2
  module Base64
3
- VERSION = "2.2.0"
3
+ VERSION = '2.3.0'.freeze
4
4
  end
5
5
  end
@@ -0,0 +1 @@
1
+ lebedev@MacBook-Pro-2.local.4354
data/spec/adapter_spec.rb CHANGED
@@ -1,7 +1,5 @@
1
- require "spec_helper"
2
-
3
1
  RSpec.describe Carrierwave::Base64::Adapter do
4
- describe ".mount_base64_uploader" do
2
+ describe '.mount_base64_uploader' do
5
3
  let(:uploader) { Class.new CarrierWave::Uploader::Base }
6
4
 
7
5
  subject do
@@ -9,40 +7,64 @@ RSpec.describe Carrierwave::Base64::Adapter do
9
7
  Post.new
10
8
  end
11
9
 
12
- it "mounts the uploader on the image field" do
10
+ it 'mounts the uploader on the image field' do
13
11
  expect(subject.image).to be_an_instance_of(uploader)
14
12
  end
15
13
 
16
- it "handles normal file uploads" do
17
- sham_rack_app = ShamRack.at('www.example.com').stub
18
- sham_rack_app.register_resource("/test.jpg", file_path("fixtures", "test.jpg"), "images/jpg")
19
- subject[:image] = "test.jpg"
20
- expect(subject.changed?).to be_truthy
21
- subject.save!
22
- subject.reload
23
- expect(subject.image.current_path).to eq file_path("../uploads", "test.jpg")
24
- end
14
+ context 'normal file uploads' do
15
+ before(:each) do
16
+ sham_rack_app = ShamRack.at('www.example.com').stub
17
+ sham_rack_app.register_resource(
18
+ '/test.jpg', file_path('fixtures', 'test.jpg'), 'images/jpg'
19
+ )
20
+ subject[:image] = 'test.jpg'
21
+ end
22
+
23
+ it 'sets will_change for the attribute' do
24
+ expect(subject.changed?).to be_truthy
25
+ end
25
26
 
26
- it "handles data-urls" do
27
- subject.image = File.read(file_path("fixtures", "base64_image.fixture")).strip
28
- subject.save!
29
- subject.reload
30
- expect(subject.image.current_path).to eq file_path("../uploads", "file.jpg")
27
+ it 'saves the file' do
28
+ subject.save!
29
+ subject.reload
30
+
31
+ expect(
32
+ subject.image.current_path
33
+ ).to eq file_path('../uploads', 'test.jpg')
34
+ end
31
35
  end
32
36
 
33
- it "marks the attribute as changed" do
34
- subject.image = File.read(file_path("fixtures", "base64_image.fixture")).strip
35
- expect(subject.changed?).to be_truthy
37
+ context 'base64 strings' do
38
+ before(:each) do
39
+ subject.image = File.read(
40
+ file_path('fixtures', 'base64_image.fixture')
41
+ ).strip
42
+ end
43
+
44
+ it 'creates a file' do
45
+ subject.save!
46
+ subject.reload
47
+
48
+ expect(
49
+ subject.image.current_path
50
+ ).to eq file_path('../uploads', 'file.jpg')
51
+ end
52
+
53
+ it 'sets will_change for the attribute' do
54
+ expect(subject.changed?).to be_truthy
55
+ end
36
56
  end
37
57
 
38
- context "stored uploads exist for the field" do
58
+ context 'stored uploads exist for the field' do
39
59
  before :each do
40
- subject.image = File.read(file_path("fixtures", "base64_image.fixture")).strip
60
+ subject.image = File.read(
61
+ file_path('fixtures', 'base64_image.fixture')
62
+ ).strip
41
63
  subject.save!
42
64
  subject.reload
43
65
  end
44
66
 
45
- it "removes persisted files when remove_ with the field name attribute is set to true" do
67
+ it 'removes files when remove_* is set to true' do
46
68
  subject.remove_image = true
47
69
  subject.save!
48
70
  expect(subject.reload.image.file).to be_nil
@@ -1,55 +1,34 @@
1
- require "spec_helper"
2
-
3
1
  RSpec.describe Carrierwave::Base64::Base64StringIO do
4
- context "correct image data" do
5
- let(:image_data) { "data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q==" }
6
- subject { described_class.new image_data }
7
-
8
- it "determines the image format from the Data URI scheme" do
9
- expect(subject.file_format).to eql("jpg")
10
- end
11
-
12
- it "should respond to :original_filename" do
13
- expect(subject.original_filename).to eql("file.jpg")
14
- end
15
- end
2
+ %w(image/jpg application/pdf audio/mp3).each do |content_type|
3
+ context "correct #{content_type} data" do
4
+ let(:data) do
5
+ "data:#{content_type};base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q=="
6
+ end
16
7
 
17
- context "correct pdf data" do
18
- let(:image_data) { "data:application/pdf;base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q==" }
19
- subject { described_class.new image_data }
8
+ let(:file_format) { content_type.split('/').last }
20
9
 
21
- it "determines the image format from the Data URI scheme" do
22
- expect(subject.file_format).to eql("pdf")
23
- end
24
-
25
- it "should respond to :original_filename" do
26
- expect(subject.original_filename).to eql("file.pdf")
27
- end
28
- end
29
-
30
- context "correct mp3 data" do
31
- let(:audio_data) { "data:audio/mp3;base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q==" }
32
- subject { described_class.new audio_data }
10
+ subject { described_class.new data, 'file' }
33
11
 
34
- it "determines the image format from the Data URI scheme" do
35
- expect(subject.file_format).to eql("mp3")
36
- end
12
+ it 'determines the file format from the Data URI content type' do
13
+ expect(subject.file_format).to eql(file_format)
14
+ end
37
15
 
38
- it "should respond to :original_filename" do
39
- expect(subject.original_filename).to eql("file.mp3")
16
+ it 'should respond to :original_filename' do
17
+ expect(subject.original_filename).to eql("file.#{file_format}")
18
+ end
40
19
  end
41
20
  end
42
21
 
43
- context "incorrect image data" do
44
- it "raises an ArgumentError if Data URI scheme format is missing" do
22
+ context 'invalid image data' do
23
+ it 'raises an ArgumentError if Data URI content type is missing' do
45
24
  expect do
46
- described_class.new("/9j/4AAQSkZJRgABAQEASABIAADKdhH//2Q==")
25
+ described_class.new('/9j/4AAQSkZJRgABAQEASABIAADKdhH//2Q==', 'file')
47
26
  end.to raise_error(Carrierwave::Base64::Base64StringIO::ArgumentError)
48
27
  end
49
28
 
50
- it "raises ArgumentError if base64 data eql (null)" do
29
+ it 'raises ArgumentError if base64 data eql (null)' do
51
30
  expect do
52
- described_class.new("data:image/jpg;base64,(null)")
31
+ described_class.new('data:image/jpg;base64,(null)', 'file')
53
32
  end.to raise_error(Carrierwave::Base64::Base64StringIO::ArgumentError)
54
33
  end
55
34
  end
data/spec/spec_helper.rb CHANGED
@@ -1,21 +1,22 @@
1
- require "rubygems"
2
- require "bundler/setup"
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
3
 
4
- require "pry"
5
- require "sham_rack"
4
+ require 'pry'
5
+ require 'sham_rack'
6
6
 
7
- require "rails"
8
- require "active_record"
7
+ require 'rails'
8
+ require 'active_record'
9
9
 
10
- require "carrierwave"
11
- require "carrierwave/orm/activerecord"
10
+ require 'carrierwave'
11
+ require 'carrierwave/orm/activerecord'
12
12
 
13
- require "carrierwave/base64"
13
+ require 'carrierwave/base64'
14
14
 
15
- ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
15
+ ActiveRecord::Base.raise_in_transactional_callbacks = true
16
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
16
17
 
17
- load "support/schema.rb"
18
- require "support/models"
18
+ load 'support/schema.rb'
19
+ require 'support/models'
19
20
 
20
21
  def file_path(*paths)
21
22
  File.expand_path(File.join(File.dirname(__FILE__), *paths))
@@ -1,9 +1,9 @@
1
1
  ActiveRecord::Schema.define do
2
2
  self.verbose = false
3
3
 
4
- create_table "posts", force: :cascade do |t|
5
- t.string "image"
6
- t.datetime "created_at", null: false
7
- t.datetime "updated_at", null: false
4
+ create_table 'posts', force: :cascade do |t|
5
+ t.string 'image'
6
+ t.datetime 'created_at', null: false
7
+ t.datetime 'updated_at', null: false
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-base64
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Lebedev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-21 00:00:00.000000000 Z
11
+ date: 2016-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carrierwave
@@ -128,6 +128,20 @@ dependencies:
128
128
  - - ">="
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: rubocop
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
131
145
  description: This gem can be useful, if you need to upload files to your API from
132
146
  mobile devises.
133
147
  email:
@@ -137,7 +151,10 @@ extensions: []
137
151
  extra_rdoc_files: []
138
152
  files:
139
153
  - ".gitignore"
154
+ - ".rspec"
155
+ - ".rubocop.yml"
140
156
  - ".travis.yml"
157
+ - CHANGELOG.md
141
158
  - Gemfile
142
159
  - LICENSE.txt
143
160
  - README.md
@@ -147,6 +164,7 @@ files:
147
164
  - lib/carrierwave/base64/adapter.rb
148
165
  - lib/carrierwave/base64/base64_string_io.rb
149
166
  - lib/carrierwave/base64/version.rb
167
+ - spec/.#base64_string_io_spec.rb
150
168
  - spec/adapter_spec.rb
151
169
  - spec/base64_string_io_spec.rb
152
170
  - spec/fixtures/base64_image.fixture
@@ -179,6 +197,7 @@ signing_key:
179
197
  specification_version: 4
180
198
  summary: Upload images encoded as base64 to carrierwave.
181
199
  test_files:
200
+ - spec/.#base64_string_io_spec.rb
182
201
  - spec/adapter_spec.rb
183
202
  - spec/base64_string_io_spec.rb
184
203
  - spec/fixtures/base64_image.fixture