1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-08-28 15:26:18 +03:00

Сравнить коммиты

..

10 Коммитов

Автор SHA1 Сообщение Дата
Max Melentiev
2eb2670ed2 v0.13.1 2018-05-28 13:27:35 +03:00
Max Melentiev
6a5e642470 Fix #chat for non-message updates with TypedUpdates enabled
Fixes #82
2018-05-19 12:53:15 +03:00
printercu
4ff5a58b98 Merge pull request #79 from telegram-bot-rb/fix-travis
Fix travis build
2018-05-14 13:20:35 +03:00
printercu
ffb13321b4 Fix travis build 2018-05-14 12:18:14 +03:00
printercu
3bdc4f5ae0 Merge pull request #72 from WA9ACE/patch-1
Spelling correction
2018-04-25 11:15:23 +03:00
Max Melentiev
5603016e4a Extracted typed response mappings to telegram-bot-types gem
Fixes #75
2018-04-25 11:07:34 +03:00
Caleb Albritton
fa6b9a825e Spelling correction 2018-04-13 11:50:05 -04:00
printercu
bdff6b2e1c Merge pull request #65 from telegram-bot-rb/rails-6
Prepare for rails 6
2018-02-21 21:34:49 +03:00
Max Melentiev
f39a451192 Add rails-6 to build matrix 2018-02-21 20:35:03 +03:00
Max Melentiev
9af6de8d36 Avoid Hash#transform_values. Fixes #63 2018-02-21 19:49:09 +03:00
17 изменённых файлов: 134 добавлений и 54 удалений

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

@@ -1,17 +1,34 @@
language: ruby
cache: bundler
rvm:
- 2.5
- 2.4
- 2.3
- 2.5
- 2.4
gemfile:
- gemfiles/rails_52.gemfile
- gemfiles/rails_51.gemfile
- gemfiles/rails_50.gemfile
- gemfiles/rails_42.gemfile
- gemfiles/rails_52.gemfile
- gemfiles/rails_51.gemfile
matrix:
include:
# old rails
- rvm: 2.5
gemfile: gemfiles/rails_50.gemfile
- rvm: 2.5
gemfile: gemfiles/rails_42.gemfile
# old rubies
- rvm: 2.3
gemfile: gemfiles/rails_52.gemfile
# future rails
- rvm: 2.5
gemfile: gemfiles/rails_60.gemfile
# Allow rails 6 to fail while master branch is used
allow_failures:
- rvm: 2.5
gemfile: gemfiles/rails_60.gemfile
notifications:
email: false
# for 2.5.0 until 2.5.1 is released: https://github.com/travis-ci/travis-ci/issues/8978
# for 2.5.0 until 2.5.1 is released:
# https://github.com/travis-ci/travis-ci/issues/8978
# https://github.com/travis-ci/travis-ci/issues/8969#issuecomment-354135622
before_install:
- gem update --system
- gem install bundler

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

@@ -1,3 +1,10 @@
appraise 'rails-60' do
git 'https://github.com/rails/rails.git' do
gem 'actionpack', '~> 6.0.0.alpha'
gem 'railties', '~> 6.0.0.alpha'
end
end
appraise 'rails-52' do
gem 'actionpack', '~> 5.2.0.beta2'
gem 'railties', '~> 5.2.0.beta2'

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

@@ -1,4 +1,8 @@
# Unreleased
# 0.13.1
- 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

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

@@ -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'

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

@@ -11,7 +11,7 @@ in development, but you can use poller in production if you want.
Package contains:
- Ligthweight client for bot API (with fast and thread-safe
- Lightweight client for bot API (with fast and thread-safe
[httpclient](https://github.com/nahi/httpclient) under the hood).
- Controller with message parser: define methods for commands, not `case` branches.
- Middleware and routes helpers for production env.

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

@@ -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"

23
gemfiles/rails_60.gemfile Обычный файл
Просмотреть файл

@@ -0,0 +1,23 @@
# This file was generated by Appraisal
source "https://rubygems.org"
git "https://github.com/rails/rails.git" do
gem "actionpack", "~> 6.0.0.alpha"
gem "railties", "~> 6.0.0.alpha"
end
group :development do
gem "appraisal", "~> 2.2"
gem "pry", "~> 0.10.1"
gem "pry-byebug", "~> 3.2.0"
gem "sdoc", "~> 0.4.1"
gem "telegram-bot-types", "~> 0.6.0"
gem "rspec", "~> 3.5.0"
gem "rspec-its", "~> 1.1.0"
gem "rspec-rails", "~> 3.5.0"
gem "rubocop", "~> 0.52.1"
gem "coveralls", "~> 0.8.2", require: false
end
gemspec path: "../"

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

@@ -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

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

@@ -1,5 +1,4 @@
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/hash/transform_values'
require 'active_support/core_ext/hash/indifferent_access'
module Telegram
@@ -43,7 +42,7 @@ module Telegram
# Hash of botan clients made from #bots.
def botans
@botans ||= bots.transform_values(&:botan)
@botans ||= bots.map { |k, v| [k, v.botan] }.to_h
end
# Returns config for .bots method. By default uses `telegram['bots']` section

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

@@ -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

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

@@ -35,7 +35,7 @@ RSpec.shared_context 'telegram/bot/updates_controller' do
def deep_stringify(input)
case input
when Array then input.map(&method(__callee__))
when Hash then input.transform_keys(&:to_s).transform_values(&method(__callee__))
when Hash then input.map { |k, v| [k.to_s, deep_stringify(v)] }.to_h
else input
end
end

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

@@ -1,6 +1,6 @@
module Telegram
module Bot
VERSION = '0.13.0'.freeze
VERSION = '0.13.1'.freeze
def self.gem_version
Gem::Version.new VERSION

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

@@ -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

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

@@ -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