algoliasearch-jekyll 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5fc07f49e01332879569776774bc13b38b1fa601
4
- data.tar.gz: 05bde1d8646bb06eb04049e5fb322d35c86744c2
3
+ metadata.gz: dc842f3c5823b8a809ffcd12e6d520f15eeefc60
4
+ data.tar.gz: 3fbfef0340971043048d1e0d71b439a50727e451
5
5
  SHA512:
6
- metadata.gz: 27a25e5b66d6fbd44450897024bc837de069ae78d1142ad8d12e70def6334f593700299126875ae7bdea4dd8819d5dfee80c5832a7a31332b0d27fde5a611eb9
7
- data.tar.gz: cf69b239d17b80fac3af079474b046b929bd5d7582b4e321ad2568b83184ea7f5b24971d62bc895f8206caaa6d2a54c292898e10ac84d8ae7b02763b337c223b
6
+ metadata.gz: 8b80bcd9875ac15f96fa76a1f2cb676c6b4a38d98be4c30936710f5ffb9e6c9d843ecf9f2af6ee1b2af56cc77afa3c54b9eb8ce0f814ec1392f0df89de5067ae
7
+ data.tar.gz: 70dc1b288661933a38f5b4845d073985d74f5e7f6dc947799af2c85f2afbdebbad6c9fa732b0c3f5a7538f6f596c8338368aba55982384cd83a78eec8fcb8385
data/CONTRIBUTING.md CHANGED
@@ -34,6 +34,9 @@ I suggest updating the website `Gemfile` to point to the correct local directory
34
34
  ```ruby
35
35
  gem "algoliasearch-jekyll", :path => "/path/to/local/gem/folder"
36
36
  ```
37
+ You should also run `rake gemspec` from the `algoliasearch-jekyll` repository if
38
+ you added new files or dependencies.
39
+
37
40
 
38
41
  If you plan on submitting a PR, I suggest you install the git hooks. This will
39
42
  run pre-commit and pre-push checks. Those checks will also be run by TravisCI,
data/Gemfile CHANGED
@@ -3,8 +3,10 @@ source 'http://rubygems.org'
3
3
  gem 'algoliasearch', '~> 1.4'
4
4
  gem 'appraisal', '~> 2.1.0'
5
5
  gem 'awesome_print', '~> 1.6'
6
+ gem 'jekyll', '~> 2.5'
6
7
  gem 'json', '~> 1.8'
7
8
  gem 'nokogiri', '~> 1.6'
9
+ gem 'verbal_expressions', '~> 0.1.5'
8
10
 
9
11
  group :development do
10
12
  gem 'coveralls', '~> 0.8'
data/Guardfile CHANGED
@@ -1,17 +1,7 @@
1
- group :jekyll_v3 do
2
- guard :rspec, cmd: 'appraisal jekyll-v3 bundle exec rspec --color --format documentation' do
3
- watch(%r{^spec/.+_spec\.rb$})
4
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
5
- watch('spec/spec_helper.rb') { 'spec' }
6
- end
7
- end
8
-
9
- group :jekyll_v2 do
10
- guard :rspec, cmd: 'appraisal jekyll-v2 bundle exec rspec --color --format documentation' do
11
- watch(%r{^spec/.+_spec\.rb$})
12
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
13
- watch('spec/spec_helper.rb') { 'spec' }
14
- end
1
+ guard :rspec, cmd: 'bundle exec rspec --color --format documentation' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { 'spec' }
15
5
  end
16
6
 
17
7
  notification :off
