algoliasearch-rails 1.12.1 → 1.12.2

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: d0687fa4b4aca95ec66bd4510bad437cbc246232
4
- data.tar.gz: 1e37ff5b68c78600c40518ff1567b2a7678a009f
3
+ metadata.gz: ff6182784c89b9f5b8b139e15939bcc3aab827d5
4
+ data.tar.gz: f5d523d2c98723a91ade26e2a4945bdb41f81af7
5
5
  SHA512:
6
- metadata.gz: 460f63d74615acde329a19f174527b60f4eed599ddff0eba4fd703ffb9ff751dce3cf7c8e09128452eff49ada7ee70ad8199bbf5eec3ae54168e6aed89df158a
7
- data.tar.gz: a4c6fa6eaccc32d945c1f79d843d4f092e0f085cdd6cc38a9d447bd1c9ff533ed9dd068564afa05bf4e10ecc42fcf05114eaccfbbb4b5eff3d736a6273d58765
6
+ metadata.gz: 82f1d265c3d176f03b0ec53902dcc7a4ad22eb6f5b3b9811c17d07060638820ef3dccdddd2051df13f44df78bbcbac845fafbaaab9beb0bc1b4f771fc23442ed
7
+ data.tar.gz: 9217e898e4933f731bec9dc54841791bd45be21ef3d0a4fa7b65d46301a3392395237a930be7ec049f23c321f15905f8181dd34921d5876404be796249989048
data/.travis.yml CHANGED
@@ -6,8 +6,8 @@ rvm:
6
6
  - 1.8.7
7
7
  - 1.9.3
8
8
  - 2.0.0
9
- - 2.1.0
10
- - 2.2.0
9
+ - 2.1
10
+ - 2.2
11
11
  - jruby
12
12
  - rbx-2
13
13
  env:
data/ChangeLog CHANGED
@@ -1,5 +1,9 @@
1
1
  CHANGELOG
2
2
 
3
+ 2015-05-28 1.12.2
4
+
5
+ * Add a new `raise_on_failure` option, controlling whereas exceptions are raised while trying to reach Algolia's API.
6
+
3
7
  2015-05-12 1.12.1
4
8
 
5
9
  * Upgrade to algoliasearch-client-js 3.3.0
data/README.md CHANGED
@@ -187,7 +187,7 @@ Options
187
187
 
188
188
  #### Auto-indexing & asynchronism
189
189
 
190
- Each time a record is saved; it will be - asynchronously - indexed. On the other hand, each time a record is destroyed, it will be - asynchronously - removed from the index.
190
+ Each time a record is saved; it will be - asynchronously - indexed. On the other hand, each time a record is destroyed, it will be - asynchronously - removed from the index. That means that a network call with the ADD/DELETE operation is sent **synchronously** to the Algolia API but then the engine will **asynchronously** process the operation (so if you do a search just after, the results may not reflect it yet).
191
191
 
192
192
  You can disable auto-indexing and auto-removing setting the following options:
193
193
 
@@ -211,7 +211,7 @@ end
211
211
  Contact.reindex! # will use batch operations
212
212
  ```
213
213
 
214
- You can force indexing and removing to be synchronous by setting the following option:
214
+ You can force indexing and removing to be synchronous (in that case the gem will call the `wait_task` method to ensure the operation has been taken into account once the method returns) by setting the following option: (this is **NOT** recommended, except for testing purpose)
215
215
 
216
216
  ```ruby
217
217
  class Contact < ActiveRecord::Base
@@ -223,6 +223,22 @@ class Contact < ActiveRecord::Base
223
223
  end
