Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve #15 - Rename the Dispatcher to Process #19

Merged
merged 5 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ There are 7 currently implemented use cases:
* WIP limit exceeded - from Notion to Discord
* Support email notification - from IMAP to Discord

For this example we'll analyze the birthday notification use case, bringing data from a notion database, and dispatching the
For this example we'll analyze the birthday notification use case, bringing data from a notion database, and sending the
notifications to a Discord channel.

A *Use Case* object, consists on 4 main components, having it's own responsibility:
Expand Down Expand Up @@ -67,11 +67,11 @@ The **Formatter**, is in charge of preparing the message to be sent in our notif
The template or 'format' to be used should be included in the use case configurations, which we'll review in a further step. It's
implementation can be found under `/bas/formatter/birthday.rb`.

### 4. Dispatcher - Sending your notification
### 4. Process - Optional Data Process

Finally, the **Dispatcher** basically, sends or dispatches the formatted message into a destination, since the use case was implemented for
Finally, the **Process** basically, allow required data process depending on the use case like sending formatted messages into a destination. In this case, since the use case was implemented for
Discord, it implements specific logic to communicate with a Discord channel using a webhook. The webhook configuration and name for the 'Sender'
in the channel should be provided with the initial use case configurations. It can be found under `/bas/dispatcher/discord/implementation.rb`
in the channel should be provided with the initial use case configurations. It can be found under `/bas/process/discord/implementation.rb`

## Examples

