1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-03 17:55:52 +03:00

Fix #chat for non-message updates with TypedUpdates enabled

Fixes #82
Этот коммит содержится в:
Max Melentiev
2018-05-19 12:51:05 +03:00
родитель 4ff5a58b98
Коммит 6a5e642470
3 изменённых файлов: 38 добавлений и 7 удалений

Просмотреть файл

@@ -2,6 +2,7 @@
- Extracted typed response mappings to telegram-bot-types gem.
It now provides definitions for all API v3.5 methods.
- Fix #chat for non-message updates with TypedUpdates enabled
# 0.13.0

Просмотреть файл

@@ -157,7 +157,14 @@ module Telegram
#
# Can be overriden with `chat` option for #initialize.
def chat
@_chat ||= payload.try! { |x| x['chat'] || x['message'] && x['message']['chat'] }
@_chat ||=
if payload
if payload.is_a?(Hash)
payload['chat'] || payload['message'] && payload['message']['chat']
else
payload.try(:chat) || payload.try(:message).try!(:chat)
end
end
end
# Accessor to `'from'` field of payload. Can be overriden with `from` option

Просмотреть файл

@@ -9,17 +9,40 @@ RSpec.describe Telegram::Bot::UpdatesController::TypedUpdate do
context 'when `update` is a virtus model' do
subject { controller }
%w[
message
inline_query
chosen_inline_result
].each do |type|
unique_types = Telegram::Bot::UpdatesController::PAYLOAD_TYPES - %w[
edited_message
channel_post
edited_channel_post
]
unique_types.each do |type|
context "with #{type}" do
type_class = Telegram::Bot::Types.const_get(type.camelize)
let(:payload_type) { type }
let(:payload) { {} }
let(:payload) do
{}.tap do |result|
result[:chat] = chat if type_class.instance_methods.include?(:chat)
result[:from] = from if type_class.instance_methods.include?(:from)
end
end
let(:chat) { {id: 'chat_id'} }
let(:from) { {id: 'from_id'} }
its(:payload_type) { should eq payload_type }
its(:payload) { should be_instance_of type_class }
if type_class.instance_methods.include?(:chat)
# Virtus does not support ==. :(
its(:chat) { should be_instance_of Telegram::Bot::Types::Chat }
its('chat.to_hash') { should include chat }
else
its(:chat) { should eq nil }
end
if type_class.instance_methods.include?(:from)
its(:from) { should be_instance_of Telegram::Bot::Types::User }
its('from.to_hash') { should include from }
else
its(:from) { should eq nil }
end
end
end
end