event_store_client 0.1.1 → 0.1.2

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: 72306acfd8fecce9f4ed423a4f72421b553fb8892ec73d4c4da702505470eed1
4
- data.tar.gz: 8c686d2ae4c1a4db9f55083b18cb24132c9187a8b47b1d6180064915adb5dd12
3
+ metadata.gz: 264ab7bf160dd4fafffd13ac73f15ce0ab6bffa2f762c0ee4da6cb628f70ec6a
4
+ data.tar.gz: 73c9478c8fc52f4729cafb4c55181afecc006269e2148fee2edb4830df9bab31
5
5
  SHA512:
6
- metadata.gz: 50ea4daa3e0ae81deb0b85fc1cd5a3210f742b23c981d2d23d0708830714e2f2a1e94208b4a007303065fe2a9d3480ce1febc7f3c9072568db27c1948d54aafd
7
- data.tar.gz: 4ef0826898e6ab83ffec67167d089ba023736771baf0a671eca0da371cbf6d9813ab3459c183795c3cf13fd3ed406885992f8a9f5f1db4a71eab1bc762b7d5fe
6
+ metadata.gz: 64e69a708344ab5cbf7b94a539220e07ac0564374f36edbabfc63a79a4bbd76396053b4b3d1f04d0067626555431de1f8966686008bb2227225fca79d7d94beb
7
+ data.tar.gz: 7c96a05e7f86c5b12c562de3642ca908bfcb536f3393425a99c686f491d461183bdd74b800ece5c5beebed3ba5558d31dac9acdf679d45b3c41cd018f7b0fdb9
data/README.md CHANGED
@@ -50,29 +50,20 @@ Before you start, add this to the `initializer` or to the top of your script:
50
50
  To test out the behavior, you'll need a sample event and handler to work with:
51
51
 
52
52
  ```ruby
53
- # Sample Event using dry-struct (recommended)
54
- require 'dry-struct'
55
- class SomethingHappened < Dry::Struct
56
- attribute :data, EventStoreClient::Types::Strict::Hash
57
- attribute :metadata, EventStoreClient::Types::Strict::Hash
58
- end
59
-
60
- # Sample Event without types check (not recommended)
61
-
62
- class SomethingHappened < Dry::Struct
63
- attr_reader :data, :metadata
64
53
 
65
- private
54
+ require 'securerandom'
66
55
 
67
- def initialize(data: {}, metadata: {})
68
- @data = data
69
- @metadata = metadata
56
+ class SomethingHappened < EventStoreClient::DeserializedEvent
57
+ def schema
58
+ Dry::Schema.Params do
59
+ required(:user_id).value(:string)
60
+ required(:email).value(:string)
61
+ end
70
62
  end
71
63
  end
72
64
 
73
65
  event = SomethingHappened.new(
74
66
  data: { user_id: SecureRandom.uuid, title: "Something happened" },
75
- metadata: {}
76
67
  )
77
68
  ```
78
69
 
@@ -1,11 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EventStoreClient
4
+ def self.configure(&block)
5
+ config = Configuration.instance
6
+ config.configure(&block)
7
+ end
4
8
  end
5
9
 
6
10
  require 'event_store_client/configuration'
7
11
  require 'event_store_client/types'
8
12
  require 'event_store_client/event'
13
+ require 'event_store_client/deserialized_event'
9
14
 
10
15
  require 'event_store_client/serializer/json'
11
16
 
@@ -13,7 +13,7 @@ module EventStoreClient
13
13
  def read(stream, direction: 'forward')
14
14
  response =
15
15
  client.read(stream, start: 0, direction: direction)
16
- return [] if response.body&.empty?
16
+ return [] if response.body.nil? || response.body.empty?
17
17
  JSON.parse(response.body)['entries'].map do |entry|
18
18
  event = EventStoreClient::Event.new(
19
19
  id: entry['eventId'],
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/schema'
4
+
5
+ module EventStoreClient
6
+ class DeserializedEvent
7
+ InvalidDataError = Class.new(StandardError)
8
+
9
+ attr_reader :data
10
+ attr_reader :metadata
11
+ attr_reader :type
12
+
13
+ def schema
14
+ Dry::Schema.Params do
15
+ end
16
+ end
17
+
18
+ def initialize(**args)
19
+ validation = schema.call(args[:data] || {})
20
+ raise InvalidDataError.new(message: validation.errors.to_h) if validation.errors.any?
21
+
22
+ @data = args.fetch(:data) { {} }
23
+ @metadata = args.fetch(:metadata) { {} }
24
+ @type = args[:type] || self.class.name
25
+ end
26
+ end
27
+ end
@@ -15,9 +15,17 @@ module EventStoreClient
15
15
  metadata = serializer.deserialize(event.metadata)
16
16
  data = serializer.deserialize(event.data)
17
17
 
18
- Object.const_get(event.type).new(
18
+ event_class =
19
+ begin
20
+ Object.const_get(event.type)
21
+ rescue NameError
22
+ EventStoreClient::DeserializedEvent
23
+ end
24
+
25
+ event_class.new(
19
26
  metadata: metadata,
20
- data: data
27
+ data: data,
28
+ type: event.type
21
29
  )
22
30
  end
23
31
 
@@ -21,9 +21,9 @@ module EventStoreClient
21
21
 
22
22
  def read(stream_name, direction: 'forward', start: 0, count: per_page)
23
23
  if direction == 'forward'
24
- read_stream_forward(stream_name, start: start, count: per_page)
24
+ read_stream_forward(stream_name, start: start, count: count)
25
25
  else
26
- read_stream_backward(stream_name, start: start, count: per_page)
26
+ read_stream_backward(stream_name, start: start, count: count)
27
27
  end
28
28
  end
29
29
 
@@ -4,6 +4,12 @@ require 'dry-types'
4
4
 
5
5
  module EventStoreClient
6
6
  module Types
7
+ UUID_REGEXP = /\A[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}\z/i
8
+
7
9
  include Dry.Types()
10
+
11
+ UUID = Types::Strict::String.constrained(
12
+ format: UUID_REGEXP
13
+ )
8
14
  end
9
15
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EventStoreClient
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_store_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Wilgosz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-04 00:00:00.000000000 Z
11
+ date: 2019-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-schema
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.1
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: dry-struct
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -95,6 +109,7 @@ files:
95
109
  - lib/event_store_client/client.rb
96
110
  - lib/event_store_client/configuration.rb
97
111
  - lib/event_store_client/connection.rb
112
+ - lib/event_store_client/deserialized_event.rb
98
113
  - lib/event_store_client/endpoint.rb
99
114
  - lib/event_store_client/event.rb
100
115
  - lib/event_store_client/mapper.rb