algoliasearch-jekyll 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.coveralls.yml +1 -0
  3. data/.travis.yml +11 -0
  4. data/Gemfile +4 -1
  5. data/README.md +8 -0
  6. data/Rakefile +2 -1
  7. data/VERSION +1 -1
  8. data/algoliasearch-jekyll.gemspec +129 -0
  9. data/lib/algoliasearch-jekyll.rb +13 -1
  10. data/lib/credential_checker.rb +74 -0
  11. data/lib/push.rb +52 -89
  12. data/lib/record_extractor.rb +1 -0
  13. data/scripts/check_flay +30 -0
  14. data/scripts/check_flog +31 -0
  15. data/scripts/git_hooks/pre-commit +2 -1
  16. data/scripts/git_hooks/pre-push +6 -1
  17. data/spec/credential_checker_spec.rb +116 -0
  18. data/spec/fixtures/_config.yml +9 -4
  19. data/spec/fixtures/_layouts/default.html +6 -0
  20. data/spec/fixtures/_posts/2015-07-03-test-post-again.md +7 -0
  21. data/spec/fixtures/_site/2015/07/02/test-post.html +26 -0
  22. data/spec/fixtures/_site/2015/07/03/test-post-again.html +3 -0
  23. data/spec/fixtures/_site/about.html +31 -0
  24. data/spec/fixtures/_site/assets/ring.png +0 -0
  25. data/spec/fixtures/_site/authors.html +1 -0
  26. data/spec/fixtures/_site/excluded.html +1 -0
  27. data/spec/fixtures/_site/hierarchy.html +31 -0
  28. data/spec/fixtures/_site/index.html +17 -0
  29. data/spec/fixtures/_site/my-collection/collection-item.html +2 -0
  30. data/spec/fixtures/_site/page2/index.html +40 -0
  31. data/spec/fixtures/_site/weight.html +15 -0
  32. data/spec/fixtures/index.html +13 -0
  33. data/spec/push_spec.rb +134 -113
  34. data/spec/record_extractor_spec.rb +10 -0
  35. data/spec/spec_helper.rb +21 -11
  36. data/spec/spec_helper_simplecov.rb +1 -1
  37. data/txt/api_key_missing +4 -0
  38. data/txt/application_id_missing +8 -0
  39. data/txt/index_name_missing +9 -0
  40. metadata +69 -5
  41. data/scripts/run_tests +0 -2
  42. data/scripts/update_gem +0 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cde772c27a05882aba2a47f5c0fa626440ad7755
4
- data.tar.gz: d0c6dafb3d6b3ade9a35057f6c91b9c6b3c3bfdc
3
+ metadata.gz: 098e21fa10bcd28c1713731c361914e37a7287d5
4
+ data.tar.gz: 754759e48284cf27db5b8f01ef2ce3a5d0377a60
5
5
  SHA512:
6
- metadata.gz: ad814fa1f1eee3d87a6407740d9aad6501874a86b395bda116c0ddeb82d35dae9ed17c87bbb59d98c11fc32c5c75a52a187838b8a8bf7714ec158c0b9a8137a0
7
- data.tar.gz: 43d778da3ad04dec941d1f46e7a1943aeb8e60e771171243f9f502994d52a18325d26a85497f5919ccf2437c57e1c0b5fd1400ddaab0e7b8bf883650628f9b2a
6
+ metadata.gz: 146538e8edb1c89aab54a66f73acc3574118156953df6ae5238738bf9663cc6ef9ab57dea63d2e8823f3d01112793bbea0245d79fd8dc595d0bf83132036dd62
7
+ data.tar.gz: 12b8d1c7138319af9cae98188f8789ffab6a888859561179bda157acd36bbae72f58358a9eeeeec7aa9e2eec90166422a74de602b16b5cd87f37311d8b11b332
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ cache: bundler
3
+ before_script: bundle update
4
+ rvm:
5
+ - 2.2
6
+ - 2.1
7
+ - 2.0
8
+ notifications:
9
+ email:
10
+ on_success: never
11
+ on_failure: never
data/Gemfile CHANGED
@@ -6,8 +6,11 @@ gem 'json', '~> 1.8'
6
6
  gem 'nokogiri', '~> 1.6'
