cloaca 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0a8b896aa3c3f9979d1bba828c593722ca01af67
4
+ data.tar.gz: 36d5878f8736269e34927ca45fbab582398b9086
5
+ SHA512:
6
+ metadata.gz: 4b97a537a00d78b872673044ea85b7dbdc34d8958cccee577a537a73074600d625aba7e96f7fdecd3fc0b5b445d409247efd1be21f387e0cc14402f467324fbc
7
+ data.tar.gz: 32d8e03adc10631bbfaade2cabcfa0d76031f3af646176e9aa7b4355cce436086c5638176f620799990ab75d27749ceac0073fda75995685d8aeb4693118a441
@@ -0,0 +1,10 @@
1
+ .DS_Store
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at matthew.chadwick@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cloaca.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Matthew Chadwick
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ # Cloaca
2
+
3
+ Cloaca provides a library of transformations for CSV files and other row structured data.
4
+
5
+ It does this by providing command-line operations for manipulating a data stream. Operations can be concatenated to perform more complex file transformations.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'cloaca'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install cloaca
23
+
24
+
25
+ ## Usage
26
+
27
+ ```bash
28
+ exe/cloaca
29
+
30
+ Cloaca commands:
31
+ cloaca add-fixed-value-column --col-value=column cell value # adds a fixed value column to a stream of data
32
+ cloaca add-integer-index-column # adds a index column to a stream of data
33
+ cloaca add-numeric-index-column # adds a index column to a stream of data
34
+ cloaca change-col-delimiter --new-col-delim=column delimiter # change the column delimiter for a stream of data
35
+ cloaca check-headers-unique # checks that each column has a unique header value
36
+ cloaca extract-unique-col-values --index-or-value=column index or header # extract unique vlaues for a column, one per line
37
+ cloaca generate-int N # generates N random integers, one per line
38
+ cloaca help [COMMAND] # Describe available commands or one specific command
39
+ cloaca remove-col-quotes --index-or-value=column index or header # removes quotes from a column's values
40
+ cloaca remove-column (index or value) --index-or-value=column index or header # removes the column
41
+ cloaca remove-header-row # removes the first N rows (default N = 1)
42
+ ```
43
+
44
+
45
+ ## What's in a Name?
46
+
47
+ cloaca [kloh-ey-kuh]
48
+
49
+ Definitions:
50
+ 1. maw of voracious person
51
+ 2. privy (medieval)
52
+ 3. sewer, underground drain
53
+
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mchadwick/cloaca. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
65
+
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cloaca"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cloaca/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cloaca"
8
+ spec.version = Cloaca::VERSION
9
+ spec.authors = ["Matthew Chadwick"]
10
+ spec.email = ["matthew.chadwick@gmail.com"]
11
+
12
+ spec.summary = %q{Cloaca providers a library of transformations for CSV files and other row structured data.}
13
+ spec.homepage = "https://github.com/mchadwick/cloaca"
14
+ spec.license = "MIT"
15
+
16
+ spec.add_dependency "thor", "~> 0.19"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Exit cleanly from an early interrupt
4
+ Signal.trap("INT") { exit 1 }
5
+
6
+ # Flush output immediately
7
+ $stdout.sync = true
8
+
9
+ require "bundler/setup"
10
+ require "cloaca"
11
+
12
+ Cloaca::CLI.start(ARGV, debug: true)
@@ -0,0 +1,16 @@
1
+ require "bigdecimal"
2
+ require "bigdecimal/util"
3
+ require "thor"
4
+
5
+ require "cloaca/version"
6
+ require "cloaca/cli"
7
+ require "cloaca/util/column_lookup"
8
+ require "cloaca/operations/add_fixed_value_column"
9
+ require "cloaca/operations/add_numeric_index_column"
10
+ require "cloaca/operations/change_column_delimiter"
11
+ require "cloaca/operations/check_row_values_unique"
12
+ require "cloaca/operations/extract_unique_column_values"
13
+ require "cloaca/operations/generate_random_integers"
14
+ require "cloaca/operations/remove_column"
15
+ require "cloaca/operations/remove_column_quotes"
16
+ require "cloaca/operations/remove_sequential_rows"
@@ -0,0 +1,148 @@
1
+ module Cloaca
2
+ class CLI < Thor
3
+
4
+ package_name "Cloaca"
5
+
6
+ desc "add-integer-index-column", "adds a index column to a stream of data"
7
+ method_option :"col-delim", type: :string, default: "|", banner: "column delimiter"
8
+ method_option :"index-header", type: :string, banner: "index header value or omit if no header row"
9
+ method_option :"index-delta", type: :numeric, default: 1, banner: "delta between consecutive index values"
10
+ method_option :"index-seed", type: :numeric, default: 0, banner: "initial index value"
11
+
12
+ def add_integer_index_column
13
+ assert_integer(:"index-delta")
14
+ assert_integer(:"index-seed")
15
+ Operations::AddNumericIndexColumn.new(parse_options).run!
16
+ end
17
+
18
+ desc "add-numeric-index-column", "adds a index column to a stream of data"
19
+ method_option :"col-delim", type: :string, default: "|", banner: "column delimiter"
20
+ method_option :"index-header", type: :string, banner: "index header value or omit if no header row"
21
+ method_option :"index-delta", type: :numeric, default: 1, banner: "delta between consecutive index values"
22
+ method_option :"index-seed", type: :numeric, default: 0, banner: "initial index value"
23
+
24
+ def add_numeric_index_column
25
+ Operations::AddNumericIndexColumn.new(parse_options).run!
26
+ end
27
+
28
+ desc "add-fixed-value-column", "adds a fixed value column to a stream of data"
29
+ method_option :"col-delim", type: :string, default: "|", banner: "column delimiter"
30
+ method_option :"col-header", type: :string, banner: "header value or omit if no header row"
31
+ method_option :"col-value", type: :string, required: true, banner: "column cell value"
32
+
33
+ def add_fixed_value_column
34
+ Operations::AddFixedValueColumn.new(parse_options).run!
35
+ end
36
+
37
+ desc "change-col-delimiter", "change the column delimiter for a stream of data"
38
+ method_option :"old-col-delim", type: :string, default: "|", banner: "column delimiter"
39
+ method_option :"new-col-delim", type: :string, required: true, banner: "column delimiter"
40
+
41
+ def change_col_delimiter
42
+ Operations::ChangeColumnDelimiter.new(parse_options).run!
43
+ end
44
+
45
+
46
+ desc "check-headers-unique", "checks that each column has a unique header value"
47
+ method_option :"col-delim", type: :string, default: "|", banner: "column delimiter"
48
+ method_option :"case-sensitive", type: :boolean, default: false, banner: "perform case sensitive check"
49
+
50
+ def check_headers_unique
51
+ Operations::CheckRowValuesUnique.new(parse_options).run!
52
+ end
53
+
54
+ desc "extract-unique-col-values", "extract unique vlaues for a column, one per line"
55
+ method_option :"col-delim", type: :string, default: "|", banner: "column delimiter"
56
+ method_option :"index-or-value", type: :string, required: true, banner: "column index or header"
57
+ method_option :"row-offset", type: :numeric, default: 0, banner: "initial offset (use 1 to skip a single row header)"
58
+
59
+ def extract_unique_col_values
60
+ assert_integer(:"row-offset")
61
+
62
+ Operations::ExtractUniqueColumnValues.new(parse_options).run!
63
+ end
64
+
65
+ desc "generate-int N", "generates N random integers, one per line"
66
+ method_option :count, type: :numeric, default: 1
67
+ method_option :min, type: :numeric, default: 0
68
+ method_option :max, type: :numeric, default: 100000
69
+ method_option :delay_in_ms, type: :numeric, default: 1000, banner: "N milliseconds"
70
+
71
+ def generate_int
72
+ assert_integer(:min)
73
+ assert_integer(:max)
74
+ assert_less_than(:min, :max)
75
+
76
+ Operations::GenerateRandomIntegers.new(parse_options).run!
77
+ end
78
+
79
+ desc "remove-column (index or value)", "removes the column"
80
+ method_option :"col-delim", type: :string, default: "|", banner: "column delimiter"
81
+ method_option :"index-or-value", type: :string, required: true, banner: "column index or header"
82
+
83
+ def remove_column
84
+ Operations::RemoveColumn.new(parse_options).run!
85
+ end
86
+
87
+ desc "remove-col-quotes", "removes quotes from a column's values"
88
+ method_option :"col-delim", type: :string, default: "|", banner: "column delimiter"
89
+ method_option :"index-or-value", type: :string, required: true, banner: "column index or header"
90
+
91
+ def remove_col_quotes
92
+ Operations::RemoveColumnQuotes.new(parse_options).run!
93
+ end
94
+
95
+ desc "remove-header-row", "removes the first N rows (default N = 1)"
96
+ method_option :count, type: :numeric, default: 1
97
+
98
+ def remove_header_row
99
+ assert_greater_than_zero(:count)
100
+ Operations::RemoveSequentialRows.new(parse_options).run!
101
+ end
102
+
103
+ private
104
+
105
+ def assert_integer(key)
106
+ if options[key] != options[key].to_i
107
+ raise(Thor::MalformattedArgumentError.new("Expected integer value for '--#{key}'; got \"#{options[key]}\""))
108
+ end
109
+ end
110
+
111
+ def assert_greater_than_zero(key)
112
+ if options[key] < 0
113
+ raise(Thor::MalformattedArgumentError.new("Expected #{jet} to be greater than 0 ; got \"#{options[key]}\" which is not > 0"))
114
+ end
115
+ end
116
+
117
+ def assert_less_than(a, b)
118
+ if options[a] > options[b]
119
+ raise(Thor::MalformattedArgumentError.new("Expected #{a} to be less than or equal to #{b} ; got \"#{options[a]}\" which is not <= \"#{options[b]}\""))
120
+ end
121
+ end
122
+
123
+ def parse_options
124
+ {
125
+ case_sensitive: options[:"case-sensitive"],
126
+ column_delimiter: options[:"col-delim"],
127
+ column_header: options[:"col-header"],
128
+ column_value: options[:"col-value"],
129
+ count: options[:count],
130
+ delay_in_ms: options[:delay_in_ms],
131
+ ending_row_offset: options[:"ending-row-offset"] || options[:count],
132
+ index_delta: options[:"index-delta"],
133
+ index_header: options[:"index-header"],
134
+ index_or_value: options[:"index-or-value"],
135
+ index_seed: options[:"index-seed"],
136
+ input: $stdin,
137
+ max: options[:max],
138
+ min: options[:min],
139
+ new_column_delimiter: options[:"new-col-delim"],
140
+ old_column_delimiter: options[:"old-col-delim"],
141
+ row_offset: options[:"row-offset"],
142
+ starting_row_offset: options[:"starting-row-offset"] || 0,
143
+ output: $stdout,
144
+ }
145
+ end
146
+
147
+ end
148
+ end
@@ -0,0 +1,28 @@
1
+ module Cloaca::Operations
2
+ class AddFixedValueColumn
3
+
4
+ def initialize(
5
+ column_delimiter:,
6
+ column_header:,
7
+ column_value:,
8
+ input:,
9
+ output:,
10
+ **
11
+ )
12
+ @column_delimiter = column_delimiter
13
+ @column_header = column_header
14
+ @column_value = column_value
15
+ @input = input
16
+ @output = output
17
+ end
18
+
19
+ def run!
20
+ @input.each_with_index do |line, index|
21
+ @output << (index == 0 && @column_header ? @column_header : @column_value)
22
+ @output << @column_delimiter
23
+ @output << line
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,52 @@
1
+ module Cloaca::Operations
2
+ class AddNumericIndexColumn
3
+
4
+ def initialize(
5
+ column_delimiter:,
6
+ index_delta:,
7
+ index_header:,
8
+ index_seed:,
9
+ input:,
10
+ output:,
11
+ **
12
+ )
13
+ @column_delimiter = column_delimiter
14
+ @index_delta = index_delta
15
+ @index_header = index_header
16
+ @index_seed = index_header ? index_seed - index_delta : index_seed
17
+ @input = input
18
+ @output = output
19
+ end
20
+
21
+ def run!
22
+ use_big_decimal_math? ? run_with_big_decimals : run_with_integers
23
+ end
24
+
25
+ private
26
+
27
+ def run_with_big_decimals
28
+ cumulative_delta = BigDecimal.new(@index_seed)
29
+ @input.each_with_index do |line, index|
30
+ @output << (index == 0 && @index_header ? @index_header : cumulative_delta.to_digits)
31
+ @output << @column_delimiter
32
+ @output << line
33
+ cumulative_delta = cumulative_delta + @index_delta
34
+ end
35
+ end
36
+
37
+ def run_with_integers
38
+ cumulative_delta = @index_seed.to_i
39
+ @input.each_with_index do |line, index|
40
+ @output << (index == 0 && @index_header ? @index_header : cumulative_delta.to_i)
41
+ @output << @column_delimiter
42
+ @output << line
43
+ cumulative_delta = cumulative_delta + @index_delta
44
+ end
45
+ end
46
+
47
+ def use_big_decimal_math?
48
+ (@index_seed != @index_seed.to_i) || (@index_delta != @index_delta.to_i)
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,25 @@
1
+ module Cloaca::Operations
2
+ class ChangeColumnDelimiter
3
+
4
+ def initialize(
5
+ input:,
6
+ new_column_delimiter:,
7
+ old_column_delimiter:,
8
+ output:,
9
+ **
10
+ )
11
+ @old_column_delimiter = old_column_delimiter
12
+ @new_column_delimiter = new_column_delimiter
13
+ @col_sep_changed = (old_column_delimiter != new_column_delimiter)
14
+ @input = input
15
+ @output = output
16
+ end
17
+
18
+ def run!
19
+ @input.each_with_index do |line, index|
20
+ @output << (@col_sep_changed ? line.split(@old_column_delimiter).join(@new_column_delimiter) : line)
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,39 @@
1
+ module Cloaca::Operations
2
+ class CheckRowValuesUnique
3
+
4
+ def initialize(
5
+ case_sensitive: false,
6
+ column_delimiter:,
7
+ index: 0,
8
+ input:,
9
+ output:,
10
+ **
11
+ )
12
+ @case_sensitive = case_sensitive
13
+ @column_delimiter = column_delimiter
14
+ @index = index
15
+ @input = input
16
+ @output = output
17
+ end
18
+
19
+ def run!
20
+ duplicates = []
21
+ @input.each_with_index do |line, index|
22
+ if @index == index
23
+ values = line.strip.split(@column_delimiter)
24
+ if !@case_sensitive
25
+ values = values.group_by { |x| x.downcase.to_s }
26
+ else
27
+ values = values.group_by(&:to_s)
28
+ end
29
+ duplicates = values.select { |k,v| v.count > 1 }.map { |k, v| v.first }.sort
30
+ if duplicates.any?
31
+ raise("duplicate value#{'s' if duplicates.count > 1} found: #{duplicates.join(', ')}")
32
+ end
33
+ end
34
+ @output << line
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,44 @@
1
+ module Cloaca::Operations
2
+ class ExtractUniqueColumnValues
3
+ include Cloaca::Util::ColumnLookup
4
+
5
+ def initialize(
6
+ column_delimiter:,
7
+ index_or_value:,
8
+ input:,
9
+ output:,
10
+ row_offset: 0,
11
+ **
12
+ )
13
+ @index_or_value = index_or_value
14
+ @row_offset = row_offset
15
+ @column_delimiter = column_delimiter
16
+ @input = input
17
+ @output = output
18
+ end
19
+
20
+ def run!
21
+ unique_values = Set.new
22
+
23
+ @input.each_with_index do |line, index|
24
+ if index == 0
25
+ @column_index = extract_index(line, @column_delimiter, @index_or_value)
26
+ end
27
+
28
+ if @column_index
29
+ if index < @row_offset
30
+ next
31
+ end
32
+
33
+ value = line.split(@column_delimiter)[@column_index]
34
+
35
+ if !unique_values.include?(value)
36
+ unique_values.add(value)
37
+ @output.puts(value)
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ module Cloaca::Operations
2
+ class GenerateRandomIntegers
3
+
4
+ def initialize(
5
+ count:,
6
+ delay_in_ms:,
7
+ max:,
8
+ min:,
9
+ output:,
10
+ **
11
+ )
12
+ @count = count.to_i
13
+ @delay_in_seconds = delay_in_ms / 1000.0
14
+ @max = max
15
+ @min = min
16
+ @output = output
17
+ @range = max - min + 1
18
+ end
19
+
20
+
21
+ def run!
22
+ last = @count - 1
23
+ @count.times do |n|
24
+ @output.puts @min + rand(@range)
25
+ sleep(@delay_in_seconds) unless n == last || @delay_in_seconds <= 0
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,43 @@
1
+ module Cloaca::Operations
2
+ class RemoveColumn
3
+ include Cloaca::Util::ColumnLookup
4
+
5
+ def initialize(
6
+ column_delimiter:,
7
+ index_or_value:,
8
+ input:,
9
+ output:,
10
+ **
11
+ )
12
+ @index_or_value = index_or_value
13
+ @column_delimiter = column_delimiter
14
+ @input = input
15
+ @output = output
16
+ end
17
+
18
+ def run!
19
+ @input.each_with_index do |line, index|
20
+ if index == 0
21
+ @column_index = extract_index(line, @column_delimiter, @index_or_value)
22
+ end
23
+
24
+ if @column_index
25
+ # TODO: need to spec other operations with multi-line input and ensure output is consistent
26
+ # close proceeding line; last line does not have a newline
27
+ # if index > 0
28
+ # @output << "\n"
29
+ # end
30
+
31
+ new_line = line.strip.split(@column_delimiter)
32
+ new_line.delete_at(@column_index)
33
+ @output << new_line.join(@column_delimiter)
34
+ @output << "\n"
35
+ else
36
+ # Only catches absence of named header, numeric values for non-existing columns will always populate @column_index
37
+ @output << line
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,35 @@
1
+ module Cloaca::Operations
2
+ class RemoveColumnQuotes
3
+ include Cloaca::Util::ColumnLookup
4
+
5
+ def initialize(
6
+ column_delimiter:,
7
+ index_or_value:,
8
+ input:,
9
+ output:,
10
+ **
11
+ )
12
+ @index_or_value = index_or_value
13
+ @column_delimiter = column_delimiter
14
+ @input = input
15
+ @output = output
16
+ end
17
+
18
+ def run!
19
+ @input.each_with_index do |line, index|
20
+ if index == 0
21
+ @column_index = extract_index(line, @column_delimiter, @index_or_value)
22
+ end
23
+
24
+ if @column_index
25
+ new_line = line.split(@column_delimiter)
26
+ new_line[@column_index] = new_line[@column_index].gsub(/^\"(.*)\"$/, "\\1")
27
+ @output << new_line.join(@column_delimiter)
28
+ else
29
+ @output << line
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ module Cloaca::Operations
2
+ class RemoveSequentialRows
3
+
4
+ def initialize(
5
+ ending_row_offset:,
6
+ input:,
7
+ output:,
8
+ starting_row_offset:,
9
+ **
10
+ )
11
+ @starting_row_offset = starting_row_offset
12
+ @ending_row_offset = ending_row_offset
13
+ @input = input
14
+ @output = output
15
+ end
16
+
17
+ def run!
18
+ @input.each_with_index do |line, index|
19
+ if index < @starting_row_offset || index >= @ending_row_offset
20
+ @output << line
21
+ end
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ module Cloaca::Util
2
+ module ColumnLookup
3
+
4
+ def extract_index(line, column_delimiter, index_or_value)
5
+ header = line.strip.split(column_delimiter)
6
+
7
+ # Preference given to header name instead of position
8
+ column_index = header.index(index_or_value)
9
+
10
+ # Fallback to position
11
+ if !column_index && index_or_value.to_s.match(/\d+/)
12
+ column_index = index_or_value.to_i
13
+ end
14
+
15
+ column_index
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Cloaca
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloaca
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Chadwick
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.19'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.19'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - matthew.chadwick@gmail.com
72
+ executables:
73
+ - cloaca
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - cloaca.gemspec
88
+ - exe/cloaca
89
+ - lib/cloaca.rb
90
+ - lib/cloaca/cli.rb
91
+ - lib/cloaca/operations/add_fixed_value_column.rb
92
+ - lib/cloaca/operations/add_numeric_index_column.rb
93
+ - lib/cloaca/operations/change_column_delimiter.rb
94
+ - lib/cloaca/operations/check_row_values_unique.rb
95
+ - lib/cloaca/operations/extract_unique_column_values.rb
96
+ - lib/cloaca/operations/generate_random_integers.rb
97
+ - lib/cloaca/operations/remove_column.rb
98
+ - lib/cloaca/operations/remove_column_quotes.rb
99
+ - lib/cloaca/operations/remove_sequential_rows.rb
100
+ - lib/cloaca/util/column_lookup.rb
101
+ - lib/cloaca/version.rb
102
+ homepage: https://github.com/mchadwick/cloaca
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.8
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Cloaca providers a library of transformations for CSV files and other row
126
+ structured data.
127
+ test_files: []