loady 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,8 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ .rvmrc
5
+ .idea
6
+ *#
7
+ .#*
8
+ *~
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # see loady.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ loady (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ shoulda (2.11.3)
10
+ test-unit (2.1.2)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ loady!
17
+ shoulda
18
+ test-unit
data/README.rdoc ADDED
@@ -0,0 +1,41 @@
1
+ = Loady - A CSV file loader with simple logging
2
+
3
+ Loady is a simple file reader and logger. Use it to quickly load a csv file, continue on error rows, and do basic logging.
4
+
5
+ == Install
6
+
7
+ gem install loady
8
+
9
+ Use bunder. Include the gem in your Gemfile:
10
+
11
+ gem 'loady'
12
+
13
+ == Use
14
+
15
+ If any error occurs, Loady will continue reading the file, ignoring problems rows and logging a warning for each.
16
+
17
+ By default, messages are logged to the standard output.
18
+
19
+ Basic usage:
20
+
21
+ Loady::CsvLoader.read "/your/file.csv" do |row|
22
+ puts "#{row[0]},#{row[1]},etc."
23
+ end
24
+
25
+ Skip first row and log to a file:
26
+
27
+ logger = Logger.new("/your/file.log")
28
+
29
+ Loady::CsvLoader.read "/your/file.csv", :logger => logger, :skip_first_row => true do |row|
30
+ # do some stuff
31
+ end
32
+
33
+ == Development
34
+
35
+ Fork away. Please create a topic branch and write passing tests if you are submitting a pull request.
36
+
37
+ git clone git://github.com/[yourfork]/loady.git
38
+ cd loady
39
+ bundle install
40
+ rake test
41
+ git checkout -b your_fix
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'bundler'
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ require 'rake/testtask'
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.test_files = ['test/**/*.rb']
10
+ t.verbose = false
11
+ end
data/lib/loady.rb ADDED
@@ -0,0 +1 @@
1
+ require 'loady/csv_loader.rb'
@@ -0,0 +1,60 @@
1
+ require 'csv'
2
+ require 'logger'
3
+
4
+ module Loady
5
+ class CsvLoader
6
+
7
+ # options:
8
+ # :skip_first_row => true -- default = false
9
+ # :logger => Logger.new('/somewhere/file.log') -- default = Logger.new(STDOUT)
10
+ def self.read(filename, options = {})
11
+ success = 0
12
+ warning = 0
13
+
14
+ logger = options[:logger]
15
+
16
+ unless logger
17
+ logger = Logger.new(STDOUT)
18
+ logger.datetime_format = "%H:%M:%S"
19
+ end
20
+
21
+ begin
22
+ f = File.new(filename)
23
+ f.readline if options[:skip_first_row]
24
+
25
+ line_number = 0
26
+
27
+ begin
28
+ while (line = f.readline)
29
+ begin
30
+ line_number += 1
31
+
32
+ row = CSV.parse(line)[0]
33
+
34
+ unless row.empty?
35
+ yield row
36
+
37
+ success += 1
38
+ end
39
+ rescue Exception => message
40
+ warning += 1
41
+ logger.warn "#{message.to_s.gsub("line 1", "line #{line_number}")}\n#{line}"
42
+ end
43
+ end
44
+ rescue EOFError
45
+ f.close
46
+ rescue Exception => message
47
+ warning += 1
48
+ line_number += 1
49
+ logger.error "#{message.to_s + " - at line #{line_number}"}\n#{line}"
50
+ end
51
+
52
+ logger.info "Finished. Loaded #{success} rows. #{warning} unprocessed rows."
53
+
54
+ rescue Exception => message
55
+ logger.fatal "Unhandled error: \n#{message}\n"
56
+ end
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Loady
2
+ VERSION = "0.0.1"
3
+ end
data/loady.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "loady/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "loady"
7
+ s.version = Loady::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Tee Parham"]
10
+ s.email = ["tee@stackpilot.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{CSV file loader with simple logging}
13
+ s.description = %q{CSV file loader with simple logging}
14
+
15
+ s.rubyforge_project = "loady"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency "test-unit"
23
+ s.add_development_dependency "shoulda"
24
+ end
@@ -0,0 +1,11 @@
1
+ monkey name,best year
2
+ Bubbles,2000
3
+ Grape Ape,1975
4
+ Albert,1948
5
+ Koko,1971
6
+ Curious George,1941
7
+ Clyde,1975
8
+ Bear,1980
9
+ Mighty Joe Young,1998
10
+ Donkey Kong,1981
11
+ King Kong,1933
@@ -0,0 +1,15 @@
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,1998
14
+ Donkey Kong,1981
15
+ King Kong,1933
@@ -0,0 +1,38 @@
1
+ require File.expand_path('test_helper.rb', File.dirname(__FILE__))
2
+
3
+ class CsvLoaderTest < Test::Unit::TestCase
4
+ context "Simple class" do
5
+ setup do
6
+ end
7
+
8
+ should "read file1" do
9
+ monkeys = []
10
+
11
+ Loady::CsvLoader.read "test/csv/file1.csv", :skip_first_row => true do |row|
12
+ monkeys << { :name => row[0], :year => row[1] }
13
+ end
14
+
15
+ assert monkeys.count == 10, "total rows read"
16
+ assert monkeys[0][:name] == "Bubbles", "first row name"
17
+ assert monkeys[0][:year] == "2000", "first row year"
18
+ assert monkeys[9][:name] == "King Kong", "last row name"
19
+ assert monkeys[9][:year] == "1933", "last row year"
20
+ end
21
+
22
+ should "read file2" do
23
+ logger = Logger.new("/dev/null")
24
+ monkeys = []
25
+
26
+ Loady::CsvLoader.read "test/csv/file2.csv", :logger => logger do |row|
27
+ monkeys << { :name => row[0], :year => row[1] }
28
+ end
29
+
30
+ assert monkeys.count == 10, "total rows read"
31
+ assert monkeys[0][:name] == "Bubbles", "first row name"
32
+ assert monkeys[0][:year] == "2000", "first row year"
33
+ assert monkeys[9][:name] == "King Kong", "last row name"
34
+ assert monkeys[9][:year] == "1933", "last row year"
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,4 @@
1
+ require 'test/unit'
2
+ require 'shoulda'
3
+
4
+ require 'loady'
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: loady
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Tee Parham
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-13 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: test-unit
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: shoulda
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ description: CSV file loader with simple logging
47
+ email:
48
+ - tee@stackpilot.com
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ extra_rdoc_files: []
54
+
55
+ files:
56
+ - .gitignore
57
+ - Gemfile
58
+ - Gemfile.lock
59
+ - README.rdoc
60
+ - Rakefile
61
+ - lib/loady.rb
62
+ - lib/loady/csv_loader.rb
63
+ - lib/loady/version.rb
64
+ - loady.gemspec
65
+ - test/csv/file1.csv
66
+ - test/csv/file2.csv
67
+ - test/test_csv_loader.rb
68
+ - test/test_helper.rb
69
+ has_rdoc: true
70
+ homepage: ""
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options: []
75
+
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project: loady
97
+ rubygems_version: 1.3.7
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: CSV file loader with simple logging
101
+ test_files:
102
+ - test/csv/file1.csv
103
+ - test/csv/file2.csv
104
+ - test/test_csv_loader.rb
105
+ - test/test_helper.rb