windows_csv 0.0.6 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 42e240c1188e10fcbdc2e092ba0a3174abe85e8dfb58cd83edb73ffc4313e70d
4
+ data.tar.gz: ba1756793a0286cebfb43050d48fa6e621a8dcf1a5796a2f6a756e729869dae5
5
+ SHA512:
6
+ metadata.gz: a988733b0fd2a6fe9facaaf26072bd3c7e2fe05fe1197a72ac316d79d1206fb30c0aabf254853037d3003d618986160a4a374139207b7828158285fc71f5530c
7
+ data.tar.gz: 0f7cf5c38263f31db5b1a03e899ea1b27eadf11799cf1a6ed9cff1b77055cfc176fdf1c0b75aabf96d4331be588eee959cce6def622fcd9821fb2b1d859bbc6b
data/Rakefile CHANGED
@@ -1,49 +1,37 @@
1
- # encoding: utf-8
2
-
3
- require 'rubygems'
4
- require 'bundler'
1
+ require "rubygems"
2
+ require "bundler"
5
3
  begin
6
4
  Bundler.setup(:default, :development)
7
5
  rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
6
+ warn e.message
7
+ warn "Run `bundle install` to install missing gems"
10
8
  exit e.status_code
11
9
  end
12
- require 'rake'
10
+ require "rake"
13
11
 
14
- require 'jeweler'
12
+ require "jeweler"
15
13
  Jeweler::Tasks.new do |gem|
16
14
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
15
  gem.name = "windows_csv"
18
16
  gem.homepage = "http://github.com/kaspernj/windows_csv"
19
17
  gem.license = "MIT"
20
- gem.summary = %Q{A framework to write Windows CSV files.}
21
- gem.description = %Q{A framework to write Windows CSV files.}
18
+ gem.summary = %(A framework to write Windows CSV files.)
19
+ gem.description = %(A framework to write Windows CSV files.)
22
20
  gem.email = "k@spernj.org"
23
21
  gem.authors = ["kaspernj"]
24
22
  # dependencies defined in Gemfile
25
23
  end
26
24
  Jeweler::RubygemsDotOrgTasks.new
27
25
 
28
- require 'rspec/core'
29
- require 'rspec/core/rake_task'
26
+ require "rspec/core"
27
+ require "rspec/core/rake_task"
30
28
  RSpec::Core::RakeTask.new(:spec) do |spec|
31
- spec.pattern = FileList['spec/**/*_spec.rb']
29
+ spec.pattern = FileList["spec/**/*_spec.rb"]
32
30
  end
33
31
 
34
32
  RSpec::Core::RakeTask.new(:rcov) do |spec|
35
- spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.pattern = "spec/**/*_spec.rb"
36
34
  spec.rcov = true
37
35
  end
38
36
 
39
- task :default => :spec
40
-
41
- require 'rdoc/task'
42
- Rake::RDocTask.new do |rdoc|
43
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
-
45
- rdoc.rdoc_dir = 'rdoc'
46
- rdoc.title = "windows_csv #{version}"
47
- rdoc.rdoc_files.include('README*')
48
- rdoc.rdoc_files.include('lib/**/*.rb')
49
- end
37
+ task default: :spec
data/lib/windows_csv.rb CHANGED
@@ -1,27 +1,37 @@
1
- #This class was heavily inspired by the great Dipth! https://github.com/dipth
1
+ # This class was heavily inspired by the great Dipth! https://github.com/dipth
2
2
  class WindowsCsv
3
- ENCODING = Encoding::UTF_8
4
-
5
3
  BOM = "\377\376".force_encoding(Encoding::UTF_16LE) # Byte Order Mark
6
- COL_SEP = "\t"
7
- QUOTE_CHAR = "\""
8
- ROW_SEP = "\r\n"
9
- ARGS = {:col_sep => COL_SEP, :quote_char => QUOTE_CHAR, :row_sep => ROW_SEP}
10
-
11
- def self.foreach(path, args = {})
4
+ COL_SEP = ";".freeze
5
+ QUOTE_CHAR = "\"".freeze
6
+ ROW_SEP = "\r\n".freeze
7
+
8
+ ARGS = { # rubocop:disable Style/MutableConstant
9
+ col_sep: COL_SEP,
10
+ quote_char: QUOTE_CHAR,
11
+ row_sep: ROW_SEP
12
+ }
13
+
14
+ REPLACES = {
15
+ "\r\n" => "\\r\\n",
16
+ "\r" => "\\r",
17
+ "\n" => "\\n"
18
+ }.freeze
19
+
20
+ # Loops through a Windows CSV file with leading BOM, tabs as col-sep, quote char " and row sep \r\n
21
+ def self.foreach(path, args = {}) # rubocop:disable Metrics/AbcSize
12
22
  require "csv"
