waistband 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Waistband::QueryResult do
4
+
5
+ let(:hit) { {"_index"=>"search", "_type"=>"search", "_id"=>"task_14969", "_score"=>5.810753, "_source"=>{"id"=>14969, "name"=>"shopping"}} }
6
+ let(:result) { Waistband::QueryResult.new(hit) }
7
+
8
+ it "allows access to inner document" do
9
+ result.source.should eql hit['_source']
10
+ result._id.should eql 'task_14969'
11
+ result.score.should eql 5.810753
12
+ end
13
+
14
+ it "provides methods for source attributes" do
15
+ result.id.should eql 14969
16
+ result.name.should eql 'shopping'
17
+ end
18
+
19
+ it "returns nil if the method missing misses" do
20
+ result.something_else.should be_nil
21
+ end
22
+
23
+ end
@@ -0,0 +1,279 @@
1
+ require 'spec_helper'
2
+ require 'active_support/core_ext/kernel/reporting'
3
+
4
+ Kernel.silence_stderr do
5
+ require 'kaminari'
6
+ end
7
+
8
+ describe Waistband::Query do
9
+
10
+ let(:index) { Waistband::Index.new('search') }
11
+ let(:query) { index.query('shopping ikea') }
12
+
13
+ describe '#execute!' do
14
+
15
+ it "gets results from elastic search" do
16
+ add_result!
17
+
18
+ json = query.send(:execute!)
19
+ json['hits'].should be_a Hash
20
+ json['hits']['total'].should > 0
21
+ json['hits']['hits'].size.should eql 1
22
+
23
+ json['hits']['hits'].each do |hit|
24
+ hit['_id'].should match(/^task_.*/)
25
+ hit['_source'].should be_a Hash
26
+ hit['_source']['id'].should eql 123123
27
+ hit['_source']['name'].should eql 'some shopping in ikea'
28
+ hit['_source']['user_id'].should eql 999
29
+ hit['_source']['description'].should eql 'i need you to pick up some stuff in ikea'
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ describe '#add_sort' do
36
+
37
+ it "adds sort field" do
38
+ query.add_sort('created_at', 'asc')
39
+ query.instance_variable_get('@sorts').should eql [{key: 'created_at', ord: 'asc'}]
40
+
41
+ query.add_sort('updated_at', 'desc')
42
+ query.instance_variable_get('@sorts').should eql [{key: 'created_at', ord: 'asc'}, {key: 'updated_at', ord: 'desc'}]
43
+ end
44
+
45
+ end
46
+
47
+ describe '#add_range' do
48
+
49
+ it "adds a range" do
50
+ query.add_range('task_id', 5, 10)
51
+ query.instance_variable_get('@ranges').should eql [{field: 'task_id', min: 5, max: 10}]
52
+ end
53
+
54
+ end
55
+
56
+ describe '#add_field' do
57
+
58
+ it "adds field" do
59
+ query.add_field('name')
60
+ query.instance_variable_get('@fields').should eql ['name']
61
+ end
62
+
63
+ it "adds multiple fields at once" do
64
+ query.add_field('name', 'description')
65
+ query.instance_variable_get('@fields').should eql ['name', 'description']
66
+ end
67
+
68
+ end
69
+
70
+ describe '#add_term' do
71
+
72
+ it "adds the term on the key" do
73
+ query.add_term('metro', 'boston')
74
+ query.instance_variable_get('@terms')['metro'][:keywords].should eql ['boston']
75
+ end
76
+
77
+ it "adds several terms on multiple words" do
78
+ query.add_term('metro', 'sf bay area')
79
+ query.instance_variable_get('@terms')['metro'][:keywords].should eql ['sf', 'bay', 'area']
80
+ end
81
+
82
+ end
83
+
84
+ describe '#terms' do
85
+
86
+ it "builds an array of all terms" do
87
+ query.add_term('metro', 'sf bay area')
88
+ query.send(:terms).should eql([
89
+ {
90
+ terms: {
91
+ metro: ['sf', 'bay', 'area']
92
+ }
93
+ }
94
+ ])
95
+ end
96
+
97
+ it "builds an array of single terms" do
98
+ query.add_term('metro', 'boston')
99
+ query.send(:terms).should eql([
100
+ {
101
+ terms: {
102
+ metro: ['boston']
103
+ }
104
+ }
105
+ ])
106
+ end
107
+
108
+ it "constructs correctly with multiple terms" do
109
+ query.add_term('metro', 'sf bay area')
110
+ query.add_term('geography', 'San Francisco')
111
+ query.send(:terms).should eql([
112
+ {
113
+ terms: {
114
+ metro: ["sf", "bay", "area"]}
115
+ },
116
+ {
117
+ terms: {
118
+ geography: ["San", "Francisco"]
119
+ }
120
+ }
121
+ ])
122
+ end
123
+
124
+ end
125
+
126
+ describe '#must_to_hash' do
127
+
128
+ it "creates an array of the must of the query" do
129
+ query.add_term('metro', 'sf bay area')
130
+ query.add_field('name')
131
+ query.send(:must_to_hash).should eql([
132
+ {
133
+ multi_match: {
134
+ query: "shopping ikea",
135
+ fields: ['name']
136
+ }
137
+ },
138
+ {
139
+ terms: {
140
+ metro: ["sf", "bay", "area"]
141
+ }
142
+ }
143
+ ])
144
+ end
145
+
146
+ end
147
+
148
+ describe '#from' do
149
+
150
+ it "returns 0 when page is 1" do
151
+ Waistband::Query.new('search', 'shopping ikea', page: 1, page_size: 20).send(:from).should eql 0
152
+ end
153
+
154
+ it "returns 20 when page is 2 and page_size is 20" do
155
+ Waistband::Query.new('search', 'shopping ikea', page: 2, page_size: 20).send(:from).should eql 20
156
+ end
157
+
158
+ it "returns 30 when page is 4 and page_size is 10" do
159
+ Waistband::Query.new('search', 'shopping ikea', page: 4, page_size: 10).send(:from).should eql 30
160
+ end
161
+
162
+ it "returns 10 when page is 2 and page_size is 10" do
163
+ Waistband::Query.new('search', 'shopping ikea', page: 2, page_size: 10).send(:from).should eql 10
164
+ end
165
+
166
+ end
167
+
168
+ describe '#to_hash' do
169
+
170
+ it "constructs the query's json" do
171
+ query.add_term('metro', 'sf bay area')
172
+ query.add_field('name')
173
+ query.add_sort('created_at', 'desc')
174
+ query.add_range('task_id', 1, 10)
175
+
176
+ query.send(:to_hash).should eql({
177
+ query: {
178
+ bool: {
179
+ must: [
180
+ {
181
+ multi_match: {
182
+ query: "shopping ikea",
183
+ fields: ['name']
184
+ },
185
+ },
186
+ {
187
+ range: {
188
+ 'task_id' => {
189
+ lte: 10,
190
+ gte: 1
191
+ }
192
+ }
193
+ },
194
+ {
195
+ terms: {
196
+ metro: ["sf","bay","area"]
197
+ }
198
+ }
199
+ ],
200
+ must_not: [],
201
+ should: []
202
+ }
203
+ },
204
+ from: 0,
205
+ size: 20,
206
+ sort: [
207
+ {'created_at' => 'desc'},
208
+ '_score'
209
+ ]
210
+ })
211
+ end
212
+
213
+ it 'constructs the query with several terms' do
214
+ query.add_term('metro', 'sf bay area')
215
+ query.add_term('geography', 'San Francisco')
216
+ query.add_field('name')
217
+ query.add_field('description')
218
+
219
+ query.send(:to_hash).should eql({
220
+ query: {
221
+ bool: {
222
+ must: [
223
+ {
224
+ multi_match: {
225
+ query: "shopping ikea",
226
+ fields: ['name', 'description']
227
+ }
228
+ },
229
+ {
230
+ terms: {
231
+ metro: ["sf", "bay", "area"]
232
+ }
233
+ },
234
+ {
235
+ terms: {
236
+ geography: ["San", "Francisco"]
237
+ }
238
+ }
239
+ ],
240
+ must_not: [],
241
+ should: []
242
+ }
243
+ },
244
+ from: 0,
245
+ size: 20,
246
+ sort: ['_score']
247
+ })
248
+ end
249
+
250
+ end
251
+
252
+ describe '#results' do
253
+
254
+ it "returns a QueryResult array" do
255
+ add_result!
256
+
257
+ query.results.first.should be_a Waistband::QueryResult
258
+ end
259
+
260
+ end
261
+
262
+ describe '#paginated_results' do
263
+
264
+ it "returns a kaminari paginated array" do
265
+ add_result!
266
+
267
+ query.paginated_results.should be_an Array
268
+ end
269
+
270
+ end
271
+
272
+ def add_result!
273
+ index.store!("task_123123", {id: 123123, name: 'some shopping in ikea', user_id: 999, description: 'i need you to pick up some stuff in ikea'})
274
+ index.refresh
275
+
276
+ query.add_field('name')
277
+ end
278
+
279
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Waistband::StringifyAll do
4
+
5
+ before do
6
+ Array.send(:include, Waistband::StringifyAll::Array)
7
+ Hash.send(:include, Waistband::StringifyAll::Hash)
8
+ end
9
+
10
+ it "stringifies everything in an array" do
11
+ [1, 2, 3].stringify_all.should eql %w(1 2 3)
12
+ end
13
+
14
+ it "stringifies everything in a hash" do
15
+ {'name' => :peter, 'description' => {'full' => 'ok'}}.stringify_all.should eql({'name' => 'peter', 'description' => "{\"full\"=>\"ok\"}"})
16
+ end
17
+
18
+ it "recurses" do
19
+ {'name' => :peter, 'description' => [1, 2, 3]}.stringify_all.should eql({'name' => 'peter', 'description' => "[1, 2, 3]"})
20
+ end
21
+
22
+ end
@@ -0,0 +1,31 @@
1
+ ENV['RACK_ENV'] ||= 'test'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ APP_DIR ||= File.expand_path('../../', __FILE__)
6
+
7
+ require 'waistband'
8
+ require 'rspec'
9
+ require 'timecop'
10
+ require 'active_support/core_ext/integer/time'
11
+
12
+ Dir["#{APP_DIR}/spec/support/**/*.rb"].each {|f| require f}
13
+
14
+ RSpec.configure do |config|
15
+
16
+ config.before(:all) do
17
+ Waistband.configure do |c|
18
+ c.config_dir = "#{APP_DIR}/spec/config/waistband"
19
+ end
20
+ end
21
+
22
+ config.before(:each) do |c|
23
+ IndexHelper.destroy_all!
24
+ IndexHelper.create_all!
25
+ end
26
+
27
+ config.after(:each) do
28
+ Timecop.return
29
+ end
30
+
31
+ end
@@ -0,0 +1,21 @@
1
+ class IndexHelper
2
+
3
+ INDEXES = %w(events search)
4
+
5
+ class << self
6
+
7
+ def destroy_all!
8
+ IndexHelper::INDEXES.each do |index|
9
+ Waistband::Index.new(index).destroy!
10
+ end
11
+ end
12
+
13
+ def create_all!
14
+ IndexHelper::INDEXES.each do |index|
15
+ Waistband::Index.new(index).create!
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'waistband/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "waistband"
8
+ spec.version = Waistband::VERSION
9
+ spec.authors = ["David Jairala"]
10
+ spec.email = ["davidjairala@gmail.com"]
11
+ spec.description = %q{Ruby interface to Elastic Search}
12
+ spec.summary = %q{Elastic Search all the things!}
13
+ spec.homepage = "https://github.com/taskrabbit/waistband"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activesupport', '>= 3.0.0'
22
+ spec.add_dependency 'rest-client', '~> 1.6.7'
23
+ spec.add_dependency 'json', '~> 1.8.0'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.2.3"
26
+ spec.add_development_dependency "rake"
27
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: waistband
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.15
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Jairala
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.6.7
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.2.3
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.2.3
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Ruby interface to Elastic Search
95
+ email:
96
+ - davidjairala@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - .rvmrc
104
+ - Gemfile
105
+ - LICENSE.txt
106
+ - README.md
107
+ - Rakefile
108
+ - lib/waistband.rb
109
+ - lib/waistband/configuration.rb
110
+ - lib/waistband/index.rb
111
+ - lib/waistband/model.rb
112
+ - lib/waistband/query.rb
113
+ - lib/waistband/query_result.rb
114
+ - lib/waistband/quick_error.rb
115
+ - lib/waistband/stringify_all.rb
116
+ - lib/waistband/version.rb
117
+ - spec/config/waistband/waistband.yml
118
+ - spec/config/waistband/waistband_events.yml
119
+ - spec/config/waistband/waistband_search.yml
120
+ - spec/lib/configuration_spec.rb
121
+ - spec/lib/index_spec.rb
122
+ - spec/lib/model_spec.rb
123
+ - spec/lib/query_result_spec.rb
124
+ - spec/lib/query_spec.rb
125
+ - spec/lib/stringify_all_spec.rb
126
+ - spec/spec_helper.rb
127
+ - spec/support/index_helper.rb
128
+ - waistband.gemspec
129
+ homepage: https://github.com/taskrabbit/waistband
130
+ licenses:
131
+ - MIT
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 1.8.25
151
+ signing_key:
152
+ specification_version: 3
153
+ summary: Elastic Search all the things!
154
+ test_files:
155
+ - spec/config/waistband/waistband.yml
156
+ - spec/config/waistband/waistband_events.yml
157
+ - spec/config/waistband/waistband_search.yml
158
+ - spec/lib/configuration_spec.rb
159
+ - spec/lib/index_spec.rb
160
+ - spec/lib/model_spec.rb
161
+ - spec/lib/query_result_spec.rb
162
+ - spec/lib/query_spec.rb
163
+ - spec/lib/stringify_all_spec.rb
164
+ - spec/spec_helper.rb
165
+ - spec/support/index_helper.rb