asari 0.3.0 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/asari.rb CHANGED
@@ -8,9 +8,12 @@ require "json"
8
8
  require "cgi"
9
9
 
10
10
  class Asari
11
- class << self
12
- attr_accessor :mode
13
- @mode = :sandbox
11
+ def self.mode
12
+ @@mode
13
+ end
14
+
15
+ def self.mode=(mode)
16
+ @@mode = mode
14
17
  end
15
18
 
16
19
  attr_writer :api_version
@@ -83,7 +86,11 @@ class Asari
83
86
  #
84
87
  def add_item(id, fields)
85
88
  return nil if self.class.mode == :sandbox
86
- query = { "type" => "add", "id" => id, "version" => 1, "lang" => "en" }
89
+ query = { "type" => "add", "id" => id.to_s, "version" => 1, "lang" => "en" }
90
+ fields.each do |k,v|
91
+ fields[k] = "" if v.nil?
92
+ end
93
+
87
94
  query["fields"] = fields
88
95
  doc_request(query)
89
96
  end
@@ -123,7 +130,7 @@ class Asari
123
130
  def remove_item(id)
124
131
  return nil if self.class.mode == :sandbox
125
132
 
126
- query = { "type" => "delete", "id" => id, "version" => 2 }
133
+ query = { "type" => "delete", "id" => id.to_s, "version" => 2 }
127
134
  doc_request query
128
135
  end
129
136
 
@@ -149,3 +156,5 @@ class Asari
149
156
  nil
150
157
  end
151
158
  end
159
+
160
+ Asari.mode = :sandbox # default to sandbox
@@ -2,7 +2,7 @@ class Asari
2
2
  # Public: This module should be included in any class inheriting from
3
3
  # ActiveRecord::Base that needs to be indexed. Every time this module is
4
4
  # included, asari_index *must* be called (see below). Including this module
5
- # will automatically create before_delete, after_create, and after_update AR
5
+ # will automatically create before_destroy, after_create, and after_update AR
6
6
  # callbacks to remove, add, and update items in the CloudSearch index
7
7
  # (respectively).
8
8
  #
@@ -10,9 +10,11 @@ class Asari
10
10
  def self.included(base)
11
11
  base.extend(ClassMethods)
12
12
 
13
- base.before_delete :asari_remove_from_index
14
- base.after_create :asari_add_to_index
15
- base.after_update :asari_update_in_index
13
+ base.class_eval do
14
+ before_destroy :asari_remove_from_index
15
+ after_create :asari_add_to_index
16
+ after_update :asari_update_in_index
17
+ end
16
18
  end
17
19
 
18
20
  def asari_remove_from_index
@@ -46,40 +48,48 @@ class Asari
46
48
  # asari_index("my-companies-users-asglkj4rsagkjlh34", [:name, :email])
47
49
  #
48
50
  def asari_index(search_domain, fields)
49
- @asari = Asari.new(search_domain)
50
- @fields = fields
51
+ self.class_variable_set(:@@asari, Asari.new(search_domain))
52
+ self.class_variable_set(:@@fields, fields)
53
+ end
54
+
55
+ def asari_instance
56
+ self.class_variable_get(:@@asari)
57
+ end
58
+
59
+ def asari_fields
60
+ self.class_variable_get(:@@fields)
51
61
  end
52
62
 
53
63
  # Internal: method for adding a newly created item to the CloudSearch
54
64
  # index. Should probably only be called from asari_add_to_index above.
55
65
  def asari_add_item(obj)
56
66
  data = {}
57
- @fields.each do |field|
58
- data[field] = obj.send(field)
67
+ self.asari_fields.each do |field|
68
+ data[field] = obj.send(field) || ""
59
69
  end
60
- @asari.add_item(obj.send(:id), data)
70
+ self.asari_instance.add_item(obj.send(:id), data)
61
71
  rescue Asari::DocumentUpdateException => e
62
- asari_on_error(e)
72
+ self.asari_on_error(e)
63
73
  end
64
74
 
65
75
  # Internal: method for updating a freshly edited item to the CloudSearch
66
76
  # index. Should probably only be called from asari_update_in_index above.
67
77
  def asari_update_item(obj)
68
78
  data = {}
69
- @fields.each do |field|
79
+ self.asari_fields.each do |field|
70
80
  data[field] = obj.send(field)
71
81
  end