13
-
23
+
14
24
  File.open(path, "rb:bom|utf-16le") do |fp|
15
25
  csv_args = ARGS.clone
16
26
  csv_args.merge!(args[:csv_args]) if args[:csv_args]
17
-
18
- CSV.foreach(fp, csv_args) do |row|
27
+
28
+ CSV.foreach(fp, **csv_args) do |row|
19
29
  if csv_args[:headers]
20
30
  real = {}
21
31
  else
22
32
  real = []
23
33
  end
24
-
34
+
25
35
  row.each do |col|
26
36
  if csv_args[:headers]
27
37
  real[col[0].to_sym] = WindowsCsv.unescape(col[1])
@@ -29,37 +39,37 @@ class WindowsCsv
29
39
  real << WindowsCsv.unescape(col)
30
40
  end
31
41
  end
32
-
42
+
33
43
  yield real
34
44
  end
35
45
  end
36
-
46
+
37
47
  nil
38
48
  end
39
-
49
+
40
50
  def initialize(args)
41
51
  require "csv"
42
-
52
+
43
53
  @args = args
44
-
54
+
45
55
  if @args[:path]
46
- fp = File.open(@args[:path], "w", :encoding => ENCODING)
56
+ fp = File.open(@args[:path], "w", encoding: Encoding::UTF_8)
47
57
  @args[:io] = fp
48
58
  end
49
-
59
+
50
60
  begin
51
61
  @args[:io].write(BOM)
52
62
  yield self
53
63
  ensure
54
- fp.close if fp
64
+ fp&.close
55
65
  end
56
66
  end
57
-
67
+
58
68
  def <<(row)
59
69
  encoded = []
60
-
70
+
61
71
  row.each do |col|
62
- if col.is_a?(Time) or col.is_a?(DateTime)
72
+ if col.is_a?(Time) || col.is_a?(DateTime)
63
73
  encoded << col.strftime("%Y-%m-%d %H:%M")
64
74
  elsif col.is_a?(Date)
65
75
  encoded << col.strftime("%Y-%m-%d")
@@ -67,17 +77,29 @@ class WindowsCsv
67
77
  encoded << WindowsCsv.escape(col)
68
78
  end
69
79
  end
70
-
71
- @args[:io].puts CSV.generate_line(encoded, ARGS).encode(Encoding::UTF_16LE)
72
-
73
- return nil
80
+
81
+ @args[:io].write CSV.generate_line(encoded, **ARGS).encode(Encoding::UTF_16LE)
82
+
83
+ nil
74
84
  end
75
-
85
+
76
86
  def self.escape(str)
77
- return str.to_s.gsub("\n", "\\r\\n")
87
+ str = str.to_s
88
+
89
+ REPLACES.each do |key, val|
90
+ str = str.gsub(key, val)
91
+ end
92
+
93
+ str
78
94
  end
79
-
95
+
80
96
  def self.unescape(str)
81
- return str.to_s.gsub("\\r\\n", "\r\n")
97
+ str = str.to_s
98
+
99
+ REPLACES.each do |key, val|
100
+ str = str.gsub(val, key)
101
+ end
102
+
103
+ str
82
104
  end
83
- end
105
+ end
metadata CHANGED
@@ -1,80 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: windows_csv
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.6
4
+ version: 0.0.9
6
5
  platform: ruby
7
6
  authors:
8
7
  - kaspernj
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-02 00:00:00.000000000 Z
11
+ date: 2022-12-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :development
21
+ prerelease: false
15
22
  version_requirements: !ruby/object:Gem::Requirement
16
- none: false
17
23
  requirements:
18
- - - ~>
24
+ - - ">="
19
25
  - !ruby/object:Gem::Version
20
- version: 2.8.0
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
21
28
  name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.12.0
22
34
  type: :development
23
35
  prerelease: false
24
- requirement: !ruby/object:Gem::Requirement
25
- none: false
36
+ version_requirements: !ruby/object:Gem::Requirement
26
37
  requirements:
27
- - - ~>
38
+ - - "~>"
28
39
  - !ruby/object:Gem::Version
29
- version: 2.8.0
40
+ version: 3.12.0
30
41
  - !ruby/object:Gem::Dependency
31
- version_requirements: !ruby/object:Gem::Requirement
32
- none: false
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
33
44
  requirements:
