slingshot-rb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,6 +1,8 @@
1
1
  Slingshot
2
2
  =========
3
3
 
4
+ ![Slingshot](https://github.com/karmi/slingshot/raw/master/slingshot.png)
5
+
4
6
  _Slingshot_ aims to provide a rich Ruby API and DSL for the
5
7
  [ElasticSearch](http://www.elasticsearch.org/) search engine/database.
6
8
 
@@ -25,7 +27,7 @@ First, you need a running _ElasticSearch_ server. Thankfully, it's easy. Let's d
25
27
 
26
28
  OK, easy. Now, install the gem via Rubygems:
27
29
 
28
- $ gem install slingshot
30
+ $ gem install slingshot-rb
29
31
 
30
32
  or from source:
31
33
 
@@ -86,7 +88,7 @@ from the database:
86
88
  Let's display the results:
87
89
 
88
90
  s.results.each do |document|
89
- puts "* #{ document['_source']['title'] }"
91
+ puts "* #{ document.title }"
90
92
  end
91
93
 
92
94
  # * Two
@@ -135,7 +137,6 @@ Todo & Plans
135
137
 
136
138
  In order of importance:
137
139
 
138
- * Basic wrapper class for _hits_ in results, so we could write `results.first.document.title` instead of using the raw Hash
139
140
  * Getting document [by ID](http://www.elasticsearch.org/guide/reference/api/get.html)
140
141
  * Seamless _ActiveModel_ compatibility for easy usage in _Rails_ applications (this also means nearly full _ActiveRecord_ compatibility)
141
142
  * Allowing to set custom non-ActiveModel wrapper class (your own)
data/examples/dsl.rb CHANGED
@@ -56,7 +56,7 @@ puts s.to_curl
56
56
 
57
57
  puts "", "Results:", "-"*80
58
58
  s.results.each_with_index do |document, i|
59
- puts "#{i+1}. #{ document['_source']['title'].ljust(20) } [id] #{document['_id']}"
59
+ puts "#{i+1}. #{ document.title.ljust(20) } [id] #{document._id}"
60
60
  end
61
61
 
62
62
  puts "", "Facets: tags distribution across the whole database:", "-"*80
@@ -8,7 +8,11 @@ module Slingshot
8
8
  def initialize(response)
9
9
  @time = response['took']
10
10
  @total = response['hits']['total']
11
- @results = response['hits']['hits']
11
+ @results = response['hits']['hits'].map do |h|
12
+ document = h['_source'] ? h['_source'] : h['fields']
13
+ h.update document if document
14
+ Item.new h
15
+ end
12
16
  @facets = response['facets']
13
17
  end
14
18
 
@@ -0,0 +1,36 @@
1
+ module Slingshot
2
+ module Results
3
+
4
+ class Item < Hash
5
+
6
+ # Create new instance, recursively converting all Hashes to Item
7
+ # and leaving everything else alone.
8
+ #
9
+ def initialize(args={})
10
+ if args.is_a? Hash
11
+ args.each_pair do |key, value|
12
+ self[key.to_sym] = value.is_a?(Hash) ? self.class.new(value) : value
13
+ end
14
+ super.replace self
15
+ else
16
+ super()
17
+ end
18
+ end
19
+
20
+ # Delegate method to a key in underlying hash, if present,
21
+ # otherwise return +nil+.
22
+ #
23
+ def method_missing(method_name, *arguments)
24
+ self.has_key?(method_name.to_sym) ? self[method_name.to_sym] : nil
25
+ end
26
+
27
+ def inspect
28
+ s = []; self.each { |k,v| s << ":#{k} => #{v.inspect}" }
29
+ %Q|<Item #{s.join(', ')}>|
30
+ end
31
+
32
+ alias_method :to_indexed_json, :to_json
33
+ end
34
+
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  class Hash
2
- alias_method :to_indexed_json, :to_json if respond_to?(:to_json)
2
+ alias_method :to_indexed_json, :to_json
3
3
  end
@@ -1,3 +1,3 @@
1
1
  module Slingshot
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
File without changes
data/lib/slingshot.rb CHANGED
@@ -10,6 +10,7 @@ require 'slingshot/search/query'
10
10
  require 'slingshot/search/sort'
11
11
  require 'slingshot/search/facet'
12
12
  require 'slingshot/results/collection'
13
+ require 'slingshot/results/item'
13
14
  require 'slingshot/index'
14
15
  require 'slingshot/dsl'
15
16
 
data/slingshot.png ADDED
Binary file
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ module Slingshot
4
+
5
+ class ResultsIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "Query results" do
9
+
10
+ should "allow easy access to returned documents" do
11
+ q = 'title:one'
12
+ s = Slingshot.search('articles-test') { query { string q } }
13
+ assert_equal 'One', s.results.first.title
14
+ assert_equal 'ruby', s.results.first.tags[0]
15
+ end
16
+
17
+ should "allow easy access to returned documents with limited fields" do
18
+ q = 'title:one'
19
+ s = Slingshot.search('articles-test') { query { string q }.fields :title }
20
+ assert_equal 'One', s.results.first.title
21
+ assert_nil s.results.first.tags
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -6,24 +6,42 @@ module Slingshot
6
6
 
7
7
  context "Collection" do
8
8
  setup do
9
- @default_response = { 'hits' => { 'hits' => [1, 2, 3] } }
9
+ @default_response = { 'hits' => { 'hits' => [{:_id => 1}, {:_id => 2}, {:_id => 3}] } }
10
10
  end
11
11
 
12
12
  should "be iterable" do
13
13
  assert_respond_to Results::Collection.new(@default_response), :each
14
14
  assert_nothing_raised do
15
- Results::Collection.new(@default_response).each { |item| item + 1 }
16
- Results::Collection.new(@default_response).map { |item| item + 1 }
15
+ Results::Collection.new(@default_response).each { |item| item[:_id] + 1 }
16
+ Results::Collection.new(@default_response).map { |item| item[:_id] + 1 }
17
17
  end
18
18
  end
19
19
 
20
20
  should "be initialized with parsed json" do
21
21
  assert_nothing_raised do
22
22
  collection = Results::Collection.new( @default_response )
23
- assert_equal [1,2,3], collection.results
23
+ assert_equal 3, collection.results.count
24
24
  end
25
25
  end
26
26
 
27
+ context "wrapping results" do
28
+
29
+ should "wrap hits in Item" do
30
+ response = { 'hits' => { 'hits' => [ { '_id' => 1, '_source' => { :title => 'Test', :body => 'Lorem' } } ] } }
31
+ document = Results::Collection.new(response).first
32
+ assert_kind_of Results::Item, document
33
+ assert_equal 'Test', document.title
34
+ end
35
+
36
+ should "allow access to raw underlying Hash" do
37
+ response = { 'hits' => { 'hits' => [ { '_id' => 1, '_source' => { :title => 'Test', :body => 'Lorem' } } ] } }
38
+ document = Results::Collection.new(response).first
39
+ assert_not_nil document[:_source][:title]
40
+ assert_equal 'Test', document[:_source][:title]
41
+ end
42
+
43
+ end
44
+
27
45
  end
28
46
 
29
47
  end
@@ -0,0 +1,50 @@
1
+ require 'test_helper'
2
+
3
+ module Slingshot
4
+
5
+ class ResultsItemTest < Test::Unit::TestCase
6
+
7
+ context "Item" do
8
+
9
+ setup do
10
+ @document = Results::Item.new :title => 'Test', :author => { :name => 'Kafka' }
11
+ end
12
+
13
+ should "respond to :to_indexed_json" do
14
+ assert_respond_to Results::Item.new, :to_indexed_json
15
+ end
16
+
17
+ should "retrieve the values from underlying hash" do
18
+ assert_equal 'Test', @document[:title]
19
+ end
20
+
21
+ should "allow to retrieve the values by methods" do
22
+ assert_not_nil @document.title
23
+ assert_equal 'Test', @document.title
24
+ end
25
+
26
+ should "return nil for non-existing keys/methods" do
27
+ assert_nothing_raised { @document.whatever }
28
+ assert_nil @document.whatever
29
+ end
30
+
31
+ should "not care about symbols or strings in keys" do
32
+ @document = Results::Item.new 'title' => 'Test'
33
+ assert_not_nil @document.title
34
+ assert_equal 'Test', @document.title
35
+ end
36
+
37
+ should "allow to retrieve hashes" do
38
+ assert_equal 'Kafka', @document.author[:name]
39
+ end
40
+
41
+ should "allow to retrieve values from nested hashes" do
42
+ assert_not_nil @document.author.name
43
+ assert_equal 'Kafka', @document.author.name
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slingshot-rb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
4
+ hash: 27
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Karel Minarik
@@ -152,20 +152,22 @@ files:
152
152
  - README.markdown
153
153
  - Rakefile
154
154
  - examples/dsl.rb
155
+ - lib/slingshot-rb.rb
155
156
  - lib/slingshot.rb
156
157
  - lib/slingshot/client.rb
157
158
  - lib/slingshot/configuration.rb
158
159
  - lib/slingshot/dsl.rb
159
160
  - lib/slingshot/index.rb
160
161
  - lib/slingshot/results/collection.rb
162
+ - lib/slingshot/results/item.rb
161
163
  - lib/slingshot/rubyext/hash.rb
162
164
  - lib/slingshot/search.rb
163
165
  - lib/slingshot/search/facet.rb
164
166
  - lib/slingshot/search/query.rb
165
167
  - lib/slingshot/search/sort.rb
166
- - lib/slingshot/slingshot-rb.rb
167
168
  - lib/slingshot/version.rb
168
169
  - slingshot.gemspec
170
+ - slingshot.png
169
171
  - test/fixtures/articles/1.json
170
172
  - test/fixtures/articles/2.json
171
173
  - test/fixtures/articles/3.json
@@ -173,12 +175,14 @@ files:
173
175
  - test/fixtures/articles/5.json
174
176
  - test/integration/facets_test.rb
175
177
  - test/integration/query_string_test.rb
178
+ - test/integration/results_test.rb
176
179
  - test/integration/sort_test.rb
177
180
  - test/test_helper.rb
178
181
  - test/unit/client_test.rb
179
182
  - test/unit/configuration_test.rb
180
183
  - test/unit/index_test.rb
181
184
  - test/unit/results_collection_test.rb
185
+ - test/unit/results_item_test.rb
182
186
  - test/unit/search_facet_test.rb
183
187
  - test/unit/search_query_test.rb
184
188
  - test/unit/search_sort_test.rb
@@ -216,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
216
220
  requirements: []
217
221
 
218
222
  rubyforge_project: slingshot
219
- rubygems_version: 1.3.7
223
+ rubygems_version: 1.5.0
220
224
  signing_key:
221
225
  specification_version: 3
222
226
  summary: Ruby API for ElasticSearch
@@ -228,12 +232,14 @@ test_files:
228
232
  - test/fixtures/articles/5.json
229
233
  - test/integration/facets_test.rb
230
234
  - test/integration/query_string_test.rb
235
+ - test/integration/results_test.rb
231
236
  - test/integration/sort_test.rb
232
237
  - test/test_helper.rb
233
238
  - test/unit/client_test.rb
234
239
  - test/unit/configuration_test.rb
235
240
  - test/unit/index_test.rb
236
241
  - test/unit/results_collection_test.rb
242
+ - test/unit/results_item_test.rb
237
243
  - test/unit/search_facet_test.rb
238
244
  - test/unit/search_query_test.rb
239
245
  - test/unit/search_sort_test.rb