activesearch 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MjhhY2NlMzY1NDJmZGU1YjdlNzk0NGIyOGFhOTM5M2RkNjA3YTM1MA==
5
+ data.tar.gz: !binary |-
6
+ YTdhYzQwM2NiNGI5NWFiYmY5ZjA3YzYyOGU4ZTJhMjU0ZTY5YTRjMQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YzAxNTg0M2NkYzRiYmE2MzMzOThhZTI4MzkyMWU5N2Y2YjZmMGMyOGJhZmQy
10
+ ODk5MTBiNTczNTkwNjYwODdkM2JmM2Q0ODViNmE4NWYwNzg1OTU5NGZkMWUz
11
+ NGZmYzI4YjM0NzA5YTFlYjFiNWE1Y2IzOGQ5NDMwNDc3NTc3NmQ=
12
+ data.tar.gz: !binary |-
13
+ YWM5Mzg0YjA5MDllZjhhZDllMzBkZDlkODU2YWRlZDQ0NjU2YTk0ZWQ4Yzg2
14
+ MGZlYmViMWNmNmY0MGYxYTdlYjNiYTY3NzNkNDA3MjlmZDY2YWRhYmQ4NTky
15
+ YmIzMzg5MzYyOGMwM2NlYzFhOWIyZDQxNTMzMWZmNTI5NzEyNmQ=
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ config/algolia.yml
1
2
  *.gem
2
3
  *.rbc
3
4
  .bundle
@@ -16,4 +17,5 @@ test/tmp
16
17
  test/version_tmp
17
18
  tmp
18
19
  log
20
+
19
21
  *.log
data/README.md CHANGED
@@ -18,6 +18,19 @@ It works by storing keywords taken from the specified fields and storing them in
18
18
 
19
19
  gem 'tire'
20
20
  gem 'activesearch'
21
+
22
+ ###Algolia
23
+
24
+ gem 'httparty'
25
+ gem 'activesearch'
26
+
27
+ Then you need to configure the client. In rails, put this in an algolia.rb initializer:
28
+
29
+ Algolia.configure do |c|
30
+ c.api_key = "1234567890"
31
+ c.app_id = "someapp"
32
+ c.host = "https://apieu1.algolia.com"
33
+ end
21
34
 
22
35
  ##Configuration
23
36
 
data/activesearch.gemspec CHANGED
@@ -26,4 +26,5 @@ Gem::Specification.new do |gem|
26
26
  gem.add_development_dependency "mongoid", "~> 2"
27
27
  gem.add_development_dependency "tire"
28
28
  gem.add_development_dependency "parallel_tests"
29
+ gem.add_development_dependency "httparty"
29
30
  end
@@ -0,0 +1,2 @@
1
+ api_key: "1234567890"
2
+ app_id: "someapp"
@@ -0,0 +1,34 @@
1
+ require "httparty"
2
+ require "active_support/core_ext/object/to_json"
3
+
4
+ module ActiveSearch::Algolia
5
+ class Client
6
+ include HTTParty
7
+
8
+ def self.configure(api_key, app_id, index = "activesearch")
9
+ base_uri "https://apieu1.algolia.com/1/indexes/#{index}"
10
+ headers({
11
+ "X-Algolia-API-Key" => api_key,
12
+ "X-Algolia-Application-Id" => app_id,
13
+ "Content-Type" => "application/json; charset=utf-8"
14
+ })
15
+ end
16
+
17
+ def delete_index
18
+ self.class.delete("")
19
+ end
20
+
21
+ def delete(id)
22
+ return false if id.nil?
23
+ self.class.delete("/#{id}")
24
+ end
25
+
26
+ def save(id, object)
27
+ self.class.put("/#{id}", body: object.to_json)
28
+ end
29
+
30
+ def query(text)
31
+ self.class.get("", query: {query: text})
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,54 @@
1
+ require "activesearch/algolia/client"
2
+ require "activesearch/base"
3
+ require "activesearch/proxy"
4
+
5
+ module ActiveSearch
6
+ def self.search(text)
7
+ Proxy.new(text) do |text|
8
+ Algolia::Client.new.query(text)["hits"].map! do |hit|
9
+ if hit["_tags"]
10
+ hit["_tags"].each do |tag|
11
+ k, v = tag.split(':')
12
+ hit[k] = v
13
+ end
14
+ hit.delete("_tags")
15
+ end
16
+ hit
17
+ end
18
+ end
19
+ end
20
+
21
+ module Algolia
22
+ def self.included(base)
23
+ base.class_eval do
24
+ include Base
25
+ end
26
+ end
27
+
28
+ protected
29
+ def reindex
30
+ algolia_client.save(indexable_id, self.to_indexable)
31
+ end
32
+
33
+ def deindex
34
+ algolia_client.delete(indexable_id)
35
+ end
36
+
37
+ def to_indexable
38
+ doc = {}
39
+ search_fields.each do |field|
40
+ doc[field.to_s] = attributes[field.to_s] if attributes[field.to_s]
41
+ end
42
+
43
+ (Array(search_options[:store]) - search_fields).each do |field|
44
+ doc["_tags"] ||= []
45
+ doc["_tags"] << "#{field}:#{self.send(field)}"
46
+ end
47
+ doc
48
+ end
49
+
50
+ def algolia_client
51
+ @algolia_client ||= Client.new
52
+ end
53
+ end
54
+ end
@@ -1,12 +1,18 @@
1
1
  require 'tire'
