csvash 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/csvash.gemspec +17 -0
- data/lib/csvash/version.rb +3 -0
- data/lib/csvash.rb +27 -0
- data/test/example.csv +5 -0
- data/test/fixtures/example.csv +5 -0
- data/test/fixtures/example_values.txt +1 -0
- data/test/hashifying_test.rb +20 -0
- data/test/test_helper.rb +7 -0
- metadata +66 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Lukas Alexandre
|
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
|
+
# Csvash
|
2
|
+
|
3
|
+
Csvash automates your CSV extraction by mapping its headers with the columns contents and turning them into hashes that can be easily converted into ruby models.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'csvash'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install csvash
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Csvash.import_from_path '/path/of/your/file.csv'
|
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 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/csvash.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/csvash/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = "csvash"
|
6
|
+
gem.authors = ["Lukas Alexandre"]
|
7
|
+
gem.email = ["lukeskytm@gmail.com"]
|
8
|
+
gem.description = %q{Csvash automates your CSV extraction by mapping its headers with the columns contents and turning them into hashes that can be easily converted into ruby models.}
|
9
|
+
gem.summary = %q{Csvash automates your CSV extraction by mapping its headers with the columns contents and turning them into hashes that can be easily converted into ruby models.}
|
10
|
+
gem.homepage = "https://github.com/lukasalexandre/csvash"
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Csvash::VERSION
|
17
|
+
end
|
data/lib/csvash.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "csvash/version"
|
2
|
+
require 'csv'
|
3
|
+
|
4
|
+
module Csvash
|
5
|
+
def self.import_from_path(path)
|
6
|
+
transactions = []
|
7
|
+
|
8
|
+
cols = nil
|
9
|
+
first_line = true
|
10
|
+
collection = []
|
11
|
+
|
12
|
+
CSV.foreach(path, :encoding => "ISO-8859-1:UTF-8", :col_sep => "\;") do |line|
|
13
|
+
if first_line
|
14
|
+
# getting colum names and turning them into symbols
|
15
|
+
cols = line.map! { |c| c.downcase.to_sym }
|
16
|
+
first_line = false
|
17
|
+
next
|
18
|
+
end
|
19
|
+
|
20
|
+
# map the csv columns with the current line values
|
21
|
+
current_line = Hash[cols.zip(line.flatten)]
|
22
|
+
# storing the final line result
|
23
|
+
collection << current_line
|
24
|
+
end
|
25
|
+
collection
|
26
|
+
end
|
27
|
+
end
|
data/test/example.csv
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
[['1997', 'Ford', 'E350', 'ac, abs, moon', '3000.00'], ['1999', 'Chevy', 'Venture Extended Edition', nil, '4900.00'], ['1999', 'Chevy', 'Venture Extended Edition, Very Large', '', '5000.00'], ['1996', 'Jeep', 'Grand Cherokee', 'MUST SELL! air, moon roof, loaded', '4799.00']]
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'csvash'
|
4
|
+
require "./test/test_helper"
|
5
|
+
|
6
|
+
include TestHelper
|
7
|
+
|
8
|
+
describe 'Hashifiyng' do
|
9
|
+
it "checking columns" do
|
10
|
+
hash = Csvash.import_from_path fetch_fixture_path('example.csv')
|
11
|
+
hash.first.keys.size.must_equal 5
|
12
|
+
hash.first.keys.must_equal [:year, :make, :model, :description, :price]
|
13
|
+
end
|
14
|
+
|
15
|
+
it "checking values" do
|
16
|
+
hash = Csvash.import_from_path fetch_fixture_path('example.csv')
|
17
|
+
txt = File.read fetch_fixture_path('example_values.txt')
|
18
|
+
hash.map(&:values).must_equal eval(txt)
|
19
|
+
end
|
20
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csvash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lukas Alexandre
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-29 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Csvash automates your CSV extraction by mapping its headers with the
|
15
|
+
columns contents and turning them into hashes that can be easily converted into
|
16
|
+
ruby models.
|
17
|
+
email:
|
18
|
+
- lukeskytm@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- csvash.gemspec
|
29
|
+
- lib/csvash.rb
|
30
|
+
- lib/csvash/version.rb
|
31
|
+
- test/example.csv
|
32
|
+
- test/fixtures/example.csv
|
33
|
+
- test/fixtures/example_values.txt
|
34
|
+
- test/hashifying_test.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
homepage: https://github.com/lukasalexandre/csvash
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.24
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Csvash automates your CSV extraction by mapping its headers with the columns
|
60
|
+
contents and turning them into hashes that can be easily converted into ruby models.
|
61
|
+
test_files:
|
62
|
+
- test/example.csv
|
63
|
+
- test/fixtures/example.csv
|
64
|
+
- test/fixtures/example_values.txt
|
65
|
+
- test/hashifying_test.rb
|
66
|
+
- test/test_helper.rb
|