Permalink
Browse files

fix restclient integration

  • Loading branch information...
1 parent cda1706 commit dff4729963ea75b3e24aca376f2fce00fe36db85 Ryan Sonnek committed Feb 3, 2011
Showing with 24 additions and 13 deletions.
  1. +1 −1 Gemfile
  2. +14 −2 lib/elastic_searchable.rb
  3. +3 −5 lib/elastic_searchable/index.rb
  4. +1 −1 lib/elastic_searchable/queries.rb
  5. +5 −4 test/test_elastic_searchable.rb
View
@@ -1,7 +1,7 @@
source "http://rubygems.org"
# Add dependencies required to use your gem here.
gem "activerecord", "~> 2.3.5"
-gem 'rest-client', '1.4.0', :require => 'rest_client'
+gem 'rest-client', '1.6.1', :require => 'rest_client'
gem 'crack', '0.1.8'
gem 'backgrounded', '~> 0.7.0'
gem 'will_paginate', '~> 2.3.15'
View
@@ -17,10 +17,22 @@ def default_index
end
#perform a request to the elasticsearch server
- def request(method, path, params = {})
+ def request(method, path, params = {}, options = {})
+ options.reverse_merge! :content_type => :json, :accept => :json
url = ['http://', 'localhost:9200', path].join
RestClient.log = Logger.new(STDOUT)
- response = RestClient.send method, url, params
+ response = case method
+ when :get
+ RestClient.get url, params.merge(options)
+ when :put
+ RestClient.put url, params, options
+ when :post
+ RestClient.post url, params, options
+ when :delete
+ RestClient.delete url, params.merge(options)
+ else
+ raise ElasticSearchable::ElasticError("Unknown request method: #{method}")
+ end
json = Crack::JSON.parse(response.body)
#puts "elasticsearch request: #{method} #{url} #{" finished in #{json['took']}ms" if json['took']}"
json
@@ -4,11 +4,7 @@ module Index
# helper method to clean out existing index and reindex all objects
def rebuild_index
- begin
- self.clean_index
- rescue ElasticSearchable::ElasticError
- # no index
- end
+ self.clean_index
self.update_index_mapping
self.find_each do |record|
record.index_in_elastic_search if record.should_index?
@@ -20,6 +16,8 @@ def rebuild_index
# http://www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/delete_mapping/
def clean_index
ElasticSearchable.request :delete, index_type_path
+ rescue RestClient::BadRequest
+ # index doesn't exist
end
# configure the index for this type
@@ -15,7 +15,7 @@ def search(query, options = {})
options[:size] ||= (options.delete(:per_page) || 20)
options[:from] ||= options[:size] * (page - 1)
- response = ElasticSearchable.request :get, index_type_path('_search'), :query => options
+ response = ElasticSearchable.request :get, index_type_path('_search'), :params => options
hits = response['hits']
ids = hits['hits'].collect {|h| h['_id'].to_i }
results = self.find(ids).sort_by {|result| ids.index(result.id) }
@@ -69,7 +69,7 @@ def indexed_on_create?
context 'requesting invalid url' do
should 'raise error' do
- assert_raises ElasticSearchable::ElasticError do
+ assert_raises RestClient::InternalServerError do
ElasticSearchable.request :get, '/elastic_searchable/foobar/notfound'
end
end
@@ -79,7 +79,7 @@ def indexed_on_create?
setup do
begin
ElasticSearchable.request :delete, '/elastic_searchable'
- rescue ElasticSearchable::ElasticError
+ rescue RestClient::ResourceNotFound
#already deleted
end
end
@@ -165,8 +165,9 @@ def indexed_on_create?
Post.refresh_index
end
should 'be removed from the index' do
- @request = ElasticSearchable.request :get, "/elastic_searchable/posts/#{@first_post.id}"
- assert @request.response.is_a?(Net::HTTPNotFound), @request.inspect
+ assert_raises RestClient::ResourceNotFound do
+ ElasticSearchable.request :get, "/elastic_searchable/posts/#{@first_post.id}"
+ end
end
end
end

0 comments on commit dff4729

Please sign in to comment.