diff --git a/lib/push.rb b/lib/push.rb index 1efb443..b520006 100644 --- a/lib/push.rb +++ b/lib/push.rb @@ -124,8 +124,15 @@ class AlgoliaSearchJekyllPush < Jekyll::Command index.set_settings(settings) end + # Change the User-Agent header to isolate calls from this plugin + def set_user_agent_header + version = Gem::Specification.load('algoliasearch-jekyll.gemspec').version + Algolia.set_extra_header('User-Agent', "Algolia for Jekyll #{version}") + end + # Create an index to push our data def create_index(index_name) + set_user_agent_header index = Algolia::Index.new(index_name) configure_index(index) unless @is_dry_run index diff --git a/spec/push_spec.rb b/spec/push_spec.rb index 45507d4..50f1064 100644 --- a/spec/push_spec.rb +++ b/spec/push_spec.rb @@ -184,6 +184,29 @@ describe(AlgoliaSearchJekyllPush) do end end + describe 'set_user_agent_header' do + it 'should set a User-Agent with the plugin name and version' do + # Given + expected = '48.1516.2342' + spec = Gem::Specification.load('algoliasearch-jekyll.gemspec') + spec.version = expected + allow(Algolia).to receive(:set_extra_header) + + # When + push.set_user_agent_header + + # Then + expect(Algolia).to have_received(:set_extra_header).with( + 'User-Agent', + /Jekyll/ + ) + expect(Algolia).to have_received(:set_extra_header).with( + 'User-Agent', + /#{expected}/ + ) + end + end + describe 'push' do let(:index_double) { double('Algolia Index').as_null_object } let(:config) do @@ -199,6 +222,7 @@ describe(AlgoliaSearchJekyllPush) do # Mock all calls to not send anything allow_any_instance_of(AlgoliaSearchCredentialChecker) .to receive(:assert_valid) + allow(Algolia).to receive(:set_extra_header) allow(Algolia).to receive(:init) allow(Algolia).to receive(:move_index) allow(Algolia::Index).to receive(:new).and_return(index_double) diff --git a/todo b/todo new file mode 100644 index 0000000..31b55ca --- /dev/null +++ b/todo @@ -0,0 +1,2 @@ +Add User-Agent when indexing +