Class: EventSource::EventStore::HTTP::Get::Last

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

Constant Summary collapse

Assertions =
Get::Assertions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(session: nil) ⇒ Object



13
14
15
16
17
# File 'lib/event_source/event_store/http/get/last.rb', line 13

def self.build(session: nil)
  instance = new
  instance.configure session: session
  instance
end

.call(stream_name, **build_arguments) ⇒ Object



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

def self.call(stream_name, **build_arguments)
  instance = build **build_arguments
  instance.(stream_name)
end

Instance Method Details

#call(stream_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/event_source/event_store/http/get/last.rb', line 36

def call(stream_name)
  logger.trace { "Getting last event of stream (StreamName: #{stream_name})" }

  begin
    event, * = read_stream.(
      stream_name,
      position: :head,
      direction: :backward,
      batch_size: 1
    )
  rescue ::EventStore::HTTP::ReadStream::StreamNotFoundError
  end

  logger.debug { "Got last event of stream (StreamName: #{stream_name}, EventType: #{event&.type.inspect}, Position: #{event&.position.inspect}, GlobalPosition: #{event&.global_position.inspect})" }

  event
end

#configure(session: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/event_source/event_store/http/get/last.rb', line 24

def configure(session: nil)
  session = Session.configure self, session: session

  read_stream = ::EventStore::HTTP::ReadStream.configure(
    self,
    session: session
  )

  read_stream.embed_body
  read_stream.output_schema = Result
end