csv_in_zip 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .rvmrc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csv_in_zip.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2, :cli => "--color --fail-fast --drb", :all_on_start => false, :all_after_pass => false do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 ore
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # CsvInZip
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'csv_in_zip'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install csv_in_zip
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'csv_in_zip/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "csv_in_zip"
8
+ gem.version = CsvInZip::VERSION
9
+ gem.authors = ["ore"]
10
+ gem.email = ["orenoimac@gmail.com"]
11
+ gem.summary = %q{extract csv in zip}
12
+ gem.description = %q{extract all csv files in a zip and run each row in a block}
13
+ gem.homepage = "https://github.com/github0013/csv_in_zip"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'rubyzip'
21
+ gem.add_dependency 'activesupport'
22
+
23
+ gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'guard-rspec'
25
+ gem.add_development_dependency 'rb-fsevent' if RUBY_PLATFORM =~ /darwin/i
26
+ gem.add_development_dependency 'growl' if RUBY_PLATFORM =~ /darwin11/
27
+
28
+ end
@@ -0,0 +1,3 @@
1
+ module CsvInZip
2
+ VERSION = "0.0.1"
3
+ end
data/lib/csv_in_zip.rb ADDED
@@ -0,0 +1,39 @@
1
+ require "csv_in_zip/version"
2
+ require "zip/zip"
3
+ require "kconv"
4
+ require "csv"
5
+ require "tempfile"
6
+ require 'active_support/core_ext/string/inflections'
7
+ require 'active_support/core_ext/object/blank'
8
+
9
+ module CsvInZip
10
+ class << self
11
+
12
+ 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
+
23
+ zip_file = Zip::ZipFile.open(target_zip)
24
+ Zip::ZipFile.foreach(target_zip) do |entry|
25
+ next if entry.to_s =~ /^__MACOSX/
26
+
27
+ csv_lines = zip_file.read(entry).gsub("\r\n", "\n").split("\n")
28
+ csv_lines = csv_lines[1..-1] if options[:remove_header_line]
29
+ output.write csv_lines.join("\n").toutf8
30
+ end
31
+
32
+ output.close
33
+
34
+ CSV.parse(File.read(output.path), :headers => true).each{|row| yield row}
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require "csv_in_zip"
4
+
5
+ describe CsvInZip do
6
+ before(:all) do
7
+ base = File.expand_path("../../support/data", __FILE__)
8
+ @no_headers_zip = File.join(base, "no_headers.csv.zip")
9
+ @multi_data_zip = File.join(base, "multi_data.csv.zip")
10
+ @utf8_zip = File.join(base, "utf8.csv.zip")
11
+ @sjis_zip = File.join(base, "sjis.csv.zip")
12
+ end
13
+
14
+ it "should add headers" do
15
+ headers = %w[header1 header2]
16
+ CsvInZip.extract(@no_headers_zip, :headers => headers) do |row|
17
+ row.headers.should == headers
18
+ end
19
+ end
20
+
21
+ it "should add headers" do
22
+ CsvInZip.extract(@multi_data_zip, :remove_header_line => true) do |row|
23
+ row.headers.should == %w[データ1 データ2]
24
+ end
25
+ end
26
+
27
+ it "should change headers, part1" do
28
+ changed_headers = %w[changed1 changed2]
29
+ CsvInZip.extract(@utf8_zip, :headers => changed_headers, :remove_header_line => true) do |row|
30
+ row.headers.should == changed_headers
31
+ end
32
+ end
33
+
34
+ it "should change headers, part2" do
35
+ changed_headers = %w[changed1 changed2]
36
+ CsvInZip.extract(@utf8_zip, :change_headers => changed_headers) do |row|
37
+ row.headers.should == changed_headers
38
+ end
39
+ end
40
+
41
+ it "should open sjis file in utf8" do
42
+ CsvInZip.extract(@sjis_zip) do |row|
43
+ row["sjis-header"].should == "データ1"
44
+ row["日本語ヘッダー"].should == "データ2"
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'rspec/autorun'
3
+ Dir["support/**/*.rb"].each {|f| require f}
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rspec
7
+ end
@@ -0,0 +1,3 @@
1
+ ヘッダー1,ヘッダー2
2
+ データ1,データ2
3
+ データ3,データ4
@@ -0,0 +1 @@
1
+ データ1,データ2
@@ -0,0 +1,2 @@
1
+ sjis-header,���{��w�b�_�[
2
+ �f�[�^1,�f�[�^2
Binary file
@@ -0,0 +1,2 @@
1
+ utf8-header,日本語ヘッダー
2
+ データ1,データ2
Binary file
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_in_zip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ore
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rubyzip
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: guard-rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rb-fsevent
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: growl
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: extract all csv files in a zip and run each row in a block
111
+ email:
112
+ - orenoimac@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - Gemfile
119
+ - Guardfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - csv_in_zip.gemspec
124
+ - lib/csv_in_zip.rb
125
+ - lib/csv_in_zip/version.rb
126
+ - spec/lib/csv_in_zip_spec.rb
127
+ - spec/spec_helper.rb
128
+ - spec/support/data/multi_data.csv
129
+ - spec/support/data/multi_data.csv.zip
130
+ - spec/support/data/no_headers.csv
131
+ - spec/support/data/no_headers.csv.zip
132
+ - spec/support/data/sjis.csv
133
+ - spec/support/data/sjis.csv.zip
134
+ - spec/support/data/utf8.csv
135
+ - spec/support/data/utf8.csv.zip
136
+ homepage: https://github.com/github0013/csv_in_zip
137
+ licenses: []
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 1.8.24
157
+ signing_key:
158
+ specification_version: 3
159
+ summary: extract csv in zip
160
+ test_files:
161
+ - spec/lib/csv_in_zip_spec.rb
162
+ - spec/spec_helper.rb
163
+ - spec/support/data/multi_data.csv
164
+ - spec/support/data/multi_data.csv.zip
165
+ - spec/support/data/no_headers.csv
166
+ - spec/support/data/no_headers.csv.zip
167
+ - spec/support/data/sjis.csv
168
+ - spec/support/data/sjis.csv.zip
169
+ - spec/support/data/utf8.csv
170
+ - spec/support/data/utf8.csv.zip