зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-07 19:35:52 +03:00
Fixed Readme, added spec for protected methods in controller
Этот коммит содержится в:
@@ -142,7 +142,7 @@ You can enable typecasting of `update` with `telegram-bot-types` by including
|
|||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Telegram::WebhookController < Telegram::Bot::UpdatesController
|
class Telegram::WebhookController < Telegram::Bot::UpdatesController
|
||||||
include Telegram::Bot::UpdatesPoller::TypedUpdate
|
include Telegram::Bot::UpdatesController::TypedUpdate
|
||||||
|
|
||||||
def message(message)
|
def message(message)
|
||||||
message.class # => Telegram::Bot::Types::Message
|
message.class # => Telegram::Bot::Types::Message
|
||||||
@@ -205,7 +205,7 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
|
|||||||
# You can do it in other way:
|
# You can do it in other way:
|
||||||
def rename(name = nil, *)
|
def rename(name = nil, *)
|
||||||
if name
|
if name
|
||||||
update_name message[:text]
|
update_name name
|
||||||
reply_with :message, text: 'Renamed!'
|
reply_with :message, text: 'Renamed!'
|
||||||
else
|
else
|
||||||
save_context :rename
|
save_context :rename
|
||||||
|
|||||||
@@ -147,17 +147,41 @@ RSpec.describe Telegram::Bot::UpdatesController do
|
|||||||
describe '#process' do
|
describe '#process' do
|
||||||
subject { -> { controller.process(:action, *args) } }
|
subject { -> { controller.process(:action, *args) } }
|
||||||
let(:args) { [:arg1, :arg2] }
|
let(:args) { [:arg1, :arg2] }
|
||||||
|
let(:controller_class) do
|
||||||
|
Class.new(described_class) do
|
||||||
|
attr_reader :acted, :hooked
|
||||||
|
|
||||||
|
def action(*args)
|
||||||
|
@acted = true
|
||||||
|
[from, chat, args]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when action is protected' do
|
||||||
|
before { controller_class.send :protected, :action }
|
||||||
|
its(:call) { should eq nil }
|
||||||
|
|
||||||
|
context 'when action_missing defined' do
|
||||||
|
before do
|
||||||
|
controller.class_eval do
|
||||||
|
protected
|
||||||
|
|
||||||
|
def action_missing(*args)
|
||||||
|
args
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
its(:call) { should eq ['action', *args] }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when callbacks are defined' do
|
context 'when callbacks are defined' do
|
||||||
let(:controller_class) do
|
before do
|
||||||
Class.new(described_class) do
|
controller_class.class_eval do
|
||||||
before_action :hook, only: :action
|
before_action :hook, only: :action
|
||||||
attr_reader :acted, :hooked
|
attr_reader :hooked
|
||||||
|
|
||||||
def action(*args)
|
|
||||||
@acted = true
|
|
||||||
args
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
@@ -169,7 +193,7 @@ RSpec.describe Telegram::Bot::UpdatesController do
|
|||||||
|
|
||||||
it { should change(controller, :hooked).to true }
|
it { should change(controller, :hooked).to true }
|
||||||
it { should change(controller, :acted).to true }
|
it { should change(controller, :acted).to true }
|
||||||
its(:call) { should eq args }
|
its(:call) { should eq [nil, nil, args] }
|
||||||
|
|
||||||
context 'when callback returns false' do
|
context 'when callback returns false' do
|
||||||
before do
|
before do
|
||||||
@@ -191,14 +215,6 @@ RSpec.describe Telegram::Bot::UpdatesController do
|
|||||||
let(:controller) { controller_class.new(bot, from: from, chat: chat) }
|
let(:controller) { controller_class.new(bot, from: from, chat: chat) }
|
||||||
let(:from) { {'id' => 'user_id'} }
|
let(:from) { {'id' => 'user_id'} }
|
||||||
let(:chat) { {'id' => 'chat_id'} }
|
let(:chat) { {'id' => 'chat_id'} }
|
||||||
let(:controller_class) do
|
|
||||||
Class.new(described_class) do
|
|
||||||
def action(*args)
|
|
||||||
[from, chat, args]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
its(:call) { should eq [from, chat, args] }
|
its(:call) { should eq [from, chat, args] }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user