kiba-common 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9a78f79fe5c727bce8d34e47d627ab90989373a
4
- data.tar.gz: 1e5c2928148dbd5a4179f4e28fc4a575d3c0f586
3
+ metadata.gz: ae21d45df8c55e0afa30187fc42eaad9ee4dade7
4
+ data.tar.gz: bc3abd7f9003f1fccbe946a65103aa4fceb0e733
5
5
  SHA512:
6
- metadata.gz: 2073edaeeea322fc85fbf0909c439d53465669b1b0a1de99bff42d73d6673fb3a43f11080da344da3dc00d6cf39f14292adfbe073873762a96a7a890b9b73a2e
7
- data.tar.gz: bf8d5816f1d0801a848fa0991e37b219dc0d1601a375aa8cdbf843c835a9ac7481ecbef6dd7f49557c582add51a13e081965eb5f5cc9e5c50e7a487a4ef7a285
6
+ metadata.gz: a38adada1e8f470dc7475d69db51c7502e72c3891a82380f669af3222ebcb199da0b49dd9f8056b6d50c1cf210846befd848b5b1ae7ee70f388cddb5f5b582b8
7
+ data.tar.gz: 66183f174f044a72c139ecf5d2a3c324faee62d81acb85daf6d02e2079faf944154020ab961442f0e6787bbd37b95175ab3b6426aa0d01b74c92f81538dc8494
data/.travis.yml CHANGED
@@ -2,10 +2,6 @@ language: ruby
2
2
  before_install:
3
3
  - gem update bundler
4
4
  rvm:
5
- - 2.4.0
6
- - 2.3.4
7
- - 2.2
8
- - 2.1
9
- - 2.0
10
- - jruby-1.7
11
- - jruby-9
5
+ - 2.4.3
6
+ - 2.3.6
7
+ - jruby-9.1.15.0
data/Changes.md CHANGED
@@ -1,6 +1,12 @@
1
1
  HEAD
2
2
  ----
3
3
 
4
+ 0.0.3
5
+ -----
6
+
7
+ - Breaking: Kiba::Common requires Ruby 2.3+ from now on.
8
+ - New: Kiba::Common::Destinations:CSV allows to write ruby hashes to a CSV file.
9
+
4
10
  0.0.2
5
11
  ----
6
12
 
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
- ## Available extensions
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
@@ -1,5 +1,5 @@
1
1
  module Kiba
2
2
  module Common
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
5
5
  end
@@ -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.2
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-20 00:00:00.000000000 Z
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