Expand Down Expand Up @@ -113,7 +113,7 @@ today = Date.now
}
```

* A template for the formatter to be used for formatting the payload to dispatch to Discord. For this specific case, the format of the messages should be:
* A template for the formatter to be used for formatting the payload to be send to Discord. For this specific case, the format of the messages should be:

`"NAME, Wishing you a very happy birthday! Enjoy your special day! :birthday: :gift:"`

Expand Down Expand Up @@ -145,7 +145,7 @@ options = {
secret: NOTION_API_INTEGRATION_SECRET,
filter: filter
},
dispatch_options: {
process_options: {
webhook: "https://discord.com/api/webhooks/1199213527672565760/KmpoIzBet9xYG16oFh8W1RWHbpIqT7UtTBRrhfLcvWZdNiVZCTM-gpil2Qoy4eYEgpdf",
name: "Birthday Bot"
}
Expand Down Expand Up @@ -235,7 +235,7 @@ module Notifier
# Service description
class UseCaseName
def self.notify(*)
options = { read_options:, dispatch_options: }
options = { read_options:, process_options: }

begin
use_case = UseCases.use_case_build_function(options)
Expand Down Expand Up @@ -266,7 +266,7 @@ module Notifier
]
end

def self.dispatch_options
def self.process_options
{
webhook: DISCORD_WEBHOOK,
name: DISCORD_BOT_NAME
Expand Down
2 changes: 1 addition & 1 deletion lib/bas/formatter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def initialize(config = {})
# <br>
# <b>raises</b> <tt>Domain::Exceptions::FunctionNotImplemented</tt> when missing implementation.
#
# <b>returns</b> <tt>String</tt> Formatted payload suitable for a Dispatcher::Base implementation.
# <b>returns</b> <tt>String</tt> Formatted payload suitable for a Process::Base implementation.
#
def format(_domain_data)
raise Domain::Exceptions::FunctionNotImplemented
Expand Down
4 changes: 2 additions & 2 deletions lib/bas/formatter/birthday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module Formatter
##
# This class implements methods from the Formatter::Base module, tailored to format the
# Domain::Birthday structure for a dispatcher.
# Domain::Birthday structure for a Process.
class Birthday < Base
# Implements the logic for building a formatted payload with the given template for birthdays.
#
Expand All @@ -19,7 +19,7 @@ class Birthday < Base
# <b>raises</b> <tt>Formatter::Exceptions::InvalidData</tt> when invalid data is provided.
#
# <br>
# <b>returns</b> <tt>String</tt> payload: formatted payload suitable for a Dispatcher.
# <b>returns</b> <tt>String</tt> payload: formatted payload suitable for a Process.
#
def format(birthdays_list)
raise Formatter::Exceptions::InvalidData unless birthdays_list.all? do |brithday|
Expand Down
4 changes: 2 additions & 2 deletions lib/bas/formatter/pto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
module Formatter
##
# This class implements methods from the Formatter::Base module, tailored to format the
# Domain::Pto structure for a dispatcher.
# Domain::Pto structure for a Process.
class Pto < Base
DEFAULT_TIME_ZONE = "+00:00"

Expand All @@ -32,7 +32,7 @@ def initialize(config = {})
# <b>raises</b> <tt>Formatter::Exceptions::InvalidData</tt> when invalid data is provided.
#
# <br>
# <b>returns</b> <tt>String</tt> payload, formatted payload suitable for a Dispatcher.
# <b>returns</b> <tt>String</tt> payload, formatted payload suitable for a Process.
#

def format(ptos_list)
Expand Down
4 changes: 2 additions & 2 deletions lib/bas/formatter/support_emails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module Formatter
##
# This class implements methods from the Formatter::Base module, tailored to format the
# Domain::Email structure for a dispatcher.
# Domain::Email structure for a Process.
class SupportEmails < Base
DEFAULT_TIME_ZONE = "+00:00"

Expand All @@ -31,7 +31,7 @@ def initialize(config = {})
# <b>raises</b> <tt>Formatter::Exceptions::InvalidData</tt> when invalid data is provided.
#
# <br>
# <b>returns</b> <tt>String</tt> payload: formatted payload suitable for a Dispatcher.
# <b>returns</b> <tt>String</tt> payload: formatted payload suitable for a Process.
#
def format(support_emails_list)
raise Formatter::Exceptions::InvalidData unless support_emails_list.all? do |support_email|
Expand Down
4 changes: 2 additions & 2 deletions lib/bas/formatter/work_items_limit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module Formatter
##
# This class implements methods from the Formatter::Base module, tailored to format the
# Domain::WorkItemsLimit structure for a dispatcher.
# Domain::WorkItemsLimit structure for a Process.
class WorkItemsLimit < Base
DEFAULT_DOMAIN_LIMIT = 6

Expand All @@ -30,7 +30,7 @@ def initialize(config = {})
# <b>raises</b> <tt>Formatter::Exceptions::InvalidData</tt> when invalid data is provided.
#
# <br>
# <b>returns</b> <tt>String</tt> payload, formatted payload suitable for a Dispatcher.
# <b>returns</b> <tt>String</tt> payload, formatted payload suitable for a Process.
#

def format(work_items_list)
Expand Down
10 changes: 5 additions & 5 deletions lib/bas/dispatcher/base.rb → lib/bas/process/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

require_relative "../domain/exceptions/function_not_implemented"

module Dispatcher
module Process
##
# Serves as a foundational structure for implementing specific dispatchers. Acting as an interface,
# Serves as a foundational structure for implementing specific process. Acting as an interface,
# this class defines essential attributes and methods, providing a blueprint for creating custom
# dispatchers tailored to different platforms or services.
# process tailored to different platforms or services.
#
class Base
attr_reader :webhook, :name

# Initializes the dispatcher with essential configuration parameters.
# Initializes the process with essential configuration parameters.
#
def initialize(config)
@webhook = config[:webhook]
Expand All @@ -24,7 +24,7 @@ def initialize(config)
# <br>
# <b>returns</b> a <tt>Discord::Response</tt>
#
def dispatch(_payload)
def execute(_payload)
raise Domain::Exceptions::FunctionNotImplemented
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module Dispatcher
module Process
module Discord
module Exceptions
##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
require_relative "./exceptions/invalid_webhook_token"
require_relative "./types/response"

module Dispatcher
module Process
module Discord
##
# This class is an implementation of the Dispatcher::Base interface, specifically designed
# for dispatching messages to Discord.
# This class is an implementation of the Process::Base interface, specifically designed
# for sending messages to Discord.
#
class Implementation < Base
# Implements the dispatching logic for the Discord use case. It sends a POST request to
# Implements the sending process logic for the Discord use case. It sends a POST request to
# the Discord webhook with the specified payload.
#
# <br>
# <b>Params:</b>
# * <tt>String</tt> payload: Payload to be dispatched to discord.
# * <tt>String</tt> payload: Payload to be send to discord.
# <br>
# <b>raises</b> <tt>Exceptions::Discord::InvalidWebookToken</tt> if the provided webhook token is invalid.
#
# <br>
# <b>returns</b> <tt>Dispatcher::Discord::Types::Response</tt>
# <b>returns</b> <tt>Process::Discord::Types::Response</tt>
#
def dispatch(payload)
def execute(payload)
body = {
username: name,
avatar_url: "",
content: payload
}.to_json
response = HTTParty.post(webhook, { body: body, headers: { "Content-Type" => "application/json" } })

discord_response = Dispatcher::Discord::Types::Response.new(response)
discord_response = Process::Discord::Types::Response.new(response)

validate_response(discord_response)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module Dispatcher
module Process
module Discord
module Types
##
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module Dispatcher
module Process
module Slack
module Exceptions
##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
require_relative "./exceptions/invalid_webhook_token"
require_relative "./types/response"

module Dispatcher
module Process
module Slack
##
# This class is an implementation of the Dispatcher::Base interface, specifically designed
# for dispatching messages to Slack.
# This class is an implementation of the Process::Base interface, specifically designed
# for sending messages to Slack.
#
class Implementation < Base
# Implements the dispatching logic for the Slack use case. It sends a POST request to
# Implements the sending process logic for the Slack use case. It sends a POST request to
# the Slack webhook with the specified payload.
#
# <br>
# <b>Params:</b>
# * <tt>String</tt> payload: Payload to be dispatched to slack.
# * <tt>String</tt> payload: Payload to be send to slack.
# <br>
# <b>raises</b> <tt>Exceptions::Slack::InvalidWebookToken</tt> if the provided webhook token is invalid.
#
# <br>
# <b>returns</b> <tt>Dispatcher::Slack::Types::Response</tt>
# <b>returns</b> <tt>Process::Slack::Types::Response</tt>
#
def dispatch(payload)
def execute(payload)
body = {
username: name,
text: payload
}.to_json

response = HTTParty.post(webhook, { body: body, headers: { "Content-Type" => "application/json" } })

slack_response = Dispatcher::Discord::Types::Response.new(response)
slack_response = Process::Discord::Types::Response.new(response)

validate_response(slack_response)
end
Expand All @@ -41,7 +41,7 @@ def dispatch(payload)
def validate_response(response)
case response.http_code
when 403
raise Dispatcher::Slack::Exceptions::InvalidWebookToken, response.message
raise Process::Slack::Exceptions::InvalidWebookToken, response.message
else
response
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module Dispatcher
module Process
module Slack
module Types
##
Expand Down
6 changes: 3 additions & 3 deletions lib/bas/use_cases/types/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ module Types
# Represents a the configuration composing the initial components required by a UseCases::UseCase implementation.
#
class Config
attr_reader :read, :serialize, :formatter, :dispatcher
attr_reader :read, :serialize, :formatter, :process

def initialize(read, serialize, formatter, dispatcher)
def initialize(read, serialize, formatter, process)
@read = read
@serialize = serialize
@formatter = formatter
@dispatcher = dispatcher
@process = process
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/bas/use_cases/use_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module UseCases
# logic flow by coordinating the execution of its components to fulfill a specific use case.
#
class UseCase
attr_reader :read, :serialize, :formatter, :dispatcher
attr_reader :read, :serialize, :formatter, :process

# Initializes the use case with the necessary components.
#
Expand All @@ -18,13 +18,13 @@ def initialize(config)
@read = config.read
@serialize = config.serialize
@formatter = config.formatter
@dispatcher = config.dispatcher
@process = config.process
end

# Executes the use case by orchestrating the sequential execution of the read, serialize, formatter, and dispatcher.
# Executes the use case by orchestrating the sequential execution of the read, serialize, formatter, and process.
#
# <br>
# <b>returns</b> <tt>Dispatcher::Discord::Types::Response</tt>
# <b>returns</b> <tt>Process::Discord::Types::Response</tt>
#
def perform
response = read.execute
Expand All @@ -33,7 +33,7 @@ def perform

formatted_payload = formatter.format(serialization)

dispatcher.dispatch(formatted_payload)
process.execute(formatted_payload)
end
end
end
Loading
Loading