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

Simplified .wrap method, added for Botan

Этот коммит содержится в:
Max Melentiev
2016-05-18 21:09:45 +03:00
родитель 91e0aa2096
Коммит 27602b20bd
11 изменённых файлов: 169 добавлений и 92 удалений

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

@@ -30,6 +30,8 @@ module Telegram
autoload :Botan, 'telegram/bot/botan'
autoload :Client, 'telegram/bot/client'
autoload :ClientStub, 'telegram/bot/client_stub'
autoload :DebugClient, 'telegram/bot/debug_client'
autoload :Initializers, 'telegram/bot/initializers'
autoload :Middleware, 'telegram/bot/middleware'
autoload :UpdatesController, 'telegram/bot/updates_controller'
autoload :UpdatesPoller, 'telegram/bot/updates_poller'

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

@@ -6,13 +6,20 @@ module Telegram
autoload :ControllerHelpers, 'telegram/bot/botan/controller_helpers'
class Error < Bot::Error; end
extend Initializers
include DebugClient
class << self
def by_id(id)
Telegram.botans[id]
end
end
attr_reader :client, :token
def initialize(token)
def initialize(token = nil, **options)
@client = HTTPClient.new
@token = token
@token = token || options[:token]
end
def track(event, uid, payload = {})

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

@@ -2,7 +2,6 @@ require 'json'
require 'httpclient'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/hash/keys'
require 'telegram/bot/debug_client'
module Telegram
module Bot
@@ -10,23 +9,12 @@ module Telegram
URL_TEMPLATE = 'https://api.telegram.org/bot%s/'.freeze
autoload :TypedResponse, 'telegram/bot/client/typed_response'
extend Initializers
include DebugClient
class << self
# Accepts different options to initialize bot.
def wrap(input)
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
def by_id(id)
Telegram.bots[id]
end
# Prepend TypedResponse module.
@@ -45,12 +33,11 @@ module Telegram
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
@token = token
@username = username
@base_uri = format URL_TEMPLATE, token
@botan = Botan.new(botan) if botan
@token = token || options[:token]
@username = username || options[:username]
@base_uri = format URL_TEMPLATE, self.token
end
def request(action, body = {}) # rubocop:disable PerceivedComplexity

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

@@ -9,7 +9,7 @@ module Telegram
if self == ClientStub || !ClientStub.stub_all?
super
else
ClientStub.new(args[1])
ClientStub.new(*args)
end
end
end
@@ -34,8 +34,8 @@ module Telegram
end
end
def initialize(username = nil)
@username = username
def initialize(token = nil, username = nil, **options)
@username = username || options[:username] || token
reset
end

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

@@ -34,6 +34,11 @@ module Telegram
@bot ||= bots[:default]
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
# from `secrets.yml` merging `telegram['bot']` at `:default` key.
#
@@ -52,6 +57,7 @@ module Telegram
@bots = nil
@bot = nil
@bots_config = nil
@botans = nil
end
end
end

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/types'
Dir[GEM_ROOT.join('spec/support/**/*.rb')].each { |f| require f }
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`

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 Обычный файл
Просмотреть файл

@@ -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(:botan_token) { double(:botan_token) }
describe '.wrap' do
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
include_examples 'initializers'
describe '.prepare_body' do
subject { described_class.prepare_body(input) }
@@ -88,6 +24,24 @@ RSpec.describe Telegram::Bot::Client do
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
subject { instance.botan }
it { should eq nil }

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

@@ -1,7 +1,7 @@
RSpec.describe Telegram::Bot::ClientStub do
describe '#stub_all!' do
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|
it 'makes Client.new return ClientStub' do
@@ -9,7 +9,7 @@ RSpec.describe Telegram::Bot::ClientStub do
expect(client.username).to eq 'bot_name'
end
it 'makes Client.wrap raturn ClientStub' do
it 'makes Client.wrap return ClientStub' do
expect(clients).to contain_exactly instance_of(expected_class),
instance_of(expected_class)
end
@@ -35,4 +35,19 @@ RSpec.describe Telegram::Bot::ClientStub do
include_examples 'constructors', Telegram::Bot::Client
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