zipline 0.0.7 → 0.0.8

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: 3dced1a62ccd13637dce8896c6809922661aa25d
4
- data.tar.gz: 32b3a8cb4809dedd83248501f8ac2eb973482a39
3
+ metadata.gz: e29199e5ffa8970199f9586ea13025d68373594f
4
+ data.tar.gz: b155365912bf26cdedc37221d4061571fbe7c985
5
5
  SHA512:
6
- metadata.gz: 89bf143bc7f27eddf9cba7064d2b43a248b594a35363643796ece7a51bdf96dcc1a61fa2f5fcd946f3b505b5afb2b1be8c5e189d107ddbfa44251f7b355bd414
7
- data.tar.gz: 032abbeebc338a1959f95d35a3fd21e43d1a06190445bf2861387b53e5b2916f4b56d0157b299b9670f432f47e5983d6c28ed4655d0bdc19ba0e7f4cc3dd269e
6
+ metadata.gz: e9b51d097401fe6e7142ed4220b4c779b2f3cdc0e77f7665c85f0ad60daa62d33557b53c37e006bca87bd7584153d6a622d25e85e0366e5b32f4dbcbc604524d
7
+ data.tar.gz: 35b0a617645946e17306617b5e21f4f88f10c11a291b5c2079fe9d93c440eb68df71b600d30c305c76720c6359ee682d0782fefa236776b5c5cf804909ab5d90
data/Gemfile CHANGED
@@ -2,3 +2,10 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in zipline.gemspec
4
4
  gemspec
5
+
6
+ group :development, :test do
7
+ gem 'rspec', '~> 3.0.0.beta'
8
+ gem 'fog'
9
+ gem 'carrierwave'
10
+ gem 'paperclip'
11
+ end
data/README.md CHANGED
@@ -48,6 +48,5 @@ For directories, just give the files names like "directory/file".
48
48
  ## TODO (possible contributions?)
49
49
 
50
50
  * tests!
51
- * support rails 4.0 streaming
52
51
  * extract library for plain ruby streaming zips, which this will depend on.
53
52
  * get my changes to support streaming zips checked in to the rubyzip library.
@@ -29,7 +29,7 @@ module Zipline
29
29
  end
30
30
 
31
31
  def <<(x)
32
- return if x.nil?
32
+ return if x.blank?
33
33
  throw "bad class #{x.class}" unless x.class == String
34
34
  @pos += x.bytesize
35
35
  @block.call(x.to_s)
@@ -1,3 +1,3 @@
1
1
  module Zipline
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -1,6 +1,5 @@
1
1
  # this class acts as a streaming body for rails
2
2
  # initialize it with an array of the files you want to zip
3
- # right now only carrierwave is supported with file storage or S3
4
3
  module Zipline
5
4
  class ZipGenerator
6
5
  # takes an array of pairs [[uploader, filename], ... ]
@@ -16,27 +15,29 @@ module Zipline
16
15
  def each(&block)
17
16
  output = new_output(&block)
18
17
  OutputStream.open(output) do |zip|
19
- @files.each do |file, name|
20
- file = file.file if file.respond_to? :file
18
+ @files.each {|file, name| handle_file(zip, file, name) }
19
+ end
20
+ end
21
21
 
22
- #normalize file
23
- if file.class.to_s == 'CarrierWave::Storage::Fog::File'
24
- file = file.send(:file)
25
- end
26
- if file.class.to_s == 'CarrierWave::SanitizedFile'
27
- path = file.send(:file)
28
- file = File.open(path)
29
- end
30
- if file.class.to_s == 'Paperclip::Attachment'
31
- path = file.send(:path)
32
- file = File.open(path)
33
- end
34
- throw "bad_file" unless %w{Fog::Storage::AWS::File File}.include? file.class.to_s
35
-
36
- name = uniquify_name(name)
37
- write_file(zip, file, name)
22
+ def handle_file(zip, file, name)
23
+ file = normalize(file)
24
+ name = uniquify_name(name)
25
+ write_file(zip, file, name)
26
+ end
27
+
28
+ def normalize(file)
29
+ unless is_io?(file)
30
+ if file.respond_to?(:url) && (!defined?(::Paperclip::Attachment) || !file.is_a?(::Paperclip::Attachment))
31
+ file = file
32
+ elsif file.respond_to? :file
33
+ file = File.open(file.file)
34
+ elsif file.respond_to? :path
35
+ file = File.open(file.path)
36
+ else
37
+ raise(ArgumentError, 'Bad File/Stream')
38
38
  end
39
39
  end
40
+ file
40
41
  end
41
42
 
42
43
  def new_output(&block)
@@ -45,10 +46,9 @@ module Zipline
45
46
 
46
47
  def write_file(zip, file, name)
47
48
  size = get_size(file)
48
-
49
49
  zip.put_next_entry name, size
50
50
 
51
- if file.is_a? File
51
+ if is_io?(file)
52
52
  while buffer = file.read(2048)
53
53
  zip << buffer
54
54
  end
@@ -65,18 +65,22 @@ module Zipline
65
65
  end
66
66
 
67
67
  def get_size(file)
68
- case file.class.to_s
69
- when 'File'
68
+ if is_io?(file)
70
69
  file.size
71
- when 'Fog::Storage::AWS::FILE'
70
+ elsif file.respond_to? :content_length
72
71
  file.content_length
72
+ else
73
+ throw 'cannot determine file size'
73
74
  end
