Skip to content

Commit

Permalink
spike around
Browse files Browse the repository at this point in the history
  • Loading branch information
mbj committed Apr 9, 2013
1 parent 1d5a735 commit 03aaf74
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 9 deletions.
1 change: 1 addition & 0 deletions eventstorm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ Gem::Specification.new do |s|
s.add_dependency('adamantium', '0.0.7')
s.add_dependency('abstract_type', '0.0.5')
s.add_dependency('concord', '0.0.3')
s.add_dependency('anima', '0.0.6')
end
64 changes: 64 additions & 0 deletions lib/eventstorm.rb
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'
73 changes: 73 additions & 0 deletions lib/eventstorm/sink.rb
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
63 changes: 63 additions & 0 deletions lib/eventstorm/source.rb
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
44 changes: 44 additions & 0 deletions spec/integration/eventstorm/spike_spec.rb
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
9 changes: 0 additions & 9 deletions spec/shared/idempotend_method_behaviour.rb

This file was deleted.

0 comments on commit 03aaf74

Please sign in to comment.