refactor(codeclimate): Split long check_credentials method
This commit is contained in:
parent
61057c77d9
commit
a87d9ac4ea
3
Gemfile
3
Gemfile
@ -6,11 +6,12 @@ gem 'json', '~> 1.8'
|
||||
gem 'nokogiri', '~> 1.6'
|
||||
|
||||
group :development do
|
||||
gem 'coveralls', '~> 0.8'
|
||||
gem 'flog', '~> 4.3'
|
||||
gem 'guard-rspec', '~> 4.6'
|
||||
gem 'jekyll', '~> 2.5' # Jekyll custom commands only available from 2.5
|
||||
gem 'jeweler', '~> 2.0'
|
||||
gem 'rspec', '~> 3.0'
|
||||
gem 'rubocop', '~> 0.31'
|
||||
gem 'simplecov', '~> 0.10'
|
||||
gem 'coveralls', '~> 0.8'
|
||||
end
|
||||
|
||||
94
lib/push.rb
94
lib/push.rb
@ -83,46 +83,62 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
|
||||
nil
|
||||
end
|
||||
|
||||
# Check that all credentials are present, and stop with a helpfull message
|
||||
# if not
|
||||
# Check that the API key is available
|
||||
def check_api_key
|
||||
return if api_key
|
||||
Jekyll.logger.error 'Algolia Error: No API key defined'
|
||||
Jekyll.logger.warn ' You have two ways to configure your API key:'
|
||||
Jekyll.logger.warn ' - The ALGOLIA_API_KEY environment variable'
|
||||
Jekyll.logger.warn ' - A file named ./_algolia_api_key in your '\
|
||||
'source folder'
|
||||
exit 1
|
||||
end
|
||||
|
||||
# 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.warn ' Please set your application id in the '\
|
||||
'_config.yml file, like so:'
|
||||
Jekyll.logger.warn ''
|
||||
# The spaces are needed otherwise the text is centered
|
||||
Jekyll.logger.warn ' algolia: '
|
||||
Jekyll.logger.warn ' application_id: \'{your_application_id}\''
|
||||
Jekyll.logger.warn ''
|
||||
Jekyll.logger.warn ' Your application ID can be found in your algolia'\
|
||||
' dashboard'
|
||||
Jekyll.logger.warn ' https://www.algolia.com/licensing'
|
||||
exit 1
|
||||
end
|
||||
|
||||
# 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.warn ' Please set your index name in the _config.yml'\
|
||||
' file, like so:'
|
||||
Jekyll.logger.warn ''
|
||||
# The spaces are needed otherwise the text is centered
|
||||
Jekyll.logger.warn ' algolia: '
|
||||
Jekyll.logger.warn ' index_name: \'{your_index_name}\''
|
||||
Jekyll.logger.warn ''
|
||||
Jekyll.logger.warn ' You can edit your indices in your dashboard'
|
||||
Jekyll.logger.warn ' https://www.algolia.com/explorer'
|
||||
exit 1
|
||||
end
|
||||
|
||||
# Check that all credentials are present
|
||||
# Stop with a helpful message if not
|
||||
def check_credentials
|
||||
unless api_key
|
||||
Jekyll.logger.error 'Algolia Error: No API key defined'
|
||||
Jekyll.logger.warn ' You have two ways to configure your API key:'
|
||||
Jekyll.logger.warn ' - The ALGOLIA_API_KEY environment variable'
|
||||
Jekyll.logger.warn ' - A file named ./_algolia_api_key in your '\
|
||||
'source folder'
|
||||
exit 1
|
||||
end
|
||||
check_api_key
|
||||
check_application_id
|
||||
check_index_name
|
||||
|
||||
unless @config['algolia'] && @config['algolia']['application_id']
|
||||
Jekyll.logger.error 'Algolia Error: No application ID defined'
|
||||
Jekyll.logger.warn ' Please set your application id in the '\
|
||||
'_config.yml file, like so:'
|
||||
Jekyll.logger.warn ''
|
||||
# The spaces are needed otherwise the text is centered
|
||||
Jekyll.logger.warn ' algolia: '
|
||||
Jekyll.logger.warn ' application_id: \'{your_application_id}\''
|
||||
Jekyll.logger.warn ''
|
||||
Jekyll.logger.warn ' Your application ID can be found in your algolia'\
|
||||
' dashboard'
|
||||
Jekyll.logger.warn ' https://www.algolia.com/licensing'
|
||||
exit 1
|
||||
end
|
||||
Algolia.init(
|
||||
application_id: @config['algolia']['application_id'],
|
||||
api_key: api_key
|
||||
)
|
||||
|
||||
unless @config['algolia']['index_name']
|
||||
Jekyll.logger.error 'Algolia Error: No index name defined'
|
||||
Jekyll.logger.warn ' Please set your index name in the _config.yml'\
|
||||
' file, like so:'
|
||||
Jekyll.logger.warn ''
|
||||
# The spaces are needed otherwise the text is centered
|
||||
Jekyll.logger.warn ' algolia: '
|
||||
Jekyll.logger.warn ' index_name: \'{your_index_name}\''
|
||||
Jekyll.logger.warn ''
|
||||
Jekyll.logger.warn ' You can edit your indices in your dashboard'
|
||||
Jekyll.logger.warn ' https://www.algolia.com/explorer'
|
||||
exit 1
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
@ -162,10 +178,6 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
|
||||
|
||||
def push(items)
|
||||
check_credentials
|
||||
Algolia.init(
|
||||
application_id: @config['algolia']['application_id'],
|
||||
api_key: api_key
|
||||
)
|
||||
|
||||
# Create a temporary index
|
||||
index_name = @config['algolia']['index_name']
|
||||
|
||||
@ -188,6 +188,22 @@ describe(AlgoliaSearchJekyllPush) do
|
||||
expect(Jekyll.logger).to receive(:warn).at_least(:once)
|
||||
expect(-> { push.check_credentials }).to raise_error SystemExit
|
||||
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
|
||||
|
||||
describe 'configure_index' do
|
||||
@ -286,20 +302,6 @@ describe(AlgoliaSearchJekyllPush) do
|
||||
allow(Jekyll.logger).to receive(:info)
|
||||
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
|
||||
# Given
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user