csv_in_zip 0.0.2 → 0.0.3

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.
data/lib/csv_in_zip.rb CHANGED
@@ -7,32 +7,70 @@ require 'active_support/core_ext/string/inflections'
7
7
  require 'active_support/core_ext/object/blank'
8
8
 
9
9
  module CsvInZip
10
+
11
+ class CsvFile
12
+ def initialize(options = {}, &block)
13
+ if options[:change_headers].present?
14
+ options[:headers] = options[:change_headers]
15
+ options[:remove_header_line] = true
16
+ end
17
+ @options = options
18
+
19
+ @tempfile = Tempfile.open("csv_in_zip")
20
+ if options[:headers].present?
21
+ @tempfile.puts Array(options[:headers]).join(",")
22
+ end
23
+
24
+ instance_eval(&block)
25
+ ensure
26
+ @tempfile.close
27
+ end
28
+
29
+ private
30
+
31
+ def add(csv_content)
32
+ csv_lines = csv_content.gsub("\r\n", "\n").split("\n")
33
+ csv_lines = csv_lines[1..-1] if @options[:remove_header_line]
34
+ @tempfile.puts csv_lines.join("\n").toutf8
35
+ end
36
+
37
+ def to_csv
38
+ @tempfile.close
39
+ CSV.parse(File.read(@tempfile.path), :headers => true)
40
+ end
41
+
42
+ end
43
+
10
44
  class << self
45
+
46
+ def skip?(entry)
47
+ file_name = entry.to_s
48
+ file_name =~ /^__MACOSX/ || File.extname(file_name) != ".csv"
49
+ end
11
50
 
12
51
  def extract(target_zip, options = {})
13
- Tempfile.open("csv_in_zip") do |output|
14
- if options[:change_headers].present?
15
- options[:headers] = options[:change_headers]
16
- options[:remove_header_line] = true
17
- end
18
-
19
- if options[:headers].present?
20
- output.puts Array(options[:headers]).join(",")
21
- end
22
-
52
+ CsvFile.new(options) do
23
53
  zip_file = Zip::ZipFile.open(target_zip)
24
54
  Zip::ZipFile.foreach(target_zip) do |entry|
25
- next if entry.to_s =~ /^__MACOSX/
26
- next unless File.extname(entry.to_s) == ".csv"
55
+ next if CsvInZip.skip?(entry)
27
56
 
28
- csv_lines = zip_file.read(entry).gsub("\r\n", "\n").split("\n")
29
- csv_lines = csv_lines[1..-1] if options[:remove_header_line]
30
- output.write csv_lines.join("\n").toutf8
57
+ add zip_file.read(entry)
31
58
  end
32
59
 
33
- output.close
60
+ to_csv.each{|row| yield row}
61
+ end
62
+ end
63
+
64
+ def extract_each(target_zip, options = {})
65
+ zip_file = Zip::ZipFile.open(target_zip)
66
+ Zip::ZipFile.foreach(target_zip) do |entry|
67
+ next if CsvInZip.skip?(entry)
34
68
 
35
- CSV.parse(File.read(output.path), :headers => true).each{|row| yield row}
69
+ CsvFile.new(options) do
70
+ add zip_file.read(entry)
71
+ csv = to_csv
72
+ yield csv, csv.headers
73
+ end
36
74
  end
37
75
  end
38
76
 
@@ -1,3 +1,3 @@
1
1
  module CsvInZip
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -10,49 +10,70 @@ describe CsvInZip do
10
10
  @utf8_zip = File.join(base, "utf8.csv.zip")
11
11
  @sjis_zip = File.join(base, "sjis.csv.zip")
12
12
  @multi_files_zip = File.join(base, "multi_files.zip")
13
- end
14
-
15
- it "should add headers" do
16
- headers = %w[header1 header2]
17
- CsvInZip.extract(@no_headers_zip, :headers => headers) do |row|
18
- row.headers.should == headers
19
- end
13
+ @sjis_utf8_zip = File.join(base, "sjis+utf8.zip")
20
14
  end
21
15
 
