srchio 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- OTBkM2M3MzQyNjQ1MzM1YzM0MDQ2MDZlMzFmMTdkOGQyYTEyMmY1NQ==
5
- data.tar.gz: !binary |-
6
- YTJhNjJiN2UwMDJiOTllMzFhYzMyYzRkMjJiYTI0Y2E4ZTFjNWMyNg==
2
+ SHA1:
3
+ metadata.gz: c88ec650b5d8a0f702bd76a8116d08fdeccf8b39
4
+ data.tar.gz: 684d660029a1a5fdc1a75a12edf5b42691bea66f
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- MWY0OTk4YmJmYjY3MjkyZDdjMGI4NGFlY2RmMDQxOWJjY2I4YzViMmQ1ZWNi
10
- MDJlYmQ4NTY2ODQ3YTBlYTYzNjQxNDE0YTA5ZTkxNmYyNDdhNjMwZDVjNWI2
11
- MmU0ODJkYzUxMzAzNTI3OTFlOGU3MDFkMGFlZTA2YzJlOTQ5NjA=
12
- data.tar.gz: !binary |-
13
- ODZkYzZmMjU0YmYxOTVkMDM1ZjRjZTdiNTY0MDA5NmZlNDlmZDJlZDVjOWFj
14
- MzQ3M2U0ZjBhZDJkODc4NjM0MzkxZDYzYTIwZmQxZjc0ZjdkNTc0ZWE4YjI0
15
- NDM3Mjk1YmJmYzZjZThlMjM2YTg0ZTJhMTRjYmE0NTI1YjZkNTA=
6
+ metadata.gz: 1612e171146e36f41f4072b49fc82ec92679866ab9ddde8b740e41f51de318e81062e068ec5bb5eccde56063e76b1b1aa5c1a68d7f20131d12c1d03b3a0f6dad
7
+ data.tar.gz: f28473469566460da5020ec5559938037f4ebd3f3c26ed70ad9ee421a8dcdced4f2d1ee56ea9ec318ba2106106c2acbbb6e276ab1b8dc6c433bcacea07495bcb
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'httparty'
3
+ gem 'httparty'
4
+ gem 'activesupport', '>= 3.2.0'
data/README.md CHANGED
@@ -38,4 +38,57 @@ If you need to delete a document, you can do it either with your remote_id or th
38
38
 
39
39
  <pre><code>client.destroy_document(remote_id: 1)</code></pre>
40
40
 