224
224
  ```
225
225
 
226
+ #### Exceptions
227
+
228
+ You can disable exceptions that could be raised while trying to reach Algolia's API by using the `raise_on_failure` option:
229
+
230
+ ```ruby
231
+ class Contact < ActiveRecord::Base
232
+ include AlgoliaSearch
233
+
234
+ # only raise exceptions in development env
235
+ algoliasearch raise_on_failure: Rails.env.development? do
236
+ attribute :first_name, :last_name, :email
237
+ end
238
+ end
239
+ ```
240
+
241
+
226
242
  #### Custom index name
227
243
 
228
244
  By default, the index name will be the class name, e.g. "Contact". You can customize the index name by using the `index_name` option:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.12.1
1
+ 1.12.2
@@ -17,6 +17,8 @@ if defined? Rails
17
17
  end
18
18
  end
19
19
 
20
+ require 'logger'
21
+
20
22
  module AlgoliaSearch
21
23
 
22
24
  class NotConfigured < StandardError; end
@@ -205,6 +207,58 @@ module AlgoliaSearch
205
207
  end
206
208
  end
207
209
 
210
+ # this class wraps an Algolia::Index object ensuring all raised exceptions
211
+ # are correctly logged or thrown depending on the `raise_on_failure` option
212
+ class SafeIndex
213
+ def initialize(name, raise_on_failure)
214
+ @index = ::Algolia::Index.new(name)
215
+ @raise_on_failure = raise_on_failure.nil? || raise_on_failure
216
+ end
217
+
218
+ ::Algolia::Index.instance_methods(false).each do |m|
219
+ define_method(m) do |*args|
220
+ SafeIndex.log_or_throw(m) do
221
+ @index.send(m, *args)
222
+ end
223
+ end
224
+ end
225
+
226
+ # special handling of wait_task to handle null task_id
227
+ def wait_task(task_id)
228
+ return if task_id.nil? && !@raise_on_failure # ok
229
+ SafeIndex.log_or_throw(:wait_task) do
230
+ @index.wait_task(task_id)
231
+ end
232
+ end
233
+
234
+ # expose move as well
235
+ def self.move_index(old_name, new_name)
236
+ SafeIndex.log_or_throw(:move_index) do
237
+ ::Algolia.move_index(old_name, new_name)
238
+ end
239
+ end
240
+
241
+ private
242
+ def self.log_or_throw(method, &block)
243
+ begin
244
+ yield
245
+ rescue Algolia::AlgoliaError => e
246
+ raise e if @raise_on_failure
247
+ # log the error
248
+ (Rails.logger || Logger.new(STDOUT)).error("[algoliasearch-rails] #{e.message}")
249
+ # return something
250
+ case method.to_s
251
+ when 'search'
252
+ # some attributes are required
253
+ { 'hits' => [], 'hitsPerPage' => 0, 'page' => 0, 'facets' => {}, 'error' => e }
254
+ else
255
+ # empty answer
256
+ { 'error' => e }
257
+ end
258
+ end
259
+ end
260
+ end
261
+
208
262
  # these are the class methods added when AlgoliaSearch is included
209
263
  module ClassMethods
210
264
 
@@ -353,7 +407,7 @@ module AlgoliaSearch
353
407
  tmp_index.save_objects(objects)
354
408
  end
355
409
 
356
- move_task = ::Algolia.move_index(tmp_index.name, index_name)
410
+ move_task = SafeIndex.move_index(tmp_index.name, index_name)
357
411
  tmp_index.wait_task(move_task["taskID"]) if synchronous == true
358
412
  end
359
413
  nil
@@ -526,7 +580,7 @@ module AlgoliaSearch
526
580
  options ||= algoliasearch_options
527
581
  settings ||= algoliasearch_settings
528
582
  return @algolia_indexes[settings] if @algolia_indexes[settings]
529
- @algolia_indexes[settings] = Algolia::Index.new(algolia_index_name(options))
583
+ @algolia_indexes[settings] = SafeIndex.new(algolia_index_name(options), algoliasearch_options[:raise_on_failure])
530
584
  current_settings = @algolia_indexes[settings].get_settings rescue nil # if the index doesn't exist
531
585
  if !algolia_indexing_disabled?(options) && (index_settings || algoliasearch_settings_changed?(current_settings, settings.to_settings))
532
586
  index_settings ||= settings.to_settings
metadata CHANGED
@@ -1,111 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: algoliasearch-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.12.1
4
+ version: 1.12.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Algolia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-12 00:00:00.000000000 Z
11
+ date: 2015-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 1.5.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.5.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: algoliasearch
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 1.2.14
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 1.2.14
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: will_paginate
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: 2.3.15
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 2.3.15
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: kaminari
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: travis
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rdoc
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  description: AlgoliaSearch integration to your favorite ORM
@@ -117,9 +117,9 @@ extra_rdoc_files:
117
117
  - LICENSE
118
118
  - README.md
119
119
  files:
120
- - .document
121
- - .rspec
122
- - .travis.yml
120
+ - ".document"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
123
  - ChangeLog
124
124
  - Gemfile
125
125
  - Gemfile.lock
@@ -172,17 +172,17 @@ require_paths:
172
172
  - lib
173
173
  required_ruby_version: !ruby/object:Gem::Requirement
174
174
  requirements:
175
- - - '>='
175
+ - - ">="
176
176
  - !ruby/object:Gem::Version
177
177
  version: '0'
178
178
  required_rubygems_version: !ruby/object:Gem::Requirement
179
179
  requirements:
180
- - - '>='
180
+ - - ">="
181
181
  - !ruby/object:Gem::Version
182
182
  version: '0'
183
183
  requirements: []
184
184
  rubyforge_project:
185
- rubygems_version: 2.0.14
185
+ rubygems_version: 2.4.5
186
186
  signing_key:
187
187
  specification_version: 4
188
188
  summary: AlgoliaSearch integration to your favorite ORM