refactor(codeclimate): Split long check_credentials method

This commit is contained in:
Pixelastic 2015-07-17 18:31:26 +02:00
parent 61057c77d9
commit a87d9ac4ea
3 changed files with 71 additions and 56 deletions

View File

@ -6,11 +6,12 @@ gem 'json', '~> 1.8'
gem 'nokogiri', '~> 1.6' gem 'nokogiri', '~> 1.6'
group :development do group :development do
gem 'coveralls', '~> 0.8'
gem 'flog', '~> 4.3'
gem 'guard-rspec', '~> 4.6' gem 'guard-rspec', '~> 4.6'
gem 'jekyll', '~> 2.5' # Jekyll custom commands only available from 2.5 gem 'jekyll', '~> 2.5' # Jekyll custom commands only available from 2.5
gem 'jeweler', '~> 2.0' gem 'jeweler', '~> 2.0'
gem 'rspec', '~> 3.0' gem 'rspec', '~> 3.0'
gem 'rubocop', '~> 0.31' gem 'rubocop', '~> 0.31'
gem 'simplecov', '~> 0.10' gem 'simplecov', '~> 0.10'
gem 'coveralls', '~> 0.8'
end end

View File

@ -83,10 +83,9 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
nil nil
end end
# Check that all credentials are present, and stop with a helpfull message # Check that the API key is available
# if not def check_api_key
def check_credentials return if api_key
unless api_key
Jekyll.logger.error 'Algolia Error: No API key defined' Jekyll.logger.error 'Algolia Error: No API key defined'
Jekyll.logger.warn ' You have two ways to configure your API key:' Jekyll.logger.warn ' You have two ways to configure your API key:'
Jekyll.logger.warn ' - The ALGOLIA_API_KEY environment variable' Jekyll.logger.warn ' - The ALGOLIA_API_KEY environment variable'
@ -95,7 +94,9 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
exit 1 exit 1
end end
unless @config['algolia'] && @config['algolia']['application_id'] # Check that the application id is defined
def check_application_id
return if @config['algolia'] && @config['algolia']['application_id']
Jekyll.logger.error 'Algolia Error: No application ID defined' Jekyll.logger.error 'Algolia Error: No application ID defined'
Jekyll.logger.warn ' Please set your application id in the '\ Jekyll.logger.warn ' Please set your application id in the '\
'_config.yml file, like so:' '_config.yml file, like so:'
@ -110,7 +111,9 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
exit 1 exit 1
end end
unless @config['algolia']['index_name'] # Check that the index name is defined
def check_index_name
return if @config['algolia'] && @config['algolia']['index_name']
Jekyll.logger.error 'Algolia Error: No index name defined' Jekyll.logger.error 'Algolia Error: No index name defined'
Jekyll.logger.warn ' Please set your index name in the _config.yml'\ Jekyll.logger.warn ' Please set your index name in the _config.yml'\
' file, like so:' ' file, like so:'
@ -123,6 +126,19 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
Jekyll.logger.warn ' https://www.algolia.com/explorer' Jekyll.logger.warn ' https://www.algolia.com/explorer'
exit 1 exit 1
end end
# Check that all credentials are present
# Stop with a helpful message if not
def check_credentials
check_api_key
check_application_id
check_index_name
Algolia.init(
application_id: @config['algolia']['application_id'],
api_key: api_key
)
nil nil
end end
@ -162,10 +178,6 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
def push(items) def push(items)
check_credentials check_credentials
Algolia.init(
application_id: @config['algolia']['application_id'],
api_key: api_key
)
# Create a temporary index # Create a temporary index
index_name = @config['algolia']['index_name'] index_name = @config['algolia']['index_name']

View File

@ -188,6 +188,22 @@ describe(AlgoliaSearchJekyllPush) do
expect(Jekyll.logger).to receive(:warn).at_least(:once) expect(Jekyll.logger).to receive(:warn).at_least(:once)
expect(-> { push.check_credentials }).to raise_error SystemExit expect(-> { push.check_credentials }).to raise_error SystemExit
end end
it 'should init the Algolia client' do
# Given
push.init_options(nil, options, config)
stub_const('ENV', 'ALGOLIA_API_KEY' => 'APIKEY_FROM_ENV')
allow(Algolia).to receive(:init)
# When
push.check_credentials
# Then
expect(Algolia).to have_received(:init).with(
application_id: 'APPID',
api_key: 'APIKEY_FROM_ENV'
)
end
end end
describe 'configure_index' do describe 'configure_index' do
@ -286,20 +302,6 @@ describe(AlgoliaSearchJekyllPush) do
allow(Jekyll.logger).to receive(:info) allow(Jekyll.logger).to receive(:info)
end end
it 'should init the Algolia client' do
# Given
stub_const('ENV', 'ALGOLIA_API_KEY' => 'APIKEY_FROM_ENV')
# When
push.push(items)
# Then
expect(Algolia).to have_received(:init).with(
application_id: 'APPID',
api_key: 'APIKEY_FROM_ENV'
)
end
it 'should create a temporary index' do it 'should create a temporary index' do
# Given # Given