flare 0.0.1

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ flare-solr.pid
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Michael Dwan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = flare
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Michael Dwan. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "flare"
8
+ gem.executables= 'flare-solr'
9
+ gem.summary = 'This needs to get updated'
10
+ gem.description = 'This needs to get updated'
11
+ gem.email = "mpdwan@gmail.com"
12
+ gem.homepage = "http://github.com/michaeldwan/flare"
13
+ gem.authors = ["Michael Dwan"]
14
+ gem.add_dependency('rsolr', '>= 0.9.6')
15
+ gem.add_dependency('escape', '>= 0.0.4')
16
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
22
+ end
23
+
24
+ require 'rake/testtask'
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << 'lib' << 'test'
27
+ test.pattern = 'test/**/test_*.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/test_*.rb'
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+ task :test => :check_dependencies
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "flare #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/bin/flare-solr ADDED
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env ruby
2
+ using_gems = false
3
+ begin
4
+ require 'fileutils'
5
+ require 'tempfile'
6
+ require 'tmpdir'
7
+ require 'optparse'
8
+ require 'daemons'
9
+ rescue LoadError => e
10
+ if using_gems
11
+ raise(e)
12
+ else
13
+ using_gems = true
14
+ require 'rubygems'
15
+ retry
16
+ end
17
+ end
18
+
19
+ working_directory = FileUtils.pwd
20
+ solr_install = File.expand_path(File.join(File.dirname(__FILE__), '..', 'solr'))
21
+
22
+ port = '8983'
23
+ data_dir = File.expand_path(File.join(Dir.tmpdir, 'solr_data'))
24
+ solr_home = File.join(solr_install, 'solr')
25
+ pid_dir = working_directory
26
+ log_file = nil
27
+ log_level = 'OFF'
28
+
29
+ OptionParser.new do |opts|
30
+ opts.banner = "Usage: flare-solr start [options]"
31
+
32
+ opts.on '-p', '--port=PORT', 'Port on which to run Solr (default 8983)' do |p|
33
+ port = p
34
+ end
35
+
36
+ opts.on '-d', '--data-directory=DIRECTORY', 'Solr data directory' do |d|
37
+ data_dir = File.expand_path(d)
38
+ end
39
+
40
+ opts.on '-s', '--solr-home=HOME', 'Solr home directory (should contain conf/ directory)' do |s|
41
+ solr_home = File.expand_path(s)
42
+ end
43
+
44
+ opts.on '--pid-dir=PID_DIR', 'Directory for pid files' do |pd|
45
+ pid_dir = File.expand_path(pd)
46
+ end
47
+
48
+ opts.on '-l', '--log-level=LOG_LEVEL', 'Solr logging level' do |l|
49
+ log_level = l
50
+ end
51
+
52
+ opts.on '--log-file=LOG_FILE', 'Path to Solr log file' do |lf|
53
+ log_file = File.expand_path(lf)
54
+ end
55
+ end.parse!
56
+
57
+ options = { :dir_mode => :normal, :dir => pid_dir }
58
+
59
+ def logging_properties( log_file, log_level )
60
+ temp_file = Tempfile.new 'logging.properties'
61
+ temp_file << <<PROPERTIES
62
+ .level = #{log_level}
63
+ handlers = java.util.logging.FileHandler
64
+ java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
65
+ java.util.logging.FileHandler.pattern = #{log_file}
66
+ PROPERTIES
67
+ temp_file.flush
68
+ temp_file.close
69
+ temp_file.path
70
+ end
71
+
72
+ Daemons.run_proc('flare-solr', options) do
73
+ FileUtils.cd(solr_install) do
74
+ args = ['java']
75
+ args << "-Djetty.port=#{port}" if port
76
+ args << "-Dsolr.data.dir=#{data_dir}" if data_dir
77
+ args << "-Dsolr.solr.home=#{solr_home}" if solr_home
78
+ args << "-Djava.util.logging.config.file=#{logging_properties(log_file, log_level)}" if log_file and log_level != 'OFF'
79
+ args << '-jar' << File.join(solr_install, 'start.jar')
80
+ STDERR.puts(args * ' ')
81
+ Kernel.exec(*args)
82
+ end
83
+ end
data/flare.gemspec ADDED
@@ -0,0 +1,91 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{flare}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Dwan"]
12
+ s.date = %q{2009-11-03}
13
+ s.default_executable = %q{flare-solr}
14
+ s.description = %q{This needs to get updated}
15
+ s.email = %q{mpdwan@gmail.com}
16
+ s.executables = ["flare-solr"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/flare-solr",
29
+ "flare.gemspec",
30
+ "lib/flare.rb",
31
+ "lib/flare/active_record.rb",
32
+ "lib/flare/collection.rb",
33
+ "lib/flare/configuration.rb",
34
+ "lib/flare/index_builder.rb",
35
+ "lib/flare/session.rb",
36
+ "lib/flare/tasks.rb",
37
+ "solr/etc/jetty.xml",
38
+ "solr/etc/webdefault.xml",
39
+ "solr/lib/jetty-6.1.3.jar",
40
+ "solr/lib/jetty-util-6.1.3.jar",
41
+ "solr/lib/jsp-2.1/ant-1.6.5.jar",
42
+ "solr/lib/jsp-2.1/core-3.1.1.jar",
43
+ "solr/lib/jsp-2.1/jsp-2.1.jar",
44
+ "solr/lib/jsp-2.1/jsp-api-2.1.jar",
45
+ "solr/lib/servlet-api-2.5-6.1.3.jar",
46
+ "solr/solr/conf/elevate.xml",
47
+ "solr/solr/conf/protwords.txt",
48
+ "solr/solr/conf/schema.xml",
49
+ "solr/solr/conf/solrconfig.xml",
50
+ "solr/solr/conf/stopwords.txt",
51
+ "solr/solr/conf/synonyms.txt",
52
+ "solr/solr/lib/geoapi-nogenerics-2.1-M2.jar",
53
+ "solr/solr/lib/gt2-referencing-2.3.1.jar",
54
+ "solr/solr/lib/jsr108-0.01.jar",
55
+ "solr/solr/lib/locallucene.jar",
56
+ "solr/solr/lib/localsolr.jar",
57
+ "solr/start.jar",
58
+ "solr/webapps/solr.war",
59
+ "test/helper.rb",
60
+ "test/test_flare.rb"
61
+ ]
62
+ s.homepage = %q{http://github.com/michaeldwan/flare}
63
+ s.rdoc_options = ["--charset=UTF-8"]
64
+ s.require_paths = ["lib"]
65
+ s.rubygems_version = %q{1.3.5}
66
+ s.summary = %q{This needs to get updated}
67
+ s.test_files = [
68
+ "test/helper.rb",
69
+ "test/test_flare.rb"
70
+ ]
71
+
72
+ if s.respond_to? :specification_version then
73
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
74
+ s.specification_version = 3
75
+
76
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
77
+ s.add_runtime_dependency(%q<rsolr>, [">= 0.9.6"])
78
+ s.add_runtime_dependency(%q<escape>, [">= 0.0.4"])
79
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
80
+ else
81
+ s.add_dependency(%q<rsolr>, [">= 0.9.6"])
82
+ s.add_dependency(%q<escape>, [">= 0.0.4"])
83
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
84
+ end
85
+ else
86
+ s.add_dependency(%q<rsolr>, [">= 0.9.6"])
87
+ s.add_dependency(%q<escape>, [">= 0.0.4"])
88
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
89
+ end
90
+ end
91
+
@@ -0,0 +1,102 @@
1
+ module Flare
2
+ module ActiveRecord
3
+ def self.included(base)
4
+ base.extend Hook
5
+ end
6
+
7
+ module Hook
8
+ def searchable(&block)
9
+ Flare.indexed_models << self unless Flare.indexed_models.include?(self)
10
+
11
+ builder = IndexBuilder.new(&block)
12
+ solr_index = builder.index
13
+
14
+ write_inheritable_attribute :solr_index, solr_index
15
+ class_inheritable_reader :solr_index
16
+
17
+ after_save :solr_save
18
+ after_destroy :solr_destroy
19
+
20
+ include InstanceMethods
21
+ extend ClassMethods
22
+ end
23
+
24
+ def searchable?
25
+ false
26
+ end
27
+ end
28
+
29
+ module ClassMethods
30
+ def search_for_ids(*args)
31
+ options = args.extract_options!
32
+ options[:types] ||= []
33
+ options[:types] << self
34
+ Flare.session.search_for_ids(*[args, options].flatten)
35
+ end
36
+
37
+ def search(*args)
38
+ options = args.extract_options!
39
+ options[:types] ||= []
40
+ options[:types] << self
41
+ Flare.session.search(*[args, options].flatten)
42
+ end
43
+
44
+ def search_count(*args)
45
+ options = args.extract_options!
46
+ options[:types] ||= []
47
+ options[:types] << self
48
+ Flare.session.count(*[args, options].flatten)
49
+ end
50
+
51
+ def searchable?
52
+ true
53
+ end
54
+
55
+ def rebuild_solr_index
56
+ total = self.count
57
+ count = 0
58
+ self.find_in_batches(:batch_size => 100) do |batch|
59
+ Flare.session.index(batch)
60
+ count += batch.length
61
+ printf "\r#{count}/#{total} complete"
62
+ STDOUT.flush
63
+ end
64
+ puts
65
+ Flare.session.commit
66
+ Flare.session.optimize
67
+ end
68
+
69
+ def clear_solr_index
70
+ Flare.session.remove_all!(self)
71
+ end
72
+ end
73
+
74
+ module InstanceMethods
75
+ def to_solr_doc
76
+ doc = { :id => solr_document_id, :type => self.class.name }
77
+ solr_index[:fields].each do |field|
78
+ value = send(field[:source])
79
+ # Need to convert dates to utc xmlschema.
80
+ #TODO: move this translation to rsolr gem
81
+ if value.respond_to?(:utc)
82
+ value = value.utc.xmlschema
83
+ end
84
+ doc[field[:name]] = value
85
+ end
86
+ doc
87
+ end
88
+
89
+ def solr_document_id
90
+ "#{self.class.name}:#{self.id}"
91
+ end
92
+
93
+ def solr_save
94
+ Flare.session.index!(self)
95
+ end
96
+
97
+ def solr_destroy
98
+ Flare.session.remove!(self)
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,47 @@
1
+ module Flare
2
+ class Collection < ::Array
3
+ attr_reader :total_entries, :total_pages, :current_page, :per_page
4
+ attr_accessor :response
5
+
6
+ def initialize(page, per_page, total)
7
+ @current_page, @per_page, @total_entries = page, per_page, total
8
+
9
+ @total_pages = (@total_entries / @per_page.to_f).ceil
10
+ end
11
+
12
+ def self.ids_from_response(response, page, per_page, options)
13
+ collection = self.new(page, per_page, response['response']['numFound'] || 0)
14
+ collection.response = response
15
+ collection.replace(response['response']['docs'].map {|doc| doc['id']})
16
+ return collection
17
+ end
18
+
19
+ def self.create_from_response(response, page, per_page, options)
20
+ # raise response.inspect
21
+ collection = self.new(page, per_page, response['response']['numFound'] || 0)
22
+ collection.response = response
23
+ collection.replace(instantiate_objects(response))
24
+ return collection
25
+ end
26
+
27
+ def previous_page
28
+ current_page > 1 ? (current_page - 1) : nil
29
+ end
30
+
31
+ def next_page
32
+ current_page < total_pages ? (current_page + 1): nil
33
+ end
34
+
35
+ def offset
36
+ (current_page - 1) * @per_page
37
+ end
38
+
39
+ private
40
+ def self.instantiate_objects(response)
41
+ response['response']['docs'].map do |doc|
42
+ type, id = doc['id'].split(':')
43
+ type.constantize.find(id)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,76 @@
1
+ require 'yaml'
2
+
3
+ module Flare
4
+ class Configuration
5
+ class << self
6
+ attr_reader :client, :server
7
+
8
+ def server(reload = false)
9
+ @server = nil if reload
10
+ @server ||= Server.new
11
+ end
12
+
13
+ def client(reload = false)
14
+ @client = nil if reload
15
+ @client = Client.new
16
+ end
17
+ end
18
+
19
+ private
20
+ class Server
21
+ def initialize
22
+ @config = YAML::load_file(File.join(Rails.root, 'config', 'solr.yml'))[Rails.env]
23
+ end
24
+
25
+ def port
26
+ @port ||= (@config['port'] || 8983).to_i
27
+ end
28
+
29
+ def log_dir
30
+ @log_dir ||= File.expand_path(@config['log_dir'] || 'log')
31
+ end
32
+
33
+ def log_level
34
+ @log_level ||= @config['log_level'] || 'INFO'
35
+ end
36
+
37
+ def data_dir
38
+ @data_dir ||= File.expand_path(@config['data_dir'] || File.join('solr', 'data', Rails.env))
39
+ end
40
+
41
+ def solr_home
42
+ @solr_home ||= File.expand_path(@config['solr_home'] || File.join(File.dirname(__FILE__), '..', '..', 'solr', 'solr'))
43
+ end
44
+
45
+ def pid_dir
46
+ @pid_dir ||= File.expand_path(@config['pid_dir'] || 'tmp/pids')
47
+ end
48
+
49
+ def jvm_options
50
+ @jvm_options ||= @config['jvm_options']
51
+ end
52
+ end
53
+
54
+ class Client
55
+ def initialize
56
+ @config = YAML::load_file(File.join(Rails.root, 'config', 'flare.yml'))[Rails.env]
57
+ end
58
+
59
+ def port
60
+ @port ||= (@config['port'] || 8983).to_i
61
+ end
62
+
63
+ def host
64
+ @host ||= @config['host'] || '127.0.0.1'
65
+ end
66
+
67
+ def path
68
+ @path ||= @config['path'] || 'solr'
69
+ end
70
+
71
+ def url
72
+ @url ||= "http://#{host}:#{port}/#{path}"
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,24 @@
1
+ module Flare
2
+ class IndexBuilder
3
+ attr_reader :fields
4
+
5
+ def initialize(&block)
6
+ @fields = []
7
+ self.instance_eval(&block) if block_given?
8
+ end
9
+
10
+ def index
11
+ { :fields => @fields }
12
+ end
13
+
14
+ protected
15
+ def field(*args)
16
+ field, options = args.first, args.extract_options!
17
+ @fields << {
18
+ :source => field,
19
+ :name => options[:as] || field,
20
+ :boost => options[:boost] || nil
21
+ }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,142 @@
1
+ module Flare
2
+ class Session
3
+ RESULT_LIMIT = 1000
4
+ PER_PAGE = 16
5
+
6
+ def connection
7
+ @connection ||= RSolr.connect(:url => Flare::Configuration.client.url)
8
+ end
9
+
10
+ delegate :commit, :optimize, :to => :connection
11
+
12
+ def search_for_ids(*args)
13
+ response = execute(*args)
14
+ Flare::Collection.ids_from_response(response, response[:request][:page], response[:request][:per_page], response[:request])
15
+ end
16
+
17
+ def search(*args)
18
+ response = execute(*args)
19
+ Flare::Collection.create_from_response(response, response[:request][:page], response[:request][:per_page], response[:request])
20
+ end
21
+
22
+ def count(*args)
23
+ execute(*args)[:response][:numFound]
24
+ end
25
+
26
+ def index(*objects)
27
+ objects = ensure_searchable(objects)
28
+ connection.add(objects.collect(&:to_solr_doc))
29
+ end
30
+
31
+ def index!(*objects)
32
+ index(objects)
33
+ commit
34
+ end
35
+
36
+ def remove(*objects)
37
+ objects = ensure_searchable(objects)
38
+ connection.delete_by_id(objects.collect(&:solr_document_id))
39
+ end
40
+
41
+ def remove!(*objects)
42
+ remove(objects)
43
+ commit
44
+ end
45
+
46
+ def remove_all(*types)
47
+ types = Array(types).flatten.select { |type| type.searchable? }
48
+ connection.delete_by_query(types.map { |type| "type:#{type.name}" }.join(' OR '))
49
+ end
50
+
51
+ def remove_all!(*types)
52
+ remove_all(types)
53
+ commit
54
+ optimize
55
+ end
56
+
57
+ private
58
+ def execute(*args)
59
+ options = args.extract_options!
60
+
61
+ options.reverse_merge!({
62
+ :page => 1,
63
+ :per_page => PER_PAGE,
64
+ :limit => RESULT_LIMIT,
65
+ :fields => '* score',
66
+ })
67
+
68
+ options.assert_valid_keys(:filter, :types, :page, :per_page, :limit, :fields, :order, :facet)
69
+
70
+ query = {
71
+ :q => args,
72
+ :fq => Array(options[:filter]).flatten,
73
+ :fl => options[:fields],
74
+ :start => start = (options[:page] -1) * options[:per_page],
75
+ :rows => options[:per_page]
76
+ }
77
+
78
+ if options[:facets]
79
+ query[:facets] = true
80
+ query['facet.field'] = options[:facets][:fields]
81
+ query['facet.query'] = options[:facets][:queries]
82
+ query['facet.mincount'] = options[:facets][:mincount] || 1
83
+ query["facet.limit"] = options[:facets][:limit]
84
+
85
+ query["facet.missing"] = @params[:facets][:missing]
86
+ query["facet.mincount"] = @params[:facets][:mincount]
87
+ query["facet.prefix"] = @params[:facets][:prefix]
88
+ query["facet.offset"] = @params[:facets][:offset]
89
+ end
90
+
91
+
92
+ if options[:types]
93
+ query[:fq] << Array(options[:types]).map {|type| "type:#{type}"}.join(" OR ")
94
+ end
95
+
96
+ query[:q] = query.delete(:fq) if query[:q].blank?
97
+
98
+ ::ActiveRecord::Base.logger.debug(<<-SOLR.squish)
99
+ \e[4;32mSolr Query:\e[0;1m
100
+ #{query[:q].join(', ')}
101
+ #{"(#{query[:fq].join(' AND ')})," if query[:fq] }
102
+ sort: #{query[:order]}
103
+ start: #{query[:start]},
104
+ rows: #{query[:rows]}
105
+ SOLR
106
+
107
+ response = connection.select(query)
108
+ response[:request] = query
109
+ response[:request][:page] = options[:page]
110
+ response[:request][:per_page] = options[:per_page]
111
+ response.with_indifferent_access
112
+ end
113
+
114
+ def ensure_searchable(*objects)
115
+ Array(objects).flatten.select { |object| object.class.searchable? }
116
+ end
117
+
118
+ def prepare_query(query)
119
+ query[:page] ||= query[:page] ? query[:page].to_i : 1
120
+ query[:per_page] ||= PER_PAGE
121
+ query[:limit] ||= RESULT_LIMIT
122
+ query[:fields] ||= '* score'
123
+ query
124
+ end
125
+
126
+ # I dont like this... Can we move it to hash if a library like ActiveSupport doesn't already have it?
127
+ def symbolize_keys(hash)
128
+ hash.inject({}){|result, (key, value)|
129
+ new_key = case key
130
+ when String then key.to_sym
131
+ else key
132
+ end
133
+ new_value = case value
134
+ when Hash then symbolize_keys(value)
135
+ else value
136
+ end
137
+ result[new_key] = new_value
138
+ result
139
+ }
140
+ end
141
+ end
142
+ end