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

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

..

3 Коммитов

Автор SHA1 Сообщение Дата
Max Melentiev
2d5ca981ce Rails 5.1 deep symbolized secrets support
Fixes #26
2017-04-22 12:02:59 +03:00
Max Melentiev
cf9bd87cf2 Add notice about how to require gem to Readme
Fixes #24
2017-03-11 21:33:20 +03:00
Max Melentiev
48ff7a4f43 Fix poller for typed responses
Fixes: #22
2017-03-05 17:56:32 +03:00
9 изменённых файлов: 67 добавлений и 11 удалений

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

@@ -15,6 +15,7 @@ Style/DotPosition: {EnforcedStyle: trailing}
Style/FirstParameterIndentation: {EnforcedStyle: consistent}
Style/IfUnlessModifier: {Enabled: false}
Style/ModuleFunction: {Enabled: false}
Style/MultilineMethodCallIndentation: {EnforcedStyle: indented}
Style/MultilineOperationIndentation: {EnforcedStyle: indented}
Style/NestedParenthesizedCalls: {Enabled: false}
Style/PredicateName: {Enabled: false}

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

@@ -1,3 +1,11 @@
# 0.11.2
- Rails 5.1 deep symbolized secrets support.
# 0.11.1
- Fixed poller for typed response.
# 0.11.0
- Remove Bot::StaleChat in favor of Bot::Forbidden, as Telegram adds more

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

@@ -43,6 +43,12 @@ Or install it yourself as:
$ gem install telegram-bot
Require if necessary:
```ruby
require 'telegram/bot'
```
## Usage
### Configuration

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

@@ -1,5 +1,6 @@
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
module Bot
@@ -48,9 +49,10 @@ module Telegram
def bots_config
@bots_config ||=
if defined?(Rails.application)
telegram_config = Rails.application.secrets[:telegram] || {}
(telegram_config['bots'] || {}).symbolize_keys.tap do |config|
default = telegram_config['bot']
secrets = Rails.application.secrets.
fetch(:telegram, {}).with_indifferent_access
secrets.fetch(:bots, {}).symbolize_keys.tap do |config|
default = secrets[:bot]
config[:default] = default if default
end
else

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

@@ -65,14 +65,16 @@ module Telegram
def fetch_updates
response = bot.async(false) { bot.get_updates(offset: offset, timeout: timeout) }
return unless response['ok'] && response['result'].any?
updates = response.is_a?(Array) ? response : response['result']
return unless updates && updates.any?
reload! do
response['result'].each do |update|
updates.each do |update|
@offset = update['update_id'] + 1
yield update
end
end
rescue Timeout::Error # rubocop:disable HandleExceptions
rescue Timeout::Error
log { 'Fetch timeout' }
end
def reload!

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

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

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

@@ -16,6 +16,7 @@ $LOAD_PATH.unshift GEM_ROOT.join('lib')
require 'telegram/bot'
require 'telegram/bot/updates_controller/rspec_helpers'
require 'telegram/bot/types'
require 'active_support/core_ext/object/json'
Dir[GEM_ROOT.join('spec/support/**/*.rb')].each { |f| require f }

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

@@ -87,6 +87,12 @@ RSpec.describe Telegram::Bot::ConfigMethods do
end
it { should include default: config[:bot] }
it { should include config[:bots] }
context 'on rails >5.1 (deep symbolized keys)' do
let(:secrets) { {telegram: config.deep_symbolize_keys} }
it { should include default: config[:bot] }
it { should include config[:bots] }
end
end
end
end

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

@@ -1,9 +1,39 @@
RSpec.describe Telegram::Bot::UpdatesPoller do
describe '#initialize' do
subject { described_class.new bot, controller }
let(:bot) { double }
let(:controller) { double }
let(:instance) { described_class.new(bot, controller) }
let(:bot) { Telegram::Bot::Client.new('token') }
let(:controller) { double(:controller) }
describe '#initialize' do
subject { instance }
it { should be }
end
describe '#fetch_updates' do
subject { -> { instance.fetch_updates(&block) } }
let(:block) { ->(x) { expect(x).to eq expected_results.shift } }
let(:results) { [{update_id: 12}, {update_id: 34}] }
let(:expected_results) { results.as_json }
let(:request_result) { {ok: true, result: results}.as_json }
before do
allow(bot).to receive(:get_updates) do
expect(bot.async).to be_falsy
request_result
end
end
it { should change(instance, :offset).to(results.last[:update_id] + 1) }
it { should change { expected_results }.to([]) }
context 'with typed response' do
let(:request_result) { results.as_json.map { |x| Telegram::Bot::Types::Update.new(x) } }
let(:expected_results) { request_result.dup }
it { should change(instance, :offset).to(results.last[:update_id] + 1) }
it { should change { expected_results }.to([]) }
end
context 'when bot is in async mode' do
let(:bot) { Telegram::Bot::Client.new('token', async: Class.new) }
it { should change { expected_results }.to([]) }
end
end
end