72
- @asari.update_item(obj.send(:id), data)
82
+ self.asari_instance.update_item(obj.send(:id), data)
73
83
  rescue Asari::DocumentUpdateException => e
74
- asari_on_error(e)
84
+ self.asari_on_error(e)
75
85
  end
76
86
 
77
87
  # Internal: method for removing a soon-to-be deleted item from the CloudSearch
78
88
  # index. Should probably only be called from asari_remove_from_index above.
79
89
  def asari_remove_item(obj)
80
- @asari.remove_item(obj.send(:id))
90
+ self.asari_instance.remove_item(obj.send(:id))
81
91
  rescue Asari::DocumentUpdateException => e
82
- asari_on_error(e)
92
+ self.asari_on_error(e)
83
93
  end
84
94
 
85
95
  # Public: method for searching the index for the specified term and
@@ -91,7 +101,7 @@ class Asari
91
101
  # Raises: an Asari::SearchException error if there are issues
92
102
  # communicating with the CloudSearch server.
93
103
  def asari_find(term)
94
- ids = @asari.search(term).map { |id| id.to_i }
104
+ ids = self.asari_instance.search(term).map { |id| id.to_i }
95
105
  begin
96
106
  self.find(*ids)
97
107
  rescue ::ActiveRecord::RecordNotFound
data/lib/asari/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Asari
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.4"
3
3
  end
@@ -5,11 +5,11 @@ describe Asari do
5
5
  describe "when CloudSearch is responding without error" do
6
6
  before :each do
7
7
  @asari = double()
8
- ActiveRecordFake.instance_variable_set(:@asari, @asari)
8
+ ActiveRecordFake.class_variable_set(:@@asari, @asari)
9
9
  end
10
10
 
11
- it "correctly sets up a before_delete listener" do
12
- expect(ActiveRecordFake.instance_variable_get(:@before_delete)).to eq(:asari_remove_from_index)
11
+ it "correctly sets up a before_destroy listener" do
12
+ expect(ActiveRecordFake.instance_variable_get(:@before_destroy)).to eq(:asari_remove_from_index)
13
13
  end
14
14
 
15
15
  it "correctly sets up an after_create listener" do
@@ -60,7 +60,7 @@ describe Asari do
60
60
 
61
61
  describe "When CloudSearch is being a problem" do
62
62
  before :each do
63
- ActiveRecordFake.instance_variable_set(:@asari, Asari.new("test-domain"))
63
+ ActiveRecordFake.class_variable_set(:@@asari, Asari.new("test-domain"))
64
64
  stub_const("HTTParty", double())
65
65
  HTTParty.stub(:post).and_return(fake_error_response)
66
66
  HTTParty.stub(:get).and_return(fake_error_response)
data/spec_helper.rb CHANGED
@@ -2,6 +2,9 @@ require 'asari'
2
2
  require 'asari/active_record'
3
3
  require 'ostruct'
4
4
 
5
+ # Fake production mode to test.
6
+ Asari.mode = :production
7
+
5
8
  RSpec.configuration.expect_with(:rspec) { |c| c.syntax = :expect }
6
9
 
7
10
  def fake_response
@@ -29,8 +32,8 @@ end
29
32
 
30
33
  class ActiveRecordFake
31
34
  class << self
32
- def before_delete(sym)
33
- @before_delete = sym
35
+ def before_destroy(sym)
36
+ @before_destroy = sym
34
37
  end
35
38
 
36
39
  def after_create(sym)
@@ -68,10 +71,6 @@ class ActiveRecordFake
68
71
  end
69
72
 
70
73
  class ActiveRecordFakeWithErrorOverride < ActiveRecordFake
71
- include Asari::ActiveRecord
72
-
73
- asari_index("test-domain", [:name, :email])
74
-
75
74
  def self.asari_on_error(exception)
76
75
  false
77
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asari
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-07-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
16
- requirement: &70106563512800 !ruby/object:Gem::Requirement
16
+ requirement: &70351657843560 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70106563512800
24
+ version_requirements: *70351657843560
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70106563528480 !ruby/object:Gem::Requirement
27
+ requirement: &70351657841900 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70106563528480
35
+ version_requirements: *70351657841900
36
36
  description: Asari s a Ruby interface for AWS CloudSearch
37
37
  email:
38
38
  - tommy@wellbredgrapefruit.com