2
2
  require "active_support/core_ext"
3
3
  require "activesearch/base"
4
- require "activesearch/elastic_search/proxy"
4
+ require "activesearch/proxy"
5
5
 
6
6
  module ActiveSearch
7
7
 
8
8
  def self.search(text)
9
- ElasticSearch::Proxy.new(text)
9
+ Proxy.new(text) do |text|
10
+ Tire.search('_all') do |search|
11
+ search.query do |query|
12
+ query.match("_all", text)
13
+ end
14
+ end.results
15
+ end
10
16
  end
11
17
 
12
18
  module ElasticSearch
@@ -0,0 +1,16 @@
1
+ require "activesearch/result"
2
+
3
+ module ActiveSearch
4
+ class Proxy
5
+ include Enumerable
6
+
7
+ def initialize(text, &implementation)
8
+ @text = text
9
+ @implementation = implementation
10
+ end
11
+
12
+ def each(&block)
13
+ @implementation.call(@text).each { |result| block.call(Result.new(result)) }
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveSearch
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
3
3
  end
data/spec/engines_spec.rb CHANGED
@@ -4,6 +4,11 @@ require File.join(File.dirname(__FILE__), 'spec_helper')
4
4
 
5
5
  def cleanup(engine)
6
6
  case engine
7
+ when "Algolia"
8
+ YAML.load_file(File.dirname(__FILE__) + '/../config/algolia.yml').tap do |config|
9
+ ActiveSearch::Algolia::Client.configure(config["api_key"], config["app_id"])
10
+ end
11
+ ActiveSearch::Algolia::Client.new.delete_index
7
12
  when "ElasticSearch"
8
13
  Tire::Configuration.client.delete "#{Tire::Configuration.url}/_all"
9
14
  load File.join(File.dirname(__FILE__), 'models', 'elastic_search.rb')
@@ -0,0 +1,24 @@
1
+ require 'activesearch/algolia'
2
+
3
+ class AlgoliaModel < ActiveMimic
4
+ include ActiveSearch::Algolia
5
+
6
+ attribute :title
7
+ attribute :text
8
+ attribute :junk
9
+ attribute :special, default: false
10
+
11
+ search_by [:title, :text, store: [:title, :junk]], if: lambda { !self.special }
12
+
13
+ end
14
+
15
+ class AnotherAlgoliaModel < ActiveMimic
16
+ include ActiveSearch::Algolia
17
+
18
+ attribute :title, type: String
19
+ search_by [:title, store: [:title, :virtual]]
20
+
21
+ def virtual
22
+ "virtual"
23
+ end
24
+ end
data/spec/spec_helper.rb CHANGED
@@ -18,6 +18,10 @@ class ActiveMimic
18
18
  new(attrs).tap(&:save)
19
19
  end
20
20
 
21
+ def indexable_id
22
+ "#{self.class.to_s}_#{self.id}"
23
+ end
24
+
21
25
  def type
22
26
  self.class.to_s
23
27
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activesearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
5
- prerelease:
4
+ version: 0.0.13
6
5
  platform: ruby
7
6
  authors:
8
7
  - Rodrigo Alvarez
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-04-04 00:00:00.000000000 Z
11
+ date: 2013-04-19 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec-mocks
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: bson_ext
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ! '>='
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ! '>='
76
67
  - !ruby/object:Gem::Version
