loady 0.8.0 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +22 -0
- data/README.md +26 -6
- data/lib/loady/attribute_array.rb +3 -3
- data/lib/loady/csv_loader.rb +7 -7
- data/lib/loady/memory_logger.rb +3 -3
- data/lib/loady/version.rb +1 -1
- metadata +20 -38
- data/.gitignore +0 -8
- data/.ruby-version +0 -1
- data/.travis.yml +0 -5
- data/Gemfile +0 -7
- data/Rakefile +0 -10
- data/loady.gemspec +0 -25
- data/test/csv/file1.csv +0 -11
- data/test/csv/file2.csv +0 -15
- data/test/csv/file3.dat +0 -11
- data/test/test_attribute_array.rb +0 -33
- data/test/test_csv_loader.rb +0 -62
- data/test/test_helper.rb +0 -6
- data/test/test_memory_logger.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ecc3df9a14b6e88e79402ef6f27f4f5fa9a451bf
|
4
|
+
data.tar.gz: 2d3fed1bdaed9630f5f518eeeab491fbf4aa041d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0f459bcfacda6e99aeef4a7850e07db3722506c9142ac56f19a16db97e2f71486ebdc80487d47fc1e5639f16c6cb085526672d672bac99121bed08f5073e75f
|
7
|
+
data.tar.gz: 21b249d660c826e3ccfeb4503681bb40105c7ff97e7bccd6f37091a0ba539d3f95656a45b524727a10727f55537f4eeea9f39ccfb92e00e37e8ec63e732ef73a
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Tee Parham
|
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
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
-
|
2
|
-
[![Build Status](https://api.travis-ci.org/teeparham/loady.png)](https://travis-ci.org/teeparham/loady)
|
3
|
-
[![Code Climate](https://codeclimate.com/github/teeparham/loady.png)](https://codeclimate.com/github/teeparham/loady)
|
4
|
-
[![Coverage Status](https://coveralls.io/repos/teeparham/loady/badge.png)](https://coveralls.io/r/teeparham/loady)
|
1
|
+
# Loady
|
5
2
|
|
6
|
-
|
3
|
+
[![Gem Version](http://img.shields.io/gem/v/loady.svg)](http://rubygems.org/gem/loady)
|
4
|
+
[![Build Status](http://img.shields.io/travis/teeparham/loady.svg)](https://travis-ci.org/teeparham/loady)
|
5
|
+
[![Code Climate](http://img.shields.io/codeclimate/github/teeparham/loady.svg)](https://codeclimate.com/github/teeparham/loady)
|
6
|
+
[![Coverage Status](http://img.shields.io/coveralls/teeparham/loady.svg)](https://coveralls.io/r/teeparham/loady)
|
7
7
|
|
8
|
-
Loady is a simple file reader and logger. Use it to
|
8
|
+
Loady is a simple file reader and logger. Use it to read any delimited file. Loady makes it easy to conveniently convert input fields to an attribute hash, continue on error rows, and do basic logging.
|
9
|
+
|
10
|
+
Loady was initially created to load hundreds of millions of rows from CSV files that had various data errors.
|
9
11
|
|
10
12
|
It works with MRI ruby 1.9.3+. It uses ruby's CSV library to parse rows.
|
11
13
|
|
@@ -64,3 +66,21 @@ Loady.read "/your/file.tab", col_sep: "\t" do |row|
|
|
64
66
|
end
|
65
67
|
```
|
66
68
|
|
69
|
+
### Collect log messages in memory
|
70
|
+
|
71
|
+
`Loady::MemoryLoader` is a class that collects log messages in memory.
|
72
|
+
|
73
|
+
``` ruby
|
74
|
+
memory_logger = Loady::MemoryLogger.new
|
75
|
+
|
76
|
+
Loady.read "/your/file.csv", logger: memory_logger do |row|
|
77
|
+
# do things
|
78
|
+
end
|
79
|
+
|
80
|
+
memory_logger.messages
|
81
|
+
=> [
|
82
|
+
'Line 123: Something bad happened.',
|
83
|
+
'Line 456: Exception of some sort.',
|
84
|
+
'Finished. Loaded 9998 rows. 2 unprocessed rows.'
|
85
|
+
]
|
86
|
+
```
|
@@ -9,15 +9,15 @@ module Loady
|
|
9
9
|
# options:
|
10
10
|
# strip: false -- default = true
|
11
11
|
# -- array values must be strings if :strip is true
|
12
|
-
def to_attributes(names, options={})
|
12
|
+
def to_attributes(names, options = {})
|
13
13
|
options = { strip: true }.merge(options)
|
14
14
|
|
15
15
|
attr_hash = {}
|
16
16
|
|
17
17
|
names.each_with_index do |name, i|
|
18
18
|
attr_hash[name] =
|
19
|
-
if i <
|
20
|
-
options[:strip] ? self[i].strip : self[i]
|
19
|
+
if i < size && self[i]
|
20
|
+
options[:strip] ? self[i].to_s.strip : self[i]
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
data/lib/loady/csv_loader.rb
CHANGED
@@ -14,18 +14,18 @@ module Loady
|
|
14
14
|
# logger: Logger.new('/somewhere/file.log') -- default = Logger.new(STDOUT)
|
15
15
|
# plus any valid options you can pass to CSV.new:
|
16
16
|
# see http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/classes/CSV.html#M000190
|
17
|
-
def read(filename, options={}, &block)
|
17
|
+
def read(filename, options = {}, &block)
|
18
18
|
@logger = options.delete(:logger) || default_logger
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
file = File.new(filename)
|
21
|
+
file.autoclose = true
|
22
|
+
file.gets if options.delete(:skip_first_row)
|
23
23
|
|
24
|
-
|
24
|
+
file.each do |line|
|
25
25
|
readline line, options, &block
|
26
26
|
end
|
27
27
|
|
28
|
-
@logger.info "Finished. Loaded
|
28
|
+
@logger.info "Finished. Loaded #{@success} rows. #{@warning} unprocessed rows."
|
29
29
|
end
|
30
30
|
|
31
31
|
class << self
|
@@ -46,7 +46,7 @@ module Loady
|
|
46
46
|
end
|
47
47
|
rescue Exception => ex
|
48
48
|
@warning += 1
|
49
|
-
@logger.warn "#{ex.to_s.gsub("line 1", "line
|
49
|
+
@logger.warn "#{ex.to_s.gsub("line 1", "line #{@line_number}")}\n#{line}"
|
50
50
|
end
|
51
51
|
|
52
52
|
def default_logger
|
data/lib/loady/memory_logger.rb
CHANGED
@@ -2,7 +2,7 @@ module Loady
|
|
2
2
|
class MemoryLogger
|
3
3
|
attr_reader :messages
|
4
4
|
|
5
|
-
def initialize(options={})
|
5
|
+
def initialize(options = {})
|
6
6
|
@messages = []
|
7
7
|
end
|
8
8
|
|
@@ -10,7 +10,7 @@ module Loady
|
|
10
10
|
@messages << message
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
alias warn info
|
14
|
+
alias error info
|
15
15
|
end
|
16
16
|
end
|
data/lib/loady/version.rb
CHANGED
metadata
CHANGED
@@ -1,83 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loady
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tee Parham
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mocha
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
19
|
+
version: '1.0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
26
|
+
version: '1.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '10.1'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '10.1'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '5.2'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
55
|
-
description:
|
54
|
+
version: '5.2'
|
55
|
+
description: File loader with simple logging
|
56
56
|
email:
|
57
57
|
- tee@neighborland.com
|
58
58
|
executables: []
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
-
|
63
|
-
- ".ruby-version"
|
64
|
-
- ".travis.yml"
|
65
|
-
- Gemfile
|
62
|
+
- LICENSE.txt
|
66
63
|
- README.md
|
67
|
-
- Rakefile
|
68
64
|
- lib/loady.rb
|
69
65
|
- lib/loady/attribute_array.rb
|
70
66
|
- lib/loady/csv_loader.rb
|
71
67
|
- lib/loady/memory_logger.rb
|
72
68
|
- lib/loady/version.rb
|
73
|
-
- loady.gemspec
|
74
|
-
- test/csv/file1.csv
|
75
|
-
- test/csv/file2.csv
|
76
|
-
- test/csv/file3.dat
|
77
|
-
- test/test_attribute_array.rb
|
78
|
-
- test/test_csv_loader.rb
|
79
|
-
- test/test_helper.rb
|
80
|
-
- test/test_memory_logger.rb
|
81
69
|
homepage: http://github.com/teeparham/loady
|
82
70
|
licenses:
|
83
71
|
- MIT
|
@@ -98,15 +86,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
86
|
version: '0'
|
99
87
|
requirements: []
|
100
88
|
rubyforge_project:
|
101
|
-
rubygems_version: 2.
|
89
|
+
rubygems_version: 2.4.4
|
102
90
|
signing_key:
|
103
91
|
specification_version: 4
|
104
|
-
summary:
|
105
|
-
test_files:
|
106
|
-
|
107
|
-
- test/csv/file2.csv
|
108
|
-
- test/csv/file3.dat
|
109
|
-
- test/test_attribute_array.rb
|
110
|
-
- test/test_csv_loader.rb
|
111
|
-
- test/test_helper.rb
|
112
|
-
- test/test_memory_logger.rb
|
92
|
+
summary: File loader with simple logging
|
93
|
+
test_files: []
|
94
|
+
has_rdoc:
|
data/.gitignore
DELETED
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
ruby-2.1.0
|
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
data/loady.gemspec
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
require "./lib/loady/version"
|
3
|
-
|
4
|
-
Gem::Specification.new do |s|
|
5
|
-
s.name = "loady"
|
6
|
-
s.version = Loady::VERSION
|
7
|
-
s.platform = Gem::Platform::RUBY
|
8
|
-
s.authors = ["Tee Parham"]
|
9
|
-
s.email = %w(tee@neighborland.com)
|
10
|
-
s.homepage = "http://github.com/teeparham/loady"
|
11
|
-
s.summary = %q{CSV file loader with simple logging}
|
12
|
-
s.description = %q{CSV file loader with simple logging}
|
13
|
-
s.license = "MIT"
|
14
|
-
|
15
|
-
s.files = `git ls-files`.split("\n")
|
16
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
-
s.executables = []
|
18
|
-
s.require_paths = %w(lib)
|
19
|
-
|
20
|
-
s.required_ruby_version = '>= 1.9.3'
|
21
|
-
|
22
|
-
s.add_development_dependency "mocha"
|
23
|
-
s.add_development_dependency "rake"
|
24
|
-
s.add_development_dependency "minitest"
|
25
|
-
end
|
data/test/csv/file1.csv
DELETED
data/test/csv/file2.csv
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
"Bubbles","2000"
|
2
|
-
"Grape Ape",1975
|
3
|
-
Albert,1948,note: everything is read as a string whether it is quoted or not
|
4
|
-
Koko,1971
|
5
|
-
Curious George,1941
|
6
|
-
blah.......,
|
7
|
-
",,,,,,,,,,,,,,,,,,,,this is a bad row because of the quote
|
8
|
-
",,,,,,,,,,,",,,/"\",,,,,,this is a bad row too
|
9
|
-
|
10
|
-
" blank rows are skipped, this is bad though
|
11
|
-
|
12
|
-
Bear,1980,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"this is ok"
|
13
|
-
Mighty Joe Young
|
14
|
-
Donkey Kong,1981
|
15
|
-
King Kong,1933
|
data/test/csv/file3.dat
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class AttributeArrayTest < MiniTest::Spec
|
4
|
-
it "respond to to_attributes" do
|
5
|
-
aa = Loady::AttributeArray.new
|
6
|
-
assert aa.respond_to? :to_attributes
|
7
|
-
end
|
8
|
-
|
9
|
-
it "initialize with Array" do
|
10
|
-
aa = Loady::AttributeArray.new([1,2])
|
11
|
-
assert_equal [1,2], aa
|
12
|
-
end
|
13
|
-
|
14
|
-
describe "#to_attributes" do
|
15
|
-
it "return named attributes" do
|
16
|
-
row = Loady::AttributeArray.new(['Bubbles ', '2000', ' King Kong '])
|
17
|
-
attrs = row.to_attributes [:name, :year, :mom]
|
18
|
-
assert_equal attrs.size, 3
|
19
|
-
assert_equal attrs[:name], 'Bubbles'
|
20
|
-
assert_equal attrs[:year], '2000'
|
21
|
-
assert_equal attrs[:mom], 'King Kong'
|
22
|
-
end
|
23
|
-
|
24
|
-
it "return named attributes when missing values" do
|
25
|
-
row = Loady::AttributeArray.new(['Bubbles ', '2000'])
|
26
|
-
attrs = row.to_attributes [:name, :year, :mom]
|
27
|
-
assert_equal attrs.size, 3
|
28
|
-
assert_equal attrs[:name], 'Bubbles'
|
29
|
-
assert_equal attrs[:year], '2000'
|
30
|
-
assert_nil attrs[:mom]
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
data/test/test_csv_loader.rb
DELETED
@@ -1,62 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class CsvLoaderTest < MiniTest::Spec
|
4
|
-
it "delegate Loady.csv to instance #read" do
|
5
|
-
Loady::CsvLoader.any_instance.expects :read
|
6
|
-
Loady.csv("file")
|
7
|
-
end
|
8
|
-
|
9
|
-
it "delegate Loady.read to instance #read" do
|
10
|
-
Loady::CsvLoader.any_instance.expects :read
|
11
|
-
Loady.read("file")
|
12
|
-
end
|
13
|
-
|
14
|
-
it "delegate #read to instance #read" do
|
15
|
-
Loady::CsvLoader.any_instance.expects :read
|
16
|
-
Loady::CsvLoader.read("file")
|
17
|
-
end
|
18
|
-
|
19
|
-
it "read file1" do
|
20
|
-
monkeys = []
|
21
|
-
|
22
|
-
Loady.read "test/csv/file1.csv", skip_first_row: true do |row|
|
23
|
-
monkeys << { name: row[0], year: row[1] }
|
24
|
-
end
|
25
|
-
|
26
|
-
assert_equal monkeys.count, 10, "total rows read"
|
27
|
-
assert_equal monkeys[0][:name], "Bubbles", "first row name"
|
28
|
-
assert_equal monkeys[0][:year], "2000", "first row year"
|
29
|
-
assert_equal monkeys[9][:name], "King Kong", "last row name"
|
30
|
-
assert_equal monkeys[9][:year], "1933", "last row year"
|
31
|
-
end
|
32
|
-
|
33
|
-
it "read file2 with logger using named attributes" do
|
34
|
-
logger = Logger.new("/dev/null")
|
35
|
-
monkeys = []
|
36
|
-
|
37
|
-
Loady.read "test/csv/file2.csv", logger: logger do |row|
|
38
|
-
monkeys << row.to_attributes([:name, :year])
|
39
|
-
end
|
40
|
-
|
41
|
-
assert_equal monkeys.count, 10, "total rows read"
|
42
|
-
assert_equal monkeys[0][:name], "Bubbles", "first row name"
|
43
|
-
assert_equal monkeys[0][:year], "2000", "first row year"
|
44
|
-
assert_equal monkeys[9][:name], "King Kong", "last row name"
|
45
|
-
assert_equal monkeys[9][:year], "1933", "last row year"
|
46
|
-
end
|
47
|
-
|
48
|
-
it "read file3, a tab-delimited file" do
|
49
|
-
monkeys = []
|
50
|
-
|
51
|
-
Loady.read "test/csv/file3.dat", skip_first_row: true, col_sep: "\t" do |row|
|
52
|
-
monkeys << { name: row[0], year: row[1] }
|
53
|
-
end
|
54
|
-
|
55
|
-
assert_equal monkeys.count, 10, "total rows read"
|
56
|
-
assert_equal monkeys[0][:name], "Bubbles", "first row name"
|
57
|
-
assert_equal monkeys[0][:year], "2000", "first row year"
|
58
|
-
assert_equal monkeys[9][:name], "King Kong", "last row name"
|
59
|
-
assert_equal monkeys[9][:year], "1933", "last row year"
|
60
|
-
end
|
61
|
-
|
62
|
-
end
|
data/test/test_helper.rb
DELETED
data/test/test_memory_logger.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class MemoryLoggerTest < MiniTest::Spec
|
4
|
-
describe "memory logger" do
|
5
|
-
before do
|
6
|
-
@logger = Loady::MemoryLogger.new
|
7
|
-
end
|
8
|
-
|
9
|
-
it "have no messages" do
|
10
|
-
assert_empty @logger.messages
|
11
|
-
end
|
12
|
-
|
13
|
-
it "log warning" do
|
14
|
-
@logger.warn "message"
|
15
|
-
assert_equal "message", @logger.messages.first
|
16
|
-
end
|
17
|
-
|
18
|
-
it "log info" do
|
19
|
-
@logger.info "message"
|
20
|
-
assert_equal "message", @logger.messages.first
|
21
|
-
end
|
22
|
-
|
23
|
-
it "log error" do
|
24
|
-
@logger.error "message"
|
25
|
-
assert_equal "message", @logger.messages.first
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|