arkency / http_event_store

HttpEventStore is a HTTP connector to the Greg's Event Store.
http://httpeventstore.arkency.com
MIT License
42 stars 11 forks source link

Stream name #1

Closed litch closed 9 years ago

litch commented 9 years ago

When using categories of events, it is necessary to have the stream name of the event so that the proper action can be taken.

client = HttpEventstore::Connection.new

stream_name = 'accounts-001'
event_data = { event_type: "AccountCreated",
               data: { phoneNumber: '15125551234' }}
client.append_to_stream(stream_name, event_data)

EventData = Struct.new(:data, :event_type)

event_data = EventData.new({amount: 1000}, "DepositCreated")
client.append_to_stream(stream_name, event_data)

all_accounts_stream_name = '$ce-accounts'
events = client.read_all_events_forward(all_accounts_stream_name)

@balances = {}

events.each do |event|
  if event.type == "AccountCreated"
    @balances[event.stream_name] = {amount: 0, position: event.id}
  elsif event.type == "DepositCreated"
    prev_balance = @balances[event.stream_name][:amount]
    @balances[event.stream_name] = {amount: prev_balance + event.data['amount'], position: event.id}
  end
end

# ?> @balances
# => {"accounts-001"=>{:amount=>1000, :position=>5}}