-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
245 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,68 @@ | ||
'abstract_type' | ||
require 'adamantium' | ||
require 'composition' | ||
require 'elasticsearch' | ||
require 'anima' | ||
|
||
# Root namespace | ||
module Eventstorm | ||
|
||
# The event that storms around ;) | ||
class Event | ||
include Anima.new(:id, :time, :type, :payload) | ||
|
||
# Build object a pretty lame way | ||
# | ||
# Will be refactored away later. | ||
# | ||
# @param [String] type | ||
# @param [Hash] payload | ||
# | ||
# @return [Event] | ||
# | ||
# @api private | ||
# | ||
def self.build(type, payload) | ||
new( | ||
:id => payload.object_id, | ||
:type => type, | ||
:time => Time.now, | ||
:payload => payload | ||
) | ||
end | ||
|
||
# Return hash representation | ||
# | ||
# Will be refactored away later. | ||
# | ||
# @return [Hash] | ||
# | ||
# @api private | ||
# | ||
def to_hash | ||
self.class.attributes_hash(self) | ||
end | ||
|
||
end | ||
|
||
# Connect source to sink | ||
# | ||
# Will be refactored away later. | ||
# | ||
# @param [Source] source | ||
# @param [Sink] sink | ||
# | ||
# @return [self] | ||
# | ||
# @api private | ||
# | ||
def self.connect(source, sink) | ||
source.each do |event| | ||
sink.push(event) | ||
end | ||
self | ||
end | ||
end | ||
|
||
require 'eventstorm/sink' | ||
require 'eventstorm/source' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
module Eventstorm | ||
|
||
# Abstract base class for event sinks | ||
class Sink | ||
include Adamantium::Flat, AbstractType | ||
|
||
# Push event to sink | ||
# | ||
# @param [Event] event | ||
# | ||
# @return [self] | ||
# | ||
# @api private | ||
# | ||
abstract_method :push | ||
|
||
# Memory sink | ||
class Memory < self | ||
|
||
# Return sunken events | ||
# | ||
# @return [Enumerable<Event>] | ||
# | ||
# @api private | ||
# | ||
attr_reader :events | ||
|
||
# Initialize object | ||
# | ||
# @return [undefined] | ||
# | ||
# @api private | ||
# | ||
def initialize | ||
@events = [] | ||
end | ||
|
||
# Push event to sink | ||
# | ||
# @param [Event] event | ||
# | ||
# @return [self] | ||
# | ||
# @api private | ||
# | ||
def push(event) | ||
events << event | ||
self | ||
end | ||
|
||
end | ||
|
||
# Elasticsearch index sink | ||
class Elasticsearch < self | ||
include Composition.new(:index) | ||
|
||
# Push event to sink | ||
# | ||
# @param [Event] event | ||
# | ||
# @return [self] | ||
# | ||
# @api private | ||
# | ||
def push(event) | ||
index.type(event.type).document(event.id).index(event.to_hash) | ||
self | ||
end | ||
|
||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module Eventstorm | ||
# Abstract base class for event sources | ||
class Source | ||
include Adamantium::Flat, AbstractType | ||
|
||
# Enumerate events | ||
# | ||
# @return [self] | ||
# if block given | ||
# | ||
# @return [Enumerator<Event>] | ||
# otherwise | ||
# | ||
# @api private | ||
# | ||
abstract_method :each | ||
|
||
class Elasticsearch < self | ||
include Composition.new(:index) | ||
|
||
# Enumerate events | ||
# | ||
# @return [self] | ||
# if block given | ||
# | ||
# @return [Enumerator<Event>] | ||
# | ||
# @api private | ||
# | ||
def each(&block) | ||
return to_enum unless block_given? | ||
result = index.read({:match_all => {}}) | ||
result.hits.map do |foo| | ||
raise | ||
p foo | ||
end | ||
end | ||
|
||
end | ||
|
||
# Event source with predefined events | ||
class Static < self | ||
include Composition.new(:events) | ||
|
||
# Enumerate events | ||
# | ||
# @return [self] | ||
# if block given | ||
# | ||
# @return [Enumerator<Event>] | ||
# otherwise | ||
# | ||
# @api private | ||
# | ||
def each(&block) | ||
return to_enum unless block_given? | ||
events.each(&block) | ||
self | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'spec_helper' | ||
require 'logger' | ||
|
||
describe Eventstorm do | ||
let(:static_source) do | ||
Eventstorm::Source::Static.new(events) | ||
end | ||
|
||
let(:events) do | ||
[ | ||
Eventstorm::Event.build('foo', 'bar' => 'baz') | ||
] | ||
end | ||
|
||
let(:es_cluster) do | ||
Elasticsearch::Cluster.connect('http://helium:9200') | ||
end | ||
|
||
let(:es_index) do | ||
es_cluster.index('eventstorm-test') | ||
end | ||
|
||
let(:es_sink) do | ||
Eventstorm::Sink::Elasticsearch.new(es_index) | ||
end | ||
|
||
let(:inmemory_sink) do | ||
Eventstorm::Sink::Memory.new | ||
end | ||
|
||
let(:es_source) do | ||
Eventstorm::Source::Elasticsearch.new(es_index) | ||
end | ||
|
||
it 'elasticsearch should consume events' do | ||
es_index.delete if es_index.exist? | ||
Eventstorm.connect(static_source, es_sink) | ||
end | ||
|
||
it 'should read events' do | ||
Eventstorm.connect(es_source, inmemory_sink) | ||
inmemory_sink.events.should eql(events) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.