zq 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 89a087d942cb796f6d5e764aee46c3ed62d0e18d
4
+ data.tar.gz: 6379dcb496fd572e22496e347f640298e56346d5
5
+ SHA512:
6
+ metadata.gz: 48e38b7d61ebf57dcfaad46fa5dd0fe817f00e7b721bcc07cc1ccc993b39c16acbc38002088f51b5d848294cd604fbc9e52493eccb9d7276c62cdcd5ae00b45e
7
+ data.tar.gz: e9bcf62c2ffa295a7e324bee410a68c32cea43fd1e28444596982b358af739ff24330789cb873fa3387c09270b0b99a251bc60796ea2f0d834c58ad7c341605f
data/bin/zq ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+ LIB_PATH = Pathname.new(__FILE__).realpath.dirname.parent.join('lib').to_s
4
+ $LOAD_PATH.unshift(LIB_PATH)
5
+
6
+ require 'zq/cli'
7
+
8
+ begin
9
+ ZQ::CLI.start(ARGV)
10
+ rescue NoOrchestrasFound
11
+ abort 'No Orchestras found'
12
+ end
data/lib/zq/cli.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'thor'
2
+ require 'zq/orchestra'
3
+ require 'zq/orchestras/echo'
4
+ require 'zq/exceptions'
5
+
6
+ module ZQ
7
+ class CLI < Thor
8
+ class_option :file, aliases: ['-r'], type: :string, desc: "Require file to load orchestras from"
9
+
10
+ desc 'list', 'List available orchestras.'
11
+ def list
12
+ setup_env(options)
13
+ orchestras = ZQ.live_orchestras
14
+ fail NoOrchestrasFound if orchestras.empty?
15
+ orchestras.each do |o|
16
+ puts o
17
+ end
18
+ end
19
+
20
+ desc 'play ORCHESTRA_NAME', 'Start orchestrating.'
21
+ option :forever, aliases: ['-d'], type: :boolean, default: false, desc: "Keep running even if source is exhausted"
22
+ option :interval, aliases: ['-i'], type: :numeric, default: 0, desc: "Play orchestra every N seconds"
23
+ def play(orchestra_name)
24
+ setup_env(options)
25
+ orchestra = ZQ.find_live_orchestra(orchestra_name)
26
+ fail OrchestraDoesNotExist unless orchestra
27
+ run(orchestra, options[:forever], options[:interval])
28
+ end
29
+
30
+ private
31
+
32
+ def setup_env(options)
33
+ return unless options[:file]
34
+ cwd = Pathname.pwd
35
+ require cwd + options[:file]
36
+ end
37
+
38
+ def run(orchestra_cls, forever, interval)
39
+ orchestra = orchestra_cls.new
40
+ if forever
41
+ orchestra.process_forever(interval)
42
+ elsif interval
43
+ orchestra.process_with_interval(interval)
44
+ else
45
+ orchestra.process_until_exhausted
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ module ZQ
2
+ module Composer
3
+
4
+ class NoOp
5
+ def compose(raw_data, composite = nil)
6
+ raw_data
7
+ end
8
+ end
9
+
10
+ class Echo
11
+ def initialize(ioo = nil)
12
+ @file = ioo || $stdout
13
+ end
14
+ def compose(raw_data, composite = nil)
15
+ composite ||= raw_data
16
+ @file.puts composite
17
+ composite
18
+ end
19
+ end
20
+ class Tee < Echo
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ module ZQ
2
+ module Composer
3
+ class RedisPublish
4
+ def initialize(channel_name, client)
5
+ @channel_name = channel_name
6
+ @client = client
7
+ end
8
+ def compose(raw_data, composite = nil)
9
+ composite ||= raw_data
10
+ @client.publish(@channel_name, composite)
11
+ composite
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module ZQ
2
+ module Composer
3
+ class JsonParse
4
+ def compose(raw_data, composite = nil)
5
+ composite ||= raw_data
6
+ JSON.parse(composite)
7
+ end
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,16 @@
1
+ module ZQ
2
+ module Composer
3
+ class UUID4Hash
4
+ def initialize(key = nil)
5
+ @key = key || 'uuid'
6
+ end
7
+ def compose(raw_data, composite = nil)
8
+ composite ||= raw_data
9
+ if composite.kind_of? Hash
10
+ composite[@key] = SecureRandom.uuid
11
+ end
12
+ composite
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ class NoOrchestrasFound < RuntimeError; end
2
+ class OrchestraDoesNotExist < RuntimeError; end
3
+ class NoSourceProvided < RuntimeError; end
4
+ class NoComposerProvided < RuntimeError; end
@@ -0,0 +1,113 @@
1
+ module ZQ
2
+ @@live_orchestras = []
3
+ @@_all_known_orchestras = []
4
+ @@autoregister = true
5
+
6
+ def self.create_orchestra(&block)
7
+ klass = Class.new
8
+ klass.class_exec do
9
+ include ZQ::Orchestra
10
+ end
11
+ klass.class_exec(&block) if block_given?
12
+ klass
13
+ end
14
+
15
+ def self.reset!
16
+ @@live_orchestras = []
17
+ end
18
+
19
+ def self.autoregister_orchestra(orc)
20
+ register_orchestra(orc) if autoregister_orchestra?
21
+ @@_all_known_orchestras = @@_all_known_orchestras.push(orc)
22
+ end
23
+
24
+ def self.register_orchestra(orc)
25
+ @@live_orchestras = @@live_orchestras.push orc
26
+ end
27
+
28
+ def self.deregister_orchestra(orc)
29
+ @@live_orchestras.reject! { |o| o == orc }
30
+ end
31
+
32
+ def self.live_orchestras
33
+ @@live_orchestras
34
+ end
35
+
36
+ def self.find_live_orchestra(orc_name)
37
+ live_orchestras.detect { |o| o.name == orc_name }
38
+ end
39
+
40
+ def self.stop_autoregister_orchestra!
41
+ @@autoregister = false
42
+ end
43
+
44
+ def self.autoregister_orchestra!
45
+ @@autoregister = true
46
+ end
47
+
48
+ def self.autoregister_orchestra?
49
+ @@autoregister
50
+ end
51
+
52
+ module Orchestra
53
+ def self.included(base)
54
+ ::ZQ.autoregister_orchestra(base)
55
+ base.extend ClassMethods
56
+ end
57
+
58
+ module ClassMethods
59
+ def desc(desc)
60
+ @desc = desc
61
+ end
62
+
63
+ def source(source)
64
+ @source = source
65
+ end
66
+
67
+ def compose_with(*composers)
68
+ composers.each do |c|
69
+ add_composer c
70
+ end
71
+ end
72
+
73
+ def add_composer(composer)
74
+ @composers ||= []
75
+ @composers = @composers.push composer
76
+ end
77
+
78
+ def to_s
79
+ super + ' - ' + @desc
80
+ end
81
+ end
82
+
83
+ def initialize
84
+ @source, @composers = [:@source, :@composers].map do |m|
85
+ self.class.instance_variable_get(m)
86
+ end
87
+ fail NoSourceProvided unless @source
88
+ fail NoComposerProvided unless @composers
89
+ end
90
+
91
+ def process_forever(interval = 1)
92
+ loop do
93
+ process_with_interval(interval)
94
+ end
95
+ end
96
+
97
+ def process_with_interval(interval)
98
+ process_until_exhausted
99
+ Kernel.sleep(interval)
100
+ end
101
+
102
+ def process_until_exhausted
103
+ loop do
104
+ item = @source.read_next
105
+ break if item.nil?
106
+ composite = nil
107
+ @composers.each do |c|
108
+ composite = c.compose item, composite
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,11 @@
1
+ require 'zq/orchestra'
2
+ require 'zq/sources/io'
3
+ require 'zq/composers/echo'
4
+
5
+ class Echo
6
+ include ZQ::Orchestra
7
+ source ZQ::Sources::IOSource.new $stdin
8
+ compose_with ZQ::Composer::Echo.new
9
+ desc 'prints contents from stdin'
10
+ end
11
+
@@ -0,0 +1,16 @@
1
+ module ZQ
2
+ module Sources
3
+ class IOSource
4
+ def initialize file
5
+ @file = file
6
+ end
7
+
8
+ def read_next
9
+ begin
10
+ @file.readline[0..-2]
11
+ rescue EOFError
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module ZQ
2
+ module Sources
3
+ class RedisLPOP
4
+ def initialize(client, listname)
5
+ @client = client
6
+ @listname = listname
7
+ end
8
+
9
+ def read_next
10
+ @client.lpop @listname
11
+ end
12
+ end
13
+ end
14
+ end
data/lib/zq/utils.rb ADDED
@@ -0,0 +1,92 @@
1
+ class Tty
2
+ class << self
3
+ def blue
4
+ bold 34
5
+ end
6
+
7
+ def white
8
+ bold 39
9
+ end
10
+
11
+ def red
12
+ underline 31
13
+ end
14
+
15
+ def yellow
16
+ underline 33
17
+ end
18
+
19
+ def reset
20
+ escape 0
21
+ end
22
+
23
+ def em
24
+ underline 39
25
+ end
26
+
27
+ def green
28
+ color 92
29
+ end
30
+
31
+ def gray
32
+ bold 30
33
+ end
34
+
35
+ def width
36
+ `/usr/bin/tput cols`.strip.to_i
37
+ end
38
+
39
+ def truncate(str)
40
+ str.to_s[0, width - 4]
41
+ end
42
+
43
+ private
44
+
45
+ def color(n)
46
+ escape "0;#{n}"
47
+ end
48
+
49
+ def bold(n)
50
+ escape "1;#{n}"
51
+ end
52
+
53
+ def underline(n)
54
+ escape "4;#{n}"
55
+ end
56
+
57
+ def escape(n)
58
+ "\033[#{n}m" if $stdout.tty?
59
+ end
60
+ end
61
+ end
62
+
63
+ def ohai(title, *sput)
64
+ title = Tty.truncate(title) if $stdout.tty? && !ARGV.verbose?
65
+ puts "#{Tty.blue}==>#{Tty.white} #{title}#{Tty.reset}"
66
+ puts sput unless sput.empty?
67
+ end
68
+
69
+ def oh1(title)
70
+ title = Tty.truncate(title) if $stdout.tty? && !ARGV.verbose?
71
+ puts "#{Tty.green}==>#{Tty.white} #{title}#{Tty.reset}"
72
+ end
73
+
74
+ def opoo(warning)
75
+ STDERR.puts "#{Tty.red}Warning#{Tty.reset}: #{warning}"
76
+ end
77
+
78
+ def onoe(error)
79
+ lines = error.to_s.split("\n")
80
+ STDERR.puts "#{Tty.red}Error#{Tty.reset}: #{lines.shift}"
81
+ STDERR.puts lines unless lines.empty?
82
+ end
83
+
84
+ def ofail(error)
85
+ onoe error
86
+ Lace.failed = true
87
+ end
88
+
89
+ def odie(error)
90
+ onoe error
91
+ exit 1
92
+ end
data/lib/zq/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module ZQ
2
+ VERSION = '0.1.1'
3
+ end
data/lib/zq.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'zq/orchestra'
2
+ require 'zq/orchestras/echo'
3
+ require 'zq/composers/uuid4'
4
+ require 'zq/composers/struct'
5
+ require 'zq/composers/redis'
6
+ require 'zq/cli'
7
+ require 'zq/sources/redis_lpop'
8
+
9
+ module ZQ
10
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Kai Richard Koenig
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-25 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email: kai@kairichardkoenig.de
29
+ executables:
30
+ - zq
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/zq/cli.rb
35
+ - lib/zq/composers/echo.rb
36
+ - lib/zq/composers/redis.rb
37
+ - lib/zq/composers/struct.rb
38
+ - lib/zq/composers/uuid4.rb
39
+ - lib/zq/exceptions.rb
40
+ - lib/zq/orchestra.rb
41
+ - lib/zq/orchestras/echo.rb
42
+ - lib/zq/sources/io.rb
43
+ - lib/zq/sources/redis_lpop.rb
44
+ - lib/zq/utils.rb
45
+ - lib/zq/version.rb
46
+ - lib/zq.rb
47
+ - bin/zq
48
+ homepage: https://github.com/kairichard/zimtw
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: 1.8.6
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.0.14
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Pull stuff from source then digest and create inside a repository
72
+ test_files: []