snoopka 0.0.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 +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Guardfile +24 -0
- data/LICENSE.txt +22 -0
- data/README.md +50 -0
- data/Rakefile +1 -0
- data/lib/snoopka.rb +6 -0
- data/lib/snoopka/listener.rb +57 -0
- data/lib/snoopka/version.rb +3 -0
- data/snoopka.gemspec +27 -0
- data/spec/listener_spec.rb +49 -0
- data/spec/spec_helper.rb +17 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1d04723c28cbeb3fc2142fa4a6081176a47a2cb0
|
4
|
+
data.tar.gz: 8ddf2763e1b17453f55b8b71bf8536a47c34de91
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2df85e50958ab105b29cff338bf17c0ecf89b71f73fc4cda35e861ce3665032f40b5a09e95b37ae668a765e15bc25a5d3c07699fc52f84e965d12e25e7dd5f23
|
7
|
+
data.tar.gz: 41b85d2a3457885fa94ef7d77ec2a5afde499d37f8092aaad00b7cc96f4ae633a88bc5578821524d6d2b37ba9484642933914221949aed411d80016aca49e320
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :rspec do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
11
|
+
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
12
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
13
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
14
|
+
watch('config/routes.rb') { "spec/routing" }
|
15
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
16
|
+
|
17
|
+
# Capybara features specs
|
18
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
19
|
+
|
20
|
+
# Turnip features and steps
|
21
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
22
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
23
|
+
end
|
24
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 tristan
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Snoopka
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'snoopka'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install snoopka
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Sample code:
|
22
|
+
|
23
|
+
require 'snoopka'
|
24
|
+
require 'logger'
|
25
|
+
|
26
|
+
# pass the custom behavior as a block
|
27
|
+
namespace :kafka do
|
28
|
+
desc 'Starts the kafka listener'
|
29
|
+
task :listen, [:daemonized] => :environment do |t, args|
|
30
|
+
Process.daemon(true, true) if args.daemonized
|
31
|
+
puts 'Starting the Kafka listener'
|
32
|
+
|
33
|
+
listener = Snoopka::Listener.new host: "localhost", port: 9092
|
34
|
+
|
35
|
+
handler = Handler.new
|
36
|
+
listener.add_observer 'test', &handler
|
37
|
+
|
38
|
+
loop do
|
39
|
+
listener.consume
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
1. Fork it
|
47
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
48
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
49
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
50
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/snoopka.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'kafka'
|
2
|
+
|
3
|
+
module Snoopka
|
4
|
+
class Listener
|
5
|
+
|
6
|
+
def initialize(settings = {})
|
7
|
+
@settings = settings
|
8
|
+
@observers = {}
|
9
|
+
@consumers = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def settings
|
13
|
+
@settings
|
14
|
+
end
|
15
|
+
|
16
|
+
def observers
|
17
|
+
@observers
|
18
|
+
end
|
19
|
+
|
20
|
+
def observer_count
|
21
|
+
@observers.count
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_observer(topic = '', &proc)
|
25
|
+
@observers[topic] ||= []
|
26
|
+
@observers[topic] << proc
|
27
|
+
@consumers << create_consumer(topic)
|
28
|
+
end
|
29
|
+
|
30
|
+
# loop through all observers of this topic and call the associated blocks
|
31
|
+
def notify_observers(topic, messages)
|
32
|
+
@observers[topic].each do |observer|
|
33
|
+
observer.call messages
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# loop through all consumers to read from kafka
|
38
|
+
def consume
|
39
|
+
@consumers.each do |consumer|
|
40
|
+
messages = consumer.consume
|
41
|
+
notify_observers(consumer.topic, messages) if messages.length > 0
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# create a kafka consumer for the topic
|
46
|
+
def create_consumer(topic)
|
47
|
+
Kafka::Consumer.new(
|
48
|
+
{
|
49
|
+
host: @settings[:host] || 'localhost',
|
50
|
+
port: @settings[:port] || 9092,
|
51
|
+
topic: topic
|
52
|
+
}
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
data/snoopka.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'snoopka/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "snoopka"
|
8
|
+
spec.version = Snoopka::VERSION
|
9
|
+
spec.authors = ["tristan"]
|
10
|
+
spec.email = ["tristan.gomez@gmail.com"]
|
11
|
+
spec.description = %q{Listens to a Kafka server and executes provided block}
|
12
|
+
spec.summary = %q{Listens to a Kafka server and executes provided block}
|
13
|
+
spec.homepage = "https://github.com/parasquid/snoopka"
|
14
|
+
spec.license = "LGPLv3"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "guard-rspec"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
|
26
|
+
spec.add_runtime_dependency "kafka-rb"
|
27
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'snoopka/listener'
|
3
|
+
|
4
|
+
describe Snoopka::Listener do
|
5
|
+
let(:settings_hash) { {host: "127.0.0.1", port: 9092} }
|
6
|
+
let(:listener) { Snoopka::Listener.new settings_hash }
|
7
|
+
|
8
|
+
it 'retains the hash of settings' do
|
9
|
+
expect(listener.settings).to eq settings_hash
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'exposes the list of observers' do
|
13
|
+
expect(listener.observers).to_not eq nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'adds an observer to the listener' do
|
17
|
+
listener.add_observer 'awesomecommerce' do |message|
|
18
|
+
puts message.inspect
|
19
|
+
end
|
20
|
+
expect(listener.observer_count).to eq 1
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'consumer' do
|
24
|
+
|
25
|
+
let(:mocked_socket) { double(TCPSocket) }
|
26
|
+
before :each do
|
27
|
+
TCPSocket.stub(:new).and_return(mocked_socket) # don't use a real socket
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'creates a consumer given a topic and settings' do
|
31
|
+
consumer = listener.create_consumer('awesomecommerce')
|
32
|
+
expect(consumer).to_not be_nil
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'peforms the block when a message is received in the subscribed topic' do
|
36
|
+
bytes = [0].pack("n") + [1].pack('N') + [21346].pack('q').reverse # it's magic!!
|
37
|
+
allow(mocked_socket).to receive(:read).and_return(bytes)
|
38
|
+
allow(mocked_socket).to receive(:write)
|
39
|
+
|
40
|
+
@test = 0
|
41
|
+
listener.add_observer 'awesomecommerce' do |message|
|
42
|
+
@test += 1
|
43
|
+
end
|
44
|
+
|
45
|
+
listener.consume
|
46
|
+
expect(@test).to eq 1
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: snoopka
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tristan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-03 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: guard-rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: kafka-rb
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Listens to a Kafka server and executes provided block
|
84
|
+
email:
|
85
|
+
- tristan.gomez@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- Gemfile
|
93
|
+
- Guardfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/snoopka.rb
|
98
|
+
- lib/snoopka/listener.rb
|
99
|
+
- lib/snoopka/version.rb
|
100
|
+
- snoopka.gemspec
|
101
|
+
- spec/listener_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
homepage: https://github.com/parasquid/snoopka
|
104
|
+
licenses:
|
105
|
+
- LGPLv3
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.1.11
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Listens to a Kafka server and executes provided block
|
127
|
+
test_files:
|
128
|
+
- spec/listener_spec.rb
|
129
|
+
- spec/spec_helper.rb
|