From f654071c4cf19d09e41824bd384c374d1f9de318 Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Fri, 27 Nov 2020 09:37:55 +0000 Subject: [PATCH] Drop Initializer module It was required for Botan support. --- lib/telegram/bot.rb | 1 - lib/telegram/bot/client.rb | 12 ++++- lib/telegram/bot/initializers.rb | 21 --------- spec/support/examples/initializers.rb | 65 --------------------------- spec/telegram/bot/client_spec.rb | 65 ++++++++++++++++++++++++++- 5 files changed, 75 insertions(+), 89 deletions(-) delete mode 100644 lib/telegram/bot/initializers.rb delete mode 100644 spec/support/examples/initializers.rb diff --git a/lib/telegram/bot.rb b/lib/telegram/bot.rb index f6c1ffc..50d4e3b 100644 --- a/lib/telegram/bot.rb +++ b/lib/telegram/bot.rb @@ -25,7 +25,6 @@ module Telegram 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 :RSpec, 'telegram/bot/rspec' autoload :Tasks, 'telegram/bot/tasks' diff --git a/lib/telegram/bot/client.rb b/lib/telegram/bot/client.rb index 9ff861e..beb32ee 100644 --- a/lib/telegram/bot/client.rb +++ b/lib/telegram/bot/client.rb @@ -1,3 +1,4 @@ +require 'active_support/core_ext/hash/keys' require 'json' require 'httpclient' @@ -7,7 +8,6 @@ module Telegram URL_TEMPLATE = 'https://api.telegram.org/bot%s/'.freeze autoload :TypedResponse, 'telegram/bot/client/typed_response' - extend Initializers prepend Async include DebugClient @@ -15,6 +15,16 @@ module Telegram include ApiHelper class << self + # 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) Telegram.bots[id] end diff --git a/lib/telegram/bot/initializers.rb b/lib/telegram/bot/initializers.rb deleted file mode 100644 index 3dfb08e..0000000 --- a/lib/telegram/bot/initializers.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'active_support/core_ext/hash/keys' - -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 diff --git a/spec/support/examples/initializers.rb b/spec/support/examples/initializers.rb deleted file mode 100644 index 552c985..0000000 --- a/spec/support/examples/initializers.rb +++ /dev/null @@ -1,65 +0,0 @@ -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 diff --git a/spec/telegram/bot/client_spec.rb b/spec/telegram/bot/client_spec.rb index 0a82bd3..1b0ae06 100644 --- a/spec/telegram/bot/client_spec.rb +++ b/spec/telegram/bot/client_spec.rb @@ -2,9 +2,72 @@ RSpec.describe Telegram::Bot::Client do let(:instance) { described_class.new 'token' } let(:token) { 'token' } - include_examples 'initializers' it_behaves_like 'async', request_args: -> { [double(:action), {body: :content}] } + 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(:bots) { {client_1: instance} } } + it { should eq Telegram.bots[:client_1] } + + context 'and there is no such bot' do + let(:input) { :invalid } + it { expect { subject }.to raise_error(/not configured/) } + end + end + end + describe '.prepare_body' do subject { described_class.prepare_body(input) }