data/README.md CHANGED
@@ -4,6 +4,7 @@
4
4
  [![Build Status][2]](https://travis-ci.org/algolia/algoliasearch-jekyll)
5
5
  [![Coverage Status][3]](https://coveralls.io/github/algolia/algoliasearch-jekyll?branch=master)
6
6
  [![Code Climate][4]](https://codeclimate.com/github/algolia/algoliasearch-jekyll)
7
+ ![Jekyll >= 2.5](https://img.shields.io/badge/jekyll-%3E%3D%202.5-green.svg)
7
8
 
8
9
  Jekyll plugin to automatically index your Jekyll posts and pages into an
9
10
  Algolia index by simply running `jekyll algolia push`.
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: algoliasearch-jekyll 0.6.0 ruby lib
5
+ # stub: algoliasearch-jekyll 0.6.1 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "algoliasearch-jekyll"
9
- s.version = "0.6.0"
9
+ s.version = "0.6.1"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Tim Carry"]
14
- s.date = "2016-01-11"
14
+ s.date = "2016-01-13"
15
15
  s.description = "Index all your pages and posts to an Algolia index with `jekyll algolia push`"
16
16
  s.email = "tim@pixelastic.com"
17
17
  s.extra_rdoc_files = [
@@ -36,12 +36,14 @@ Gem::Specification.new do |s|
36
36
  "gemfiles/jekyll_v3.gemfile",
37
37
  "lib/algoliasearch-jekyll.rb",
38
38
  "lib/credential_checker.rb",
39
+ "lib/error_handler.rb",
39
40
  "lib/push.rb",
40
41
  "lib/record_extractor.rb",
41
42
  "lib/version.rb",
42
43
  "scripts/bump_version",
43
44
  "scripts/check_flay",
44
45
  "scripts/check_flog",
46
+ "scripts/coverage",
45
47
  "scripts/git_hooks/pre-commit",
46
48
  "scripts/git_hooks/pre-push",
47
49
  "scripts/release",
@@ -53,6 +55,7 @@ Gem::Specification.new do |s|
53
55
  "scripts/watch_v2",
54
56
  "scripts/watch_v3",
55
57
  "spec/credential_checker_spec.rb",
58
+ "spec/error_handler_spec.rb",
56
59
  "spec/fixtures/jekyll_version_2/_config.yml",
57
60
  "spec/fixtures/jekyll_version_2/_layouts/default.html",
58
61
  "spec/fixtures/jekyll_version_2/_my-collection/collection-item.html",
@@ -89,7 +92,9 @@ Gem::Specification.new do |s|
89
92
  "spec/spec_helper_simplecov.rb",
90
93
  "txt/api_key_missing",
91
94
  "txt/application_id_missing",
92
- "txt/index_name_missing"
95
+ "txt/check_key_acl_to_tmp_index",
96
+ "txt/index_name_missing",
97
+ "txt/sample"
93
98
  ]
94
99
  s.homepage = "https://github.com/algolia/algoliasearch-jekyll"
95
100
  s.licenses = ["MIT"]
@@ -103,8 +108,10 @@ Gem::Specification.new do |s|
103
108
  s.add_runtime_dependency(%q<algoliasearch>, ["~> 1.4"])
104
109
  s.add_runtime_dependency(%q<appraisal>, ["~> 2.1.0"])
105
110
  s.add_runtime_dependency(%q<awesome_print>, ["~> 1.6"])
111
+ s.add_runtime_dependency(%q<jekyll>, ["~> 2.5"])
106
112
  s.add_runtime_dependency(%q<json>, ["~> 1.8"])
107
113
  s.add_runtime_dependency(%q<nokogiri>, ["~> 1.6"])
114
+ s.add_runtime_dependency(%q<verbal_expressions>, ["~> 0.1.5"])
108
115
  s.add_development_dependency(%q<coveralls>, ["~> 0.8"])
109
116
  s.add_development_dependency(%q<flay>, ["~> 2.6"])
110
117
  s.add_development_dependency(%q<flog>, ["~> 4.3"])
@@ -117,8 +124,10 @@ Gem::Specification.new do |s|
117
124
  s.add_dependency(%q<algoliasearch>, ["~> 1.4"])
118
125
  s.add_dependency(%q<appraisal>, ["~> 2.1.0"])
119
126
  s.add_dependency(%q<awesome_print>, ["~> 1.6"])
127
+ s.add_dependency(%q<jekyll>, ["~> 2.5"])
120
128
  s.add_dependency(%q<json>, ["~> 1.8"])
121
129
  s.add_dependency(%q<nokogiri>, ["~> 1.6"])
130
+ s.add_dependency(%q<verbal_expressions>, ["~> 0.1.5"])
122
131
  s.add_dependency(%q<coveralls>, ["~> 0.8"])
123
132
  s.add_dependency(%q<flay>, ["~> 2.6"])
124
133
  s.add_dependency(%q<flog>, ["~> 4.3"])
@@ -132,8 +141,10 @@ Gem::Specification.new do |s|
132
141
  s.add_dependency(%q<algoliasearch>, ["~> 1.4"])
133
142
  s.add_dependency(%q<appraisal>, ["~> 2.1.0"])
134
143
  s.add_dependency(%q<awesome_print>, ["~> 1.6"])
144
+ s.add_dependency(%q<jekyll>, ["~> 2.5"])
135
145
  s.add_dependency(%q<json>, ["~> 1.8"])
136
146
  s.add_dependency(%q<nokogiri>, ["~> 1.6"])
147
+ s.add_dependency(%q<verbal_expressions>, ["~> 0.1.5"])
137
148
  s.add_dependency(%q<coveralls>, ["~> 0.8"])
138
149
  s.add_dependency(%q<flay>, ["~> 2.6"])
139
150
  s.add_dependency(%q<flog>, ["~> 4.3"])
@@ -5,9 +5,10 @@ source "http://rubygems.org"
5
5
  gem "algoliasearch", "~> 1.4"
6
6
  gem "appraisal", "~> 2.1.0"
7
7
  gem "awesome_print", "~> 1.6"
8
+ gem "jekyll", "~> 2.5"
8
9
  gem "json", "~> 1.8"
9
10
  gem "nokogiri", "~> 1.6"
10
- gem "jekyll", "~> 2.5"
11
+ gem "verbal_expressions", "~> 0.1.5"
11
12
 
12
13
  group :development do
13
14
  gem "coveralls", "~> 0.8"
@@ -5,9 +5,10 @@ source "http://rubygems.org"
5
5
  gem "algoliasearch", "~> 1.4"
6
6
  gem "appraisal", "~> 2.1.0"
7
7
  gem "awesome_print", "~> 1.6"
8
+ gem "jekyll", "~> 3.0"
8
9
  gem "json", "~> 1.8"
9
10
  gem "nokogiri", "~> 1.6"
10
- gem "jekyll", "~> 3.0"
11
+ gem "verbal_expressions", "~> 0.1.5"
11
12
  gem "jekyll-paginate", "~> 1.1.0"
12
13
 
13
14
  group :development do
@@ -1,13 +1,15 @@
1
1
  require 'algoliasearch'
2
2
  require 'nokogiri'
3
3
  require 'json'
4
+ require_relative './error_handler.rb'
4
5
 
5
6
  # Given an HTML file as input, will return an array of records to index
6
7
  class AlgoliaSearchCredentialChecker
7
- attr_accessor :config
8
+ attr_accessor :config, :logger
8
9
 
9
10
  def initialize(config)
10
11
  @config = config
12
+ @logger = AlgoliaSearchErrorHandler.new
11
13
  end
12
14
 
13
15
  # Read the API key either from ENV or from an _algolia_api_key file in
@@ -24,36 +26,24 @@ class AlgoliaSearchCredentialChecker
24
26
  nil
25
27
  end
26
28
 
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
29
  # Check that the API key is available
40
30
  def check_api_key
41
31
  return if api_key
42
- display_error('api_key_missing')
32
+ @logger.display('api_key_missing')
43
33
  exit 1
44
34
  end
45
35
 
46
36
  # Check that the application id is defined
47
37
  def check_application_id
48
38
  return if @config['algolia'] && @config['algolia']['application_id']
49
- display_error('application_id_missing')
39
+ @logger.display('application_id_missing')
50
40
  exit 1
51
41
  end
52
42
 
53
43
  # Check that the index name is defined
54
44
  def check_index_name
55
45
  return if @config['algolia'] && @config['algolia']['index_name']
56
- display_error('index_name_missing')
46
+ @logger.display('index_name_missing')
57
47
  exit 1
58
48
  end
59
49
 
@@ -0,0 +1,87 @@
1
+ require 'json'
2
+ require 'verbal_expressions'
3
+
4
+ # Helps in displaying useful error messages to users, to help them debug their
5
+ # issues
6
+ class AlgoliaSearchErrorHandler
7
+ # Will output the specified error file.
8
+ # First line is displayed as error, next ones as warning
9
+ def display(file)
10
+ file = File.expand_path(File.join(File.dirname(__FILE__), '../txt', file))
11
+ content = File.open(file).readlines.map(&:chomp)
12
+ content.each_with_index do |line, index|
13
+ if index == 0
14
+ Jekyll.logger.error line
15
+ next
16
+ end
17
+ Jekyll.logger.warn line
18
+ end
19
+ end
20
+
21
+ def error_tester
22
+ # Ex: Cannot PUT to https://appid.algolia.net/1/indexes/index_name/settings:
23
+ # {"message":"Invalid Application-ID or API key","status":403} (403)
24
+ VerEx.new do
25
+ find 'Cannot '
26
+ capture('verb') { word }
27
+ find ' to '
28
+ capture('scheme') { word }
29
+ find '://'
30
+ capture('app_id') { word }
31
+ anything_but '/'
32
+ find '/'
33
+ capture('api_version') { digit }
34
+ find '/'
35
+ capture('api_section') { word }
36
+ find '/'
37
+ capture('index_name') { word }
38
+ find '/'
39
+ capture('api_action') { word }
40
+ find ': '
41
+ capture('json') do
42
+ find '{'
43
+ anything_but('}')
44
+ find '}'
45
+ end
46
+ find ' ('
47
+ capture('http_error') { word }
48
+ find ')'
49
+ end
50
+ end
51
+
52
+ def parse_algolia_error(error)
53
+ error.gsub!("\n", '')
54
+
55
+ tester = error_tester
56
+ matches = tester.match(error)
57
+
58
+ return false unless matches
59
+
60
+ hash = {}
61
+ matches.names.each do |match|
62
+ hash[match] = matches[match]
63
+ end
64
+
65
+ # Cast integers
66
+ hash['api_version'] = hash['api_version'].to_i
67
+ hash['http_error'] = hash['http_error'].to_i
68
+
69
+ # Parse JSON
70
+ hash['json'] = JSON.parse(hash['json'])
71
+
72
+ hash
73
+ end
74
+
75
+ # Given an Algolia API error message, will return the best error message
76
+ def readable_algolia_error(error)
77
+ error = parse_algolia_error(error)
78
+ return false unless error
79
+
80
+ # Given API key does not have rights on the _tmp index
81
+ if error['http_error'] == 403 && error['index_name'] =~ /_tmp$/
82
+ return 'check_key_acl_to_tmp_index'
83
+ end
84
+
85
+ false
86
+ end
87
+ end
data/lib/push.rb CHANGED
@@ -4,6 +4,7 @@ require 'json'
4
4
  require_relative './version'
5
5
  require_relative './record_extractor'
6
6
  require_relative './credential_checker'
7
+ require_relative './error_handler'
7
8
 
8
9
  # `jekyll algolia push` command
9
10
  class AlgoliaSearchJekyllPush < Jekyll::Command
@@ -126,7 +127,25 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
126
127
  end
127
128
  end
128
129
 
129
- index.set_settings(settings)
130
+ begin
131
+ index.set_settings(settings)
132
+ rescue StandardError => error
133
+ display_error(error)
134
+ exit 1
135
+ end
136
+ end
137
+
138
+ # Display the error in a human-friendly way if possible
139
+ def display_error(error)
140
+ error_handler = AlgoliaSearchErrorHandler.new
141
+ readable_error = error_handler.readable_algolia_error(error.message)
142
+
143
+ if readable_error
144
+ error_handler.display(readable_error)
145
+ else
146
+ Jekyll.logger.error 'Algolia Error: HTTP Error'
147
+ Jekyll.logger.warn error.message
148
+ end
130
149
  end
131
150
 
132
151
  # Change the User-Agent header to isolate calls from this plugin
@@ -150,8 +169,7 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
150
169
  begin
151
170
  index.add_objects!(batch) unless @is_dry_run
152
171
  rescue StandardError => error
153
- Jekyll.logger.error 'Algolia Error: HTTP Error'
154
- Jekyll.logger.warn error.message
172
+ display_error(error)
155
173
  exit 1
156
174
  end
157
175
  end
data/lib/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # Expose gem version
2
2
  class AlgoliaSearchJekyllVersion
3
3
  def self.to_s
4
- '0.6.0'
4
+ '0.6.1'
5
5
  end
6
6
  end
data/scripts/coverage ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env bash
2
+
3
+ COVERAGE=1 appraisal jekyll-v2 bundle exec rspec
data/scripts/test_ci CHANGED
@@ -4,4 +4,4 @@
4
4
  # worrying about appraisal
5
5
  cd "$(dirname "$BASH_SOURCE")"/..
6
6
 
7
- bundle exec rspec
7
+ COVERAGE=1 bundle exec rspec
data/scripts/test_v2 CHANGED
@@ -2,4 +2,4 @@
2
2
  cd "$(dirname "$BASH_SOURCE")"/..
3
3
 
4
4
  echo "Testing under Jekyll 2.5"
5
- appraisal jekyll-v2 bundle exec rspec
5
+ COVERAGE=1 appraisal jekyll-v2 bundle exec rspec
data/scripts/test_v3 CHANGED
@@ -2,5 +2,5 @@
2
2
  cd "$(dirname "$BASH_SOURCE")"/..
3
3
 
4
4
  echo "Testing under Jekyll 3.0"
5
- appraisal jekyll-v3 bundle exec rspec
5
+ COVERAGE=1 appraisal jekyll-v3 bundle exec rspec
6
6
 
data/scripts/watch CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env bash
2
2
  cd "$(dirname "$BASH_SOURCE")"/..
3
3
 
4
- guard -g jekyll_v2
4
+ appraisal jekyll-v2 guard
data/scripts/watch_v2 CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env bash
2
2
  cd "$(dirname "$BASH_SOURCE")"/..
3
3
 
4
- guard -g jekyll_v2
4
+ appraisal jekyll-v2 guard
data/scripts/watch_v3 CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env bash
2
2
  cd "$(dirname "$BASH_SOURCE")"/..
3
3
 
4
- guard -g jekyll_v3
4
+ appraisal jekyll-v3 guard
@@ -60,14 +60,16 @@ describe(AlgoliaSearchCredentialChecker) do
60
60
  end
61
61
 
62
62
  describe 'assert_valid' do
63
+ before(:each) do
64
+ allow(checker.logger).to receive(:display)
65
+ end
63
66
  it 'should display error if no api key' do
64
67
  # Given
65
- allow(checker).to receive(:api_key) { nil }
68
+ allow(checker).to receive(:api_key).and_return nil
66
69
 
67
70
  # Then
68
- expect(Jekyll.logger).to receive(:error).with(/api key/i)
69
- expect(Jekyll.logger).to receive(:warn).at_least(:once)
70
71
  expect(-> { checker.assert_valid }).to raise_error SystemExit
72
+ expect(checker.logger).to have_received(:display).with('api_key_missing')
71
73
  end
72
74
 
73
75
  it 'should display error if no application id' do
@@ -79,9 +81,10 @@ describe(AlgoliaSearchCredentialChecker) do
79
81
  stub_const('ENV', 'ALGOLIA_API_KEY' => 'APIKEY_FROM_ENV')
80
82
 
81
83
  # Then
82
- expect(Jekyll.logger).to receive(:error).with(/application id/i)
83
- expect(Jekyll.logger).to receive(:warn).at_least(:once)
84
84
  expect(-> { checker.assert_valid }).to raise_error SystemExit
85
+ expect(checker.logger)
86
+ .to have_received(:display)
87
+ .with('application_id_missing')
85
88
  end
86
89
 
87
90
  it 'should display error if no index name' do
@@ -93,9 +96,10 @@ describe(AlgoliaSearchCredentialChecker) do
93
96
  stub_const('ENV', 'ALGOLIA_API_KEY' => 'APIKEY_FROM_ENV')
94
97
 
95
98
  # Then
96
- expect(Jekyll.logger).to receive(:error).with(/index name/i)
97
- expect(Jekyll.logger).to receive(:warn).at_least(:once)
98
99
  expect(-> { checker.assert_valid }).to raise_error SystemExit
100
+ expect(checker.logger)
101
+ .to have_received(:display)
102
+ .with('index_name_missing')
99
103
  end
100
104
 
101
105
  it 'should init the Algolia client' do
@@ -0,0 +1,150 @@
1
+ require 'spec_helper'
2
+
3
+ describe(AlgoliaSearchErrorHandler) do
4
+ before(:each) do
5
+ @error_handler = AlgoliaSearchErrorHandler.new
6
+ end
7
+
8
+ describe 'display' do
9
+ before(:each) do
10
+ allow(Jekyll.logger).to receive(:error)
11
+ allow(Jekyll.logger).to receive(:warn)
12
+ end
13
+
14
+ it 'should display first line as error' do
15
+ # Given
16
+ input = 'sample'
17
+
18
+ # When
19
+ @error_handler.display(input)
20
+
21
+ # Then
22
+ expect(Jekyll.logger).to have_received(:error).exactly(1).times
23
+ end
24
+
25
+ it 'should display all other lines as warnings' do
26
+ # Given
27
+ input = 'sample'
28
+
29
+ # When
30
+ @error_handler.display(input)
31
+
32
+ # Then
33
+ expect(Jekyll.logger).to have_received(:warn).exactly(3).times
34
+ end
35
+ end
36
+
37
+ describe 'parse_algolia_error' do
38
+ before(:each) do
39
+ @algolia_error = 'Cannot PUT to ' \
40
+ 'https://appid.algolia.net/1/indexes/index_name/settings: ' \
41
+ '{"message":"Invalid Application-ID or API key","status":403} (403)'
42
+ end
43
+
44
+ it 'should extract all the url parts' do
45
+ # Given
46
+ input = @algolia_error
47
+
48
+ # When
49
+ actual = @error_handler.parse_algolia_error(input)
50
+
51
+ # Then
52
+ expect(actual['verb']).to eq 'PUT'
53
+ expect(actual['scheme']).to eq 'https'
54
+ expect(actual['app_id']).to eq 'appid'
55
+ expect(actual['api_section']).to eq 'indexes'
56
+ expect(actual['index_name']).to eq 'index_name'
57
+ expect(actual['api_action']).to eq 'settings'
58
+ end
59
+
60
+ it 'should cast integers to integers' do
61
+ # Given
62
+ input = @algolia_error
63
+
64
+ # When
65
+ actual = @error_handler.parse_algolia_error(input)
66
+
67
+ # Then
68
+ expect(actual['api_version']).to eq 1
69
+ expect(actual['http_error']).to eq 403
70
+ end
71
+
72
+ it 'should parse the JSON part' do
73
+ # Given
74
+ input = @algolia_error
75
+
76
+ # When
77
+ actual = @error_handler.parse_algolia_error(input)
78
+
79
+ # Then
80
+ expect(actual['json']).to be_a(Hash)
81
+ expect(actual['json']['status']).to eq 403
82
+ end
83
+
84
+ it 'should return false if this is not parsable' do
85
+ # Given
86
+ input = 'foo bar baz'
87
+
88
+ # When
89
+ actual = @error_handler.parse_algolia_error(input)
90
+
91
+ # Then
92
+ expect(actual).to eq(false)
93
+ end
94
+
95
+ it 'should work on multiline errors' do
96
+ # Given
97
+ input = @algolia_error.gsub('}', "}\n")
98
+
99
+ # When
100
+ actual = @error_handler.parse_algolia_error(input)
101
+
102
+ # Then
103
+ expect(actual).to be_a(Hash)
104
+ expect(actual['http_error']).to eq 403
105
+ end
106
+ end
107
+
108
+ describe 'readable_algolia_error' do
109
+ it 'should warn about key ACL' do
110
+ # Given
111
+ parsed = {
112
+ 'http_error' => 403,
113
+ 'index_name' => 'something_tmp'
114
+ }
115
+ allow(@error_handler).to receive(:parse_algolia_error).and_return(parsed)
116
+
117
+ # When
118
+ actual = @error_handler.readable_algolia_error('error')
119
+
120
+ # Then
121
+ expect(actual).to eq('check_key_acl_to_tmp_index')
122
+ end
123
+
124
+ it 'should return false if no nice message found' do
125
+ # Given
126
+ parsed = false
127
+ allow(@error_handler).to receive(:parse_algolia_error).and_return(parsed)
128
+
129
+ # When
130
+ actual = @error_handler.readable_algolia_error('error')
131
+
132
+ # Then
133
+ expect(actual).to eq(false)
134
+ end
135
+
136
+ it 'should return false if message does not match any known case' do
137
+ # Given
138
+ parsed = {
139
+ 'http_error' => 42
140
+ }
141
+ allow(@error_handler).to receive(:parse_algolia_error).and_return(parsed)
142
+
143
+ # When
144
+ actual = @error_handler.readable_algolia_error('error')
145
+
146
+ # Then
147
+ expect(actual).to eq(false)
148
+ end
149
+ end
150
+ end
data/spec/push_spec.rb CHANGED
@@ -140,6 +140,66 @@ describe(AlgoliaSearchJekyllPush) do
140
140
  # When
141
141
  push.configure_index(index)
142
142
  end
143
+
144
+ describe 'throw an error' do
145
+ before(:each) do
146
+ @index_double = double('Algolia Index').as_null_object
147
+ @error_handler_double = double('Error Handler double').as_null_object
148
+ push.init_options(nil, {}, {})
149
+ allow(@index_double).to receive(:set_settings).and_raise
150
+ allow(Jekyll.logger).to receive(:error)
151
+ end
152
+
153
+ it 'stops if API throw an error' do
154
+ # Given
155
+
156
+ # When
157
+
158
+ # Then
159
+ expect(-> { push.configure_index(@index_double) })
160
+ .to raise_error SystemExit
161
+ end
162
+
163
+ it 'displays the error directly if unknown' do
164
+ # Given
165
+ allow(@error_handler_double)
166
+ .to receive(:readable_algolia_error).and_return false
167
+ allow(@error_handler_double)
168
+ .to receive(:display)
169
+ allow(AlgoliaSearchErrorHandler)
170
+ .to receive(:new).and_return(@error_handler_double)
171
+
172
+ # When
173
+
174
+ # Then
175
+ expect(-> { push.configure_index(@index_double) })
176
+ .to raise_error SystemExit
177
+ expect(@error_handler_double)
178
+ .to have_received(:display).exactly(0).times
179
+ expect(Jekyll.logger)
180
+ .to have_received(:error).with('Algolia Error: HTTP Error')
181
+ end
182
+
183
+ it 'display a human readable version of the error if one is found' do
184
+ # Given
185
+ allow(@error_handler_double)
186
+ .to receive(:readable_algolia_error).and_return 'known_errors'
187
+ allow(@error_handler_double)
188
+ .to receive(:display)
189
+ allow(AlgoliaSearchErrorHandler)
190
+ .to receive(:new).and_return(@error_handler_double)
191
+
192
+ # When
193
+
194
+ # Then
195
+ expect(-> { push.configure_index(@index_double) })
196
+ .to raise_error SystemExit
197
+ expect(@error_handler_double)
198
+ .to have_received(:display)
199
+ .exactly(1).times
200
+ .with('known_errors')
201
+ end
202
+ end
143
203
  end
144
204
 
145
205
  describe 'jekyll_new' do
@@ -272,7 +332,6 @@ describe(AlgoliaSearchJekyllPush) do
272
332
  end
273
333
 
274
334
  it 'should display an error if `add_objects!` failed' do
275
- # Given
276
335
  allow(index_double).to receive(:add_objects!).and_raise
277
336
 
278
337
  expect(Jekyll.logger).to receive(:error)
data/spec/spec_helper.rb CHANGED
@@ -10,6 +10,7 @@ require './lib/push.rb'
10
10
 
11
11
  RSpec.configure do |config|
12
12
  config.filter_run(focus: true)
13
+ config.fail_fast = true
13
14
  config.run_all_when_everything_filtered = true
14
15
  end
15
16
 
@@ -0,0 +1,9 @@
1
+ Algolia Error: API key cannot write to `{index_name}_tmp` index
2
+ In order to do atomic pushes to your Algolia index, the plugin first pushes to
3
+ a temporary index (suffixed with `_tmp`), then renames it.
4
+
5
+ You see this error because the plugin wasn't able to push to that
6
+ `{index_name}_tmp` index, with the API key you provided.
7
+
8
+ Make sure the API key you're using has rights to write on both your index and
9
+ its `{index_name}_tmp` suffixed version.
data/txt/sample ADDED
@@ -0,0 +1,4 @@
1
+ Algolia Error: Sample Error
2
+ This is where you can explain the error and:
3
+ - one way to fix it
4
+ - or another way to fix it
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: algoliasearch-jekyll
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Carry
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-11 00:00:00.000000000 Z
11
+ date: 2016-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: algoliasearch
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jekyll
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.5'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: json
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,20 @@ dependencies:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: '1.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: verbal_expressions
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.1.5
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.1.5
83
111
  - !ruby/object:Gem::Dependency
84
112
  name: coveralls
85
113
  requirement: !ruby/object:Gem::Requirement
@@ -218,12 +246,14 @@ files:
218
246
  - gemfiles/jekyll_v3.gemfile
219
247
  - lib/algoliasearch-jekyll.rb
220
248
  - lib/credential_checker.rb
249
+ - lib/error_handler.rb
221
250
  - lib/push.rb
222
251
  - lib/record_extractor.rb
223
252
  - lib/version.rb
224
253
  - scripts/bump_version
225
254
  - scripts/check_flay
226
255
  - scripts/check_flog
256
+ - scripts/coverage
227
257
  - scripts/git_hooks/pre-commit
228
258
  - scripts/git_hooks/pre-push
229
259
  - scripts/release
@@ -235,6 +265,7 @@ files:
235
265
  - scripts/watch_v2
236
266
  - scripts/watch_v3
237
267
  - spec/credential_checker_spec.rb
268
+ - spec/error_handler_spec.rb
238
269
  - spec/fixtures/jekyll_version_2/_config.yml
239
270
  - spec/fixtures/jekyll_version_2/_layouts/default.html
240
271
  - spec/fixtures/jekyll_version_2/_my-collection/collection-item.html
@@ -271,7 +302,9 @@ files:
271
302
  - spec/spec_helper_simplecov.rb
272
303
  - txt/api_key_missing
273
304
  - txt/application_id_missing
305
+ - txt/check_key_acl_to_tmp_index
274
306
  - txt/index_name_missing
307
+ - txt/sample
275
308
  homepage: https://github.com/algolia/algoliasearch-jekyll
276
309
  licenses:
277
310
  - MIT