asari 0.7.3 → 0.8.0

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.
data/README.md CHANGED
@@ -16,6 +16,9 @@ for easy integration with your Rails apps.
16
16
  asari = Asari.new("my-search-domain-asdfkljwe4") # CloudSearch search domain
17
17
  asari.add_item("1", { :name => "Tommy Morgan", :email => "tommy@wellbredgrapefruit.com"})
18
18
  asari.search("tommy") #=> ["1"] - a list of document IDs
19
+ asari.search("tommy", :rank => "name") # Sort the search
20
+ asari.search("tommy", :rank => ["name", :desc]) # Sort the search descending
21
+ asari.search("tommy", :rank => "-name") # Another way to sort the search descending
19
22
 
20
23
  #### Sandbox Mode
21
24
 
@@ -71,6 +74,7 @@ with your AR objects as follows:
71
74
  # Klass.asari_find returns a list of model objects in an
72
75
  # Asari::Collection...
73
76
  User.asari_find("tommy") #=> [<User:...>, <User:...>, <User:...>]
77
+ User.asari_find("tommy", :rank => "name")
74
78
 
75
79
  # or with a specific instance, if you need to manually do some index
76
80
  # management...
@@ -156,6 +160,11 @@ on the current development status first.
156
160
 
157
161
  Gem requirements/etc. should be handled by Bundler.
158
162
 