74
75
  end
75
76
 
77
+ def is_io?(file)
78
+ file.is_a?(IO) || (defined?(StringIO) && file.is_a?(StringIO))
79
+ end
80
+
76
81
  def uniquify_name(name)
77
82
  @used_names ||= Set.new
78
83
 
79
-
80
84
  if @used_names.include?(name)
81
85
 
82
86
  #remove suffix e.g. ".foo"
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+
4
+ describe Zipline::ZipGenerator do
5
+
6
+ before { Fog.mock! }
7
+ let(:file_attributes){ {
8
+ key: 'fog_file_tests',
9
+ body: 'some body',
10
+ public: true
11
+ }}
12
+ let(:directory_attributes){{
13
+ key: 'fog_directory'
14
+ }}
15
+ let(:storage_attributes){{
16
+ aws_access_key_id: 'fake_access_key_id',
17
+ aws_secret_access_key: 'fake_secret_access_key',
18
+ provider: 'AWS'
19
+ }}
20
+ let(:storage){ Fog::Storage.new(storage_attributes)}
21
+ let(:directory){ storage.directories.create(directory_attributes) }
22
+ let(:file){ directory.files.create(file_attributes) }
23
+
24
+ describe '.normalize' do
25
+ let(:generator){ Zipline::ZipGenerator.new([])}
26
+ context "CarrierWave" do
27
+ context "Remote" do
28
+ let(:file){ CarrierWave::Storage::Fog::File.new(nil,nil,nil) }
29
+ it "passes through" do
30
+ expect(File).not_to receive(:open)
31
+ expect(generator.normalize(file)).to be file
32
+ end
33
+ end
34
+ context "Local" do
35
+ let(:file){ CarrierWave::SanitizedFile.new(Tempfile.new('t')) }
36
+ it "creates a File" do
37
+ expect(generator.normalize(file)).to be_a File
38
+ end
39
+ end
40
+ end
41
+ context "Paperclip" do
42
+ let(:file){ Paperclip::Attachment.new(:name, :instance) }
43
+ it "creates a File" do
44
+ expect(File).to receive(:open).once
45
+ generator.normalize(file)
46
+ end
47
+ end
48
+ context "Fog" do
49
+ it "passes through" do
50
+ expect(File).not_to receive(:open)
51
+ expect(generator.normalize(file)).to be file
52
+ end
53
+ end
54
+ context "IOStream" do
55
+ let(:file){ StringIO.new('passthrough')}
56
+ it "passes through" do
57
+ expect(generator.normalize(file)).to be file
58
+ end
59
+ end
60
+ context "invalid" do
61
+ let(:file){ Thread.new{} }
62
+ it "raises error" do
63
+ expect{generator.normalize(file)}.to raise_error(ArgumentError)
64
+ end
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,14 @@
1
+ require 'rspec'
2
+ require 'zipline'
3
+ require 'paperclip'
4
+ require 'fog'
5
+ require 'carrierwave'
6
+
7
+ Dir["#{File.expand_path('..', __FILE__)}/support/**/*.rb"].each { |f| require f }
8
+
9
+
10
+ RSpec.configure do |config|
11
+ config.color_enabled = true
12
+ config.order = :random
13
+ config.run_all_when_everything_filtered = true
14
+ end
data/zipline.gemspec CHANGED
@@ -16,6 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.version = Zipline::VERSION
17
17
 
18
18
  gem.add_dependency 'rubyzip', ['>= 1.0', '<= 1.1.2']
19
- gem.add_dependency 'rails', '>= 3.2.1'
19
+ gem.add_dependency 'rails', ['>= 3.2.1', '< 4.3']
20
20
  gem.add_dependency 'curb'
21
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zipline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ram Dobson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-24 00:00:00.000000000 Z
11
+ date: 2014-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip
@@ -37,6 +37,9 @@ dependencies:
37
37
  - - '>='
38
38
  - !ruby/object:Gem::Version
39
39
  version: 3.2.1
40
+ - - <
41
+ - !ruby/object:Gem::Version
42
+ version: '4.3'
40
43
  type: :runtime
41
44
  prerelease: false
42
45
  version_requirements: !ruby/object:Gem::Requirement
@@ -44,6 +47,9 @@ dependencies:
44
47
  - - '>='
45
48
  - !ruby/object:Gem::Version
46
49
  version: 3.2.1
50
+ - - <
51
+ - !ruby/object:Gem::Version
52
+ version: '4.3'
47
53
  - !ruby/object:Gem::Dependency
48
54
  name: curb
49
55
  requirement: !ruby/object:Gem::Requirement
@@ -75,6 +81,8 @@ files:
75
81
  - lib/zipline/output_stream.rb
76
82
  - lib/zipline/version.rb
77
83
  - lib/zipline/zip_generator.rb
84
+ - spec/lib/zipline/zip_generator_spec.rb
85
+ - spec/spec_helper.rb
78
86
  - zipline.gemspec
79
87
  homepage: http://github.com/fringd/zipline
80
88
  licenses: []
@@ -99,4 +107,6 @@ rubygems_version: 2.0.0.rc.2
99
107
  signing_key:
100
108
  specification_version: 4
101
109
  summary: stream zip files from rails
102
- test_files: []
110
+ test_files:
111
+ - spec/lib/zipline/zip_generator_spec.rb
112
+ - spec/spec_helper.rb