Closed gorrus39 closed 6 months ago
I accomplished it this way
Can this be done somehow better?
# frozen_string_literal: true
require_relative 'telegram/private_chat/commands'
require_relative 'telegram/checkers'
class TelegramDispatcherController
class << self
def dispatch(bot, update)
instance = new(bot, update)
checks_handled = instance.handle_checkers
instance.dispatch if checks_handled
end
end
attr_reader :bot, :update
def initialize(bot, update)
@bot = bot
@update = update
end
CHECKS = %i[
user_allowed?
].freeze
def handle_checkers
checker = Telegram::Checkers.new(update)
result = true
CHECKS.each do |check|
result = checker.send(check) == :next
break unless result
end
result
end
def dispatch
some_condition = true
if some_condition
Telegram::PrivateChat::Command.dispatch(bot, update)
else
# another class
end
end
end
# frozen_string_literal: true
module Telegram
class Checkers < Telegram::Bot::UpdatesController
def initializer(update)
@update = update
end
def user_allowed?
:next
end
end
end
module Telegram
module PrivateChat
class Command < Telegram::Bot::UpdatesController
def start!(*)
respond_with :message, text: t('.content')
end
end
end
end
file structure
application_controller.rb
- telegram
-- private_chat
--- commands.rb
-- checkers.rb
How can I make it possible to transfer further conditional processing in the TelegramWebhookController controller to another controller?
for example, have a file structure like this