From 85ff07de9bed522f424cceee094f6db4122a860a Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Tue, 9 Aug 2016 15:27:27 +0600 Subject: [PATCH] Extract Botan-related code from Client to Botan::ClientHelpers --- lib/telegram/bot/botan.rb | 1 + lib/telegram/bot/botan/client_helpers.rb | 15 +++++++++++ lib/telegram/bot/client.rb | 5 ++-- .../telegram/bot/botan/client_helpers_spec.rb | 25 +++++++++++++++++++ spec/telegram/bot/client_spec.rb | 11 -------- 5 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 lib/telegram/bot/botan/client_helpers.rb create mode 100644 spec/telegram/bot/botan/client_helpers_spec.rb diff --git a/lib/telegram/bot/botan.rb b/lib/telegram/bot/botan.rb index e74b8ca..34d53e9 100644 --- a/lib/telegram/bot/botan.rb +++ b/lib/telegram/bot/botan.rb @@ -3,6 +3,7 @@ module Telegram class Botan TRACK_URI = 'https://api.botan.io/track'.freeze + autoload :ClientHelpers, 'telegram/bot/botan/client_helpers' autoload :ControllerHelpers, 'telegram/bot/botan/controller_helpers' class Error < Bot::Error; end diff --git a/lib/telegram/bot/botan/client_helpers.rb b/lib/telegram/bot/botan/client_helpers.rb new file mode 100644 index 0000000..c44ca8d --- /dev/null +++ b/lib/telegram/bot/botan/client_helpers.rb @@ -0,0 +1,15 @@ +module Telegram + module Bot + class Botan + # Helpers for botan.io metrics. + module ClientHelpers + attr_reader :botan + + def initialize(*, botan: nil, **) + super + @botan = Botan.wrap(botan) if botan + end + end + end + end +end diff --git a/lib/telegram/bot/client.rb b/lib/telegram/bot/client.rb index 0daa33a..b35d216 100644 --- a/lib/telegram/bot/client.rb +++ b/lib/telegram/bot/client.rb @@ -10,6 +10,7 @@ module Telegram autoload :TypedResponse, 'telegram/bot/client/typed_response' extend Initializers + prepend Botan::ClientHelpers include DebugClient class << self @@ -31,9 +32,9 @@ module Telegram end end - attr_reader :client, :token, :username, :base_uri, :botan + attr_reader :client, :token, :username, :base_uri - def initialize(token = nil, username = nil, botan: nil, **options) + def initialize(token = nil, username = nil, **options) @client = HTTPClient.new @token = token || options[:token] @username = username || options[:username] diff --git a/spec/telegram/bot/botan/client_helpers_spec.rb b/spec/telegram/bot/botan/client_helpers_spec.rb new file mode 100644 index 0000000..a2e3cb5 --- /dev/null +++ b/spec/telegram/bot/botan/client_helpers_spec.rb @@ -0,0 +1,25 @@ +RSpec.describe Telegram::Bot::Client do + let(:instance) { described_class.new(*client_args) } + let(:token) { 'token' } + let(:botan_token) { double(:botan_token) } + let(:client_id) { 'client_id' } + let(:client_args) { [token] } + + describe '#botan' do + subject { instance.botan } + it { should eq nil } + + context 'when botan token is set' do + let(:client_args) { [token, id: client_id, async: Class.new, botan: botan_token] } + it { should be_instance_of Telegram::Bot::Botan } + its(:token) { should eq botan_token } + end + + context 'when botan is configured with hash' do + let(:client_args) { [token, id: client_id, async: Class.new, botan: botan_config] } + let(:botan_config) { {token: botan_token, async: Class.new} } + it { should be_instance_of Telegram::Bot::Botan } + its(:token) { should eq botan_token } + end + end +end diff --git a/spec/telegram/bot/client_spec.rb b/spec/telegram/bot/client_spec.rb index ed92fdc..41f0b38 100644 --- a/spec/telegram/bot/client_spec.rb +++ b/spec/telegram/bot/client_spec.rb @@ -41,15 +41,4 @@ RSpec.describe Telegram::Bot::Client do its(:base_uri) { should include args[0][:token] } end end - - describe '#botan' do - subject { instance.botan } - it { should eq nil } - - context 'when botan token is set' do - let(:instance) { described_class.new token, botan: botan_token } - it { should be_instance_of Telegram::Bot::Botan } - its(:token) { should eq botan_token } - end - end end