Skip to content

Commit

Permalink
Merge branch 'trunk' into pt-186495777
Browse files Browse the repository at this point in the history
  • Loading branch information
saikumar9 authored Jul 31, 2024
2 parents e53f7fd + ed23a94 commit a98096e
Show file tree
Hide file tree
Showing 29 changed files with 927 additions and 232 deletions.
39 changes: 1 addition & 38 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
event_source (0.5.7)
event_source (0.5.8)
addressable (>= 2.8.0)
bunny (>= 2.14)
deep_merge (~> 1.2.0)
Expand All @@ -21,7 +21,6 @@ PATH
nokogiri (>= 1.13.0)
oj (~> 3.11)
ox (~> 2.14)
resque-bus (~> 0.7.0)
typhoeus (~> 1.4.0)

GEM
Expand Down Expand Up @@ -154,8 +153,6 @@ GEM
dry-initializer (~> 3.0)
dry-schema (~> 1.5, >= 1.5.2)
erubi (1.10.0)
et-orbi (1.2.6)
tzinfo
ethon (0.15.0)
ffi (>= 1.15.0)
faker (2.18.0)
Expand All @@ -176,9 +173,6 @@ GEM
faraday_middleware (1.2.0)
faraday (~> 1.0)
ffi (1.15.4)
fugit (1.5.2)
et-orbi (~> 1.1, >= 1.1.8)
raabro (~> 1.4)
globalid (0.5.2)
activesupport (>= 5.0)
hashdiff (1.0.1)
Expand Down Expand Up @@ -207,7 +201,6 @@ GEM
mongoid (7.3.0)
activemodel (>= 5.1, < 6.2)
mongo (>= 2.10.5, < 3.0.0)
mono_logger (1.1.1)
multi_json (1.15.0)
multipart-post (2.1.1)
mustermann (1.1.1)
Expand All @@ -225,10 +218,6 @@ GEM
byebug (~> 11.0)
pry (~> 0.10)
public_suffix (4.0.6)
queue-bus (0.12.0)
multi_json
redis
raabro (1.4.0)
racc (1.6.2)
rack (2.2.3)
rack-protection (2.1.0)
Expand Down Expand Up @@ -264,29 +253,7 @@ GEM
rainbow (3.0.0)
rake (13.0.6)
rbtree (0.4.6)
redis (4.5.1)
redis-namespace (1.8.1)
redis (>= 3.0.4)
regexp_parser (2.1.1)
resque (1.27.4)
mono_logger (~> 1.0)
multi_json (~> 1.0)
redis-namespace (~> 1.3)
sinatra (>= 0.9.2)
vegas (~> 0.1.2)
resque-bus (0.7.0)
queue-bus (>= 0.7, < 1)
resque (>= 1.10.0, < 2.0)
resque-retry
resque-scheduler (>= 2.0.1)
resque-retry (1.7.6)
resque (>= 1.25, < 3.0)
resque-scheduler (~> 4.0)
resque-scheduler (4.5.0)
mono_logger (~> 1.0)
redis (>= 3.3)
resque (>= 1.27)
rufus-scheduler (~> 3.2, < 3.7)
rexml (3.2.5)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
Expand Down Expand Up @@ -318,8 +285,6 @@ GEM
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.4)
rufus-scheduler (3.6.0)
fugit (~> 1.1, >= 1.1.6)
set (1.0.2)
sinatra (2.1.0)
mustermann (~> 1.0)
Expand All @@ -343,8 +308,6 @@ GEM
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.0.0)
vegas (0.1.11)
rack (>= 1.0.0)
webmock (3.13.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
Expand Down
1 change: 0 additions & 1 deletion event_source.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ Gem::Specification.new do |spec|
spec.add_dependency 'mime-types'
spec.add_dependency 'oj', '~> 3.11'
spec.add_dependency 'ox', '~> 2.14'
spec.add_dependency 'resque-bus', '~> 0.7.0'
spec.add_dependency 'typhoeus', '~> 1.4.0'

# TODO: Change to development dependency
Expand Down
5 changes: 5 additions & 0 deletions lib/event_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@
require 'event_source/worker'
require 'event_source/publish_operation'
require 'event_source/subscribe_operation'
require 'event_source/message'
require 'event_source/command'
require 'event_source/publisher'
require 'event_source/event'
require 'event_source/subscriber'
require 'event_source/operations/codec64'
require 'event_source/operations/create_message'
require 'event_source/operations/fetch_session'
require 'event_source/operations/build_message_options'
require 'event_source/operations/build_message'

# Event source provides ability to compose, publish and subscribe to events
module EventSource
Expand Down
20 changes: 2 additions & 18 deletions lib/event_source/async_api/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,14 @@ class Message < Dry::Struct
# @!attribute [r] headers
# Schema definition of the application headers. Schema must be of type "object".
# It must not define the protocol headers.
# @return [Schema]
attribute :headers, Schema.meta(omittable: true)
# @return [Types::Any]
attribute :headers, Types::Any.meta(omittable: true)

# @!attribute [r] payload
# Definition of the message payload. It can be of any type but defaults to Schema object
# @return [Types::Any]
attribute :payload, Types::Any.meta(omittable: true)

# @!attribute [r] correlation_id
# Definition of the correlation ID used for message tracing or matching
# @return [String]
attribute :correlation_id do
# @!attribute [r] description
# An optional description of the identifier.
# CommonMark syntax can be used for rich text representation
# @return [String]
attribute :description, Types::String.meta(omittable: true)

# @!attribute [r] location
# Required. A runtime expression that specifies the location of the correlation ID
# @return [String]
attribute :location, Types::String.meta(omittable: true)
end.meta(omittable: true)

# @!attribute [r] schema_format
# A string containing the name of the schema format used to define the message payload.
# If omitted, implementations should parse the payload as a Schema object. Check out the
Expand Down
1 change: 1 addition & 0 deletions lib/event_source/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ class Error < StandardError
ConnectionNotFound = Class.new(Error)
ServerConfigurationNotFound = Class.new(Error)
ServerConfigurationInvalid = Class.new(Error)
MessageBuildError = Class.new(Error)
end
end
13 changes: 12 additions & 1 deletion lib/event_source/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Event
# @attr_reader [Array<String>] attribute_keys optional list of attributes that must be included in { Payload }
# @attr_reader [String] publisher_path namespaced key indicating the class that registers event for publishing
# @attr_reader [String] payload attribute/value pairs for the message that accompanies the event
attr_reader :attribute_keys, :publisher_path, :payload, :headers, :metadata
attr_reader :attribute_keys, :publisher_path, :payload, :headers, :metadata, :message

HeaderDefaults = {
version: '3.0',
Expand All @@ -26,12 +26,22 @@ def initialize(options = {})
send(:headers=, options[:headers] || {})

@publisher_path = klass_var_for(:publisher_path) || nil
build_message(options) if headers.delete(:build_message)

if @publisher_path.eql?(nil)
raise EventSource::Error::PublisherKeyMissing,
"add 'publisher_path' to #{self.class.name}"
end
end

def build_message(options)
@message = EventSource::Message.new(
headers: options[:headers],
payload: options[:attributes],
event_name: name
)
end

# Set payload
# @overload payload=(payload)
# @param [Hash] payload New payload
Expand Down Expand Up @@ -127,6 +137,7 @@ def set_instance_variable_for(element, value)

def validate_attribute_presence
return unless attribute_keys.present?

gapped_keys = attribute_keys - payload.keys
@event_errors = []
event_errors.push("missing required keys: #{gapped_keys}") unless gapped_keys.empty?
Expand Down
37 changes: 37 additions & 0 deletions lib/event_source/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require "forwardable"

module EventSource
# Construct async api message object
class Message
extend Forwardable

def initialize(options = {})
message_options = build_message(options)
@message = create_message(message_options)
end

def_delegators :@message, :payload, :headers

private

def build_message(options)
result = EventSource::Operations::BuildMessageOptions.new.call(options)
unless result.success?
raise EventSource::Error::MessageBuildError,
"unable to build message options due to #{result.failure}"
end
result.success
end

def create_message(options)
result = EventSource::Operations::CreateMessage.new.call(options)
unless result.success?
raise EventSource::Error::MessageBuildError,
"unable to create message due to #{result.failure}"
end
result.success
end
end
end
31 changes: 31 additions & 0 deletions lib/event_source/operations/build_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "dry/monads"
require "dry/monads/do"

module EventSource
module Operations
# create message
class BuildMessage
include Dry::Monads[:result, :do]

def call(params)
values = yield build_options(params)
message = yield create_message(values)

Success(message)
end

private

def build_options(params)
result = BuildMessageOptions.new.call(params)
result.success? ? result : Failure(result.errors.to_h)
end

def create_message(values)
CreateMessage.new.call(values)
end
end
end
end
58 changes: 58 additions & 0 deletions lib/event_source/operations/build_message_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require "dry/monads"
require "dry/monads/do"
require "securerandom"

module EventSource
module Operations
# extract message options
class BuildMessageOptions
include Dry::Monads[:result, :do]

def call(params)
headers = yield build_headers(params)
payload = yield build_payload(params)
headers = yield append_account_details(headers)

Success(headers: headers, payload: payload)
end

private

def build_headers(params)
headers = params[:headers]&.symbolize_keys || {}
headers[:correlation_id] ||= SecureRandom.uuid
headers[:message_id] ||= SecureRandom.uuid
headers[:event_name] ||= params[:event_name]
headers[:event_time] = headers[:event_time]&.utc

Success(headers)
end

def build_payload(params)
payload = params[:payload]&.symbolize_keys || {}

Success(payload)
end

def append_account_details(headers)
output = FetchSession.new.call
return output unless output.success?

session, current_user, system_account = output.value!
account = {}

if session.present? && current_user.present?
account[:session] = session&.symbolize_keys
account[:id] = current_user&.id&.to_s
else
account[:id] = system_account&.id&.to_s
end
headers[:account] = account

Success(headers)
end
end
end
end
33 changes: 33 additions & 0 deletions lib/event_source/operations/create_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require "dry/monads"
require "dry/monads/do"

module EventSource
module Operations
# create message
class CreateMessage
include Dry::Monads[:result, :do]

def call(params)
values = yield build(params)
message = yield create(values)

Success(message)
end

private

def build(params)
result =
::EventSource::AsyncApi::Contracts::MessageContract.new.call(params)

result.success? ? Success(result.to_h) : Failure(result.errors.to_h)
end

def create(values)
Success(EventSource::AsyncApi::Message.new(values))
end
end
end
end
Loading

0 comments on commit a98096e

Please sign in to comment.