163
+ ### Contributors
164
+
165
+ * [Emil Soman](https://github.com/emilsoman "emilsoman on Github")
166
+ * [Chris Vincent](https://github.com/cvincent "cvincent on Github")
167
+
159
168
  ## License
160
169
  Copyright (C) 2012 by Tommy Morgan
161
170
 
data/lib/asari.rb CHANGED
@@ -71,6 +71,11 @@ class Asari
71
71
  url << "&start=#{start}"
72
72
  end
73
73
 
74
+ if options[:rank]
75
+ rank = normalize_rank(options[:rank])
76
+ url << "&rank=#{rank}"
77
+ end
78
+
74
79
  begin
75
80
  response = HTTParty.get(url)
76
81
  rescue Exception => e
@@ -103,8 +108,9 @@ class Asari
103
108
  #
104
109
  def add_item(id, fields)
105
110
  return nil if self.class.mode == :sandbox
106
- query = { "type" => "add", "id" => id.to_s, "version" => 1, "lang" => "en" }
111
+ query = { "type" => "add", "id" => id.to_s, "version" => Time.now.to_i, "lang" => "en" }
107
112
  fields.each do |k,v|
113
+ fields[k] = convert_date_or_time(fields[k])
108
114
  fields[k] = "" if v.nil?
109
115
  end
110
116
 
@@ -147,14 +153,14 @@ class Asari
147
153
  def remove_item(id)
148
154
  return nil if self.class.mode == :sandbox
149
155
 
150
- query = { "type" => "delete", "id" => id.to_s, "version" => 2 }
156
+ query = { "type" => "delete", "id" => id.to_s, "version" => Time.now.to_i }
151
157
  doc_request query
152
158
  end
153
159
 
154
160
  # Internal: helper method: common logic for queries against the doc endpoint.
155
161
  #
156
162
  def doc_request(query)
157
- endpoint = "http://doc-#{search_domain}.us-east-1.cloudsearch.amazonaws.com/#{api_version}/documents/batch"
163
+ endpoint = "http://doc-#{search_domain}.#{aws_region}.cloudsearch.amazonaws.com/#{api_version}/documents/batch"
158
164
 
159
165
  options = { :body => [query].to_json, :headers => { "Content-Type" => "application/json"} }
160
166
 
@@ -172,6 +178,19 @@ class Asari
172
178
 
173
179
  nil
174
180
  end
181
+
182
+ protected
183
+
184
+ def normalize_rank(rank)
185
+ rank = Array(rank)
186
+ rank << :asc if rank.size < 2
187
+ rank[1] == :desc ? "-#{rank[0]}" : rank[0]
188
+ end
189
+
190
+ def convert_date_or_time(obj)
191
+ return obj unless [Time, Date, DateTime].include?(obj.class)
192
+ obj.to_time.to_i
193
+ end
175
194
  end
176
195
 
177
196
  Asari.mode = :sandbox # default to sandbox
data/lib/asari/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Asari
2
- VERSION = "0.7.3"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -6,12 +6,32 @@ describe Asari do
6
6
  @asari = Asari.new("testdomain")
7
7
  stub_const("HTTParty", double())
8
8
  HTTParty.stub(:post).and_return(fake_post_success)
9
+ Time.should_receive(:now).and_return(1)
9
10
  end
10
11
 
11
- it "allows you to add an item to the index." do
12
- HTTParty.should_receive(:post).with("http://doc-testdomain.us-east-1.cloudsearch.amazonaws.com/2011-02-01/documents/batch", { :body => [{ "type" => "add", "id" => "1", "version" => 1, "lang" => "en", "fields" => { :name => "fritters"}}].to_json, :headers => { "Content-Type" => "application/json"}})
12
+ context "when region is not specified" do
13
+ it "allows you to add an item to the index using default region." do
14
+ HTTParty.should_receive(:post).with("http://doc-testdomain.us-east-1.cloudsearch.amazonaws.com/2011-02-01/documents/batch", { :body => [{ "type" => "add", "id" => "1", "version" => 1, "lang" => "en", "fields" => { :name => "fritters"}}].to_json, :headers => { "Content-Type" => "application/json"}})
15
+
16
+ expect(@asari.add_item("1", {:name => "fritters"})).to eq(nil)
17
+ end
18
+ end
13
19
 
14
- expect(@asari.add_item("1", {:name => "fritters"})).to eq(nil)
20
+ context "when region is specified" do
21
+ before(:each) do
22
+ @asari.aws_region = 'my-region'
23
+ end
24
+ it "allows you to add an item to the index using specified region." do
25
+ HTTParty.should_receive(:post).with("http://doc-testdomain.my-region.cloudsearch.amazonaws.com/2011-02-01/documents/batch", { :body => [{ "type" => "add", "id" => "1", "version" => 1, "lang" => "en", "fields" => { :name => "fritters"}}].to_json, :headers => { "Content-Type" => "application/json"}})
26
+
27
+ expect(@asari.add_item("1", {:name => "fritters"})).to eq(nil)
28
+ end
29
+ end
30
+
31
+ it "converts Time, DateTime, and Date fields to timestamp integers for rankability" do
32
+ HTTParty.should_receive(:post).with("http://doc-testdomain.us-east-1.cloudsearch.amazonaws.com/2011-02-01/documents/batch", { :body => [{ "type" => "add", "id" => "1", "version" => 1, "lang" => "en", "fields" => { :time => 1333263600, :datetime => 1333238400, :date => 1333252800 }}].to_json, :headers => { "Content-Type" => "application/json"}})
33
+
34
+ expect(@asari.add_item("1", {:time => Time.at(1333263600), :datetime => DateTime.new(2012, 4, 1), :date => Date.new(2012, 4, 1)})).to eq(nil)
15
35
  end
16
36
 
17
37
  it "allows you to update an item to the index." do
@@ -20,8 +40,14 @@ describe Asari do
20
40
  expect(@asari.update_item("1", {:name => "fritters"})).to eq(nil)
21
41
  end
22
42
 
43
+ it "converts Time, DateTime, and Date fields to timestamp integers for rankability on update as well" do
44
+ HTTParty.should_receive(:post).with("http://doc-testdomain.us-east-1.cloudsearch.amazonaws.com/2011-02-01/documents/batch", { :body => [{ "type" => "add", "id" => "1", "version" => 1, "lang" => "en", "fields" => { :time => 1333263600, :datetime => 1333238400, :date => 1333252800 }}].to_json, :headers => { "Content-Type" => "application/json"}})
45
+
46
+ expect(@asari.update_item("1", {:time => Time.at(1333263600), :datetime => DateTime.new(2012, 4, 1), :date => Date.new(2012, 4, 1)})).to eq(nil)
47
+ end
48
+
23
49
  it "allows you to delete an item from the index." do
24
- HTTParty.should_receive(:post).with("http://doc-testdomain.us-east-1.cloudsearch.amazonaws.com/2011-02-01/documents/batch", { :body => [{ "type" => "delete", "id" => "1", "version" => 2}].to_json, :headers => { "Content-Type" => "application/json"}})
50
+ HTTParty.should_receive(:post).with("http://doc-testdomain.us-east-1.cloudsearch.amazonaws.com/2011-02-01/documents/batch", { :body => [{ "type" => "delete", "id" => "1", "version" => 1}].to_json, :headers => { "Content-Type" => "application/json"}})
25
51
 
26
52
  expect(@asari.remove_item("1")).to eq(nil)
27
53
  end
data/spec/search_spec.rb CHANGED
@@ -8,9 +8,21 @@ describe Asari do
8
8
  HTTParty.stub(:get).and_return(fake_response)
9
9
  end
10
10
 
11
- it "allows you to search." do
12
- HTTParty.should_receive(:get).with("http://search-testdomain.us-east-1.cloudsearch.amazonaws.com/2011-02-01/search?q=testsearch&size=10")
13
- @asari.search("testsearch")
11
+ context "when region is not specified" do
12
+ it "allows you to search using default region." do
13
+ HTTParty.should_receive(:get).with("http://search-testdomain.us-east-1.cloudsearch.amazonaws.com/2011-02-01/search?q=testsearch&size=10")
14
+ @asari.search("testsearch")
15
+ end
16
+ end
17
+
18
+ context "when region is not specified" do
19
+ before(:each) do
20
+ @asari.aws_region = 'my-region'
21
+ end
22
+ it "allows you to search using specified region." do
23
+ HTTParty.should_receive(:get).with("http://search-testdomain.my-region.cloudsearch.amazonaws.com/2011-02-01/search?q=testsearch&size=10")
24
+ @asari.search("testsearch")
25
+ end
14
26
  end
15
27
 
16
28
  it "escapes dangerous characters in search terms." do
@@ -28,6 +40,23 @@ describe Asari do
28
40
  @asari.search("testsearch", :page_size => 20, :page => 3)
29
41
  end
30
42
 
43
+ describe "the rank option" do
44
+ it "takes a plain string" do
45
+ HTTParty.should_receive(:get).with("http://search-testdomain.us-east-1.cloudsearch.amazonaws.com/2011-02-01/search?q=testsearch&size=10&rank=some_field")
46
+ @asari.search("testsearch", :rank => "some_field")
47
+ end
48
+
49
+ it "takes an array with :asc" do
50
+ HTTParty.should_receive(:get).with("http://search-testdomain.us-east-1.cloudsearch.amazonaws.com/2011-02-01/search?q=testsearch&size=10&rank=some_field")
51
+ @asari.search("testsearch", :rank => ["some_field", :asc])
52
+ end
53
+
54
+ it "takes an array with :desc" do
55
+ HTTParty.should_receive(:get).with("http://search-testdomain.us-east-1.cloudsearch.amazonaws.com/2011-02-01/search?q=testsearch&size=10&rank=-some_field")
56
+ @asari.search("testsearch", :rank => ["some_field", :desc])
57
+ end
58
+ end
59
+
31
60
  it "returns a list of document IDs for search results." do
32
61
  result = @asari.search("testsearch")
33
62
 
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.7.3
4
+ version: 0.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-05 00:00:00.000000000 Z
12
+ date: 2013-04-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -98,4 +98,3 @@ test_files:
98
98
  - spec/conditionals_spec.rb
99
99
  - spec/documents_spec.rb
100
100
  - spec/search_spec.rb
101
- has_rdoc: