streamer 0.1.0

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.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/.rubocop.yml +2 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +6 -0
  6. data/CODE_OF_CONDUCT.md +49 -0
  7. data/Dockerfile +7 -0
  8. data/Gemfile +5 -0
  9. data/Gemfile.lock +90 -0
  10. data/Guardfile +13 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +73 -0
  13. data/Rakefile +8 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/docker-compose.yml +18 -0
  17. data/examples/example1/data.csv +7 -0
  18. data/examples/example1/example.rb +22 -0
  19. data/examples/example1/sb_config.yml +25 -0
  20. data/lib/streamer/fact_providers/hash_provider.rb +17 -0
  21. data/lib/streamer/fact_providers/yaml_provider.rb +28 -0
  22. data/lib/streamer/fact_providers.rb +13 -0
  23. data/lib/streamer/finder.rb +14 -0
  24. data/lib/streamer/functors/average.rb +25 -0
  25. data/lib/streamer/functors/count.rb +10 -0
  26. data/lib/streamer/functors/divide.rb +17 -0
  27. data/lib/streamer/functors/eq.rb +10 -0
  28. data/lib/streamer/functors/functor.rb +59 -0
  29. data/lib/streamer/functors/group.rb +28 -0
  30. data/lib/streamer/functors/gt.rb +10 -0
  31. data/lib/streamer/functors/gte.rb +10 -0
  32. data/lib/streamer/functors/least.rb +17 -0
  33. data/lib/streamer/functors/lookup.rb +16 -0
  34. data/lib/streamer/functors/lt.rb +10 -0
  35. data/lib/streamer/functors/lte.rb +10 -0
  36. data/lib/streamer/functors/member.rb +17 -0
  37. data/lib/streamer/functors/multiply.rb +21 -0
  38. data/lib/streamer/functors/subtract.rb +18 -0
  39. data/lib/streamer/functors/sum.rb +16 -0
  40. data/lib/streamer/functors.rb +21 -0
  41. data/lib/streamer/stream.rb +63 -0
  42. data/lib/streamer/stream_builder.rb +36 -0
  43. data/lib/streamer/version.rb +4 -0
  44. data/lib/streamer.rb +11 -0
  45. data/script/console +4 -0
  46. data/script/init +7 -0
  47. data/script/shell +3 -0
  48. data/script/stop +3 -0
  49. data/script/test +3 -0
  50. data/streamer.gemspec +34 -0
  51. metadata +233 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 655dc75a47e90878f04948b1351522153a219642
4
+ data.tar.gz: 74dcb7d677292c677d23a23cfc223a5688e13a82
5
+ SHA512:
6
+ metadata.gz: 32be140c573eb44976662a107f1e8af0167ba470cd2f1b74eb196279fb1f5f59111c534a9c90e94e4f0d6fcbb8d67a43c00490a9845ba9ec43e046d5cce75711
7
+ data.tar.gz: a9d0874e28eea23d1e968f691423231aa0d9145d50b718c69e294e65ed785abefd2441a5745d1c7323757507966d744cc9ff879366986f34bf650bf5203f8189
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.swp
2
+ nohup.out
3
+ incentive_config.yml
4
+ coverage
5
+ *.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.0
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
5
+ env:
6
+ - CODECLIMATE_REPO_TOKEN=7c630218447a2761c71a13a92034f23bc3dc26983462f9c1534cf2744245a981
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at helm.scott+streamer<at>gmail.com.
39
+ All complaints will be reviewed and investigated and will result in a response
40
+ that is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Dockerfile ADDED
@@ -0,0 +1,7 @@
1
+ FROM ruby:latest
2
+
3
+ RUN mkdir /usr/src/app
4
+ WORKDIR /usr/src/app
5
+
6
+
7
+ COPY . /usr/src/app
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'pry'
4
+ gem 'codeclimate-test-reporter', group: :test, require: nil
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,90 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ streamer (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.2.0)
10
+ byebug (8.2.1)
11
+ codeclimate-test-reporter (0.4.8)
12
+ simplecov (>= 0.7.1, < 1.0.0)
13
+ coderay (1.1.0)
14
+ docile (1.1.5)
15
+ ffi (1.9.10)
16
+ formatador (0.2.5)
17
+ guard (2.13.0)
18
+ formatador (>= 0.2.4)
19
+ listen (>= 2.7, <= 4.0)
20
+ lumberjack (~> 1.0)
21
+ nenv (~> 0.1)
22
+ notiffany (~> 0.0)
23
+ pry (>= 0.9.12)
24
+ shellany (~> 0.0)
25
+ thor (>= 0.18.1)
26
+ guard-rake (1.0.0)
27
+ guard
28
+ rake
29
+ guard-rubocop (1.2.0)
30
+ guard (~> 2.0)
31
+ rubocop (~> 0.20)
32
+ json (1.8.3)
33
+ listen (3.0.5)
34
+ rb-fsevent (>= 0.9.3)
35
+ rb-inotify (>= 0.9)
36
+ lumberjack (1.0.10)
37
+ method_source (0.8.2)
38
+ minitest (5.8.3)
39
+ nenv (0.2.0)
40
+ notiffany (0.0.8)
41
+ nenv (~> 0.1)
42
+ shellany (~> 0.0)
43
+ parser (2.3.0.2)
44
+ ast (~> 2.2)
45
+ powerpack (0.1.1)
46
+ pry (0.10.3)
47
+ coderay (~> 1.1.0)
48
+ method_source (~> 0.8.1)
49
+ slop (~> 3.4)
50
+ rainbow (2.1.0)
51
+ rake (10.5.0)
52
+ rb-fsevent (0.9.7)
53
+ rb-inotify (0.9.5)
54
+ ffi (>= 0.5.0)
55
+ rubocop (0.36.0)
56
+ parser (>= 2.3.0.0, < 3.0)
57
+ powerpack (~> 0.1)
58
+ rainbow (>= 1.99.1, < 3.0)
59
+ ruby-progressbar (~> 1.7)
60
+ ruby-progressbar (1.7.5)
61
+ ruby_gntp (0.3.4)
62
+ shellany (0.0.1)
63
+ simplecov (0.11.2)
64
+ docile (~> 1.1.0)
65
+ json (~> 1.8)
66
+ simplecov-html (~> 0.10.0)
67
+ simplecov-html (0.10.0)
68
+ slop (3.6.0)
69
+ thor (0.19.1)
70
+
71
+ PLATFORMS
72
+ ruby
73
+
74
+ DEPENDENCIES
75
+ bundler (~> 1.11)
76
+ byebug (~> 8)
77
+ codeclimate-test-reporter
78
+ guard (~> 2)
79
+ guard-rake (~> 1)
80
+ guard-rubocop (~> 1)
81
+ minitest (~> 5.0)
82
+ pry
83
+ rake (~> 10.0)
84
+ rubocop (~> 0.36.0)
85
+ ruby_gntp (~> 0.3)
86
+ simplecov (~> 0)
87
+ streamer!
88
+
89
+ BUNDLED WITH
90
+ 1.11.2
data/Guardfile ADDED
@@ -0,0 +1,13 @@
1
+ notification :off unless ENV['GNTP_NOTIFY']
2
+ notification :gntp, host: '10.0.2.2', sticky: false if ENV['GNTP_NOTIFY']
3
+
4
+ guard :rake, task: :test do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { 'spec' }
7
+ watch('spec/spec_helper.rb') { 'spec' }
8
+ end
9
+
10
+ guard :rubocop do
11
+ watch(%r{/.+\.rb$/})
12
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Scott Helm
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.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Streamer
2
+
3
+ [![Build Status](https://travis-ci.org/scotthelm/streamer.svg?branch=master)](https://travis-ci.org/scotthelm/streamer)
4
+ [![Code Climate](https://codeclimate.com/github/scotthelm/streamer/badges/gpa.svg)](https://codeclimate.com/github/scotthelm/streamer)
5
+ [![Test Coverage](https://codeclimate.com/github/scotthelm/streamer/badges/coverage.svg)](https://codeclimate.com/github/scotthelm/streamer/coverage)
6
+
7
+ The Streamer gem provides a configurable stream that takes a hash payload, and
8
+ performs functions on the hash, returning an instance of the stream with the
9
+ resulting payload.
10
+
11
+ The StreamBuilder uses a hash as configuration to string together the calls
12
+ that each stream makes.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'streamer'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install streamer
29
+
30
+ ## Usage
31
+
32
+ This library transforms a hash. A StreamBuilder is fed a configuration hash
33
+ (usually a yaml file) that creates a chainable set of transformations using
34
+ the Stream object. The Stream has methods that are used to assign the results
35
+ of functions on the payload, back to the payload.
36
+
37
+ Please have a look at the [examples](./examples) directory for some simple
38
+ sample usage
39
+
40
+ ## Development
41
+ This gem includes a Docker environment for development. To begin development:
42
+
43
+ ```bash
44
+ $ script/init
45
+ ```
46
+
47
+ This will create the data container for gems at /usr/local/bundle used inside
48
+ the app and guard containers.
49
+
50
+ To start guard in the foreground,
51
+
52
+ ```bash
53
+ script/test
54
+ ```
55
+
56
+ To start an interactive session with the gem:
57
+
58
+ ```bash
59
+ script/console
60
+ ```
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at
65
+ https://github.com/scotthelm/streamer. This project is intended to be a safe,
66
+ welcoming space for collaboration, and contributors are expected to adhere to
67
+ the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
68
+
69
+ ## License
70
+
71
+ The gem is available as open source under the terms of the
72
+ [MIT License](http://opensource.org/licenses/MIT).
73
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'spec'
5
+ t.pattern = 'spec/**/*.rb'
6
+ end
7
+
8
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'streamer'
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
data/bin/setup ADDED
@@ -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,18 @@
1
+ app: &base
2
+ build: .
3
+ dockerfile: Dockerfile
4
+ volumes:
5
+ - ".:/usr/src/app"
6
+ environment:
7
+ - "GNTP_NOTIFY=true"
8
+ volumes_from:
9
+ - bundle_data
10
+ command: "rake test"
11
+ guard:
12
+ <<: *base
13
+ command: "nohup guard start --force-polling --no-interactions"
14
+ ports: []
15
+ bundle_data:
16
+ image: tianon/true
17
+ volumes:
18
+ - /usr/local/bundle
@@ -0,0 +1,7 @@
1
+ customer_id,customer_name,customer_address,customer_city,customer_state,customer_zip,product,units,amount
2
+ 1,Fred,123 Main St,Des Moines,IA,50301,Slinky,1,2.0
3
+ 2,Barney,123 Elm St,Kansas City,KS,64101,YoYo,2,7.0
4
+ 3,Wilma,123 Pine St,Des Moines,IA,50301,Monopoly,1,7.99
5
+ 4,Betty,123 Elm St,KansasCity,KS,64101,Pop Gun,1,4
6
+ 5,Pebbles,1 Park Place,Atlantic City,NJ,11090,YoYo,1,3.5
7
+ 6,Bam Bam,64 Boardwalk Blvd,Atlantic City,NJ,11090,Slinky,1,1
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bundler/setup'
3
+ require 'streamer'
4
+ require 'csv'
5
+ require 'pry'
6
+
7
+ sales_numbers = {
8
+ 'date' => '2016-02-02',
9
+ 'sales' => []
10
+ }
11
+
12
+ data_file_path = File.join(__dir__, 'data.csv')
13
+ CSV.foreach(data_file_path, headers: true) do |row|
14
+ sales_numbers['sales'] << row.to_hash
15
+ end
16
+
17
+ config_file_path = File.join(__dir__, 'sb_config.yml')
18
+ sb = Streamer::StreamBuilder.new(YAML.load_file(config_file_path))
19
+ sb.process(sales_numbers)
20
+ sb.payload['summary']['sales_by_product'].each do |k, v|
21
+ printf "%-20s %d\n", k, v
22
+ end
@@ -0,0 +1,25 @@
1
+ :filter:
2
+ - :function:
3
+ :type: member
4
+ :properties:
5
+ - sales.customer_state
6
+ :facts:
7
+ - IA
8
+ - KS
9
+ :transform:
10
+ - :assign_each:
11
+ :list: sales
12
+ :property: amount_per_unit
13
+ :function:
14
+ :type: divide
15
+ :terms:
16
+ - '#amount'
17
+ - '#units'
18
+ - :assign:
19
+ :property: summary.sales_by_product
20
+ :function:
21
+ :type: group
22
+ :list: sales
23
+ :by: product
24
+ :operator: +
25
+ :operand: amount
@@ -0,0 +1,17 @@
1
+ module Streamer
2
+ module FactProviders
3
+ # HashProvider implements the Finder Provider interface
4
+ class HashProvider
5
+ attr_reader :data
6
+ def initialize(data = nil)
7
+ @data = data || {}
8
+ end
9
+
10
+ def find(key)
11
+ string_keys = key.split('.')
12
+ sym_keys = key.split('.').map(&:to_sym)
13
+ data.dig(*string_keys) || data.dig(*sym_keys)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ require 'yaml'
2
+ module Streamer
3
+ module FactProviders
4
+ # YamlProvider provides data from a yaml file
5
+ class YamlProvider
6
+ extend Forwardable
7
+ attr_reader :data
8
+ delegate find: :provider
9
+
10
+ def initialize(path: nil, yaml: nil)
11
+ load_file(path) if path
12
+ load_yaml(yaml) if yaml
13
+ end
14
+
15
+ def load_file(path)
16
+ @data = YAML.load(File.read(path))
17
+ end
18
+
19
+ def load_yaml(yaml)
20
+ @data = YAML.load(yaml)
21
+ end
22
+
23
+ def provider
24
+ @provider ||= HashProvider.new(data)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ require 'streamer/fact_providers/yaml_provider'
2
+ require 'streamer/fact_providers/hash_provider'
3
+
4
+ module Streamer
5
+ # FactProviders are used by finders to find facts
6
+ module FactProviders
7
+ # Interface for FactProviders : for use by other libs
8
+ module Interface
9
+ def find(key)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module Streamer
2
+ # Finder maintains a consistent interface for all of the fact_providers
3
+ # It allows the Stream to have a consistent finding pattern.
4
+ class Finder
5
+ extend Forwardable
6
+ attr_reader :fact_provider
7
+ delegate find: :fact_provider
8
+
9
+ def initialize(provider = nil)
10
+ return @fact_provider = provider if provider
11
+ @fact_provider = Streamer::FactProviders::HashProvider.new
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ module Streamer
2
+ module Functors
3
+ # Average gives the average of a list given a property.
4
+ class Average < Functor
5
+ def call
6
+ Divide.new(
7
+ payload,
8
+ terms: [total, count]
9
+ ).call
10
+ end
11
+
12
+ def total
13
+ Sum.new(
14
+ payload,
15
+ list: options.fetch(:list),
16
+ property: options.fetch(:property)
17
+ ).call
18
+ end
19
+
20
+ def count
21
+ Count.new(payload, list: options.fetch(:list)).call
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ module Streamer
2
+ module Functors
3
+ # Count provides the number of items in a list
4
+ class Count < Functor
5
+ def call
6
+ payload[options.fetch(:list)].size
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ module Streamer
2
+ module Functors
3
+ # Divide divides two terms
4
+ class Divide < Functor
5
+ def call
6
+ divide
7
+ end
8
+
9
+ def divide
10
+ terms = numerify(options.fetch(:terms))
11
+ fail 'Streamer::Functor# divide: too many terms' if terms.size > 2
12
+ return 0.0 if terms.any? { |t| t.to_f == 0.0 }
13
+ terms[0].to_f / terms[1].to_f
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ module Streamer
2
+ module Functors
3
+ # Eq compares target to function or value
4
+ class Eq < Functor
5
+ def call
6
+ compare :==
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,59 @@
1
+ module Streamer
2
+ module Functors
3
+ # Functor is responsible for creating the functions that the stream uses.
4
+ class Functor
5
+ attr_reader :payload, :options
6
+ def initialize(payload, options)
7
+ @payload = payload
8
+ @options = options
9
+ end
10
+
11
+ def call
12
+ Module.const_get(class_name).new(payload, options).call
13
+ end
14
+
15
+ def class_name
16
+ "Streamer::Functors::#{options.fetch(:type).capitalize}"
17
+ end
18
+
19
+ private
20
+
21
+ def compare(op_symbol)
22
+ tar = target(options.fetch(:target))
23
+ function = options[:function]
24
+ value = options[:value]
25
+ return functor(function).call.send(op_symbol, tar) if function
26
+ return value.send(op_symbol, tar) unless value.nil?
27
+ fail 'Streamer::Functor#gte no value or fuction given'
28
+ end
29
+
30
+ def numerify(terms)
31
+ terms.map do |t|
32
+ val = prop(t) if t.is_a? String
33
+ val = t if t.is_a? Numeric
34
+ val
35
+ end
36
+ end
37
+
38
+ def prop(p)
39
+ payload.dig(*p.split('.'))
40
+ end
41
+
42
+ def functor(function_hash)
43
+ Functor.new(payload, function_hash)
44
+ end
45
+
46
+ def target(options)
47
+ return options[:value] if options[:value]
48
+ return functor(options[:function]).call if options[:function]
49
+ end
50
+
51
+ def value(pk, data = payload)
52
+ return data if pk.size == 0
53
+ data = data[pk.shift]
54
+ return data.map { |x| value(pk, x) } if data.is_a? Array
55
+ value(pk, data)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,28 @@
1
+ module Streamer
2
+ module Functors
3
+ # Group groups a list by a property and a function
4
+ class Group < Functor
5
+ def call
6
+ group
7
+ end
8
+
9
+ def group
10
+ list = options.fetch(:list)
11
+ group_key = options.fetch(:by)
12
+ operand_key = options.fetch(:operand)
13
+ operator = options.fetch(:operator).to_sym
14
+ accumulate(list, group_key, operand_key, operator)
15
+ end
16
+
17
+ def accumulate(list, group_key, operand_key, operator)
18
+ payload[list].each_with_object({}) do |item, val|
19
+ val[item[group_key]] =
20
+ (val[item[group_key]] || 0.0).send(
21
+ operator,
22
+ (item[operand_key].to_f || 0)
23
+ )
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,10 @@
1
+ module Streamer
2
+ module Functors
3
+ # Gt compares target to function or value
4
+ class Gt < Functor
5
+ def call
6
+ compare :>
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Streamer
2
+ module Functors
3
+ # Gte compares a target to a value or fuction
4
+ class Gte < Functor
5
+ def call
6
+ compare(:>=)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ module Streamer
2
+ module Functors
3
+ # least determines the least item in a list
4
+ class Least < Functor
5
+ def call
6
+ least
7
+ end
8
+
9
+ def least
10
+ vals = value(options.fetch(:list).split('.')).map do |item|
11
+ item[options.fetch(:property)]
12
+ end
13
+ vals.sort.shift
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module Streamer
2
+ module Functors
3
+ # Lookup looks up a fact given a finder
4
+ class Lookup < Functor
5
+ def call
6
+ finder = options.fetch(:finder)
7
+ finder.find(lookup_key)
8
+ end
9
+
10
+ def lookup_key
11
+ item_key = options.fetch(:terms)[0]
12
+ options[:fact] = options.fetch(:fact).gsub('#', "#{item_key}.")
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ module Streamer
2
+ module Functors
3
+ # Lt compares target to function or value
4
+ class Lt < Functor
5
+ def call
6
+ compare :<
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Streamer
2
+ module Functors
3
+ # Lte compares target to function or value
4
+ class Lte < Functor
5
+ def call
6
+ compare :<=
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ module Streamer
2
+ module Functors
3
+ # Member determines if the properties provided are members of the facts
4
+ class Member < Functor
5
+ def call
6
+ member
7
+ end
8
+
9
+ def member
10
+ values = options.fetch(:properties).map do |pk|
11
+ value(pk.split('.'))
12
+ end.flatten
13
+ (values & options.fetch(:facts)).size > 0
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ module Streamer
2
+ module Functors
3
+ # Multiply multiplies a set of terms together
4
+ class Multiply < Functor
5
+ def call
6
+ multiply
7
+ end
8
+
9
+ def multiply
10
+ options.fetch(:terms).inject(1) do |total, item|
11
+ unless item.is_a?(String) || item.is_a?(Numeric)
12
+ fail "Streamer::Functor invalid term: #{item}"
13
+ end
14
+ val = total * (prop(item) || 0) if item.is_a? String
15
+ val = total * item if item.is_a? Numeric
16
+ val
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ module Streamer
2
+ module Functors
3
+ # Subtract subtracts a set of terms
4
+ class Subtract < Functor
5
+ def call
6
+ subtract
7
+ end
8
+
9
+ def subtract
10
+ terms = options.fetch(:terms)
11
+ initial_prop = terms.shift
12
+ terms.inject(prop(initial_prop).to_f) do |total, item|
13
+ total - prop(item).to_f
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module Streamer
2
+ module Functors
3
+ # Sum sums the list using the property provided
4
+ class Sum < Functor
5
+ def call
6
+ sum
7
+ end
8
+
9
+ def sum
10
+ payload[options.fetch(:list)].inject(0.0) do |total, item|
11
+ total + item[options.fetch(:property)]
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ require 'streamer/functors/functor'
2
+ require 'streamer/functors/average'
3
+ require 'streamer/functors/count'
4
+ require 'streamer/functors/divide'
5
+ require 'streamer/functors/eq'
6
+ require 'streamer/functors/group'
7
+ require 'streamer/functors/gt'
8
+ require 'streamer/functors/gte'
9
+ require 'streamer/functors/least'
10
+ require 'streamer/functors/lookup'
11
+ require 'streamer/functors/lt'
12
+ require 'streamer/functors/lte'
13
+ require 'streamer/functors/member'
14
+ require 'streamer/functors/multiply'
15
+ require 'streamer/functors/subtract'
16
+ require 'streamer/functors/sum'
17
+ module Streamer
18
+ # Functors supply the ability to perform functions on a payload
19
+ module Functors
20
+ end
21
+ end
@@ -0,0 +1,63 @@
1
+ # Streamer is the module that is responsible for pushing data through the
2
+ # pipe
3
+ module Streamer
4
+ # Stream is the object that is responsible for mutating the data passed to it
5
+ class Stream
6
+ attr_reader :payload
7
+ attr_accessor :finder
8
+ def initialize(hash)
9
+ @payload = hash
10
+ end
11
+
12
+ def filter(function:)
13
+ assign(property: 'filter_value', value: functor(function).call)
14
+ end
15
+
16
+ def assign(property:, value: nil, function: nil)
17
+ assign_property(
18
+ structure: payload,
19
+ properties: property.to_s.split('.'),
20
+ value: value,
21
+ function: function
22
+ )
23
+ self
24
+ end
25
+
26
+ def assign_property(structure:, properties:, value: nil, function: nil)
27
+ properties.each_with_index do |prop, index|
28
+ if index == properties.size - 1
29
+ structure[prop] = value unless value.nil?
30
+ structure[prop] = functor(function).call if function
31
+ else
32
+ structure[prop] = {} unless structure[prop]
33
+ structure = structure[prop]
34
+ end
35
+ end
36
+ end
37
+
38
+ def assign_each(list:, property:, value: nil, function: nil)
39
+ payload.dig(*list.split('.')).each do |item|
40
+ item[property] = value unless value.nil?
41
+ item[property] = functor(replace_terms(item, function)).call if function
42
+ end
43
+ self
44
+ end
45
+
46
+ def replace_terms(item, function_hash)
47
+ newfunc = Marshal.load(Marshal.dump(function_hash))
48
+ newfunc[:terms] = newfunc[:terms].map do |t|
49
+ if t.is_a?(String) && t.start_with?('#')
50
+ t[0] = '' # remove the '#'
51
+ item.dig(*t.split('.'))
52
+ else
53
+ t
54
+ end
55
+ end
56
+ newfunc
57
+ end
58
+
59
+ def functor(options = {}, pl = payload)
60
+ Streamer::Functors::Functor.new(pl, options)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,36 @@
1
+ module Streamer
2
+ # StreamBuilder takes a stream configuration and builds the stream defined
3
+ # in it. StreamBuilder then performs the stream, given a document.
4
+ class StreamBuilder
5
+ attr_reader :config, :payload
6
+ def initialize(config)
7
+ @config = config
8
+ end
9
+
10
+ def process(payload)
11
+ @payload = payload
12
+ stream.payload['filter_value'] = filter_value
13
+ transform if stream.payload['filter_value']
14
+ stream
15
+ end
16
+
17
+ def transform
18
+ config[:transform].each do |tx|
19
+ tx.each do |k, v|
20
+ stream.send(k, v)
21
+ end
22
+ end
23
+ end
24
+
25
+ def filter_value
26
+ config[:filter].inject(true) do |val, f|
27
+ val &&= stream.filter(function: f[:function]).payload['filter_value']
28
+ val
29
+ end
30
+ end
31
+
32
+ def stream
33
+ @stream ||= Stream.new(payload)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Streamer
3
+ VERSION = '0.1.0'.freeze
4
+ end
data/lib/streamer.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'streamer/finder'
2
+ require 'streamer/functors'
3
+ require 'streamer/fact_providers'
4
+ require 'streamer/stream'
5
+ require 'streamer/stream_builder'
6
+ require 'streamer/version'
7
+
8
+ # Streamer is used to stream data through a configurable set of functions.
9
+ # The Stream is configurable with a StreamBuilder
10
+ module Streamer
11
+ end
data/script/console ADDED
@@ -0,0 +1,4 @@
1
+ #!/bin/bash
2
+
3
+ echo "starting development console"
4
+ docker-compose run --rm app ./bin/console
data/script/init ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+
3
+ docker-compose run --rm guard gem install bundler
4
+ docker-compose run --rm app gem install bundler
5
+ docker-compose run --rm guard bundle install
6
+ docker-compose run --rm app bundle install
7
+
data/script/shell ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env sh
2
+
3
+ docker-compose run --rm app bash
data/script/stop ADDED
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+
3
+ docker-compose stop
data/script/test ADDED
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+
3
+ docker-compose run --rm guard guard
data/streamer.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'streamer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'streamer'
8
+ spec.version = Streamer::VERSION
9
+ spec.authors = ['Scott Helm']
10
+ spec.email = ['helm.scott@gmail.com']
11
+
12
+ spec.summary = 'streams a hash through a set of configurable functions'
13
+ spec.description = 'a hash goes in, the transformed hash comes out'
14
+ spec.homepage = 'https://github.com/scotthelm/streamer'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.11'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'minitest', '~> 5.0'
27
+ spec.add_development_dependency 'byebug', '~> 8'
28
+ spec.add_development_dependency 'guard', '~> 2'
29
+ spec.add_development_dependency 'rubocop', '~> 0.36.0'
30
+ spec.add_development_dependency 'ruby_gntp', '~> 0.3'
31
+ spec.add_development_dependency 'guard-rubocop', '~> 1'
32
+ spec.add_development_dependency 'guard-rake', '~> 1'
33
+ spec.add_development_dependency 'simplecov', '~> 0'
34
+ end
metadata ADDED
@@ -0,0 +1,233 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: streamer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Scott Helm
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.36.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.36.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: ruby_gntp
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.3'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.3'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1'
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard-rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: a hash goes in, the transformed hash comes out
154
+ email:
155
+ - helm.scott@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rubocop.yml"
162
+ - ".ruby-version"
163
+ - ".travis.yml"
164
+ - CODE_OF_CONDUCT.md
165
+ - Dockerfile
166
+ - Gemfile
167
+ - Gemfile.lock
168
+ - Guardfile
169
+ - LICENSE.txt
170
+ - README.md
171
+ - Rakefile
172
+ - bin/console
173
+ - bin/setup
174
+ - docker-compose.yml
175
+ - examples/example1/data.csv
176
+ - examples/example1/example.rb
177
+ - examples/example1/sb_config.yml
178
+ - lib/streamer.rb
179
+ - lib/streamer/fact_providers.rb
180
+ - lib/streamer/fact_providers/hash_provider.rb
181
+ - lib/streamer/fact_providers/yaml_provider.rb
182
+ - lib/streamer/finder.rb
183
+ - lib/streamer/functors.rb
184
+ - lib/streamer/functors/average.rb
185
+ - lib/streamer/functors/count.rb
186
+ - lib/streamer/functors/divide.rb
187
+ - lib/streamer/functors/eq.rb
188
+ - lib/streamer/functors/functor.rb
189
+ - lib/streamer/functors/group.rb
190
+ - lib/streamer/functors/gt.rb
191
+ - lib/streamer/functors/gte.rb
192
+ - lib/streamer/functors/least.rb
193
+ - lib/streamer/functors/lookup.rb
194
+ - lib/streamer/functors/lt.rb
195
+ - lib/streamer/functors/lte.rb
196
+ - lib/streamer/functors/member.rb
197
+ - lib/streamer/functors/multiply.rb
198
+ - lib/streamer/functors/subtract.rb
199
+ - lib/streamer/functors/sum.rb
200
+ - lib/streamer/stream.rb
201
+ - lib/streamer/stream_builder.rb
202
+ - lib/streamer/version.rb
203
+ - script/console
204
+ - script/init
205
+ - script/shell
206
+ - script/stop
207
+ - script/test
208
+ - streamer.gemspec
209
+ homepage: https://github.com/scotthelm/streamer
210
+ licenses:
211
+ - MIT
212
+ metadata: {}
213
+ post_install_message:
214
+ rdoc_options: []
215
+ require_paths:
216
+ - lib
217
+ required_ruby_version: !ruby/object:Gem::Requirement
218
+ requirements:
219
+ - - ">="
220
+ - !ruby/object:Gem::Version
221
+ version: '0'
222
+ required_rubygems_version: !ruby/object:Gem::Requirement
223
+ requirements:
224
+ - - ">="
225
+ - !ruby/object:Gem::Version
226
+ version: '0'
227
+ requirements: []
228
+ rubyforge_project:
229
+ rubygems_version: 2.5.1
230
+ signing_key:
231
+ specification_version: 4
232
+ summary: streams a hash through a set of configurable functions
233
+ test_files: []