Class: EventStoreClient::HTTP::Commands::Streams::Read

Inherits:
Command
  • Object
show all
Includes:
Configuration
Defined in:
lib/event_store_client/adapters/http/commands/streams/read.rb

Instance Method Summary collapse

Methods included from Configuration

#config

Methods inherited from Command

inherited

Instance Method Details

#call(stream_name, options: {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/event_store_client/adapters/http/commands/streams/read.rb', line 10

def call(stream_name, options: {})
  count = options[:count] || config.per_page
  start = options[:start].to_i
  direction = options[:direction] || 'forward'
  headers = { 'Accept' => 'application/vnd.eventstore.atom+json' }
  headers['ES-ResolveLinkTos'] = true if options.key?(:resolve_links)

  response =
    connection.call(
      :get,
      "/streams/#{stream_name}/#{start}/#{direction}/#{count}",
      headers: headers
    )

  return Failure(:stream_not_found) unless response.success? || response.status == 404
  return Failure(:connection_failed) if response.body.nil? || response.body.empty?
  skip_decryption = options[:skip_decryption] || false
  entries = JSON.parse(response.body)['entries'].map do |entry|
    deserialize_event(entry, skip_decryption: skip_decryption)
  end.reverse
  Success(entries)
rescue Faraday::ConnectionFailed
  Failure(:connection_failed)
end