34
- - - ~>
45
+ - - ">="
35
46
  - !ruby/object:Gem::Version
36
- version: '3.12'
37
- name: rdoc
47
+ version: '0'
38
48
  type: :development
39
49
  prerelease: false
40
- requirement: !ruby/object:Gem::Requirement
41
- none: false
50
+ version_requirements: !ruby/object:Gem::Requirement
42
51
  requirements:
43
- - - ~>
52
+ - - ">="
44
53
  - !ruby/object:Gem::Version
45
- version: '3.12'
54
+ version: '0'
46
55
  - !ruby/object:Gem::Dependency
47
- version_requirements: !ruby/object:Gem::Requirement
48
- none: false
56
+ name: rubocop-performance
57
+ requirement: !ruby/object:Gem::Requirement
49
58
  requirements:
50
- - - ! '>='
59
+ - - ">="
51
60
  - !ruby/object:Gem::Version
52
- version: 1.0.0
53
- name: bundler
61
+ version: '0'
54
62
  type: :development
55
63
  prerelease: false
56
- requirement: !ruby/object:Gem::Requirement
57
- none: false
64
+ version_requirements: !ruby/object:Gem::Requirement
58
65
  requirements:
59
- - - ! '>='
66
+ - - ">="
60
67
  - !ruby/object:Gem::Version
61
- version: 1.0.0
68
+ version: '0'
62
69
  - !ruby/object:Gem::Dependency
63
- version_requirements: !ruby/object:Gem::Requirement
64
- none: false
70
+ name: rubocop-rake
71
+ requirement: !ruby/object:Gem::Requirement
65
72
  requirements:
66
- - - ~>
73
+ - - ">="
67
74
  - !ruby/object:Gem::Version
68
- version: 1.8.4
69
- name: jeweler
75
+ version: '0'
70
76
  type: :development
71
77
  prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
72
85
  requirement: !ruby/object:Gem::Requirement
73
- none: false
74
86
  requirements:
75
- - - ~>
87
+ - - ">="
76
88
  - !ruby/object:Gem::Version
77
- version: 1.8.4
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
78
97
  description: A framework to write Windows CSV files.
79
98
  email: k@spernj.org
80
99
  executables: []
@@ -83,44 +102,32 @@ extra_rdoc_files:
83
102
  - LICENSE.txt
84
103
  - README.rdoc
85
104
  files:
86
- - .document
87
- - .rspec
88
- - Gemfile
89
- - Gemfile.lock
90
105
  - LICENSE.txt
91
106
  - README.rdoc
92
107
  - Rakefile
93
- - VERSION
94
108
  - lib/windows_csv.rb
95
- - spec/spec_helper.rb
96
- - spec/windows_csv_spec.rb
97
- - windows_csv.gemspec
98
109
  homepage: http://github.com/kaspernj/windows_csv
99
110
  licenses:
100
111
  - MIT
112
+ metadata:
113
+ rubygems_mfa_required: 'true'
101
114
  post_install_message:
102
115
  rdoc_options: []
103
116
  require_paths:
104
117
  - lib
105
118
  required_ruby_version: !ruby/object:Gem::Requirement
106
- none: false
107
119
  requirements:
108
- - - ! '>='
120
+ - - ">="
109
121
  - !ruby/object:Gem::Version
110
- segments:
111
- - 0
112
- hash: -649786811259003594
113
- version: '0'
122
+ version: 2.6.8
114
123
  required_rubygems_version: !ruby/object:Gem::Requirement
115
- none: false
116
124
  requirements:
117
- - - ! '>='
125
+ - - ">="
118
126
  - !ruby/object:Gem::Version
119
127
  version: '0'
120
128
  requirements: []
121
- rubyforge_project:
122
- rubygems_version: 1.8.23
129
+ rubygems_version: 3.3.7
123
130
  signing_key:
124
- specification_version: 3
131
+ specification_version: 4
125
132
  summary: A framework to write Windows CSV files.
126
133
  test_files: []
