Class: HttpEventStore::Actions::AppendEventToStream

Inherits:
Object
  • Object
show all
Defined in:
lib/http_event_store/actions/append_event_to_stream.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ AppendEventToStream

Returns a new instance of AppendEventToStream.


7
8
9
# File 'lib/http_event_store/actions/append_event_to_stream.rb', line 7

def initialize(client)
  @client = client
end

Instance Method Details

#call(stream_name, event_data, expected_version = nil) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/http_event_store/actions/append_event_to_stream.rb', line 11

def call(stream_name, event_data, expected_version = nil)
  events = [event_data].flatten.map do |event_data|
    event_data = OpenStruct.new(event_data) if event_data.is_a?(Hash)
    event = create_event(event_data)
    raise IncorrectStreamData if event.validate || stream_name_incorrect?(stream_name)
    event
  end

  create_event_in_es(stream_name, events, expected_version)
rescue ClientError => e
  raise WrongExpectedEventNumber if e.code == 400
  raise StreamAlreadyDeleted if e.code == 410
end