7
7
 
8
8
  group :development do
9
+ gem 'coveralls', '~> 0.8'
10
+ gem 'flay', '~> 2.6'
11
+ gem 'flog', '~> 4.3'
9
12
  gem 'guard-rspec', '~> 4.6'
10
- gem 'jekyll', '~> 2.5'
13
+ gem 'jekyll', '~> 2.5' # Jekyll custom commands only available from 2.5
11
14
  gem 'jeweler', '~> 2.0'
12
15
  gem 'rspec', '~> 3.0'
13
16
  gem 'rubocop', '~> 0.31'
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # Algolia Jekyll Plugin
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/algoliasearch-jekyll.svg)](http://badge.fury.io/rb/algoliasearch-jekyll)
4
+ [![Build Status](https://travis-ci.org/algolia/algoliasearch-jekyll.svg?branch=master)](https://travis-ci.org/algolia/algoliasearch-jekyll)
5
+ [![Coverage Status](https://coveralls.io/repos/algolia/algoliasearch-jekyll/badge.svg?branch=master&service=github)](https://coveralls.io/github/algolia/algoliasearch-jekyll?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/algolia/algoliasearch-jekyll/badges/gpa.svg)](https://codeclimate.com/github/algolia/algoliasearch-jekyll)
4
7
 
5
8
  Jekyll plugin to automatically index your Jekyll posts and pages into an
6
9
  Algolia index by simply running `jekyll algolia push`.
@@ -131,6 +134,11 @@ class AlgoliaSearchRecordExtractor
131
134
  end
132
135
  ```
133
136
 
137
+ ## Dependencies
138
+
139
+ The `algoliasearch-jekyll` plugin works form versions of Jekyll starting from
140
+ 2.5, with a version of Ruby of at least 2.0.
141
+
134
142
  ## Searching
135
143
 
136
144
  This plugin will only index your data in your Algolia index. Adding search
data/Rakefile CHANGED
@@ -23,6 +23,7 @@ Jeweler::Tasks.new do |gem|
23
23
  '`jekyll algolia push`'
24
24
  gem.email = 'tim@pixelastic.com'
25
25
  gem.authors = ['Tim Carry']
26
+
26
27
  # dependencies defined in Gemfile
27
28
  end
28
29
  Jeweler::RubygemsDotOrgTasks.new
@@ -30,7 +31,7 @@ Jeweler::RubygemsDotOrgTasks.new
30
31
  require 'rspec/core'
31
32
  require 'rspec/core/rake_task'
32
33
  RSpec::Core::RakeTask.new(:spec) do |spec|
33
- # spec.rspec_opts = '--color --format documentation'
34
+ spec.rspec_opts = '--color --format documentation'
34
35
  spec.pattern = FileList['spec/**/*_spec.rb']
35
36
  end
36
37
  task test: :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.3.0
@@ -0,0 +1,129 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: algoliasearch-jekyll 0.3.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "algoliasearch-jekyll"
9
+ s.version = "0.3.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Tim Carry"]
14
+ s.date = "2015-07-21"
15
+ s.description = "Index all your pages and posts to an Algolia index with `jekyll algolia push`"
16
+ s.email = "tim@pixelastic.com"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".coveralls.yml",
23
+ ".rspec",
24
+ ".rubocop.yml",
25
+ ".travis.yml",
26
+ "Gemfile",
27
+ "Guardfile",
28
+ "LICENSE.txt",
29
+ "README.md",
30
+ "Rakefile",
31
+ "VERSION",
32
+ "algoliasearch-jekyll.gemspec",
33
+ "lib/algoliasearch-jekyll.rb",
34
+ "lib/credential_checker.rb",
35
+ "lib/push.rb",
36
+ "lib/record_extractor.rb",
37
+ "scripts/check_flay",
38
+ "scripts/check_flog",
39
+ "scripts/git_hooks/pre-commit",
40
+ "scripts/git_hooks/pre-push",
41
+ "spec/credential_checker_spec.rb",
42
+ "spec/fixtures/_config.yml",
43
+ "spec/fixtures/_layouts/default.html",
44
+ "spec/fixtures/_my-collection/collection-item.html",
45
+ "spec/fixtures/_my-collection/collection-item.md",
46
+ "spec/fixtures/_posts/2015-07-02-test-post.md",
47
+ "spec/fixtures/_posts/2015-07-03-test-post-again.md",
48
+ "spec/fixtures/_site/2015/07/02/test-post.html",
49
+ "spec/fixtures/_site/2015/07/03/test-post-again.html",
50
+ "spec/fixtures/_site/about.html",
51
+ "spec/fixtures/_site/assets/ring.png",
52
+ "spec/fixtures/_site/authors.html",
53
+ "spec/fixtures/_site/excluded.html",
54
+ "spec/fixtures/_site/hierarchy.html",
55
+ "spec/fixtures/_site/index.html",
56
+ "spec/fixtures/_site/my-collection/collection-item.html",
57
+ "spec/fixtures/_site/page2/index.html",
58
+ "spec/fixtures/_site/weight.html",
59
+ "spec/fixtures/about.md",
60
+ "spec/fixtures/api_key_dir/_algolia_api_key",
61
+ "spec/fixtures/assets/ring.png",
62
+ "spec/fixtures/authors.html",
63
+ "spec/fixtures/excluded.html",
64
+ "spec/fixtures/hierarchy.md",
65
+ "spec/fixtures/index.html",
66
+ "spec/fixtures/weight.md",
67
+ "spec/push_spec.rb",
68
+ "spec/record_extractor_spec.rb",
69
+ "spec/spec_helper.rb",
70
+ "spec/spec_helper_jekyll.rb",
71
+ "spec/spec_helper_simplecov.rb",
72
+ "txt/api_key_missing",
73
+ "txt/application_id_missing",
74
+ "txt/index_name_missing"
75
+ ]
76
+ s.homepage = "https://github.com/algolia/algoliasearch-jekyll"
77
+ s.licenses = ["MIT"]
78
+ s.rubygems_version = "2.4.8"
79
+ s.summary = "AlgoliaSearch for Jekyll"
80
+
81
+ if s.respond_to? :specification_version then
82
+ s.specification_version = 4
83
+
84
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
85
+ s.add_runtime_dependency(%q<algoliasearch>, ["~> 1.4"])
86
+ s.add_runtime_dependency(%q<awesome_print>, ["~> 1.6"])
87
+ s.add_runtime_dependency(%q<json>, ["~> 1.8"])
88
+ s.add_runtime_dependency(%q<nokogiri>, ["~> 1.6"])
89
+ s.add_development_dependency(%q<coveralls>, ["~> 0.8"])
90
+ s.add_development_dependency(%q<flay>, ["~> 2.6"])
91
+ s.add_development_dependency(%q<flog>, ["~> 4.3"])
92
+ s.add_development_dependency(%q<guard-rspec>, ["~> 4.6"])
93
+ s.add_development_dependency(%q<jekyll>, ["~> 2.5"])
94
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0"])
95
+ s.add_development_dependency(%q<rspec>, ["~> 3.0"])
96
+ s.add_development_dependency(%q<rubocop>, ["~> 0.31"])
97
+ s.add_development_dependency(%q<simplecov>, ["~> 0.10"])
98
+ else
99
+ s.add_dependency(%q<algoliasearch>, ["~> 1.4"])
100
+ s.add_dependency(%q<awesome_print>, ["~> 1.6"])
101
+ s.add_dependency(%q<json>, ["~> 1.8"])
102
+ s.add_dependency(%q<nokogiri>, ["~> 1.6"])
103
+ s.add_dependency(%q<coveralls>, ["~> 0.8"])
104
+ s.add_dependency(%q<flay>, ["~> 2.6"])
105
+ s.add_dependency(%q<flog>, ["~> 4.3"])
106
+ s.add_dependency(%q<guard-rspec>, ["~> 4.6"])
107
+ s.add_dependency(%q<jekyll>, ["~> 2.5"])
108
+ s.add_dependency(%q<jeweler>, ["~> 2.0"])
109
+ s.add_dependency(%q<rspec>, ["~> 3.0"])
110
+ s.add_dependency(%q<rubocop>, ["~> 0.31"])
111
+ s.add_dependency(%q<simplecov>, ["~> 0.10"])
112
+ end
113
+ else
114
+ s.add_dependency(%q<algoliasearch>, ["~> 1.4"])
115
+ s.add_dependency(%q<awesome_print>, ["~> 1.6"])
116
+ s.add_dependency(%q<json>, ["~> 1.8"])
117
+ s.add_dependency(%q<nokogiri>, ["~> 1.6"])
118
+ s.add_dependency(%q<coveralls>, ["~> 0.8"])
119
+ s.add_dependency(%q<flay>, ["~> 2.6"])
120
+ s.add_dependency(%q<flog>, ["~> 4.3"])
121
+ s.add_dependency(%q<guard-rspec>, ["~> 4.6"])
122
+ s.add_dependency(%q<jekyll>, ["~> 2.5"])
123
+ s.add_dependency(%q<jeweler>, ["~> 2.0"])
124
+ s.add_dependency(%q<rspec>, ["~> 3.0"])
125
+ s.add_dependency(%q<rubocop>, ["~> 0.31"])
126
+ s.add_dependency(%q<simplecov>, ["~> 0.10"])
127
+ end
128
+ end
129
+
@@ -20,8 +20,16 @@ class AlgoliaSearchJekyll < Jekyll::Command
20
20
  add_build_options(subcommand)
21
21
 
22
22
  subcommand.action do |args, options|
23
+ default_options = {
24
+ 'dry_run' => false,
25
+ 'verbose' => false
26
+ }
27
+ options = default_options.merge(options)
23
28
  @config = configuration_from_options(options)
24
- AlgoliaSearchJekyllPush.init_options(args, options, @config).process
29
+
30
+ AlgoliaSearchJekyllPush.init_options(args, options, @config)
31
+ .jekyll_new(@config)
32
+ .process
25
33
  end
26
34
  end
27
35
  end
@@ -38,6 +46,10 @@ class AlgoliaSearchJekyll < Jekyll::Command
38
46
  'Index posts in the _drafts folder'
39
47
  command.option 'unpublished', '--unpublished',
40
48
  'Index posts that were marked as unpublished'
49
+ command.option 'dry_run', '--dry-run', '-n',
50
+ 'Do a dry run, do not push records'
51
+ command.option 'verbose', '--verbose',
52
+ 'Display more information on what is indexed'
41
53
  end
42
54
  end
43
55
  end
@@ -0,0 +1,74 @@
1
+ require 'algoliasearch'
2
+ require 'nokogiri'
3
+ require 'json'
4
+
5
+ # Given an HTML file as input, will return an array of records to index
6
+ class AlgoliaSearchCredentialChecker
7
+ attr_accessor :config
8
+
9
+ def initialize(config)
10
+ @config = config
11
+ end
12
+
13
+ # Read the API key either from ENV or from an _algolia_api_key file in
14
+ # source folder
15
+ def api_key
16
+ # First read in ENV
17
+ return ENV['ALGOLIA_API_KEY'] if ENV['ALGOLIA_API_KEY']
18
+
19
+ # Otherwise from file in source directory
20
+ key_file = File.join(@config['source'], '_algolia_api_key')
21
+ if File.exist?(key_file) && File.size(key_file) > 0
22
+ return File.open(key_file).read.strip
23
+ end
24
+ nil
25
+ end
26
+
27
+ def display_error(file)
28
+ file = File.expand_path(File.join(File.dirname(__FILE__), '../txt', file))
29
+ content = File.open(file).readlines.map(&:chomp)
30
+ content.each_with_index do |line, index|
31
+ if index == 0
32
+ Jekyll.logger.error line
33
+ next
34
+ end
35
+ Jekyll.logger.warn line
36
+ end
37
+ end
38
+
39
+ # Check that the API key is available
40
+ def check_api_key
41
+ return if api_key
42
+ display_error('api_key_missing')
43
+ exit 1
44
+ end
45
+
46
+ # Check that the application id is defined
47
+ def check_application_id
48
+ return if @config['algolia'] && @config['algolia']['application_id']
49
+ display_error('application_id_missing')
50
+ exit 1
51
+ end
52
+
53
+ # Check that the index name is defined
54
+ def check_index_name
55
+ return if @config['algolia'] && @config['algolia']['index_name']
56
+ display_error('index_name_missing')
57
+ exit 1
58
+ end
59
+
60
+ # Check that all credentials are present
61
+ # Stop with a helpful message if not
62
+ def assert_valid
63
+ check_api_key
64
+ check_application_id
65
+ check_index_name
66
+
67
+ Algolia.init(
68
+ application_id: @config['algolia']['application_id'],
69
+ api_key: api_key
70
+ )
71
+
72
+ nil
73
+ end
74
+ end
data/lib/push.rb CHANGED
@@ -2,6 +2,7 @@ require 'algoliasearch'
2
2
  require 'nokogiri'
3
3
  require 'json'
4
4
  require_relative './record_extractor.rb'
5
+ require_relative './credential_checker.rb'
5
6
 
6
7
  # `jekyll algolia push` command
7
8
  class AlgoliaSearchJekyllPush < Jekyll::Command
@@ -19,10 +20,9 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
19
20
  @args = args
20
21
  @options = options
21
22
  @config = config
23
+ @is_verbose = @config['verbose']
24
+ @is_dry_run = @config['dry_run']
22
25
 
23
- # Allow for passing index name on the command line
24
- index_name = args[0]
25
- @config['algolia']['index_name'] = index_name if index_name
26
26
  self
27
27
  end
28
28
 
@@ -41,22 +41,39 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
41
41
  end
42
42
  return false unless allowed_extensions.include?(extname)
43
43
 
44
- # Exclude files manually excluded from config
45
- excluded_files = @config['algolia']['excluded_files']
46
- return false if excluded_files && excluded_files.include?(basename)
44
+ return false if excluded_file?(file.path)
47
45
 
48
46
  true
49
47
  end
50
48
 
51
- # Run the default `jekyll build` command but overwrite the actual "write
52
- # files on disk" part to instead push data to Algolia
53
- def process
54
- site = Jekyll::Site.new(@config)
49
+ # Check if the file is in the list of excluded files
50
+ def excluded_file?(filepath)
51
+ excluded = [
52
+ %r{^page([0-9]*)/index\.html}
53
+ ]
54
+ if @config['algolia']
55
+ excluded += (@config['algolia']['excluded_files'] || [])
56
+ end
57
+
58
+ excluded.each do |pattern|
59
+ pattern = /#{Regexp.quote(pattern)}/ if pattern.is_a? String
60
+ return true if filepath =~ pattern
61
+ end
62
+ false
63
+ end
55
64
 
65
+ # Return a patched version of a Jekyll instance
66
+ def jekyll_new(config)
67
+ site = Jekyll::Site.new(config)
68
+
69
+ # Patched version of `write` that will push to Algolia instead of writing
70
+ # on disk
56
71
  def site.write
57
72
  items = []
73
+ is_verbose = config['verbose']
58
74
  each_site_file do |file|
59
75
  next unless AlgoliaSearchJekyllPush.indexable?(file)
76
+ Jekyll.logger.info "Extracting data from #{file.path}" if is_verbose
60
77
  new_items = AlgoliaSearchRecordExtractor.new(file).extract
61
78
  next if new_items.nil?
62
79
 
@@ -65,66 +82,7 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
65
82
  AlgoliaSearchJekyllPush.push(items)
66
83
  end
67
84
 
68
- # This will call the build command by default, which will in turn call our
69
- # custom .write method
70
- site.process
71
- end
72
-
73
- # Read the API key either from ENV or from an _algolia_api_key file in
74
- # source folder
75
- def api_key
76
- # First read in ENV
77
- return ENV['ALGOLIA_API_KEY'] if ENV['ALGOLIA_API_KEY']
78
-
79
- # Otherwise from file in source directory
80
- key_file = File.join(@config['source'], '_algolia_api_key')
81
- if File.exist?(key_file) && File.size(key_file) > 0
82
- return File.open(key_file).read.strip
83
- end
84
- nil
85
- end
86
-
87
- # Check that all credentials are present, and stop with a helpfull message
88
- # if not
89
- def check_credentials
90
- unless api_key
91
- Jekyll.logger.error 'Algolia Error: No API key defined'
92
- Jekyll.logger.warn ' You have two ways to configure your API key:'
93
- Jekyll.logger.warn ' - The ALGOLIA_API_KEY environment variable'
94
- Jekyll.logger.warn ' - A file named ./_algolia_api_key in your '\
95
- 'source folder'
96
- exit 1
97
- end
98
-
99
- unless @config['algolia'] && @config['algolia']['application_id']
100
- Jekyll.logger.error 'Algolia Error: No application ID defined'
101
- Jekyll.logger.warn ' Please set your application id in the '\
102
- '_config.yml file, like so:'
103
- Jekyll.logger.warn ''
104
- # The spaces are needed otherwise the text is centered
105
- Jekyll.logger.warn ' algolia: '
106
- Jekyll.logger.warn ' application_id: \'{your_application_id}\''
107
- Jekyll.logger.warn ''
108
- Jekyll.logger.warn ' Your application ID can be found in your algolia'\
109
- ' dashboard'
110
- Jekyll.logger.warn ' https://www.algolia.com/licensing'
111
- exit 1
112
- end
113
-
114
- unless @config['algolia']['index_name']
115
- Jekyll.logger.error 'Algolia Error: No index name defined'
116
- Jekyll.logger.warn ' Please set your index name in the _config.yml'\
117
- ' file, like so:'
118
- Jekyll.logger.warn ''
119
- # The spaces are needed otherwise the text is centered
120
- Jekyll.logger.warn ' algolia: '
121
- Jekyll.logger.warn ' index_name: \'{your_index_name}\''
122
- Jekyll.logger.warn ''
123
- Jekyll.logger.warn ' You can edit your indices in your dashboard'
124
- Jekyll.logger.warn ' https://www.algolia.com/explorer'
125
- exit 1
126
- end
127
- nil
85
+ site
128
86
  end
129
87
 
130
88
  # Get index settings
@@ -154,41 +112,46 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
154
112
  }
155
113
 
156
114
  # Merge default settings with user custom ones
157
- (@config['algolia']['settings'] || []).each do |key, value|
158
- settings[key.to_sym] = value
115
+ if @config['algolia']
116
+ (@config['algolia']['settings'] || []).each do |key, value|
117
+ settings[key.to_sym] = value
118
+ end
159
119
  end
160
120
 
161
121
  index.set_settings(settings)
162
122
  end
163
123
 
164
- def push(items)
165
- check_credentials
166
- Algolia.init(
167
- application_id: @config['algolia']['application_id'],
168
- api_key: api_key
169
- )
170
-
171
- # Create a temporary index
172
- index_name = @config['algolia']['index_name']
173
- index_name_tmp = "#{index_name}_tmp"
174
- index_tmp = Algolia::Index.new(index_name_tmp)
175
- configure_index(index_tmp)
124
+ # Create an index to push our data
125
+ def create_index(index_name)
126
+ index = Algolia::Index.new(index_name)
127
+ configure_index(index) unless @is_dry_run
128
+ index
129
+ end
176
130
 
177
- # Push to temporary index
131
+ # Push records to the index
132
+ def batch_add_items(items, index)
178
133
  items.each_slice(1000) do |batch|
179
134
  Jekyll.logger.info "Indexing #{batch.size} items"
180
135
  begin
181
- index_tmp.add_objects!(batch)
136
+ index.add_objects!(batch) unless @is_dry_run
182
137
  rescue StandardError => error
183
138
  Jekyll.logger.error 'Algolia Error: HTTP Error'
184
139
  Jekyll.logger.warn error.message
185
140
  exit 1
186
-
187
141
  end
188
142
  end
143
+ end
144
+
145
+ def push(items)
146
+ AlgoliaSearchCredentialChecker.new(@config).assert_valid
189
147
 
190
- # Move temporary index to real one
191
- Algolia.move_index(index_name_tmp, index_name)
148
+ Jekyll.logger.info '=== DRY RUN ===' if @is_dry_run
149
+
150
+ # Add items to a temp index, then rename it
151
+ index_name = @config['algolia']['index_name']
152
+ index_name_tmp = "#{index_name}_tmp"
153
+ batch_add_items(items, create_index(index_name_tmp))
154
+ Algolia.move_index(index_name_tmp, index_name) unless @is_dry_run
192
155
 
193
156
  Jekyll.logger.info "Indexing of #{items.size} items " \
194
157
  "in #{index_name} done."