22
- it "should add headers" do
23
- CsvInZip.extract(@multi_data_zip, :remove_header_line => true) do |row|
24
- row.headers.should == %w[データ1 データ2]
16
+ describe "extract" do
17
+ it "should add headers" do
18
+ headers = %w[header1 header2]
19
+ CsvInZip.extract(@no_headers_zip, :headers => headers) do |row|
20
+ row.headers.should == headers
21
+ end
25
22
  end
26
- end
27
-
28
- it "should change headers, part1" do
29
- changed_headers = %w[changed1 changed2]
30
- CsvInZip.extract(@utf8_zip, :headers => changed_headers, :remove_header_line => true) do |row|
31
- row.headers.should == changed_headers
23
+
24
+ it "should add headers" do
25
+ CsvInZip.extract(@multi_data_zip, :remove_header_line => true) do |row|
26
+ row.headers.should == %w[データ1 データ2]
27
+ end
32
28
  end
33
- end
34
-
35
- it "should change headers, part2" do
36
- changed_headers = %w[changed1 changed2]
37
- CsvInZip.extract(@utf8_zip, :change_headers => changed_headers) do |row|
38
- row.headers.should == changed_headers
29
+
30
+ it "should change headers, part1" do
31
+ changed_headers = %w[changed1 changed2]
32
+ CsvInZip.extract(@utf8_zip, :headers => changed_headers, :remove_header_line => true) do |row|
33
+ row.headers.should == changed_headers
34
+ end
39
35
  end
40
- end
41
-
42
- it "should open sjis file in utf8" do
43
- CsvInZip.extract(@sjis_zip) do |row|
44
- row["sjis-header"].should == "データ1"
45
- row["日本語ヘッダー"].should == "データ2"
36
+
37
+ it "should change headers, part2" do
38
+ changed_headers = %w[changed1 changed2]
39
+ CsvInZip.extract(@utf8_zip, :change_headers => changed_headers) do |row|
40
+ row.headers.should == changed_headers
41
+ end
42
+ end
43
+
44
+ it "should open sjis file in utf8" do
45
+ CsvInZip.extract(@sjis_zip) do |row|
46
+ row["sjis-header"].should == "データ1"
47
+ row["日本語ヘッダー"].should == "データ2"
48
+ end
49
+ end
50
+
51
+ describe "multi files" do
52
+ it "should open only csv files" do
53
+ count = 0
54
+ CsvInZip.extract(@multi_files_zip) do |row|
55
+ count += 1
56
+ end
57
+ count.should == utf8_csv_has_only_one_row = 1
58
+ end
59
+ end
60
+
61
+ it "should join files and read as one csv" do
62
+ count = 0
63
+ CsvInZip.extract(@sjis_utf8_zip, :change_headers => %w|header1 header2|) do |row|
64
+ count += 1
65
+ end
66
+ count.should == 2
46
67
  end
47
68
  end
48
69
 
49
- describe "multi files" do
50
- it "should open only csv files" do
70
+ describe "extract_each" do
71
+ it "should return each csv" do
51
72
  count = 0
52
- CsvInZip.extract(@multi_files_zip) do |row|
73
+ CsvInZip.extract_each(@sjis_utf8_zip) do |csv, headers|
53
74
  count += 1
54
75
  end
55
- count.should == utf8_csv_has_only_one_row = 1
76
+ count.should == 2
56
77
  end
57
78
  end
58
79
 
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_in_zip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-14 00:00:00.000000000 Z
12
+ date: 2012-09-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rubyzip
@@ -131,6 +131,7 @@ files:
131
131
  - spec/support/data/multi_files.zip
132
132
  - spec/support/data/no_headers.csv
133
133
  - spec/support/data/no_headers.csv.zip
134
+ - spec/support/data/sjis+utf8.zip
134
135
  - spec/support/data/sjis.csv
135
136
  - spec/support/data/sjis.csv.zip
136
137
  - spec/support/data/utf8.csv
@@ -168,6 +169,7 @@ test_files:
168
169
  - spec/support/data/multi_files.zip
169
170
  - spec/support/data/no_headers.csv
170
171
  - spec/support/data/no_headers.csv.zip
172
+ - spec/support/data/sjis+utf8.zip
171
173
  - spec/support/data/sjis.csv
172
174
  - spec/support/data/sjis.csv.zip
173
175
  - spec/support/data/utf8.csv