Class: EventStoreClient::Client

Inherits:
Object
  • Object
show all
Includes:
Configuration
Defined in:
lib/event_store_client/client.rb

Constant Summary collapse

NoCallMethodOnSubscriber =
Class.new(StandardError)
WrongExpectedEventVersion =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration

#config

Instance Attribute Details

#connectionObject

rubocop:enable Metrics/CyclomaticComplexity



46
47
48
# File 'lib/event_store_client/client.rb', line 46

def connection
  @connection
end

#service_nameObject

rubocop:enable Metrics/CyclomaticComplexity



46
47
48
# File 'lib/event_store_client/client.rb', line 46

def service_name
  @service_name
end

Instance Method Details

rubocop:disable Metrics/CyclomaticComplexity

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
# File 'lib/event_store_client/client.rb', line 36

def link_to(stream:, events:, options: {})
  raise ArgumentError if !stream || stream == ''
  raise ArgumentError if events.nil? || (events.is_a?(Array) && events.empty?)
  res = connection.link_to(stream, events, options: options)
  raise WrongExpectedEventVersion.new(e.message) if res.failure?

  res.success?
end

#listen(wait: false) ⇒ Object



31
32
33
# File 'lib/event_store_client/client.rb', line 31

def listen(wait: false)
  broker.call(@subscriptions, wait: wait)
end

#publish(stream:, events:, options: {}) ⇒ Object



12
13
14
15
16
# File 'lib/event_store_client/client.rb', line 12

def publish(stream:, events:, options: {})
  connection.append_to_stream(stream, events, options: options)
rescue HTTP::Client::WrongExpectedEventVersion => e
  raise WrongExpectedEventVersion.new(e.message)
end

#read(stream, options: {}) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/event_store_client/client.rb', line 18

def read(stream, options: {})
  if options[:all]
    connection.read_all_from_stream(stream, options: options)
  else
    connection.read(stream, options: options)
  end
end

#subscribe(subscriber, to: []) ⇒ Object



26
27
28
29
# File 'lib/event_store_client/client.rb', line 26

def subscribe(subscriber, to: [])
  raise NoCallMethodOnSubscriber unless subscriber.respond_to?(:call)
  @subscriptions.create(subscriber, to)
end