зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-07 19:35:52 +03:00
Simplified .wrap method, added for Botan
Этот коммит содержится в:
@@ -30,6 +30,8 @@ module Telegram
|
|||||||
autoload :Botan, 'telegram/bot/botan'
|
autoload :Botan, 'telegram/bot/botan'
|
||||||
autoload :Client, 'telegram/bot/client'
|
autoload :Client, 'telegram/bot/client'
|
||||||
autoload :ClientStub, 'telegram/bot/client_stub'
|
autoload :ClientStub, 'telegram/bot/client_stub'
|
||||||
|
autoload :DebugClient, 'telegram/bot/debug_client'
|
||||||
|
autoload :Initializers, 'telegram/bot/initializers'
|
||||||
autoload :Middleware, 'telegram/bot/middleware'
|
autoload :Middleware, 'telegram/bot/middleware'
|
||||||
autoload :UpdatesController, 'telegram/bot/updates_controller'
|
autoload :UpdatesController, 'telegram/bot/updates_controller'
|
||||||
autoload :UpdatesPoller, 'telegram/bot/updates_poller'
|
autoload :UpdatesPoller, 'telegram/bot/updates_poller'
|
||||||
|
|||||||
@@ -6,13 +6,20 @@ module Telegram
|
|||||||
autoload :ControllerHelpers, 'telegram/bot/botan/controller_helpers'
|
autoload :ControllerHelpers, 'telegram/bot/botan/controller_helpers'
|
||||||
class Error < Bot::Error; end
|
class Error < Bot::Error; end
|
||||||
|
|
||||||
|
extend Initializers
|
||||||
include DebugClient
|
include DebugClient
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def by_id(id)
|
||||||
|
Telegram.botans[id]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
attr_reader :client, :token
|
attr_reader :client, :token
|
||||||
|
|
||||||
def initialize(token)
|
def initialize(token = nil, **options)
|
||||||
@client = HTTPClient.new
|
@client = HTTPClient.new
|
||||||
@token = token
|
@token = token || options[:token]
|
||||||
end
|
end
|
||||||
|
|
||||||
def track(event, uid, payload = {})
|
def track(event, uid, payload = {})
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ require 'json'
|
|||||||
require 'httpclient'
|
require 'httpclient'
|
||||||
require 'active_support/core_ext/string/inflections'
|
require 'active_support/core_ext/string/inflections'
|
||||||
require 'active_support/core_ext/hash/keys'
|
require 'active_support/core_ext/hash/keys'
|
||||||
require 'telegram/bot/debug_client'
|
|
||||||
|
|
||||||
module Telegram
|
module Telegram
|
||||||
module Bot
|
module Bot
|
||||||
@@ -10,23 +9,12 @@ module Telegram
|
|||||||
URL_TEMPLATE = 'https://api.telegram.org/bot%s/'.freeze
|
URL_TEMPLATE = 'https://api.telegram.org/bot%s/'.freeze
|
||||||
|
|
||||||
autoload :TypedResponse, 'telegram/bot/client/typed_response'
|
autoload :TypedResponse, 'telegram/bot/client/typed_response'
|
||||||
|
extend Initializers
|
||||||
include DebugClient
|
include DebugClient
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
# Accepts different options to initialize bot.
|
def by_id(id)
|
||||||
def wrap(input)
|
Telegram.bots[id]
|
||||||
case input
|
|
||||||
when self then input
|
|
||||||
when Array then input.map(&method(__callee__))
|
|
||||||
when Hash then
|
|
||||||
input = input.stringify_keys
|
|
||||||
new input['token'], input['username'], botan: input['botan']
|
|
||||||
when Symbol
|
|
||||||
Telegram.bots[input] or
|
|
||||||
raise "Bot #{input} not configured, check Telegram.bots_config."
|
|
||||||
else
|
|
||||||
new(input)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Prepend TypedResponse module.
|
# Prepend TypedResponse module.
|
||||||
@@ -45,12 +33,11 @@ module Telegram
|
|||||||
|
|
||||||
attr_reader :client, :token, :username, :base_uri, :botan
|
attr_reader :client, :token, :username, :base_uri, :botan
|
||||||
|
|
||||||
def initialize(token, username = nil, botan: nil)
|
def initialize(token = nil, username = nil, botan: nil, **options)
|
||||||
@client = HTTPClient.new
|
@client = HTTPClient.new
|
||||||
@token = token
|
@token = token || options[:token]
|
||||||
@username = username
|
@username = username || options[:username]
|
||||||
@base_uri = format URL_TEMPLATE, token
|
@base_uri = format URL_TEMPLATE, self.token
|
||||||
@botan = Botan.new(botan) if botan
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def request(action, body = {}) # rubocop:disable PerceivedComplexity
|
def request(action, body = {}) # rubocop:disable PerceivedComplexity
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ module Telegram
|
|||||||
if self == ClientStub || !ClientStub.stub_all?
|
if self == ClientStub || !ClientStub.stub_all?
|
||||||
super
|
super
|
||||||
else
|
else
|
||||||
ClientStub.new(args[1])
|
ClientStub.new(*args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -34,8 +34,8 @@ module Telegram
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(username = nil)
|
def initialize(token = nil, username = nil, **options)
|
||||||
@username = username
|
@username = username || options[:username] || token
|
||||||
reset
|
reset
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,11 @@ module Telegram
|
|||||||
@bot ||= bots[:default]
|
@bot ||= bots[:default]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Hash of botan clients made from #bots.
|
||||||
|
def botans
|
||||||
|
@botans ||= bots.transform_values(&:botan)
|
||||||
|
end
|
||||||
|
|
||||||
# Returns config for .bots method. By default uses `telegram['bots']` section
|
# Returns config for .bots method. By default uses `telegram['bots']` section
|
||||||
# from `secrets.yml` merging `telegram['bot']` at `:default` key.
|
# from `secrets.yml` merging `telegram['bot']` at `:default` key.
|
||||||
#
|
#
|
||||||
@@ -52,6 +57,7 @@ module Telegram
|
|||||||
@bots = nil
|
@bots = nil
|
||||||
@bot = nil
|
@bot = nil
|
||||||
@bots_config = nil
|
@bots_config = nil
|
||||||
|
@botans = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
19
lib/telegram/bot/initializers.rb
Обычный файл
19
lib/telegram/bot/initializers.rb
Обычный файл
@@ -0,0 +1,19 @@
|
|||||||
|
module Telegram
|
||||||
|
module Bot
|
||||||
|
module Initializers
|
||||||
|
# Accepts different options to initialize bot.
|
||||||
|
def wrap(input, **options)
|
||||||
|
case input
|
||||||
|
when Symbol then by_id(input) or raise "#{name} #{input.inspect} not configured"
|
||||||
|
when self then input
|
||||||
|
when Hash then new(**input.symbolize_keys, **options)
|
||||||
|
else new(input, **options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def by_id(_id)
|
||||||
|
raise 'Not implemented'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -17,6 +17,8 @@ require 'telegram/bot'
|
|||||||
require 'telegram/bot/updates_controller/rspec_helpers'
|
require 'telegram/bot/updates_controller/rspec_helpers'
|
||||||
require 'telegram/bot/types'
|
require 'telegram/bot/types'
|
||||||
|
|
||||||
|
Dir[GEM_ROOT.join('spec/support/**/*.rb')].each { |f| require f }
|
||||||
|
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
config.expect_with :rspec do |expectations|
|
config.expect_with :rspec do |expectations|
|
||||||
# This option will default to `true` in RSpec 4. It makes the `description`
|
# This option will default to `true` in RSpec 4. It makes the `description`
|
||||||
|
|||||||
65
spec/support/examples/initializers.rb
Обычный файл
65
spec/support/examples/initializers.rb
Обычный файл
@@ -0,0 +1,65 @@
|
|||||||
|
RSpec.shared_examples 'initializers' do |config_method = :bots|
|
||||||
|
describe '.wrap' do
|
||||||
|
subject { described_class.wrap(input, **options) }
|
||||||
|
let(:options) { {} }
|
||||||
|
let(:result) { double(:result) }
|
||||||
|
let(:username) { 'username' }
|
||||||
|
|
||||||
|
context 'when input is a string' do
|
||||||
|
let(:input) { token }
|
||||||
|
|
||||||
|
it 'treats string as token' do
|
||||||
|
expect(described_class).to receive(:new).with(token, {}) { result }
|
||||||
|
should eq result
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'and additional options are given' do
|
||||||
|
let(:options) { {id: :test} }
|
||||||
|
|
||||||
|
it 'passes them to initializer' do
|
||||||
|
expect(described_class).to receive(:new).with(input, **options) { result }
|
||||||
|
should eq result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when input is a hash' do
|
||||||
|
let(:input) { {token: token, 'username' => username, other: :options} }
|
||||||
|
|
||||||
|
it 'passes it with symbolized keys' do
|
||||||
|
expect(described_class).to receive(:new).with(**input.symbolize_keys) { result }
|
||||||
|
should eq result
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'and additional options are given' do
|
||||||
|
let(:options) { {id: :test} }
|
||||||
|
|
||||||
|
it 'passes them to initializer' do
|
||||||
|
expect(described_class).to receive(:new).
|
||||||
|
with(**input.symbolize_keys, **options) { result }
|
||||||
|
should eq result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when input is an instance of described_class' do
|
||||||
|
let!(:input) { instance }
|
||||||
|
|
||||||
|
it 'returns input' do
|
||||||
|
expect(described_class).to_not receive(:new)
|
||||||
|
should eq input
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when input is a Symbol' do
|
||||||
|
let(:input) { :client_1 }
|
||||||
|
before { allow(Telegram).to receive(config_method) { {client_1: instance} } }
|
||||||
|
it { should eq Telegram.send(config_method)[:client_1] }
|
||||||
|
|
||||||
|
context 'and there is no such bot' do
|
||||||
|
let(:input) { :invalid }
|
||||||
|
it { expect { subject }.to raise_error(/not configured/) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
20
spec/telegram/bot/botan_spec.rb
Обычный файл
20
spec/telegram/bot/botan_spec.rb
Обычный файл
@@ -0,0 +1,20 @@
|
|||||||
|
RSpec.describe Telegram::Bot::Botan do
|
||||||
|
let(:instance) { described_class.new 'token' }
|
||||||
|
let(:token) { 'token' }
|
||||||
|
|
||||||
|
include_examples 'initializers', :botans
|
||||||
|
|
||||||
|
describe '.new' do
|
||||||
|
subject { described_class.new(*args) }
|
||||||
|
|
||||||
|
context 'when usual args are given' do
|
||||||
|
let(:args) { ['secret'] }
|
||||||
|
its(:token) { should eq args[0] }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when options are given' do
|
||||||
|
let(:args) { [token: 'secret'] }
|
||||||
|
its(:token) { should eq args[0][:token] }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -3,71 +3,7 @@ RSpec.describe Telegram::Bot::Client do
|
|||||||
let(:token) { 'token' }
|
let(:token) { 'token' }
|
||||||
let(:botan_token) { double(:botan_token) }
|
let(:botan_token) { double(:botan_token) }
|
||||||
|
|
||||||
describe '.wrap' do
|
include_examples 'initializers'
|
||||||
subject { described_class.wrap(input) }
|
|
||||||
let(:result) { double(:result) }
|
|
||||||
let(:username) { 'username' }
|
|
||||||
|
|
||||||
context 'when input is a string' do
|
|
||||||
let(:input) { token }
|
|
||||||
|
|
||||||
it 'treats string as token' do
|
|
||||||
expect(described_class).to receive(:new).with(token) { result }
|
|
||||||
should eq result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when input is a hash' do
|
|
||||||
let(:input) { {token: token, username: username, ignore: :ignore} }
|
|
||||||
|
|
||||||
it 'extracts token & username' do
|
|
||||||
expect(described_class).to receive(:new).
|
|
||||||
with(token, username, botan: nil) { result }
|
|
||||||
should eq result
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when `botan` is given' do
|
|
||||||
let(:input) { super().merge(botan: botan_token) }
|
|
||||||
|
|
||||||
it 'passes it to initializer' do
|
|
||||||
expect(described_class).to receive(:new).
|
|
||||||
with(token, username, botan: botan_token) { result }
|
|
||||||
should eq result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when input is an instance of described_class' do
|
|
||||||
let!(:input) { instance }
|
|
||||||
|
|
||||||
it 'returns input' do
|
|
||||||
expect(described_class).to_not receive(:new)
|
|
||||||
should eq input
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when input is a Symbol' do
|
|
||||||
let(:input) { :bot_1 }
|
|
||||||
before { allow(Telegram).to receive(:bots) { {bot_1: instance} } }
|
|
||||||
it { should eq Telegram.bots[:bot_1] }
|
|
||||||
|
|
||||||
context 'and there is no such bot' do
|
|
||||||
let(:input) { :invalid }
|
|
||||||
it { expect { subject }.to raise_error(/not configured/) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when input is an array' do
|
|
||||||
let!(:input) { ['other_token', instance, token: token, username: username] }
|
|
||||||
let(:result_2) { double(:result_2) }
|
|
||||||
|
|
||||||
it 'calls wrap for every element' do
|
|
||||||
expect(described_class).to receive(:new).with('other_token') { result }
|
|
||||||
expect(described_class).to receive(:new).with(token, username, botan: nil) { result_2 }
|
|
||||||
should eq [result, instance, result_2]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '.prepare_body' do
|
describe '.prepare_body' do
|
||||||
subject { described_class.prepare_body(input) }
|
subject { described_class.prepare_body(input) }
|
||||||
@@ -88,6 +24,24 @@ RSpec.describe Telegram::Bot::Client do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '.new' do
|
||||||
|
subject { described_class.new(*args) }
|
||||||
|
|
||||||
|
context 'when multiple args are given' do
|
||||||
|
let(:args) { %w(secret superbot) }
|
||||||
|
its(:token) { should eq args[0] }
|
||||||
|
its(:username) { should eq args[1] }
|
||||||
|
its(:base_uri) { should include args[0] }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when hash is given' do
|
||||||
|
let(:args) { [token: 'secret', username: 'superbot'] }
|
||||||
|
its(:token) { should eq args[0][:token] }
|
||||||
|
its(:username) { should eq args[0][:username] }
|
||||||
|
its(:base_uri) { should include args[0][:token] }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#botan' do
|
describe '#botan' do
|
||||||
subject { instance.botan }
|
subject { instance.botan }
|
||||||
it { should eq nil }
|
it { should eq nil }
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
RSpec.describe Telegram::Bot::ClientStub do
|
RSpec.describe Telegram::Bot::ClientStub do
|
||||||
describe '#stub_all!' do
|
describe '#stub_all!' do
|
||||||
let(:client) { Telegram::Bot::Client.new('token', 'bot_name') }
|
let(:client) { Telegram::Bot::Client.new('token', 'bot_name') }
|
||||||
let(:clients) { Telegram::Bot::Client.wrap(['token', token: 'token2']) }
|
let(:clients) { ['token', token: 'token2'].map(&Telegram::Bot::Client.method(:wrap)) }
|
||||||
|
|
||||||
shared_examples 'constructors' do |expected_class|
|
shared_examples 'constructors' do |expected_class|
|
||||||
it 'makes Client.new return ClientStub' do
|
it 'makes Client.new return ClientStub' do
|
||||||
@@ -9,7 +9,7 @@ RSpec.describe Telegram::Bot::ClientStub do
|
|||||||
expect(client.username).to eq 'bot_name'
|
expect(client.username).to eq 'bot_name'
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'makes Client.wrap raturn ClientStub' do
|
it 'makes Client.wrap return ClientStub' do
|
||||||
expect(clients).to contain_exactly instance_of(expected_class),
|
expect(clients).to contain_exactly instance_of(expected_class),
|
||||||
instance_of(expected_class)
|
instance_of(expected_class)
|
||||||
end
|
end
|
||||||
@@ -35,4 +35,19 @@ RSpec.describe Telegram::Bot::ClientStub do
|
|||||||
include_examples 'constructors', Telegram::Bot::Client
|
include_examples 'constructors', Telegram::Bot::Client
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#new' do
|
||||||
|
subject { described_class.new(*args) }
|
||||||
|
|
||||||
|
context 'when only username is given' do
|
||||||
|
let(:args) { 'superbot' }
|
||||||
|
its(:username) { should eq args }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when username and token are given' do
|
||||||
|
let(:args) { %w(token superbot) }
|
||||||
|
its(:token) { should eq nil }
|
||||||
|
its(:username) { should eq args[1] }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user