data/.document DELETED
@@ -1,5 +0,0 @@
1
- lib/**/*.rb
2
- bin/*
3
- -
4
- features/**/*.feature
5
- LICENSE.txt
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --color
data/Gemfile DELETED
@@ -1,13 +0,0 @@
1
- source "http://rubygems.org"
2
- # Add dependencies required to use your gem here.
3
- # Example:
4
- # gem "activesupport", ">= 2.3.5"
5
-
6
- # Add dependencies to develop your gem here.
7
- # Include everything needed to run rake, tests, features, etc.
8
- group :development do
9
- gem "rspec", "~> 2.8.0"
10
- gem "rdoc", "~> 3.12"
11
- gem "bundler", ">= 1.0.0"
12
- gem "jeweler", "~> 1.8.4"
13
- end
data/Gemfile.lock DELETED
@@ -1,31 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- diff-lcs (1.1.3)
5
- git (1.2.5)
6
- jeweler (1.8.4)
7
- bundler (~> 1.0)
8
- git (>= 1.2.5)
9
- rake
10
- rdoc
11
- json (1.7.7)
12
- rake (10.0.4)
13
- rdoc (3.12.2)
14
- json (~> 1.4)
15
- rspec (2.8.0)
16
- rspec-core (~> 2.8.0)
17
- rspec-expectations (~> 2.8.0)
18
- rspec-mocks (~> 2.8.0)
19
- rspec-core (2.8.0)
20
- rspec-expectations (2.8.0)
21
- diff-lcs (~> 1.1.2)
22
- rspec-mocks (2.8.0)
23
-
24
- PLATFORMS
25
- ruby
26
-
27
- DEPENDENCIES
28
- bundler (>= 1.0.0)
29
- jeweler (~> 1.8.4)
30
- rdoc (~> 3.12)
31
- rspec (~> 2.8.0)
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.6
data/spec/spec_helper.rb DELETED
@@ -1,12 +0,0 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
- require 'rspec'
4
- require 'windows_csv'
5
-
6
- # Requires supporting files with custom matchers and macros, etc,
7
- # in ./support/ and its subdirectories.
8
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
-
10
- RSpec.configure do |config|
11
-
12
- end
@@ -1,35 +0,0 @@
1
- #encoding: utf-8
2
-
3
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
-
5
- describe "WindowsCsv" do
6
- it "works" do
7
- require "tmpdir"
8
- require "digest"
9
- require "digest/md5"
10
-
11
- tmpfile = "#{Dir.tmpdir}/windows_csv_test_#{Digest::MD5.hexdigest(Time.now.to_f.to_s)}.csv"
12
-
13
- WindowsCsv.new(:path => tmpfile) do |csv|
14
- csv << ["Name1", "Name2", "Encoding", "Date", "Time", "DateTime", "MultiLine"]
15
- csv << ["Kasper", "Christina", "æøå", Date.new, Time.new, DateTime.new, "Multi\nLine"]
16
- csv << ["Thomas", "Nikolaj", "læp", nil, nil, nil, nil]
17
- end
18
-
19
- puts "Path: #{tmpfile}"
20
-
21
- count = 0
22
- WindowsCsv.foreach(tmpfile, :csv_args => {:headers => true}) do |row|
23
- count += 1
24
- #puts "Row: #{row}"
25
-
26
- if count == 1
27
- row[:Name2].should eql("Christina")
28
- row[:Encoding].should eql("æøå")
29
- row[:MultiLine].should eql("Multi\r\nLine")
30
- end
31
- end
32
-
33
- count.should eql(2)
34
- end
35
- end
data/windows_csv.gemspec DELETED
@@ -1,60 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = "windows_csv"
8
- s.version = "0.0.6"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["kaspernj"]
12
- s.date = "2013-05-02"
13
- s.description = "A framework to write Windows CSV files."
14
- s.email = "k@spernj.org"
15
- s.extra_rdoc_files = [
16
- "LICENSE.txt",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".rspec",
22
- "Gemfile",
23
- "Gemfile.lock",
24
- "LICENSE.txt",
25
- "README.rdoc",
26
- "Rakefile",
27
- "VERSION",
28
- "lib/windows_csv.rb",
29
- "spec/spec_helper.rb",
30
- "spec/windows_csv_spec.rb",
31
- "windows_csv.gemspec"
32
- ]
33
- s.homepage = "http://github.com/kaspernj/windows_csv"
34
- s.licenses = ["MIT"]
35
- s.require_paths = ["lib"]
36
- s.rubygems_version = "1.8.23"
37
- s.summary = "A framework to write Windows CSV files."
38
-
39
- if s.respond_to? :specification_version then
40
- s.specification_version = 3
41
-
42
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
- s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
44
- s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
45
- s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
46
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
47
- else
48
- s.add_dependency(%q<rspec>, ["~> 2.8.0"])
49
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
50
- s.add_dependency(%q<bundler>, [">= 1.0.0"])
51
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
52
- end
53
- else
54
- s.add_dependency(%q<rspec>, ["~> 2.8.0"])
55
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
56
- s.add_dependency(%q<bundler>, [">= 1.0.0"])
57
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
58
- end
59
- end
60
-