diff --git a/CHANGELOG.md b/CHANGELOG.md index 82fd4d0..55183d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ - `rescue_from`. - Support for `credentials` store in Rails 5.2. +- Deprecate `telegram_webhooks` in favor of `telegram_webhook`. + It was too complicated and such routes looked ugly. # 0.12.4 diff --git a/README.md b/README.md index 44df1b9..96008f9 100644 --- a/README.md +++ b/README.md @@ -340,17 +340,19 @@ end ### Routes in Rails app -There is `telegram_webhooks` helper for rails app to define routes for webhooks. +There is `telegram_webhook` helper for rails app to define routes for webhooks. It defines routes at `telegram/#{bot.token}` and connects bots with controller. For more options see [examples in wiki](https://github.com/telegram-bot-rb/telegram-bot/wiki/Routes-helpers-in-details). ```ruby -# Create routes for all Telegram.bots using single controller: -telegram_webhooks TelegramController +# Most off apps would require +telegram_webhook TelegramController +# which is same as +telegram_webhook TelegramController, :default # Use different controllers for each bot: -telegram_webhooks chat: TelegramChatController, - auction: TelegramAuctionController +telegram_webhook TelegramChatController, :chat +telegram_webhook TelegramAuctionController, :auction ``` #### Processesing updates @@ -377,7 +379,7 @@ Use `rake telegram:bot:poller` to run poller in rails app. It automatically load changes without restart in development env. Optionally pass bot id in `BOT` envvar (`BOT=chat`) to specify bot to run poller for. -This task requires `telegram_webhooks` helper to be used as it connects bots with controller. +This task requires `telegram_webhook` helper to be used as it connects bots with controller. To run poller in other cases use: ```ruby diff --git a/lib/telegram/bot.rb b/lib/telegram/bot.rb index 85e2584..fdb15fe 100644 --- a/lib/telegram/bot.rb +++ b/lib/telegram/bot.rb @@ -12,6 +12,15 @@ module Telegram # Raised for valid telegram response with 404 status code. class NotFound < Error; end + module_function + + def deprecation_0_14 + @deprecation ||= begin + require 'active_support/deprecation' + ActiveSupport::Deprecation.new('0.14', 'Telegram::Bot') + end + end + autoload :Async, 'telegram/bot/async' autoload :Botan, 'telegram/bot/botan' autoload :Client, 'telegram/bot/client' diff --git a/lib/telegram/bot/routes_helper.rb b/lib/telegram/bot/routes_helper.rb index 28aa868..9862c83 100644 --- a/lib/telegram/bot/routes_helper.rb +++ b/lib/telegram/bot/routes_helper.rb @@ -28,25 +28,19 @@ module Telegram # telegram_webhooks TelegramController # # # Or pass custom bots usin any of supported config options: - # telegram_webhooks TelegramController, - # bot, - # {token: token, username: username}, - # other_bot_token - # - # # Use different controllers for each bot: - # telegram_webhooks bot => TelegramChatController, - # other_bot => TelegramAuctionController - # - # # telegram_webhooks creates named routes. See - # # RoutesHelper.route_name_for_bot for more info. - # # You can override this options or specify others: - # telegram_webhooks TelegramController, as: :my_webhook - # telegram_webhooks bot => [TelegramChatController, as: :chat_webhook], - # other_bot => TelegramAuctionController, - # admin_chat: TelegramAdminChatController - # - # TODO: Deprecate it in favor of telegram_webhook. + # 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] }] @@ -59,8 +53,15 @@ module Telegram # Define route which processes requests using given controller and bot. # - # See telegram_webhooks for examples. - def telegram_webhook(controller, bot, **options) + # telegram_webhook TelegramController, bot + # + # telegram_webhook TelegramController + # # same as: + # telegram_webhook TelegramController, :default + # + # # pass additional options + # telegram_webhook TelegramController, :default, as: :custom_route_name + def telegram_webhook(controller, bot = :default, **options) bot = Client.wrap(bot) params = { to: Middleware.new(bot, controller), diff --git a/spec/integration_helper.rb b/spec/integration_helper.rb index 59f51bd..941d11e 100644 --- a/spec/integration_helper.rb +++ b/spec/integration_helper.rb @@ -75,9 +75,9 @@ RSpec.configure do |config| require 'telegram/bot/routes_helper' extend Telegram::Bot::RoutesHelper - telegram_webhooks default: DefaultBotController, - other: OtherBotController, - named: NamedBotController + telegram_webhook DefaultBotController, :default + telegram_webhook OtherBotController, :other + telegram_webhook NamedBotController, :named end end end diff --git a/spec/telegram/bot/routes_helper_spec.rb b/spec/telegram/bot/routes_helper_spec.rb index 8bcc736..dbfa1bc 100644 --- a/spec/telegram/bot/routes_helper_spec.rb +++ b/spec/telegram/bot/routes_helper_spec.rb @@ -40,6 +40,62 @@ RSpec.describe Telegram::Bot::RoutesHelper do end end + describe '#telegram_webhook' do + subject { mapper.telegram_webhook(*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 } } + + def assert_routes(bot, controller, route_name, options) # rubocop:disable AbcSize + 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 + subject + end + + context 'when called with controller' do + let(:input) { [controller, option: :val] } + + it 'creates routes for default bot and this controller' do + assert_routes bot, controller, 'default_telegram_webhook', option: :val + end + + context 'and bot does not have configured token' do + let(:bot) { create_bot(nil) } + it 'creates routes for default bot and this controller' do + assert_routes bot, controller, 'default_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' + end + end + end + + context 'when called with controller and smth castable to bot' do + let(:input) { [controller, 'custom_token', option: :val] } + + it 'creates routes for every created bot and controller' do + assert_routes create_bot('custom_token'), controller, 'telegram_webhook', + option: :val + end + end + end + describe '#telegram_webhooks' do subject { mapper.telegram_webhooks(*input) } let(:mapper) { double(:mapper).tap { |x| x.extend described_class } } @@ -47,6 +103,7 @@ RSpec.describe Telegram::Bot::RoutesHelper do 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)|