-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'trunk' into pt-186495777
- Loading branch information
Showing
29 changed files
with
927 additions
and
232 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
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
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 |
---|---|---|
@@ -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 |
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,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 |
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,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 |
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,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 |
Oops, something went wrong.