Class: EventSource::EventStore::HTTP::Put

Inherits:
Object
  • Object
show all
Includes:
Log::Dependency
Defined in:
lib/event_source/event_store/http/put.rb

Defined Under Namespace

Modules: Assertions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(session: nil) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/event_source/event_store/http/put.rb', line 11

def self.build(session: nil)
  session ||= Session.build

  instance = new
  ::EventStore::HTTP::Write.configure instance, session: session
  instance
end

.call(write_event, stream_name, expected_version: nil, session: nil) ⇒ Object



19
20
21
22
# File 'lib/event_source/event_store/http/put.rb', line 19

def self.call(write_event, stream_name, expected_version: nil, session: nil)
  instance = build session: session
  instance.(write_event, stream_name, expected_version: expected_version)
end

Instance Method Details

#call(write_events, stream_name, expected_version: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/event_source/event_store/http/put.rb', line 24

def call(write_events, stream_name, expected_version: nil)
  write_events = Array(write_events)

  expected_version = ExpectedVersion.canonize expected_version

  logger.trace { "Putting event data (StreamName: #{stream_name}, BatchSize: #{write_events.count}, Types: #{write_events.map(&:type).inspect}, ExpectedVersion: #{expected_version.inspect})" }

  write_events.each do |write_event|
    write_event. = nil if write_event.&.empty?
  end

  begin
    location = write.(
      write_events,
      stream_name,
      expected_version: expected_version
    )
  rescue ::EventStore::HTTP::Write::ExpectedVersionError => error
    raise ExpectedVersion::Error, error.message
  end

  *, position = location.path.split '/'

  logger.debug { "Put event data done (StreamName: #{stream_name}, BatchSize: #{write_events.count}, Types: #{write_events.map(&:type).inspect}, Position: #{position}, ExpectedVersion: #{expected_version.inspect})" }

  position.to_i
end