event_store_client 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bee380d04b9833045afa980313c8e734f90501ba8301568855e358ed5df45590
4
- data.tar.gz: 0ce74c041e3a27406316bc7962580e4df024c3585a0390cdf87ac90693ffd0aa
3
+ metadata.gz: '08af7988b22788f181f313f057f739a3c78f538cf53758eb075391551e4d9baf'
4
+ data.tar.gz: ca38daf119afa7676e910dc4ca2cad2ab8fd16d6a675ed6fde48335d2db670bf
5
5
  SHA512:
6
- metadata.gz: 0dcd1e350f99cb88fd26cbd03ae9f5011176cdd70867a2d6afdcb054c6aa8d96e7edb9ddaca128f7884695b5dbffcdda655a5ea5d03ff97d6706af6a2ec5eabb
7
- data.tar.gz: fb9a02e5171e777d7bb635008538a000bb2148c8cf4cc9fef0c614222961ed02f1348c913a803fc54c309097de0e135dd7784ebc45fbdd9a58ba9071b05681a5
6
+ metadata.gz: 21dcbc1dafdc7362cd54373ff93f1343397aa432d90ab935f286fd49b921421b88728f5d41178f8075a465cc948fc7ecbf15b42a7489e6565e31467c367bbaff
7
+ data.tar.gz: '098272251a70ae10f095f9f118fdf2ba197cfe12e586b3ee6d31e96501ceea7032fe25964f087861b7938edd6c6204ceb057135713ef9c89377d59651bb1fc9b'
@@ -5,9 +5,12 @@ require 'dry-struct'
5
5
  module EventStoreClient
6
6
  class Client
7
7
  NoCallMethodOnSubscriber = Class.new(StandardError)
8
+ WrongExpectedEventVersion = Class.new(StandardError)
8
9
 
9
10
  def publish(stream:, events:, expected_version: nil)
10
11
  connection.publish(stream: stream, events: events, expected_version: expected_version)
12
+ rescue StoreAdapter::Api::Client::WrongExpectedEventVersion => e
13
+ raise WrongExpectedEventVersion.new(e.message)
11
14
  end
12
15
 
13
16
  def read(stream, direction: 'forward', start: 0, all: false)
@@ -61,12 +64,15 @@ module EventStoreClient
61
64
  nil
62
65
  end
63
66
 
64
- def link_to(stream:, events:)
67
+ # rubocop:disable Metrics/CyclomaticComplexity
68
+ def link_to(stream:, events:, expected_version: nil)
65
69
  raise ArgumentError if !stream || stream == ''
66
70
  raise ArgumentError if events.nil? || (events.is_a?(Array) && events.empty?)
67
-
68
- connection.link_to(stream, events)
71
+ connection.link_to(stream, events, expected_version: expected_version)
72
+ rescue StoreAdapter::Api::Client::WrongExpectedEventVersion => e
73
+ raise WrongExpectedEventVersion.new(e.message)
69
74
  end
75
+ # rubocop:enable Metrics/CyclomaticComplexity
70
76
 
71
77
  attr_accessor :connection, :service_name
72
78
 
@@ -43,8 +43,8 @@ module EventStoreClient
43
43
  events
44
44
  end
45
45
 
46
- def link_to(stream, events)
47
- client.link_to(stream, events)
46
+ def link_to(stream, events, expected_version: nil)
47
+ client.link_to(stream, events, expected_version: expected_version)
48
48
 
49
49
  true
50
50
  end
@@ -6,8 +6,7 @@ require 'json'
6
6
 
7
7
  module EventStoreClient
8
8
  class Event < Dry::Struct
9
- attr_reader :id
10
-
9
+ attribute :id, Types::Strict::String.optional.default(nil)
11
10
  attribute :type, Types::Strict::String
12
11
  attribute :title, Types::Strict::String.optional.default(nil)
13
12
  attribute :data, Types::Strict::String.default('{}')
@@ -16,7 +15,7 @@ module EventStoreClient
16
15
  private
17
16
 
18
17
  def initialize(**args)
19
- @id = SecureRandom.uuid
18
+ id = args[:id] || SecureRandom.uuid
20
19
  hash_meta = JSON.parse(args[:metadata] || '{}')
21
20
  hash_meta['created_at'] ||= Time.now
22
21
  args[:metadata] = JSON.generate(hash_meta)
@@ -4,14 +4,17 @@ module EventStoreClient
4
4
  module StoreAdapter
5
5
  module Api
6
6
  class Client
7
+ WrongExpectedEventVersion = Class.new(StandardError)
8
+
7
9
  def append_to_stream(stream_name, events, expected_version: nil)
8
10
  headers = {
9
- 'ES-ExpectedVersion' => expected_version.to_s
10
- }.reject { |_key, val| val.empty? }
11
+ 'ES-ExpectedVersion' => expected_version&.to_s
12
+ }.reject { |_key, val| val.nil? || val.empty? }
11
13
 
12
14
  data = build_events_data(events)
13
-
14
- make_request(:post, "/streams/#{stream_name}", body: data, headers: headers)
15
+ response = make_request(:post, "/streams/#{stream_name}", body: data, headers: headers)
16
+ validate_response(response, expected_version)
17
+ response
15
18
  end
16
19
 
17
20
  def delete_stream(stream_name, hard_delete)
@@ -69,14 +72,20 @@ module EventStoreClient
69
72
  )
70
73
  end
71
74
 
72
- def link_to(stream_name, events)
75
+ def link_to(stream_name, events, expected_version: nil)
73
76
  data = build_linkig_data(events)
77
+ headers = {
78
+ 'ES-ExpectedVersion' => expected_version&.to_s
79
+ }.reject { |_key, val| val.nil? || val.empty? }
74
80
 
75
- make_request(
81
+ response = make_request(
76
82
  :post,
77
83
  "/streams/#{stream_name}",
78
- body: data
84
+ body: data,
85
+ headers: headers
79
86
  )
87
+ validate_response(response, expected_version)
88
+ response
80
89
  end
81
90
 
82
91
  def ack(url)
@@ -125,6 +134,14 @@ module EventStoreClient
125
134
  def connection
126
135
  @connection ||= Api::Connection.new(endpoint).call
127
136
  end
137
+
138
+ def validate_response(resp, expected_version)
139
+ return unless resp.status == 400 && resp.reason_phrase == 'Wrong expected EventNumber'
140
+ raise WrongExpectedEventVersion.new(
141
+ "current version: #{resp.headers.fetch('es-currentversion')} | "\
142
+ "expected: #{expected_version}"
143
+ )
144
+ end
128
145
  end
129
146
  end
130
147
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EventStoreClient
4
- VERSION = '0.1.6'
4
+ VERSION = '0.1.7'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_store_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Wilgosz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-04 00:00:00.000000000 Z
11
+ date: 2020-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-schema