diff --git a/lib/telegram/bot.rb b/lib/telegram/bot.rb index fdb15fe..ce860e3 100644 --- a/lib/telegram/bot.rb +++ b/lib/telegram/bot.rb @@ -14,10 +14,10 @@ module Telegram module_function - def deprecation_0_14 + def deprecation_0_15 @deprecation ||= begin require 'active_support/deprecation' - ActiveSupport::Deprecation.new('0.14', 'Telegram::Bot') + ActiveSupport::Deprecation.new('0.15', 'Telegram::Bot') end end diff --git a/lib/telegram/bot/routes_helper.rb b/lib/telegram/bot/routes_helper.rb index 9862c83..4c386ef 100644 --- a/lib/telegram/bot/routes_helper.rb +++ b/lib/telegram/bot/routes_helper.rb @@ -24,33 +24,6 @@ module Telegram end end - # # Create routes for all Telegram.bots to use same controller: - # telegram_webhooks TelegramController - # - # # Or pass custom bots usin any of supported config options: - # telegram_webhooks TelegramController, [ - # bot, - # {token: token, username: username}, - # other_bot_token, - # ] - def telegram_webhooks(controllers, bots = nil, **options) - Bot.deprecation_0_14.deprecation_warning(:telegram_webhooks, <<-TXT.strip_heredoc) - It brings unnecessary complexity and encourages writeng less readable code. - Please use telegram_webhook method instead. - It's signature `telegram_webhook(controller, bot = :default, **options)`. - Multiple-bot environments now requires calling this method in a loop - or using statement for each bot. - TXT - unless controllers.is_a?(Hash) - bots = bots ? Array.wrap(bots) : Telegram.bots.values - controllers = Hash[bots.map { |x| [x, controllers] }] - end - controllers.each do |bot, controller| - controller, bot_options = controller if controller.is_a?(Array) - telegram_webhook(controller, bot, options.merge(bot_options || {})) - end - end - # Define route which processes requests using given controller and bot. # # telegram_webhook TelegramController, bot diff --git a/spec/telegram/bot/routes_helper_spec.rb b/spec/telegram/bot/routes_helper_spec.rb index dbfa1bc..0a92af2 100644 --- a/spec/telegram/bot/routes_helper_spec.rb +++ b/spec/telegram/bot/routes_helper_spec.rb @@ -95,108 +95,4 @@ RSpec.describe Telegram::Bot::RoutesHelper do end end end - - describe '#telegram_webhooks' do - subject { mapper.telegram_webhooks(*input) } - let(:mapper) { double(:mapper).tap { |x| x.extend described_class } } - let(:bots) { {default: bot, other: other_bot} } - let(:controller) { double(:controller, name: :controller) } - let(:other_controller) { double(:other_controller, name: :other_controller) } - before { allow(Telegram).to receive(:bots) { bots } } - around { |ex| Telegram::Bot.deprecation_0_14.silence { ex.run } } - - def assert_routes(*expected) # rubocop:disable AbcSize - expected.each do |(bot, controller, route_name, options)| - expected_path = options.delete(:path) || "telegram/#{bot.token}" - expect(mapper).to receive(:post) do |path, params| - expect(path).to eq expected_path - middleware = params[:to] - expect(middleware.controller).to eq(controller) - expect(middleware.bot.token).to eq(bot.token) - expect(middleware.bot.username).to eq(bot.username) - expect(params[:as]).to eq route_name - expect(params).to include(options) if options - end - end - subject - end - - context 'when called with controller' do - let(:input) { [controller, option: :val] } - - it 'creates routes for every bot and this controller' do - assert_routes [bot, controller, 'default_telegram_webhook', option: :val], - [other_bot, controller, 'other_telegram_webhook', option: :val] - end - - context 'and bot does not have configured token' do - let(:bot) { create_bot(nil) } - it 'creates routes for every bot and this controller' do - assert_routes [bot, controller, 'default_telegram_webhook', option: :val], - [other_bot, controller, 'other_telegram_webhook', option: :val] - end - end - - context 'and bot has colon in token' do - let(:bot) { create_bot('some:token') } - it 'replaces colon with underscore' do - assert_routes [ - bot, - controller, - 'default_telegram_webhook', - option: :val, - path: 'telegram/some_token', - ], [other_bot, controller, 'other_telegram_webhook', option: :val] - end - end - end - - context 'when called with hash' do - let(:input) do - [ - { - bot => controller, - 'custom_token' => [other_controller, as: :custom_route, option: :other_val], - other: controller, - }, - option: :val, - ] - end - - it 'creates routes for every bot and its controller' do - assert_routes [bot, controller, 'default_telegram_webhook', option: :val], - [ - create_bot('custom_token'), - other_controller, - :custom_route, - option: :other_val, - ], - [Telegram.bots[:other], controller, 'other_telegram_webhook', option: :val] - end - end - - context 'when called with controller and smth castable to bot' do - let(:input) do - [ - controller, - ['custom_token', token: bot.token, username: 'new_name'], - option: :val, - ] - end - - it 'creates routes for every created bot and controller' do - assert_routes [ - create_bot('custom_token'), - controller, - 'telegram_webhook', - option: :val, - ], [ - create_bot(bot.token, 'new_name'), - controller, - 'telegram_webhook', - option: :val, - ] - end - end - end end