@@ -78,7 +69,6 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: active_attr
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ! '>='
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ! '>='
92
81
  - !ruby/object:Gem::Version
@@ -94,7 +83,6 @@ dependencies:
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: mongoid
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
87
  - - ~>
100
88
  - !ruby/object:Gem::Version
@@ -102,7 +90,6 @@ dependencies:
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
94
  - - ~>
108
95
  - !ruby/object:Gem::Version
@@ -110,7 +97,6 @@ dependencies:
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: tire
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
101
  - - ! '>='
116
102
  - !ruby/object:Gem::Version
@@ -118,7 +104,6 @@ dependencies:
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
108
  - - ! '>='
124
109
  - !ruby/object:Gem::Version
@@ -126,7 +111,6 @@ dependencies:
126
111
  - !ruby/object:Gem::Dependency
127
112
  name: parallel_tests
128
113
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
114
  requirements:
131
115
  - - ! '>='
132
116
  - !ruby/object:Gem::Version
@@ -134,7 +118,20 @@ dependencies:
134
118
  type: :development
135
119
  prerelease: false
136
120
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: httparty
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
138
135
  requirements:
139
136
  - - ! '>='
140
137
  - !ruby/object:Gem::Version
@@ -152,50 +149,54 @@ files:
152
149
  - README.md
153
150
  - Rakefile
154
151
  - activesearch.gemspec
152
+ - config/sample.algolia.yml
155
153
  - lib/activesearch.rb
154
+ - lib/activesearch/algolia.rb
155
+ - lib/activesearch/algolia/client.rb
156
156
  - lib/activesearch/base.rb
157
157
  - lib/activesearch/elastic_search.rb
158
- - lib/activesearch/elastic_search/proxy.rb
159
158
  - lib/activesearch/mongoid.rb
160
159
  - lib/activesearch/mongoid/model.rb
160
+ - lib/activesearch/proxy.rb
161
161
  - lib/activesearch/result.rb
162
162
  - lib/activesearch/version.rb
163
163
  - spec/base_spec.rb
164
164
  - spec/config/mongoid.yml
165
165
  - spec/engines_spec.rb
166
+ - spec/models/algolia.rb
166
167
  - spec/models/elastic_search.rb
167
168
  - spec/models/mongoid.rb
168
169
  - spec/mongoid_spec.rb
169
170
  - spec/spec_helper.rb
170
171
  homepage: ''
171
172
  licenses: []
173
+ metadata: {}
172
174
  post_install_message:
173
175
  rdoc_options: []
174
176
  require_paths:
175
177
  - lib
176
178
  required_ruby_version: !ruby/object:Gem::Requirement
177
- none: false
178
179
  requirements:
179
180
  - - ! '>='
180
181
  - !ruby/object:Gem::Version
181
182
  version: '0'
182
183
  required_rubygems_version: !ruby/object:Gem::Requirement
183
- none: false
184
184
  requirements:
185
185
  - - ! '>='
186
186
  - !ruby/object:Gem::Version
187
187
  version: '0'
188
188
  requirements: []
189
189
  rubyforge_project:
190
- rubygems_version: 1.8.24
190
+ rubygems_version: 2.0.3
191
191
  signing_key:
192
- specification_version: 3
192
+ specification_version: 4
193
193
  summary: ActiveSearch lets you plug in a ruby module in any class that will allow
194
194
  you to do full text searches.
195
195
  test_files:
196
196
  - spec/base_spec.rb
197
197
  - spec/config/mongoid.yml
198
198
  - spec/engines_spec.rb
199
+ - spec/models/algolia.rb
199
200
  - spec/models/elastic_search.rb
200
201
  - spec/models/mongoid.rb
201
202
  - spec/mongoid_spec.rb
@@ -1,26 +0,0 @@
1
- require "activesearch/result"
2
-
3
- module ActiveSearch
4
- module ElasticSearch
5
- class Proxy
6
- include Enumerable
7
-
8
- def initialize(text)
9
- @text = text
10
- end
11
-
12
- def each(&block)
13
- search.results.each { |result| block.call(Result.new(result)) }
14
- end
15
-
16
- protected
17
- def search
18
- @search ||= Tire.search('_all') do |search|
19
- search.query do |query|
20
- query.match("_all", @text)
21
- end
22
- end
23
- end
24
- end
25
- end
26
- end