41
+ ## Using The ActiveSupport::Concern
42
+
43
+ Since we're so nice, we went ahead and created a handy ActiveSupport::Concern to include in your models to get all the srch.io goodness right in your model and the make wiring things up faster. Here's how you'd use it in a standard ActiveRecord model:
44
+
45
+ <pre><code>class Foo < ActiveRecord::Base
46
+ # Tell srchio where to get the fields to index (these are the methods on your model that provide those values, like foo.id):
47
+
48
+ configure_srch searcher_id: 1,
49
+ title: :foo_title,
50
+ body: :text,
51
+ remote_id: :id,
52
+ created: :created_at,
53
+ url: :url_generator
54
+
55
+ after_save :srch_save
56
+ after_destroy :srch_destroy
57
+ end</code></pre>
58
+
59
+ If you're adding search to an existing model, you'll probably want to do something like the following to get all of your documents indexed:
60
+
61
+ <pre><code>Foo.find_each do |foo|
62
+ foo.srch_save
63
+ end</code></pre>
64
+
65
+ And to search for documents:
66
+
67
+ <pre><code>results = Foo.srch(query: "Bob", page: 1, per_page: 25)</code></pre>
68
+
69
+ And that's pretty much it!
70
+
71
+ If you're like me and don't like waiting around for anything, you could do something like the following to push indexing and destroying records into your [Sidekiq](http://sidekiq.org) queue:
72
+
73
+ <pre><code>class FooIndexWorker
74
+ include Sidekiq::Worker
75
+
76
+ def perform(id)
77
+ Foo.find(id).srch_save
78
+ end
79
+ end</code></pre>
80
+
81
+ And to destroy the document:
82
+
83
+ <pre><code>class FooDestroyWorker
84
+ include Sidekiq::Worker
85
+
86
+ def perform(id)
87
+ Foo.srch_destroy(remote_id: id)
88
+ end
89
+ end</code></pre>
90
+
91
+ Then you'd just change your before and after filters to shove things into the queue instead of doing it right there. Adding and deleting documents is fast, but we like keeping things as async as possible to make the experience for your users better.
92
+ ## Help!
93
+
41
94
  If you have any issues, please create a [Github Issue](https://github.com/railsmachine/srchio/issues). Thanks for using srch.io!
@@ -0,0 +1,131 @@
1
+ require 'active_support'
2
+
3
+ module Srchio
4
+ module Concern
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+
9
+ =begin rdoc
10
+ srch_save: Sends the document to your searcher. Could be used as an after_filter or in a job.
11
+ =end
12
+ def srch_save
13
+ doc = {
14
+ :body => srch_send(:body),
15
+ :title => srch_send(:title),
16
+ :url => srch_send(:url),
17
+ :remote_id => srch_send(:remote_id)
18
+ }
19
+
20
+ if self.class.srch_config.keys.include?(:tags)
21
+ doc[:tags] = srch_send(:tags)
22
+ end
23
+
24
+ if self.class.srch_config.keys.include?(:created)
25
+ doc[:created] = srch_send(:created)
26
+ end
27
+
28
+ self.class.srch_add(doc)
29
+ end
30
+
31
+ =begin rdoc
32
+ srch_destroy: Deletes the document from your searcher. Best used in an after_filter.
33
+ =end
34
+ def srch_destroy
35
+ self.class.srch_destroy(:remote_id => srch_send(:remote_id))
36
+ end
37
+
38
+ def srch_send(key)
39
+ self.send(self.class.srch_config[key])
40
+ end
41
+
42
+ end
43
+
44
+ module ClassMethods
45
+
46
+ =begin rdoc
47
+ configure_srch: Configure the client for this model, mapping fields and setting the searcher id to use for all API call.
48
+
49
+ options:
50
+ * searcher_id: the id for the searcher to use. This should probably be set in an environment variable, so you don't accidentally use development data in your production searcher, for example.
51
+ * title: The method to use as the title when saving a document.
52
+ * body: The method to use as the body when saving a document.
53
+ * url: The method to use for the URL when saving a document.
54
+ * tags: The method to use for tags when saving a document.
55
+ * created: The method to use for setting the timestamp on a document.
56
+ * remote_id: The method to use for setting the id on a document (probably should be <code>:id</code>.
57
+ =end
58
+ def configure_srch(opts={})
59
+ raise "SearcherIdRequired" if opts[:searcher_id].nil?
60
+ raise "TitleRequired" if opts[:title].nil?
61
+ raise "BodyRequired" if opts[:body].nil?
62
+ raise "UrlRequired" if opts[:url].nil?
63
+ raise "RemoteIdRequired" if opts[:remote_id].nil?
64
+
65
+ @@srch_config = opts
66
+ end
67
+
68
+ def srch_config
69
+ @@srch_config
70
+ end
71
+
72
+ def srch_config=(opts)
73
+ @@srch_config = opts
74
+ end
75
+
76
+ =begin rdoc
77
+ srch_client: The client used to do all the API calls. The concern provides method wrappers for everything, so you probably don't need to call this directly.
78
+ =end
79
+ def srch_client
80
+ @@client ||= Srch::Client.new(:searcher_id => srch_config[:searcher_id])
81
+ end
82
+
83
+ =begin rdoc
84
+ srch: Search your content!
85
+
86
+ options:
87
+
88
+ * query: Query all the fields.
89
+ * page: The page of results to return, defaults to 1.
90
+ * per_page: The number of results to return per page. Defaults to 25, max of 100.
91
+ * body: Search just the body for something.
92
+ * title: Search just the title for something.
93
+ * tags: Find documents by their tags.
94
+ * remote_id: Find a single document by the remote_id.
95
+ =end
96
+ def srch(opts={})
97
+ srch_client.search(opts)
98
+ end
99
+
100
+ =begin rdoc
101
+ srch_destroy: Destroy a document.
102
+
103
+ options: (one of them is required)
104
+
105
+ * remote_id: The id in your system for the document.
106
+ * index_id: The id for the document in our system.
107
+ =end
108
+ def srch_destroy(opts={})
109
+ srch_client.destroy_document(opts)
110
+ end
111
+
112
+ =begin rdoc
113
+ srch_add: Add a document to your searcher.
114
+
115
+ options:
116
+
117
+ * title: *required*
118
+ * body: *required*
119
+ * url: *required*
120
+ * remote_id: *recommended* The id in your system for the object being added.
121
+ * tags: An array or comma-separated list of tags.
122
+ * created: The creation timestamp for your document.
123
+ =end
124
+ def srch_add(opts={})
125
+ srch_client.add_document(opts)
126
+ end
127
+
128
+ end
129
+
130
+ end
131
+ end
@@ -1,3 +1,3 @@
1
1
  module Srchio
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/srchio.rb CHANGED
@@ -3,6 +3,7 @@ require 'srchio/client'
3
3
  require 'srchio/response'
4
4
  require 'srchio/result'
5
5
  require 'srchio/version'
6
+ require 'srchio/concern'
6
7
 
7
8
  module Srchio
8
9
 
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: srchio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Lawver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-14 00:00:00.000000000 Z
11
+ date: 2014-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.12.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.12.0
27
27
  description: The official gem and wrapper for the srch.io API. More info @ http://srch.io
@@ -35,6 +35,7 @@ files:
35
35
  - README.md
36
36
  - lib/srchio.rb
37
37
  - lib/srchio/client.rb
38
+ - lib/srchio/concern.rb
38
39
  - lib/srchio/response.rb
39
40
  - lib/srchio/result.rb
40
41
  - lib/srchio/version.rb
@@ -49,12 +50,12 @@ require_paths:
49
50
  - lib
50
51
  required_ruby_version: !ruby/object:Gem::Requirement
51
52
  requirements:
52
- - - ! '>='
53
+ - - '>='
53
54
  - !ruby/object:Gem::Version
54
55
  version: 1.9.3
55
56
  required_rubygems_version: !ruby/object:Gem::Requirement
56
57
  requirements:
57
- - - ! '>='
58
+ - - '>='
58
59
  - !ruby/object:Gem::Version
59
60
  version: '0'
60
61
  requirements: []