-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add event and event serializers logic
- Loading branch information
1 parent
5c941c5
commit d9f6c53
Showing
13 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ | |
|
||
# rspec failure tracking | ||
.rspec_status | ||
|
||
# Debug | ||
.byebug_history |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,33 @@ | ||
require "event_router/version" | ||
require "event_router/error" | ||
require "active_job" | ||
require "event_router/destination" | ||
require "event_router/event" | ||
require "event_router/event_serializer" | ||
require "event_router/deliver_event_job" | ||
|
||
require "examples/order_placed" | ||
|
||
require "pry" | ||
|
||
module EventRouter | ||
class Error < StandardError; end | ||
# Your code goes here... | ||
module_function | ||
|
||
def run_example | ||
ActiveJob::Serializers.add_serializers EventSerializer | ||
|
||
Examples::OrderPlaced.publish(order_id: 1) | ||
end | ||
|
||
def publish(event) | ||
DeliverEventJob.perform_later(:notifications, event) | ||
end | ||
|
||
# def publish(events) | ||
# events = Array(events) | ||
# | ||
# events.map(&:destinations).flatten.each do |destination| | ||
# DeliverEventJob.perform_later(destination, event: event) | ||
# end | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
require "pry" | ||
|
||
module EventRouter | ||
class DeliverEventJob < ActiveJob::Base | ||
self.queue_adapter = :sidekiq | ||
|
||
def perform(destination, event) | ||
binding.pry | ||
event.destinations[destination]&.process(event) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module EventRouter | ||
class Destination | ||
attr_reader :name, :handler, :handler_method | ||
|
||
def initialize(name, handler:, method: nil) | ||
@name = name | ||
@handler = handler | ||
@handler_method = method | ||
end | ||
|
||
def process(event) | ||
@handler_method ||= event.underscore_name | ||
|
||
handler.send(handler_method, event) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module EventRouter | ||
class Error < StandardError; end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require "securerandom" | ||
|
||
module EventRouter | ||
class Event | ||
attr_reader :uid, :correlation_id, :created_at, :payload | ||
|
||
class_attribute :destinations, default: {}, instance_writer: false | ||
|
||
def initialize(uid: SecureRandom.uuid, correlation_id: SecureRandom.uuid, created_at: Time.current, **payload) | ||
@uid = uid | ||
@correlation_id = correlation_id | ||
@created_at = created_at | ||
@payload = payload | ||
end | ||
|
||
def to_hash | ||
{ | ||
uid: uid, | ||
correlation_id: correlation_id, | ||
payload: payload, | ||
created_at: created_at, | ||
_er_klass: self.class.name | ||
} | ||
end | ||
|
||
def underscore_name | ||
self.class.name.demodulize.underscore | ||
end | ||
|
||
class << self | ||
def inherited(base) | ||
base.destinations = destinations.dup | ||
super | ||
end | ||
|
||
def deliver_to(name, opts = {}) | ||
destinations[name] = EventRouter::Destination.new(name, **opts) | ||
end | ||
|
||
def publish(**attrs) | ||
event = new(**attrs) | ||
|
||
EventRouter.publish(event) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module EventRouter | ||
class EventSerializer < ActiveJob::Serializers::ObjectSerializer | ||
def serialize(event) | ||
event_attrs = event.to_hash | ||
payload = event_attrs.delete(:payload) | ||
serialized_payload = ActiveJob::Arguments.serialize(payload) | ||
serialized_event = super(event_attrs) | ||
|
||
serialized_event.merge(payload: serialized_payload) | ||
end | ||
|
||
def deserialize(hash) | ||
hash["_er_klass"].constantize.new( | ||
uid: hash["uid"], | ||
correlation_id: hash["correlation_id"], | ||
created_at: Time.parse(hash["created_at"]), | ||
**ActiveJob::Arguments.deserialize(hash["payload"]).to_h | ||
) | ||
end | ||
|
||
private | ||
|
||
def klass | ||
Event | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module EventRouter | ||
module Examples | ||
module EventStore | ||
class OrderPlaced | ||
def self.handle(event) | ||
binding.pry | ||
true | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module EventRouter | ||
module Examples | ||
module Notifications | ||
module_function | ||
|
||
def order_placed(event) | ||
binding.pry | ||
true | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "notifications" | ||
require_relative "event_store/order_placed" | ||
|
||
module EventRouter | ||
module Examples | ||
class OrderPlaced < EventRouter::Event | ||
deliver_to :notifications, | ||
handler: EventRouter::Examples::Notifications | ||
|
||
deliver_to :event_store, | ||
handler: EventRouter::Examples::EventStore::OrderPlaced, | ||
method: :handle | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:queues: | ||
- [default, 1] |