Skip to content
This repository has been archived by the owner on Mar 21, 2018. It is now read-only.

Commit

Permalink
feat(user-agent): Send the plugin name and version in the UA
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelastic committed Jul 28, 2015
1 parent c0db6f4 commit ecd4c67
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,15 @@ def configure_index(index)
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
Expand Down
24 changes: 24 additions & 0 deletions spec/push_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,29 @@ def push.custom_hook_excluded_file?(_file)
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
Expand All @@ -199,6 +222,7 @@ def push.custom_hook_excluded_file?(_file)
# 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)
Expand Down
2 changes: 2 additions & 0 deletions todo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add User-Agent when indexing

0 comments on commit ecd4c67

Please sign in to comment.