Skip to content
Browse files

Added mono changes with fixes

  • Loading branch information...
1 parent e56b4ed commit e6d7da6ec7564eef18e50456d7e7e14dc6e619f7 Ignacio Perez committed
Showing with 131 additions and 20 deletions.
  1. +7 −3 lib/indextank.rb
  2. +43 −5 lib/indextank/document.rb
  3. +5 −0 lib/indextank/exceptions.rb
  4. +24 −6 lib/indextank/function.rb
  5. +52 −6 lib/indextank/index.rb
View
10 lib/indextank.rb
@@ -5,15 +5,19 @@
require File.join(directory, 'indextank', 'client')
module IndexTank
- VERSION = "0.0.3"
+ VERSION = "0.0.6"
- def self.setup_connection(url)
+ def self.setup_connection(url, &block)
@conn = Faraday::Connection.new(:url => url) do |builder|
builder.adapter Faraday.default_adapter
builder.use Faraday::Response::Yajl
+ if block_given?
+ block.call builder
+ end
end
@uri = URI.parse(url)
- @conn.basic_auth @uri.user,@uri.password
+ @conn.basic_auth @uri.user,@uri.password
@conn
end
end
+
View
48 lib/indextank/document.rb
@@ -1,4 +1,5 @@
require 'json'
+require 'faraday'
module IndexTank
class Document
@@ -6,7 +7,8 @@ class Document
def initialize(document_url, docid)
@docid = docid
- @conn = IndexTank.setup_connection(document_url)
+ builder = Proc.new { |builder| builder.use ResponseDocument }
+ @conn = IndexTank.setup_connection(document_url, &builder)
end
# the options argument may contain a :variables key
@@ -15,12 +17,13 @@ def initialize(document_url, docid)
# when sorting a search
def add(fields, options = {})
options.merge!(:docid => self.docid, :fields => fields)
+
resp = @conn.put do |req|
req.url ""
req.body = options.to_json
end
- resp.status == 200
+ resp.status
end
def delete(options = {})
@@ -30,7 +33,7 @@ def delete(options = {})
req.body = options.to_json
end
- resp.status == 200
+ resp.status
end
def update_variables(variables, options = {})
@@ -40,7 +43,7 @@ def update_variables(variables, options = {})
req.body = options.to_json
end
- resp.status == 200
+ resp.status
end
# updates the categories of a given document
@@ -54,7 +57,42 @@ def update_categories(categories, options = {} )
req.body = options.to_json
end
- resp.status == 200
+ resp.status
+ end
+ #private
+ # Handles standard returns status. All methods on documents should return HTTP 200,
+ # and the errors are 'common' for any other value
+ #def handle_return_status(status)
+ # case status
+ # when 400
+ # raise InvalidArgument
+ # when 409
+ # raise IndexInitializing
+ # when 404
+ # raise IndexNotFound
+ # end
+ #end
+ end
+
+ class ResponseDocument < Faraday::Response::Middleware
+ def self.register_on_complete(env)
+ env[:response].on_complete do |finished_env|
+ case finished_env[:status]
+ when 401
+ raise InvalidApiKey
+ when 409
+ raise IndexInitializing
+ when 404
+ raise NonExistentIndex
+ when 400
+ raise InvalidArgument
+ end
+ end
+ end
+
+ def initialize(app)
+ super
+ @parser = nil
end
end
end
View
5 lib/indextank/exceptions.rb
@@ -1,5 +1,10 @@
module IndexTank
class IndexAlreadyExists < StandardError; end
+ class NonExistentIndex < StandardError; end
class TooManyIndexes < StandardError; end
class MissingFunctionDefinition < StandardError; end
+ class InvalidApiKey < StandardError; end
+ class InvalidQuery < StandardError; end
+ class IndexInitializing < StandardError; end
+ class InvalidArgument < StandardError; end
end
View
30 lib/indextank/function.rb
@@ -8,19 +8,18 @@ def initialize(function_url, index, definition)
@uri = "#{function_url}/#{index}"
@index = index
@definition = definition
- @conn = IndexTank.setup_connection(@uri)
+ builder = Proc.new { |builder| builder.use ResponseDocument }
+ @conn = IndexTank.setup_connection(@uri, &builder)
end
def add(options = {})
raise MissingFunctionDefinition unless self.definition
options.merge!(:definition => self.definition)
- resp = @conn.put do |req|
+ response = @conn.put do |req|
req.url ''
req.body = options.to_json
end
-
- resp.status == 200
end
def delete(options = {})
@@ -28,8 +27,6 @@ def delete(options = {})
req.url ''
req.body = options.to_json
end
-
- resp.status == 200
end
def ==(other)
@@ -38,4 +35,25 @@ def ==(other)
self.definition == other.definition
end
end
+ class ResponseDocument < Faraday::Response::Middleware
+ def self.register_on_complete(env)
+ env[:response].on_complete do |finished_env|
+ case finished_env[:status]
+ when 401
+ raise InvalidApiKey
+ when 409
+ raise IndexInitializing
+ when 404
+ raise NonExistentIndex
+ when 400
+ raise InvalidArgument
+ end
+ end
+ end
+
+ def initialize(app)
+ super
+ @parser = nil
+ end
+ end
end
View
58 lib/indextank/index.rb
@@ -20,6 +20,8 @@ def add
raise IndexAlreadyExists
when 409
raise TooManyIndexes
+ when 401
+ raise InvalidApiKey
end
end
@@ -34,8 +36,10 @@ def refresh
def delete
response = @conn.delete('')
-
- response.status == 200
+ case response.status
+ when 204
+ raise NonExistentIndex
+ end
end
def running?
@@ -65,15 +69,59 @@ def exists?
# for this query
# :variables => a hash int => float, with variables that can be later
# used in scoring :function
+ # :category_filters =>
+ # :docvar_filters => a hash int => Array [ Array [2] ] with variable numbers as keys
+ # and arrays of ranges allowed for docvar matching the key. Ranges
+ # can contain nil, which is interpreted as infinity.
+ # :function_filters => a hash int => Array [ Array [2] ] with variable numbers as keys
+ # and arrays of ranges allowed for function values matching the key.
+ # Ranges can contain nil, which is interpreted as infinity or -infinity.
def search(query, options = {})
options = {:start => 0, :len => 10 }.merge(options).merge(:q => query)
if options[:variables]
options[:variables].each_pair { |k, v| options.merge!( :"var#{k}" => v ) }
+ options.delete :variables
end
- @conn.get do |req|
+ if options[:docvar_filters]
+ # go from { 3 => [ [1, 3], [5, nil] ]} to filter_docvar3 => 1:3,5:*
+ options[:docvar_filters].each_pair { |k, v|
+ v.each { |val|
+ raise ArgumentError, "using a range with bound count != 2" unless val.length == 2
+ rng = "#{val[0] || '*'}:#{val[1] || '*'}"
+ options.merge!( :"filter_docvar#{k}" => rng )
+ }
+ }
+ options.delete :docvar_filters
+ end
+
+ if options[:function_filters]
+ # go from { 2 => [ [1 , 3],[5,8] ]} to filter_function2 => 1:3,5:8
+ options[:function_filters].each_pair { |k, v|
+ v.each { |rng|
+ raise ArgumentError, "using a range with bound count != 2" unless rng.length == 2
+ rng = "#{v[0] || '*'}:#{v[1] || '*'}"
+ options.merge!( :"filter_function#{k}" => rng )
+ }
+ }
+ options.delete :function_filters
+ end
+
+ if options[:category_filters]
+ options[:category_filters] = options[:category_filters].to_json
+ end
+
+ response = @conn.get do |req|
req.url 'search', options
- end.body
+ end
+ case response.status
+ when 400
+ raise InvalidQuery
+ when 409
+ raise IndexInitializing
+ end
+
+ response.body
end
def suggest(query, options = {})
@@ -91,8 +139,6 @@ def promote(docid, query, options={})
req.url 'promote'
req.body = options.to_json
end
-
- resp.status == 200
end
def document(docid)

0 comments on commit e6d7da6

Please sign in to comment.
Something went wrong with that request. Please try again.