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

Extracted typed response mappings to telegram-bot-types gem

Fixes #75
Этот коммит содержится в:
Max Melentiev
2018-04-25 11:03:38 +03:00
родитель bdff6b2e1c
Коммит 5603016e4a
9 изменённых файлов: 37 добавлений и 34 удалений

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

@@ -1,5 +1,8 @@
# Unreleased
- Extracted typed response mappings to telegram-bot-types gem.
It now provides definitions for all API v3.5 methods.
# 0.13.0
- `rescue_from`.

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

@@ -8,7 +8,7 @@ group :development do
gem 'pry-byebug', '~> 3.2.0'
gem 'sdoc', '~> 0.4.1'
gem 'telegram-bot-types', '~> 0.3.0'
gem 'telegram-bot-types', '~> 0.6.0'
gem 'rspec', '~> 3.5.0'
gem 'rspec-its', '~> 1.1.0'

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

@@ -10,7 +10,7 @@ group :development do
gem "pry", "~> 0.10.1"
gem "pry-byebug", "~> 3.2.0"
gem "sdoc", "~> 0.4.1"
gem "telegram-bot-types", "~> 0.3.0"
gem "telegram-bot-types", "~> 0.6.0"
gem "rspec", "~> 3.5.0"
gem "rspec-its", "~> 1.1.0"
gem "rspec-rails", "~> 3.5.0"

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

@@ -10,7 +10,7 @@ group :development do
gem "pry", "~> 0.10.1"
gem "pry-byebug", "~> 3.2.0"
gem "sdoc", "~> 0.4.1"
gem "telegram-bot-types", "~> 0.3.0"
gem "telegram-bot-types", "~> 0.6.0"
gem "rspec", "~> 3.5.0"
gem "rspec-its", "~> 1.1.0"
gem "rspec-rails", "~> 3.5.0"

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

@@ -10,7 +10,7 @@ group :development do
gem "pry", "~> 0.10.1"
gem "pry-byebug", "~> 3.2.0"
gem "sdoc", "~> 0.4.1"
gem "telegram-bot-types", "~> 0.3.0"
gem "telegram-bot-types", "~> 0.6.0"
gem "rspec", "~> 3.5.0"
gem "rspec-its", "~> 1.1.0"
gem "rspec-rails", "~> 3.5.0"

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

@@ -10,7 +10,7 @@ group :development do
gem "pry", "~> 0.10.1"
gem "pry-byebug", "~> 3.2.0"
gem "sdoc", "~> 0.4.1"
gem "telegram-bot-types", "~> 0.3.0"
gem "telegram-bot-types", "~> 0.6.0"
gem "rspec", "~> 3.5.0"
gem "rspec-its", "~> 1.1.0"
gem "rspec-rails", "~> 3.5.0"

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

@@ -12,7 +12,7 @@ group :development do
gem "pry", "~> 0.10.1"
gem "pry-byebug", "~> 3.2.0"
gem "sdoc", "~> 0.4.1"
gem "telegram-bot-types", "~> 0.3.0"
gem "telegram-bot-types", "~> 0.6.0"
gem "rspec", "~> 3.5.0"
gem "rspec-its", "~> 1.1.0"
gem "rspec-rails", "~> 3.5.0"

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

@@ -1,35 +1,23 @@
require 'telegram/bot/client/api_helper'
require 'active_support/core_ext/string/inflections'
module Telegram
module Bot
class Client
# Actions with type-casted results. Install `telegram-bot-types` gem first.
module TypedResponse
{
getFile: :File,
getMe: :User,
getUpdates: [:Update],
getUserProfilePhotos: :UserProfilePhotos,
# First we define methods for every available api method to return `result`
# field instead of object.
ApiHelper.methods_list.each do |method|
define_method(method.to_s.underscore) do |*args|
super(*args)['result']
end
end
forwardMessage: :Message,
sendAudio: :Message,
sendDocument: :Message,
sendLocation: :Message,
sendMessage: :Message,
sendPhoto: :Message,
sendSticker: :Message,
sendVideo: :Message,
sendVoice: :Message,
}.each do |method, type|
next unless type
if type.is_a?(Array)
type_class = Types.const_get(type.first)
define_method(method.to_s.underscore) do |*args|
request(method, *args)['result'].map { |x| type_class.new(x) }
end
else
type_class = Types.const_get(type)
define_method(method.to_s.underscore) do |*args|
type_class.new request(method, *args)['result']
end
# And then override some of them which has castable results.
Types::Response::WRAPPED_METHODS.each do |method, type|
define_method(method.to_s.underscore) do |*args|
Types::Response.wrap(super(*args)['result'], type)
end
end
end

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

@@ -3,7 +3,7 @@ RSpec.describe Telegram::Bot::Client::TypedResponse do
describe '#get_me' do
subject { bot.get_me }
before { expect(bot).to receive(:request).with(:getMe) { response } }
before { expect(bot).to receive(:request).with('getMe') { response } }
let(:response) { {'ok' => true, 'result' => {'id' => user_id}} }
let(:user_id) { 123 }
it { should be_instance_of Telegram::Bot::Types::User }
@@ -17,7 +17,7 @@ RSpec.describe Telegram::Bot::Client::TypedResponse do
describe '#get_updates' do
subject { bot.get_updates }
before { expect(bot).to receive(:request).with(:getUpdates) { response } }
before { expect(bot).to receive(:request).with('getUpdates') { response } }
let(:response) { {'ok' => true, 'result' => [{'update_id' => update_id}]} }
let(:update_id) { 123 }
it { should be_instance_of Array }
@@ -29,4 +29,16 @@ RSpec.describe Telegram::Bot::Client::TypedResponse do
it { expect { subject }.to raise_error Telegram::Bot::Error }
end
end
describe '#delete_webhook' do
subject { bot.delete_webhook }
before { expect(bot).to receive(:request).with('deleteWebhook') { response } }
let(:response) { {'ok' => true, 'result' => true} }
it { should eq true }
context 'on error' do
let(:response) { raise Telegram::Bot::Error }
it { expect { subject }.to raise_error Telegram::Bot::Error }
end
end
end