hasherize_csv 0.0.2
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/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +6 -0
- data/hasherize_csv.gemspec +24 -0
- data/lib/hasherize_csv.rb +50 -0
- data/lib/hasherize_csv/version.rb +3 -0
- data/test/sample_csv.csv +8 -0
- data/test/test_hasherize_csv.rb +46 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Brent Bradbury
|
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
|
+
# HasherizeCsv
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'hasherize_csv'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install hasherize_csv
|
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,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hasherize_csv/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "hasherize_csv"
|
8
|
+
gem.version = HasherizeCsv::VERSION
|
9
|
+
gem.authors = ["Brent Bradbury"]
|
10
|
+
gem.email = ["brent@brentium.com"]
|
11
|
+
gem.description = %q{Turns csv files into hashes without reading the entire csv into memory}
|
12
|
+
gem.summary = %q{Hasherizes a csv file line by line}
|
13
|
+
gem.homepage = "https://github.com/bbradbury/hasherize_csv"
|
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
|
+
# Dependencies
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# gem.add_runtime_dependency "activesupport", ">=3.0.0"
|
23
|
+
# gem.add_runtime_dependency "testunit"
|
24
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "hasherize_csv/version"
|
2
|
+
|
3
|
+
module HasherizeCsv
|
4
|
+
module DefaultOpts
|
5
|
+
SALESFORCE = { :separator => "\r", :value_pattern => /\"(.*?)\"/m }
|
6
|
+
end
|
7
|
+
|
8
|
+
class Csv
|
9
|
+
attr_accessor :keys, :file, :separator
|
10
|
+
def initialize file, opts = {}
|
11
|
+
@file = file
|
12
|
+
@separator = opts[:separator] || "\n"
|
13
|
+
@value_pattern = opts[:value_pattern] || /([\s\w_-]+),?/
|
14
|
+
@keys = []
|
15
|
+
next_line { |l| @keys = values_from_line l if !l.nil? }
|
16
|
+
end
|
17
|
+
|
18
|
+
def next_item
|
19
|
+
next_line { |l|
|
20
|
+
if l.nil?
|
21
|
+
yield nil
|
22
|
+
else
|
23
|
+
yield hashify_values( values_from_line l )
|
24
|
+
end
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def each
|
29
|
+
while(1)
|
30
|
+
self.next_item { |hash|
|
31
|
+
return if hash.nil?
|
32
|
+
yield hash
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def next_line
|
39
|
+
yield(@file.gets(@separator))
|
40
|
+
end
|
41
|
+
|
42
|
+
def hashify_values values
|
43
|
+
Hash[keys.zip(values)] if !values.nil? and values.length != 0
|
44
|
+
end
|
45
|
+
|
46
|
+
def values_from_line line
|
47
|
+
line.scan(@value_pattern).flatten
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/test/sample_csv.csv
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'hasherize_csv'
|
3
|
+
|
4
|
+
class HasherizeCsvTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@f = File.new(File.join(File.expand_path(File.dirname(__FILE__)),"sample_csv.csv"))
|
7
|
+
@csv = HasherizeCsv::Csv.new(@f, HasherizeCsv::DefaultOpts::SALESFORCE)
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
@f.close
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_keys
|
15
|
+
assert_equal ["Heading1", "Heading2", "H_EA_3__c"], @csv.keys
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_hash_file
|
19
|
+
@csv.next { |hash|
|
20
|
+
assert_equal({"Heading1"=>"Item1", "Heading2"=>"Item2", "H_EA_3__c"=>"Item3"}, hash)
|
21
|
+
}
|
22
|
+
|
23
|
+
@csv.next { |hash|
|
24
|
+
assert_equal({"Heading1"=>"Email",
|
25
|
+
"Heading2"=> "SUP MAN\n\nWE HAVE LOTS OF MULTILINE EMAILS IN CAPS\n\nTHEY ALSO HAVE LINEBREAKS IN THEM",
|
26
|
+
"H_EA_3__c"=>"FinalField"}, hash)
|
27
|
+
}
|
28
|
+
|
29
|
+
@csv.next { |hash|
|
30
|
+
assert_equal nil, hash
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_hash_file
|
35
|
+
result = []
|
36
|
+
@csv.each { |hash|
|
37
|
+
result << hash
|
38
|
+
}
|
39
|
+
assert_equal [{"Heading1"=>"Item1", "Heading2"=>"Item2", "H_EA_3__c"=>"Item3"},
|
40
|
+
{"Heading1"=>"Email",
|
41
|
+
"Heading2"=>"SUP MAN\n\nWE HAVE LOTS OF MULTILINE EMAILS IN CAPS\n\nTHEY ALSO HAVE LINEBREAKS IN THEM",
|
42
|
+
"H_EA_3__c"=>"FinalField"},
|
43
|
+
{"Heading1"=>"Blank1", "Heading2"=>"", "H_EA_3__c"=>""}], result
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hasherize_csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brent Bradbury
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-25 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Turns csv files into hashes without reading the entire csv into memory
|
15
|
+
email:
|
16
|
+
- brent@brentium.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- hasherize_csv.gemspec
|
27
|
+
- lib/hasherize_csv.rb
|
28
|
+
- lib/hasherize_csv/version.rb
|
29
|
+
- test/sample_csv.csv
|
30
|
+
- test/test_hasherize_csv.rb
|
31
|
+
homepage: https://github.com/bbradbury/hasherize_csv
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
none: false
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
none: false
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.24
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Hasherizes a csv file line by line
|
55
|
+
test_files:
|
56
|
+
- test/sample_csv.csv
|
57
|
+
- test/test_hasherize_csv.rb
|
58
|
+
...
|