From 27602b20bd4c16be60dbe152b612998635b1379a Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Wed, 18 May 2016 21:09:45 +0300 Subject: [PATCH] Simplified .wrap method, added for Botan --- lib/telegram/bot.rb | 2 + lib/telegram/bot/botan.rb | 11 +++- lib/telegram/bot/client.rb | 27 +++------ lib/telegram/bot/client_stub.rb | 6 +- lib/telegram/bot/config_methods.rb | 6 ++ lib/telegram/bot/initializers.rb | 19 ++++++ spec/spec_helper.rb | 2 + spec/support/examples/initializers.rb | 65 +++++++++++++++++++++ spec/telegram/bot/botan_spec.rb | 20 +++++++ spec/telegram/bot/client_spec.rb | 84 ++++++--------------------- spec/telegram/bot/client_stub_spec.rb | 19 +++++- 11 files changed, 169 insertions(+), 92 deletions(-) create mode 100644 lib/telegram/bot/initializers.rb create mode 100644 spec/support/examples/initializers.rb create mode 100644 spec/telegram/bot/botan_spec.rb diff --git a/lib/telegram/bot.rb b/lib/telegram/bot.rb index 69557e0..a51694d 100644 --- a/lib/telegram/bot.rb +++ b/lib/telegram/bot.rb @@ -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' diff --git a/lib/telegram/bot/botan.rb b/lib/telegram/bot/botan.rb index 05ef2d2..e74b8ca 100644 --- a/lib/telegram/bot/botan.rb +++ b/lib/telegram/bot/botan.rb @@ -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 = {}) diff --git a/lib/telegram/bot/client.rb b/lib/telegram/bot/client.rb index ac9a05b..0daa33a 100644 --- a/lib/telegram/bot/client.rb +++ b/lib/telegram/bot/client.rb @@ -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 diff --git a/lib/telegram/bot/client_stub.rb b/lib/telegram/bot/client_stub.rb index 5a590b9..7269815 100644 --- a/lib/telegram/bot/client_stub.rb +++ b/lib/telegram/bot/client_stub.rb @@ -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 diff --git a/lib/telegram/bot/config_methods.rb b/lib/telegram/bot/config_methods.rb index eb70be7..ea55ec1 100644 --- a/lib/telegram/bot/config_methods.rb +++ b/lib/telegram/bot/config_methods.rb @@ -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 diff --git a/lib/telegram/bot/initializers.rb b/lib/telegram/bot/initializers.rb new file mode 100644 index 0000000..b0f9c71 --- /dev/null +++ b/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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 459e2f2..cbf60f6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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` diff --git a/spec/support/examples/initializers.rb b/spec/support/examples/initializers.rb new file mode 100644 index 0000000..552c985 --- /dev/null +++ b/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 diff --git a/spec/telegram/bot/botan_spec.rb b/spec/telegram/bot/botan_spec.rb new file mode 100644 index 0000000..809d69d --- /dev/null +++ b/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 diff --git a/spec/telegram/bot/client_spec.rb b/spec/telegram/bot/client_spec.rb index 786531a..ed92fdc 100644 --- a/spec/telegram/bot/client_spec.rb +++ b/spec/telegram/bot/client_spec.rb @@ -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 } diff --git a/spec/telegram/bot/client_stub_spec.rb b/spec/telegram/bot/client_stub_spec.rb index 64af69f..59390e2 100644 --- a/spec/telegram/bot/client_stub_spec.rb +++ b/spec/telegram/bot/client_stub_spec.rb @@ -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