kiba-common 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -7
- data/Changes.md +6 -0
- data/README.md +37 -1
- data/lib/kiba-common/destinations/csv.rb +28 -0
- data/lib/kiba-common/version.rb +1 -1
- data/test/test_csv_destination.rb +48 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae21d45df8c55e0afa30187fc42eaad9ee4dade7
|
4
|
+
data.tar.gz: bc3abd7f9003f1fccbe946a65103aa4fceb0e733
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a38adada1e8f470dc7475d69db51c7502e72c3891a82380f669af3222ebcb199da0b49dd9f8056b6d50c1cf210846befd848b5b1ae7ee70f388cddb5f5b582b8
|
7
|
+
data.tar.gz: 66183f174f044a72c139ecf5d2a3c324faee62d81acb85daf6d02e2079faf944154020ab961442f0e6787bbd37b95175ab3b6426aa0d01b74c92f81538dc8494
|
data/.travis.yml
CHANGED
data/Changes.md
CHANGED
data/README.md
CHANGED
@@ -12,7 +12,43 @@ gem 'kiba-common'
|
|
12
12
|
|
13
13
|
Then see below for each module usage & require clause.
|
14
14
|
|
15
|
-
##
|
15
|
+
## Supported Ruby versions
|
16
|
+
|
17
|
+
`kiba-common` currently supports Ruby 2.3+ and JRuby (with its default 1.9 syntax). See [test matrix](https://travis-ci.org/thbar/kiba-common).
|
18
|
+
|
19
|
+
## Available components
|
20
|
+
|
21
|
+
### Kiba::Common::Destinations::CSV
|
22
|
+
|
23
|
+
A way to dump `Hash` rows as CSV.
|
24
|
+
|
25
|
+
All rows are expected to have the exact same set of keys as the first row.
|
26
|
+
|
27
|
+
The headers will be the first row keys, unless you pass an array of keys via `headers`.
|
28
|
+
|
29
|
+
All keys are mandatory (although they can have a nil value).
|
30
|
+
|
31
|
+
Use the `csv_options` keyword to control the output format like you would do when using [Ruby CSV class](http://ruby-doc.org/stdlib-2.4.0/libdoc/csv/rdoc/CSV.html#method-c-new).
|
32
|
+
|
33
|
+
Usage:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'kiba-common/destinations/csv'
|
37
|
+
|
38
|
+
# by default, the headers will be picked from the first row:
|
39
|
+
destination Kiba::Common::Destinations::CSV,
|
40
|
+
filename: 'output.csv'
|
41
|
+
|
42
|
+
# if you need a different separator:
|
43
|
+
destination Kiba::Common::Destinations::CSV,
|
44
|
+
filename: 'output.csv',
|
45
|
+
csv_options: { col_sep: ';' }
|
46
|
+
|
47
|
+
# to enforce a specific set of headers:
|
48
|
+
destination Kiba::Common::Destinations::CSV,
|
49
|
+
filename: 'output.csv',
|
50
|
+
headers: [:field, :other_field]
|
51
|
+
```
|
16
52
|
|
17
53
|
### Kiba::Common::DSLExtensions::Logger
|
18
54
|
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module Kiba
|
4
|
+
module Common
|
5
|
+
module Destinations
|
6
|
+
class CSV
|
7
|
+
attr_reader :filename, :csv_options, :csv, :headers
|
8
|
+
|
9
|
+
def initialize(filename:, csv_options: nil, headers: nil)
|
10
|
+
@filename = filename
|
11
|
+
@csv_options = csv_options
|
12
|
+
@headers = headers
|
13
|
+
end
|
14
|
+
|
15
|
+
def write(row)
|
16
|
+
@csv ||= ::CSV.open(filename, 'wb', csv_options)
|
17
|
+
@headers ||= row.keys
|
18
|
+
@headers_written ||= (csv << headers ; true)
|
19
|
+
csv << row.fetch_values(*@headers)
|
20
|
+
end
|
21
|
+
|
22
|
+
def close
|
23
|
+
csv&.close
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/kiba-common/version.rb
CHANGED
@@ -0,0 +1,48 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
require 'kiba'
|
3
|
+
require 'kiba-common/destinations/csv'
|
4
|
+
|
5
|
+
class TestCSVDestination < Minitest::Test
|
6
|
+
TEST_FILENAME = 'output.csv'
|
7
|
+
|
8
|
+
def teardown
|
9
|
+
File.delete(TEST_FILENAME) if File.exist?(TEST_FILENAME)
|
10
|
+
end
|
11
|
+
|
12
|
+
def run_etl(csv_options: nil, headers: nil)
|
13
|
+
job = Kiba.parse do
|
14
|
+
source TestEnumerableSource, [
|
15
|
+
{name: "world", age: 999}
|
16
|
+
]
|
17
|
+
destination Kiba::Common::Destinations::CSV,
|
18
|
+
filename: TEST_FILENAME,
|
19
|
+
csv_options: csv_options,
|
20
|
+
headers: headers
|
21
|
+
end
|
22
|
+
|
23
|
+
Kiba.run(job)
|
24
|
+
|
25
|
+
IO.read(TEST_FILENAME)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_classic
|
29
|
+
assert_equal <<~CSV, run_etl
|
30
|
+
name,age
|
31
|
+
world,999
|
32
|
+
CSV
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_csv_options
|
36
|
+
assert_equal <<~CSV, run_etl(csv_options: {col_sep: ';'})
|
37
|
+
name;age
|
38
|
+
world;999
|
39
|
+
CSV
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_headers_provided
|
43
|
+
assert_equal <<~CSV, run_etl(headers: [:age])
|
44
|
+
age
|
45
|
+
999
|
46
|
+
CSV
|
47
|
+
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kiba-common
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thibaut Barrère
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kiba
|
@@ -81,12 +81,14 @@ files:
|
|
81
81
|
- README.md
|
82
82
|
- Rakefile
|
83
83
|
- kiba-common.gemspec
|
84
|
+
- lib/kiba-common/destinations/csv.rb
|
84
85
|
- lib/kiba-common/dsl_extensions/logger.rb
|
85
86
|
- lib/kiba-common/dsl_extensions/show_me.rb
|
86
87
|
- lib/kiba-common/version.rb
|
87
88
|
- test/helper.rb
|
88
89
|
- test/support/assert_called.rb
|
89
90
|
- test/support/test_enumerable_source.rb
|
91
|
+
- test/test_csv_destination.rb
|
90
92
|
- test/test_logger.rb
|
91
93
|
- test/test_show_me.rb
|
92
94
|
homepage: https://github.com/thbar/kiba-common
|
@@ -117,5 +119,6 @@ test_files:
|
|
117
119
|
- test/helper.rb
|
118
120
|
- test/support/assert_called.rb
|
119
121
|
- test/support/test_enumerable_source.rb
|
122
|
+
- test/test_csv_destination.rb
|
120
123
|
- test/test_logger.rb
|
121
124
|
- test/test_show_me.rb
|